I need codes in C++. It all that I have below for this assignment. Please help me with something. Thanks.
3.1 Declare the Stack class in C++ We need an m_count private variable to hold the number of items in the Stack has. We also need another private variable to hold the top node so that the Top () operation can easily find the value of the top item from this variable. The variable name will be m top. The following is the declaration of the Stack data type that we can find in the Stack.h file. Use the STL
as the data structure to hold your Stack elements. We will use the data type to build a Stack. * template class Stack { private: int m count; Node m_top; public: Stack(); bool Is Empty(); T Top(); void Push (T val); void Pop(); }; 3.2 Implement bool IsEmtpy(); 3.3 Implement Top(); 3.4 Implement Push(); 3.5 Implement Pop(); Check your stack ADT. It is time to play with our new Stack data type. Push Popo 91 88 59 18 IsEmpty0 = false 47 32 Stack Figure 1 We are going to create the functionality in Figure 1 diagram using the Push() operation, and then print the content of the Stack class using the Pop () operation. To ensure that the stack is not empty, we are going to use the IsEmpty() operation and we'll use the Top () operation to get the topmost element to print the value. Use the following code should to test your implementations. // File : main.cpp #include #include "Stack.h" using namespace std; int main() { // NULL Stack int> stackint = Stack int>(); // Store several numbers to the stack stackint.Push (32); stackInt. Push (47); stackint. Push (18); stackint. Push (59); stackInt. Push (88); stackint.Push (91); // list the element of stack while (!stackInt.IsEmpty()) { // Get the top element cout > expr; // Check the validity bool bo = IsValid (expr); // Notify the user endl; cout class Stack { public: int size() const; bool empty() const; const E& top() const throw(StackEmpty); void push(const E& e); void pop() throw(StackEmpty); } Applications of Stacks Exceptions - Attempting the execution of an operation of ADT may sometimes cause an error condition, called an exception . Exceptions are said to be "thrown" by an operation that cannot be executed . In the Stack ADT, operations pop and top cannot be performed if the stack is empty . Attempting pop or top on an empty stack throws a StackEmpty exception Direct applications . Page-visited history in a Web browser . Undo sequence in a text editor . Chain of method calls in the C++ run-time system Indirect applications Auxiliary data structure for algorithms Component of other data structures 2010 Goodrich, Tamassia Stacks 5 2010 Goodrich, Tamassia Stacks 6 Array-based Stack Algorithm size return t+1 bar PC = 1 m = 6 C++ Run-Time Stack . The C++ run-time system main() { keeps track of the chain of int i = 5; active functions with a stack foo(i); . When a function is called, the system pushes on the stack a frame containing foo(int i) { . Local variables and return value int k; Program counter, keeping track of k = j+1; the statement being executed bar(k); . When the function ends, its } frame is popped from the stack and control is passed to the bar(int m) { function on top of the stack Allows for recursion 2010 Goodrich, Tamassia A simple way of implementing the Stack ADT uses an array . We add elements from left to right A variable keeps track of the index of the top element foo PC = 3 j = 5 k = 6 Algorithm popo if empty then throw StackEmpty else t-t-1 return S[t+ 1] main PC = 2 i = 5 ... S 0 1 2 Stacks 2010 Goodrich, Tamassia Stacks 8 Array-based Stack (cont.) Performance and Limitations The array storing the stack elements may become full A push operation will then throw a StackFull exception Limitation of the array- based implementation Not intrinsic to the Stack ADT Algorithm push(o) if t= S.size() - 1 then throw Stack Full else tt+1 S[t] + 0 a Performance - Let n be the number of elements in the stack . The space used is O(n) . Each operation runs in time 0(1) . Limitations . The maximum size of the stack must be defined a priori and cannot be changed . Trying to push a new element into a full stack causes an implementation-specific exception S 0 1 2 2010 Goodrich, Tamassia t Stacks 9 2010 Goodrich, Tamassia Stacks 10 Array-based Stack in C++ Example use in C++ void pop() { if (empty()) throw StackEmpty (Pop from empty stack"); template class ArrayStack { private: E* S; // array holding the stack int cap; // capacity int t; // index of top element public: 1/ constructor given capacity Array Stack intc) : S(new E[c]), cap(c), t(-1){} } void push(const E& e) { if (size() == cap) throw Stack Full("Push to full stack"); S[++ t] = e; ArrayStack ; A.push(7); A.push(13); cout B(10); B.push("Bob"); B.push("Alice"); cout 14 prec(refop)