Module 2 Stack
Module 2 Stack
Stack: A data structure in which the elements are added and removed from one
end only; a Last In First Out (LIFO) data structure.
Operations on Stack:
• Push- new element added to the top of the stack
• Pop - removes the top elementfrom the stack.
• Top – pointer which always points to the top of the stack.
To implement a stack, we need at least these six operations, which are described
below;
• initializeStack—Initializes the stack to an empty state.
• isEmptyStack—Determines whether the stack is empty. If the
stack is empty, it returns the value true; otherwise, it returns the
value false.
stackADT<Type>
+initializeStack(): void
+isEmptyStack(): boolean
+isFullStack(): boolean
+push(Type): void
+top(): Type
+pop(): void
The following class, stackType, implements the functions of the abstract class
stackADT.
public:
stackType(int Size = 100);
~stackType();
void initializeStack();
bool isEmptyStack();
bool isFullStack();
void push(Type& newItem);
Type top();
Type pop();
void print();
};
stackType<Type>
-maxStackSize: int
-stackTop: int
-*stack: Type
+initializeStack(): void
+isEmptyStack(): bool
+isFullStack(): bool
+push(Type&): void
+top(): Type
+pop(): void
+stackType(int = 100)
+~stackType()
If stackTopis -1, the stack is empty. If stackTopis nonzero, the stack is nonemptyand the
top element of the stack is given by stackTop – 1 because the first stack element is at
position 0.
Figure 7-6 shows this data structure, wherein stack is an object of type stackType.Note
that stackTop can range from 0 to maxStackSize. If stackTop is nonzero, then stackTop - 1
is the index of the top element of the stack. Suppose that maxStackSize= 100.
[99]
.
.
.
.
.
stack [3] D
[2] C stack
maxStackSize 100
[1] B elements
stackTop 4
[0] A
list
Initialize Stack
Let us consider the initializeStack operation. Because the value of stackTop indicates
whether the stack is empty, we can simply set stackTop to -1 to initialize the stack. (See
[99]
.
.
. unused
stack [3] D stack
[2] C
maxStackSize 100
[1] B
stackTop -1
[0] A
list
Empty Stack
We have seen that the value of stackTop indicates whether the stack is empty. If
stackTop is -1, the stack is empty; otherwise, the stack is not empty. The definition
ofthe function isEmptyStackis as follows:
template <class Type>
bool stackType<Type>::isEmptyStack()
{
return(stackTop == -1);
}
Full Stack
Next, we consider the operation isFullStack. It follows that the stack is full if stackTop
is equal to maxStackSize. The definition of the function isFullStackis as follows:
template <class Type>
bool stackType<Type>::isFullStack()
{
return(stackTop == maxStackSize-1);
}
[99] [99]
.
. .
. .
. y [4]
stack N [3] stack n [3]
stack
N [2] stack n [2] elements
maxStackSize 100 elements maxStackSize 100
U [1] u [1]
stackTop 4 stackTop 4
S [0] S [0]
list list
Pop
To remove, or pop, an element from the stack, we simply decrement stackTop by 1.
Figure 7-9(a) shows the stack before popping 'D' from the stack. Figure 7-9(b) shows
thestack after popping 'D'from the stack.
[99] [99]
. .
. .
. .
stack D [3] stack D [3]
L [2] stack L [2]
maxStackSize 100 maxStackSize 100 stack
O [1] elements O [1] elements
stackTop 4 stackTop 3
B [0] B [0]
list list
}//end constructor
void stackType<Type>::print()
{
if(stackTop!=-1)
{
for(int i=stackTop;i>=0;i--)
cout<<stack[i]<<endl;
}
}
TABLE 7-1 Time complexity of the operations of the class stackType on a stack with
n elements
isEmptyStack O (1)
isFullStack O (1)
initializeStack O (1)
Constructor O (1)
Top O (1)
Pop O (1)
copyStack O (n)
Destructor O (1)
#include <iostream>
using namespace std;
template <class Type>
class stackADT
{
public:
virtual void initializeStack() = 0;
virtual bool isEmptyStack() = 0;
virtual bool isFullStack() = 0;
virtual void push(Type& newItem) = 0;
virtual Type top() = 0;
virtual void pop() = 0;
virtual void print()=0;
};
int main()
{
stackType<type> stack1(50);
stack1.initializeStack();
stack1.push(23);
stack1.push(45);
stack1.push(38);
stack1.pop();
stack1.print();
while (!Stack1.isEmptyStack()) //print Stack
{
cout << stack1.top()endl;
stack1.pop();
}
return 0;
}
public:
linkedStackType();
~linkedStackType();
bool isEmptyStack();
void initializeStack();
void push(Type& newItem);
Type top();
void pop();
};
stack stack
stackTop stackTop C
In Figure 7-10(b), the top element of the stack is C; that is, the last element pushed
ontothe stack is C.
Default Constructor
The first operation that we consider is the default constructor. The default constructor
initializes the stack to an empty state when a stack object is declared. Thus, this
function sets stackTopto NULL. The definition of this function is as follows:
template <class Type>
linkedStackType<Type>::linkedStackType()
{
stackTop = NULL;
}
Empty Stack
The stack is empty if stackTop is NULL. Also, because the memory for a stack
element is allocated and deallocated dynamically, the stack is never full. (The
stack is full only if we run out of memory.) The definitions of the functions to
implement these operations are as follows:
template <class Type>
bool linkedStackType<Type>::isEmptyStack()
{
return(stackTop == NULL);
}
Initialize Stack
The operation initializeStack reinitializes the stack to an empty state. Because the stack
might contain some elements and we are using a linked implementation of a stack, we
Push
Consider the stack shown in Figure 7-11.
stack
stackTop C
Figure 7-12 shows the steps of the push operation. (Assume that the new element to
bepushed is 'D'.)
C
B B
B
A A
A
(a) Create newNode (b) Put newNode on
and store D the top of stack
(c) Make stackTop point
to the top element
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 top of the stack. Finally, we make
stackTop point to the top element of the stack. The definition of the function push is
as follows:
template <class Type>
void linkedStackType<Type>::push(Type& newElement)
{
nodeType<Type> *newNode; //pointer to create the new node
stack
stackTop C
temp
stack
temp
C C
stackTop
stack
B stackTop B stackTop B
A A
A
(a) Make temp point to the (b) Make stackTop point to (c) Delete temp
top element the next element
Print operation
void linkedStackType<Type>::print()
{
nodeType *temp;
if(stackTop!=NULL)
for(temp=first;temp!=NULL;temp=temp->next)
cout<<temp->info<<endl;
}
Table 7-2 summarizes the time complexity of the operations to implement a linked stack.
TABLE 7-2 Time complexity of the operations of the class linkedStackType on a stack with n
elements
isEmptyStack O (1)
isFullStack O (1)
initializeStack O (n)
Constructor O (1)
Top O (1)
Push O (1)
Pop O (1)
copyStack O (n)
Destructor O (n)
struct nodeType
{
Type info;
nodeType<Type> *link;
};
class linkedStackType: public stackADT<Type>
{
nodeType<Type> *stackTop; //pointer to the stack
public:
linkedStackType();
~linkedStackType();
bool isEmptyStack();
void initializeStack();
void push(Type& newItem);
Type top();
void pop();
};
linkedStackType<Type>::linkedStackType()
{
stackTop = NULL;
}
bool linkedStackType<Type>::isEmptyStack()
{
return(stackTop == NULL);
}
Type linkedStackType<Type>::top()
{
assert(stackTop != NULL); //if stack is empty,
//terminate the program
return stackTop->info; //return the top element
}
void linkedStackType<Type>::pop()
{
nodeType<Type> *temp; //pointer to deallocate memory
if (stackTop != NULL)
{
temp = stackTop; //set temp to point to the top node
void linkedStackType<Type>::print()
{
nodeType *temp;
if(stackTop!=NULL)
for(temp=first;temp!=NULL;temp=temp->next)
cout<<temp->info<<endl;
}
int main()
{
linkedStackType<int> stack;
while (!newStack.isEmptyStack())
{
cout << newStack.top() << endl;
newStack.pop();
}
return 0;
}