int main() { LinkedStack<int> testLinkedStack; std::cout << "testLinkedStack.isEmpty() : " << testLinkedStack.isEmpty() << std::endl; std::cout << "testLinkedStack.getAllocatedSize() : " << testLinkedStack.getAllocatedSize() << std::endl; std::cout << "testLinkedStack.getSize() : " << testLinkedStack.getSize() << std::endl; testLinkedStack.push(5); testLinkedStack.push(6); testLinkedStack.push(7); testLinkedStack.push(9); testLinkedStack.push(10); std::cout << testLinkedStack.peek() << std::endl; int a = testLinkedStack.peek(); testLinkedStack.pop(); std::cout << a << std::endl; std::cout << testLinkedStack.peek() << std::endl; a = testLinkedStack.peek(); testLinkedStack.pop(); //testLinkedStack.pop(a); std::cout << a << std::endl; std::cout << testLinkedStack.peek() << std::endl; LinkedStack<int> testLinkedStack_2; testLinkedStack_2 = testLinkedStack; std::cout << testLinkedStack_2.peek() << std::endl; std::cout << "testLinkedStack.getSize() : " << testLinkedStack.getSize() << std::endl; testLinkedStack.clean(); std::cout << "testLinkedStack.getSize() : " << testLinkedStack.getSize() << std::endl; std::cout << testLinkedStack.getAllocatedSize() << std::endl; testLinkedStack.push(5); testLinkedStack.push(6); std::cout << "testLinkedStack.getSize() : " << testLinkedStack.getSize() << std::endl; std::cout << testLinkedStack.peek() << std::endl; return 0; }
void horse_stack(Point start, Point end) { LinkedStack<LinkedStack<Point> > history; LinkedStack<Point> startStack; ofstream clog("log.txt"); startStack.push(start); history.push(startStack); while (!history.empty() && (history.peek().empty() || history.peek().peek() != end)) { if (history.peek().empty()) { // Стъпка назад history.pop(); if (!history.empty()) history.peek().pop(); clog << "Стъпка назад" << endl; } else { // стъпка напред Point current = history.peek().peek(); clog << "Стъпка напред: " << current << endl; LinkedStack<Point> possibleMoves; board[current.first][current.second] = true; for(int dx = -2; dx <= 2; dx++) if (dx != 0) for(int sign = -1; sign <= 1; sign += 2) { int dy = sign * (3 - abs(dx)); Point newstart(current.first + dx, current.second + dy); if (inside_board(newstart) && !board[newstart.first][newstart.second]) possibleMoves.push(newstart); } LinkedStack<Point> pm(possibleMoves); clog << "---\n"; printPath(pm,clog); clog << endl << "---\n"; history.push(possibleMoves); } } if (!history.empty() && !history.peek().empty()) { cout << "Успех:\n"; printHistory(history); } }
int main() { LinkedStack<int> stack = LinkedStack<int>(); for (int i = 0; i < 10; i = i + 2) { stack.push(i); std::cout << stack.peek() << std::endl; } try { for (int i = 0; i < 10; i++) { int topValue = stack.pop(); std::cout << topValue << std::endl; } } catch (std::exception e) { std::cout << e.what() << std::endl; } return 0; }
void horse_queue(Point start, Point end) { LinkedQueue<Position> q; q.enqueue(start); board[start.first][start.second] = true; Position current; LinkedStack<LinkedQueue<Position> > history; LinkedQueue<Position> level; int currentMove = 0; history.push(level); while (!q.empty() && (current = q.dequeue()).p != end) { if (current.move == currentMove) { history.peek().enqueue(current); } else { // current.move == currentMove + 1 currentMove = current.move; LinkedQueue<Position> level; level.enqueue(current); history.push(level); } for(int dx = -2; dx <= 2; dx++) if (dx != 0) for(int sign = -1; sign <= 1; sign += 2) { int dy = sign * (3 - abs(dx)); Point newstart(current.p.first + dx, current.p.second + dy); Position newposition(newstart, current.move + 1); if (inside_board(newstart) && !board[newstart.first][newstart.second]) { q.enqueue(newposition); clog << newposition << endl; board[newstart.first][newstart.second] = true; } } } // current == end -- успех // q.empty() -- неуспех clog << history; history.pop(); if (current.p == end) { cout << "Успех за " << current.move << " хода!" << endl; LinkedStack<Point> path; path.push(current.p); while(!history.empty()) { // в history.peek() търсим първата позиция position, // за която isValidMove(current.p,position.p) Position position; while (!isValidMove(current.p, (position = history.peek().dequeue()).p)); // знаем, че isValidMove(current.p,position.p) // добавим position в пътя path.push(position.p); history.pop(); current = position; } cout << path << endl; } else cout << "Неуспех!" << endl; }
int main() { bool stay = true; // create one instance for each of the test classes cout << "\nInstanciate an object of LinkedStack\n"; LinkedStack<string>* stackPtr = new LinkedStack<string>(); cout << "\nProgram started, \n" << " initiate the TEST DRIVER with a set of hard-wired data!\n" << "For example, \n" << " you may use this set of tokens as a default test case: \n" << " the, items, are, 123, 456, 789, and, abc"; string choice; // user input for choices // initialize the LinkedStack class instances stackPtr->push("the"); stackPtr->push("items"); stackPtr->push("are"); stackPtr->push("123"); stackPtr->push("456"); stackPtr->push("789"); stackPtr->push("and"); stackPtr->push("abc"); cout << "\n Use the P - peek command to view the stack.\n"; // main menu while while(stay) { // main menu while starts menu(); stay = true; cin >> choice; cin.ignore(); if(choice.size() == 1) { char ch = choice[0]; vector<string> dump; string value; switch(ch) { // main menu switch starts case 'd': // display stack case 'D': if(stackPtr->isEmpty()) { cout << "\n Stack is empty.\n"; } else { stackPtr->displayStack(); } break; case 'e': // is stack empty? case 'E': if(stackPtr->isEmpty()) { cout << "\n The list is empty\n"; } else { cout << "\n There are items in the stack\n"; } break; case 'i': // insert item case 'I': cout << " insert item: "; cin >> value; stackPtr->push(value); break; case 'r': // remove item case 'R': if(stackPtr->isEmpty()) { cout << "\n The list is empty\n"; } else { stackPtr->pop(); cout << "\n Top item removed.\n"; } break; case 'c': // clear stack case 'C': // to be completed by you; if(stackPtr->isEmpty()) { cout << "\n Stack already empty.\n"; } else { stackPtr->clear(); } break; case 'p': // peek TOP value case 'P': cout << "\n The top value is: " << stackPtr->peek() << endl; break; case 'q': // quit main menu case 'Q': stay = false; break; default: cout << "\nInvalid Selection!\nTry again!\n\n"; } // end of menu switch } // only process single character } // end of main menu while
bool checkExpression(const string& expression) { LinkedStack<char> balancedStack; bool balancedSoFar = true; unsigned int i = 0; // Used to keep track of position in expression int singleQuote = 0, doubleQuote = 0; // Used to keep track of delimiters ' and " char ch; // Holds character at position i in expression while ( balancedSoFar && (i < expression.length()) ) { // Get first character in expression, then increment position ch = expression[i]; i++; switch (ch) { // Skip next character if escape character is found case '\\': i++; break; // Push an open token case '{': case '[': case '(': balancedStack.push(ch); break; case '\'': { singleQuote++; if (singleQuote % 2 == 0) balancedStack.pop(); // Pop matching delimiter else balancedStack.push(ch); break; } // end ' case case '"': { doubleQuote++; if (doubleQuote % 2 == 0) balancedStack.pop(); // Pop matching delimiter else balancedStack.push(ch); break; } // end " case // Close tokens case '}': case ']': case ')': { // Make sure stack is not empty and the close token matches the open if (!balancedStack.isEmpty() && ( (balancedStack.peek() == '(') || (balancedStack.peek() == '{') || (balancedStack.peek() == '[') ) ) balancedStack.pop(); // Pop a matching closed token else // No matching closed token balancedSoFar = false; break; } } // end switch } // end while if ( !(balancedSoFar && balancedStack.isEmpty()) ) balancedSoFar = false; return balancedSoFar; } // end checkExpression