Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
108 views

Stack ADT: What Is A Stack?

Stack is a linear data structure that follows the LIFO (last-in, first-out) principle for insertion and deletion. Elements can only be inserted or removed from one end, called the top. Common stack operations include push to insert and pop to remove elements from the top. A stack can be implemented using arrays or linked lists, with linked lists allowing for a variable number of elements.

Uploaded by

Vanshita Rajput
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

Stack ADT: What Is A Stack?

Stack is a linear data structure that follows the LIFO (last-in, first-out) principle for insertion and deletion. Elements can only be inserted or removed from one end, called the top. Common stack operations include push to insert and pop to remove elements from the top. A stack can be implemented using arrays or linked lists, with linked lists allowing for a variable number of elements.

Uploaded by

Vanshita Rajput
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Page 1 of 16

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)

A stack data structure can be defined as follows...

Stack is a linear data structure in which the operations are performed based on LIFO principle.

Stack can also be defined as

"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...

1. Push (To insert an element on to the stack)


2. Pop (To delete an element from the stack)
3. Display (To display elements of the stack)
4. void Create_stack();
5. void top_element();
6. void find_element_index();
7. void search_element();
8. bool is_empty();
9. bool is_top();

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.

Stack Using Array


A stack data structure can be implemented using one dimensional array. But stack implemented
using array, can store only fixed number of data values. This implementation is very simple,
just define a one dimensional array of specific size and insert or delete the values into that array
by using LIFO principle with the help of a variable 'top'. Initially top is set to -1. Whenever
we want to insert a value into the stack, increment the top value by one and then insert.
Whenever we want to delete a value from the stack, then delete the top value and decrement the
top value by one.

Stack Operations using Array


A stack can be implemented using array as follows...

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.

push(value) - Inserting value into the stack


In a stack, push() is a function used to insert an element into the stack. In a stack, the new
element is always inserted at top position. Push function takes one integer value as parameter
and inserts that value into the stack. We can use the following steps to push an element on to the
stack...

• Step 1: Check whether stack is FULL. (top == SIZE-1)


• Step 2: If it is FULL, then display "Stack is FULL!!! Insertion is not possible!!!" and
terminate the function.
• Step 3: If it is NOT FULL, then increment top value by one (top++) and set stack[top]
to value (stack[top] = value).

pop() - Delete a value from the Stack


In a stack, pop() is a function used to delete an element from the stack. In a stack, the element is
always deleted from top position. Pop function does not take any value as parameter. We can
use the following steps to pop an element from the stack...

• Step 1: Check whether stack is EMPTY. (top == -1)


• Step 2: If it is EMPTY, then display "Stack is EMPTY!!! Deletion is not possible!!!"
and terminate the function.
• Step 3: If it is NOT EMPTY, then delete stack[top] and decrement top value by one
(top--).

display() - Displays the elements of a Stack


We can use the following steps to display the elements of a stack...

• Step 1: Check whether stack is EMPTY. (top == -1)


• Step 2: If it is EMPTY, then display "Stack is EMPTY!!!" and terminate the function.
• Step 3: If it is NOT EMPTY, then define a variable 'i' and initialize with top. Display
stack[i] value and decrement i value by one (i--).
• Step 3: Repeat above step until i value becomes '0'.

#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();

Stack using Linked List


Page 7 of 16

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

• Step 3: Define a Node pointer 'top' and set it to NULL.


• Step 4: Implement the main method by displaying Menu with list of operations and
make suitable function calls in the main method.

push(value) - Inserting an element into the Stack

We can use the following steps to insert a new node into the stack...

• Step 1: Create a newNode with given value.


• Step 2: Check whether stack is Empty (top == NULL)
• Step 3: If it is Empty, then set newNode → next = NULL.
• Step 4: If it is Not Empty, then set newNode → next = top.
• Step 5: Finally, set top = newNode.

pop() - Deleting an Element from a Stack


We can use the following steps to delete a node from the stack...

• Step 1: Check whether stack is Empty (top == NULL).


• Step 2: If it is Empty, then display "Stack is Empty!!! Deletion is not possible!!!" and
terminate the function
• Step 3: If it is Not Empty, then define a Node pointer 'temp' and set it to 'top'.
• Step 4: Then set 'top = top → next'.
• Step 7: Finally, delete 'temp' (free(temp)).

display() - Displaying stack of elements


We can use the following steps to display the elements (nodes) of a stack...

• Step 1: Check whether stack is Empty (top == NULL).


• Step 2: If it is Empty, then display 'Stack is Empty!!!' and terminate the function.
• Step 3: If it is Not Empty, then define a Node pointer 'temp' and initialize with top.
• Step 4: Display 'temp → data --->' and move it to the next node. Repeat the same until
temp reaches to the first node in the stack (temp → next != NULL).
• Step 4: Finally! Display 'temp → data ---> NULL'.

Program for Stack Using Linked List


#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class Node
Page 9 of 16

{
public:
int data;
Node* next;
Node* top;
};

class StackList:public Node


{
public:
void push(int);
void pop();
void display();
StackList()
{
top=NULL;

}
};
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;

cout<<"\n Stack using Linked List ::\n";


while(1){
cout<<"\n****** MENU ******\n";
cout<<"1. Push\n2. Pop\n3. Display\n4. Exit\n";
cout<<"Enter your choice: ";
cin>>choice;
switch(choice){
case 1: cout<<"Enter the value to be insert: ";
cin>>value;
l.push(value);
break;
case 2: l.pop(); break;
case 3: l.display(); break;
case 4: exit(0);
default: cout<<"\nWrong selection!!! Please try again!!!\n";
}
}
return 60;
}

Expressions
What is an Expression?
we use a set of symbols to perform the task. These set of symbols makes an expression.

An expression can be defined as follows...

An expression is a collection of operators and operands that represents a specific value.

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.

The general structure of an Infix expression is as follows...

Operand1 Operator Operand2

Example

Postfix Expression
In postfix expression, operator is used after operands. We can say that "Operator follows the
Operands".

The general structure of Postfix expression is as follows...

Operand1 Operand2 Operator

Example

Prefix Expression
In prefix expression, operator is used before operands. We can say that "Operands follows the
Operator".
Page 12 of 16

The general structure of Prefix expression is as follows...

Operator Operand1 Operand2

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...

1. Find all the operators in the given Infix Expression.


2. Find the order of operators evaluated according to their Operator precedence.
3. Convert each operator into required type of expression (Postfix or Prefix) in the same order.

Example
Consider the following Infix Expression to be converted into Postfix Expression...

D=A+B*C

• Step 1: The Operators in the given Infix Expression : = , + , *


• Step 2: The Order of Operators according to their preference : * , + , =
• Step 3: Now, convert the first operator * ----- D = A + B C *
• Step 4: Convert the next operator + ----- D = A BC* +
• Step 5: Convert the next operator = ----- D ABC*+ =
Page 13 of 16

Finally, given Infix Expression is converted into Postfix Expression as follows...

DABC*+=

Infix to Postfix Conversion using Stack Data Structure


To convert Infix Expression into Postfix Expression using a stack data structure, We can use the
following steps...

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

The final Postfix Expression is as follows...

AB+CD-*

Postfix Expression Evaluation


A postfix expression is a collection of operators and operands in which the operator is placed
after the operands. That means, in a postfix expression the operator follows the operands.

Postfix Expression has following general structure...

Operand1 Operand2 Operator

Example

Postfix Expression Evaluation using Stack Data Structure


A postfix expression can be evaluated using the Stack data structure. To evaluate a postfix
expression using Stack data structure we can use the following steps...

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

You might also like