bool is_balanced(string brackets) { if(brackets == "") return true; LinkedStack ls; for (unsigned i = 0; i < brackets.size() ; i++) { string c = brackets.substr(i,i+1); if(c == "[" || c == "(" || c == "{") { ls.push(c); } else if(c == "]" ) { if(ls.top() == "[") { ls.pop(); } else return false; } else if(c == "}") { if(ls.top() == "{") { ls.pop(); } } else if( c == ")" ) { if(ls.top() == "(") { ls.pop(); } else return false; } else { if(ls.isEmpty()) return true; } } if(ls.isEmpty()) return true; return false; }
int main() { LinkedStack<int> numbers; try { for (int i=10; i < 100; i+=10) numbers.push(i); while (!numbers.isEmpty()) cout << numbers.pop() << endl; numbers.pop(); } catch(StackEmptyException e) { cerr << e; return 1; } return 0; }
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; }
/* The main entry point for the program. */ int main ( int argc, const char * argv[] ) { char input; LinkedStack s; while ( !cin.eof() ) { cout << "Please enter a string." << endl; while ( cin.get ( input ) && input != ( char ) '\n' ) { s.push ( input ); } while ( !s.isEmpty() ) { cout << s.pop(); } 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