0% found this document useful (0 votes)
5 views18 pages

STACK DATA STRUCTURE

A stack is a linear data structure that operates on the Last In First Out (LIFO) principle, allowing operations such as push, pop, peek, isEmpty, and isFull. There are two types of stacks: fixed size, which cannot change capacity, and dynamic size, which can grow or shrink as needed. The document also discusses the implementation of stacks using arrays and the concept of two stacks within a single array.

Uploaded by

saikatpaulprint
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views18 pages

STACK DATA STRUCTURE

A stack is a linear data structure that operates on the Last In First Out (LIFO) principle, allowing operations such as push, pop, peek, isEmpty, and isFull. There are two types of stacks: fixed size, which cannot change capacity, and dynamic size, which can grow or shrink as needed. The document also discusses the implementation of stacks using arrays and the concept of two stacks within a single array.

Uploaded by

saikatpaulprint
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

STACK DATA

STRUCTURE
STACK DATA STRUCTURE
A stack is a linear data structure that follows
the principle of Last In First Out (LIFO). This
means the last element inserted inside the
stack is removed first.
Here, you can:
Put a new plate on top
Remove the top plate
LIFO PRINCIPLE OF STACK
In programming terms, putting an item on top of the stack is called push
and removing an item is called pop.
BASIC OPERATIONS OF STACK
There are some basic operations that allow us to perform different actions
on a stack.
Push: Add an element to the top of a stack.
Pop: Remove an element from the top of a stack
IsEmpty: Check if the stack is empty
IsFull: Check if the stack is full
Peek: Get the value of the top element without removing it
WORKING OF STACK DATA STRUCTURE
TYPES OF STACK DATA STRUCTURE
Fixed Size Stack : As the name suggests, a fixed size stack has a fixed
size and cannot grow or shrink dynamically. If the stack is full and an
attempt is made to add an element to it, an overflow error occurs. If the
stack is empty and an attempt is made to remove an element from it,
an underflow error occurs.
Dynamic Size Stack : A dynamic size stack can grow or shrink
dynamically. When the stack is full, it automatically increases its size to
accommodate the new element, and when the stack is empty, it
decreases its size. This type of stack is implemented using a linked list,
as it allows for easy resizing of the stack.
PUSH OPERATION IN STACK DATA STRUCTURE
Adds an item to the stack. If the stack is full, then it is said to be an Overflow condition.

Before pushing the element to the stack, we


check if the stack is full .
If the stack is full (top == capacity-1) , then
Stack Overflows and we cannot insert the
element to the stack.
Otherwise, we increment the value of top by 1
(top = top + 1) and the new value is inserted at
top position .
The elements can be pushed into the stack till
we reach the capacity of the stack.
POP OPERATION IN STACK DATA STRUCTURE
Removes an item from the stack. The items are popped in the reversed order in which they are
pushed. If the stack is empty, then it is said to be an Underflow condition.en it is said to be an
Overflow condition.

Before popping the element from the stack, we


check if the stack is empty .
If the stack is empty (top == -1), then Stack
Underflows and we cannot remove any
element from the stack.
Otherwise, we store the value at top,
decrement the value of top by 1 (top = top – 1)
and return the stored top value.
TOP OR PEEK OPERATION IN STACK DATA STRUCTURE
Returns the top element of the stack.

Before returning the top element from the


stack, we check if the stack is empty.
If the stack is empty (top == -1), we simply
print “Stack is empty”.
Otherwise, we return the element stored at
index = top .
ISEMPTY OPERATION IN STACK DATA STRUCTURE
Returns true if the stack is empty, else false.

Check for the value of top in stack.


If (top == -1) , then the stack is empty so return
true .
Otherwise, the stack is not empty so return
false .
ISFULL OPERATION IN STACK DATA STRUCTURE
Returns true if the stack is full, else false.

Check for the value of top in stack.


If (top == capacity-1), then the stack is full so
return true.
Otherwise, the stack is not full so return false.
IMPLEMENTATION OF STACK DATA STRUCTURE USING ARRAY
void push (int val,int n) //n is size of the stack int pop ()
{ {
if (top == n ) if(top == -1)
printf("\n Overflow"); {
else printf("Underflow");
{ return 0;
top = top +1; }
stack[top] = val; else
} {
} return stack[top - - ];
}
}
IMPLEMENTATION OF STACK DATA STRUCTURE USING ARRAY
int peek() int pop ()
{ {
if (top == -1) if(top == -1)
{ {
printf("Underflow"); printf("Underflow");
return 0; return 0;
} }
else else
{ {
return stack [top]; return stack[top - - ];
} }
} }
IMPLEMENT TWO STACKS IN AN ARRAY
Create a data structure twoStacks that represent two stacks.

Implementation of twoStacks should use only one array, i.e., both stacks should use the

same array for storing elements.

Following functions must be supported by twoStacks.

push1(int x) –> pushes x to first stack

push2(int x) –> pushes x to second stack

pop1() –> pops an element from first stack and return the popped element

pop2() –> pops an element from second stack and return the popped element
IMPLEMENT TWO STACKS IN AN ARRAY BY DIVIDING
THE SPACE INTO TWO HALVES:
Stack 1-----> use arr[0] to arr[n/2]

Stack 2-----> use arr[(n/2) + 1] to arr[n-1]

size of array =n

First Approach
IMPLEMENT TWO STACKS IN AN ARRAY BY DIVIDING
THE SPACE INTO TWO HALVES:
Stack 1-----> will start from 0, extends to the right direction

Stack 2-----> will start from n-1, extends to the left direction

Overflow condition-----> top1 + 1 = top2

Second Approach
INFIX TO POSTFIX CONVERSION
(a+(b*c)/(d-e))
A+(B*C-(D/E^F)*G)*H
K + L - M*N + (O^P) * W/U/V * T + Q
( ( A + B ) * ( C – D ) + E ) / (F + G)
a+b*(c^d-e)^(f+g*h)-i

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy