Stack ADT: What Is A Stack?
Stack ADT: What Is A Stack?
Stack ADT
What is a Stack?
Stack is a linear data structure in which the insertion and deletion operations are performed at
only one end. In a stack, adding and removing of elements are performed at single position
which is known as "top". That means, new element is added at top of the stack and an element
is removed from the top of the stack. In stack, the insertion and deletion operations are
performed based on LIFO (Last In First Out) principle.
Page 2 of 16
In a stack, the insertion operation is performed using a function called "push" and deletion
operation is performed using a function called "pop".
In the figure, PUSH and POP operations are performed at top position in the stack. That means,
both the insertion and deletion operations are performed at one end (i.e., at Top)
Stack is a linear data structure in which the operations are performed based on LIFO principle.
"A Collection of similar data items in which both insertion and deletion operations are
performed based on LIFO principle".
Example
If we want to create a stack by inserting 10,45,12,16,35 and 50. Then 10 becomes the bottom
most element and 50 is the top most element. Top is at 50 as shown in the image below...
Page 3 of 16
Operations on a Stack
The following operations are performed on the stack...
Stack data structure can be implement in two ways. They are as follows...
1. Using Array
2. Using Linked List
When stack is implemented using array, that stack can organize only limited number of
elements. When stack is implemented using linked list, that stack can organize unlimited
number of elements.
Before implementing actual operations, first follow the below steps to create an empty stack.
• Step 1: Include all the header files which are used in the program and define a constant
'SIZE' with specific value.
• Step 2: Declare all the functions used in stack implementation.
• Step 3: Create a one dimensional array with fixed size (int stack[SIZE])
Page 4 of 16
• Step 4: Define a integer variable 'top' and initialize with '-1'. (int top = -1)
• Step 5: In main method display menu with list of operations and make suitable function
calls to perform operation selected by the user on the stack.
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int max 50
class StackADT{
Page 5 of 16
int n,i,s[max],bottom=-1,top,e,p;
public:
void Create_stack();
void top_element();
void push();
void pop();
void display();
void find_element_index();
void search_element();
bool is_empty();
bool is_top();
};
void StackADT::Create_stack(){
cout<<"\nEnter the no of Elements of Stack:";
cin>>n;
cout<<"\n Enter the "<<n<<" Elements in to Stack:";
s[0]=bottom;
for(i=1;i<=n;i++){
cin>>s[i];
}
top=n;
}
void StackADT::top_element(){
cout<<"\n Top of Stack is: "<<s[top];
cout<<"\n Bottom of Stack is: "<<s[0];
}
void StackADT::display(){
cout<<"\n Elements of Stack are:\n";
for(i=n;i>=0;i--){
cout<<" "<<s[i]<<"\n";
}
}
void StackADT::push(){
cout<<"\nEnter the Element to push of stack:";
cin>>e;
top=top+1;
s[top]=e;
n=top;
cout<<"\n Elements After Push:\n";
for(i=n;i>=0;i--){
cout<<" "<<s[i]<<"\n";
}
}
void StackADT::pop(){
top=top-1;
Page 6 of 16
n=top;
cout<<"\n Elements After Pop:\n";
for(i=n;i>=0;i--){
cout<<" "<<s[i]<<"\n";
}
}
void StackADT::find_element_index(){
cout<<"\nEnter the Position of Stack to Find Element:";
cin>>p;
cout<<"\nElement at Position is: "<<s[p];
}
void StackADT::search_element(){
cout<<"\n Enter an Element to Search in a Stack:";
cin>>e;
for(i=1;i<=n;i++){
if(s[i]==e)
cout<<"\n Element Found";
else
cout<<"\n Element Not Found";
}
}
bool StackADT:: is_empty(){
if(top==bottom)
cout<<"\n Stack is Empty";
else
cout<<"\n Stack is Not Empty";
}
bool StackADT::is_top(){
if(top>max)
cout<<"\n Stack is Full";
else
cout<<"\n Stack is Not Full";
}
int main(){
StackADT sa;
sa.Create_stack();
sa.top_element();
sa.display();
sa.push();
sa.pop();
The major problem with the stack implemented using array is, it works only for fixed number of
data values. That means the amount of data must be specified at the beginning of the
implementation itself. Stack implemented using array is not suitable, when we don't know the
size of data which we are going to use.
A stack data structure can be implemented by using linked list data structure. The stack
implemented using linked list can work for unlimited number of values. That means stack
implemented using linked list works for variable size of data. So, there is no need to fix the size
at the beginning of the implementation. The Stack implemented using linked list can organize
as many data values as we want.
In linked list implementation of a stack, every new element is inserted as 'top' element. That
means every newly inserted element is pointed by 'top'. Whenever we want to remove an
element from the stack, simply remove the node which is pointed by 'top' by moving 'top' to its
next node in the list. The next field of the first element must be always NULL.
Example
In above example, the last inserted node is 99 and the first inserted node is 25. The order of
elements inserted is 25, 32,50 and 99.
Operations
To implement stack using linked list, we need to set the following things before implementing
actual operations.
• Step 1: Include all the header files which are used in the program. And declare all the
user defined functions.
• Step 2: Define a 'Node' structure with two members data and next.
Page 8 of 16
We can use the following steps to insert a new node into the stack...
{
public:
int data;
Node* next;
Node* top;
};
}
};
void StackList::push(int value){
Node *newNode;
newNode=new Node;
newNode->data = value;
if(top == NULL)
newNode->next = NULL;
else
newNode->next = top;
top = newNode;
printf("\nInsertion is Success!!!\n");
}
void StackList::pop(){
if(top==NULL)
{
cout<<"\nStack is Empty!!!\n";
}
else{
Node *temp;
temp=new Node;
temp= top;
cout<<" Deleted element:"<<temp->data;
top=temp->next;
delete temp;
}
}
void StackList::display(){
if(top == NULL)
cout<<"\nStack is Empty!!!\n";
else{
Node *temp;
temp=new Node;
temp=top;
Page 10 of 16
while(temp->next !=NULL){
cout<<"\n--->"<<temp->data;
temp = temp -> next;
}
cout<<"\n--->NULL"<<temp->data;
}
}
int main()
{
StackList l;
int choice,value;
Expressions
What is an Expression?
we use a set of symbols to perform the task. These set of symbols makes an expression.
In above definition, operator is a symbol which performs a particular task like arithmetic
operation or logical operation or conditional operation etc.,
Operands are the values on which the operators can perform the task. Here operand can be a
direct value or variable or address of memory location.
Expression Types
Page 11 of 16
Based on the operator position, expressions are divided into THREE types. They are as
follows...
1. Infix Expression
2. Postfix Expression
3. Prefix Expression
Infix Expression
In infix expression, operator is used in between operands.
Example
Postfix Expression
In postfix expression, operator is used after operands. We can say that "Operator follows the
Operands".
Example
Prefix Expression
In prefix expression, operator is used before operands. We can say that "Operands follows the
Operator".
Page 12 of 16
Example
Any expression can be represented using the above three different types of expressions. And we
can convert an expression from one form to another form like Infix to Postfix, Infix to Prefix,
Prefix to Postfix and vice versa
Expression Conversion
Any expression can be represented using three types of expressions (Infix, Postfix and Prefix).
We can also convert one type of expression to another type of expression like Infix to Postfix,
Infix to Prefix, Postfix to Prefix and vice versa.
To convert any Infix expression into Postfix or Prefix expression we can use the following
procedure...
Example
Consider the following Infix Expression to be converted into Postfix Expression...
D=A+B*C
DABC*+=
1. Read all the symbols one by one from left to right in the given Infix Expression.
2. If the reading symbol is operand, then directly print it to the result (Output).
3. If the reading symbol is left parenthesis '(', then Push it on to the Stack.
4. If the reading symbol is right parenthesis ')', then Pop all the contents of stack until respective left
parenthesis is poped and print each poped symbol to the result.
5. If the reading symbol is operator (+ , - , * , / etc.,), then Push it on to the Stack. However, first pop the
operators which are already on the stack that have higher or equal precedence than current operator
and print them to the result.
Example
Consider the following Infix Expression...
(A+B)*(C-D)
The given infix expression can be converted into postfix expression using Stack data Structure
as follows...
Page 14 of 16
Page 15 of 16
AB+CD-*
Example
1. Read all the symbols one by one from left to right in the given Postfix Expression
2. If the reading symbol is operand, then push it on to the Stack.
3. If the reading symbol is operator (+ , - , * , / etc.,), then perform TWO pop operations and store the
two popped oparands in two different variables (operand1 and operand2). Then perform reading
symbol operation using operand1 and operand2 and push result back on to the Stack.
4. Finally! perform a pop operation and display the popped value as final result.
Example
Consider the following Expression...
Page 16 of 16