Hariani Andi Tawakal (BI07210007) Tutorial Data Structure (IT00503) Stack Tutorial
Hariani Andi Tawakal (BI07210007) Tutorial Data Structure (IT00503) Stack Tutorial
A stack is a list of homogenous elemets in which of addition and deletion of elements occurs only at one end. it also known as Last In First Out (LIFO) data structure. There are the three operation of stack which is push, top and pop operation. The operation initializeStack reinitializes the stack to the empty spaces. We are using linked implementation of stack because the stack might contain some element, so that it must be deallocate the memory occupied by the stack elements and set stackTop to null. The push operation is that the newElement will be added at the beginning of the linked list pointed to by stackTop. In the case of pop, the node pointed to by stackTop will be removed. In both cases, the value of the pointer stackTop is updated. The operation of top returns the info of the node that stackTop is pointing top.
As shown in figure 7-12, to push D into the stack, first we create a new node and store D into it. Next , we put the new node on the top of the stack. Finally, we make stackTop point to the top element of the stack.we do not to check whether the stack is full before we push an element onto the stack because in this implementation, logically the stack is never full. The definition of the function push is as follow:
As shown in figure 7-14, firt we make a pointer temp point to the top of the snack. Next we are make stackTop point to the next element of the stack, which will become the top element of the stack . Finally, the we delete temp. The definition of the function pop is as follow:
4. Explain the function copyStack. The function copyStack makes a copy of a stack. The stack to be copied is passed as a parameter to the function copyStack. To use this function to implement the copy constructor and overload the assignment operator.