0% found this document useful (0 votes)
145 views12 pages

Ktu Btech Module 2 Data Structures Semester 3

The document discusses stacks, which are linear data structures where elements are placed one above another. Stacks follow the LIFO (last-in, first-out) principle, meaning the last element added is the first removed. Basic stack operations are PUSH to insert at the top and POP to delete from the top. Stacks can be implemented using arrays or linked lists and must check for overflow or underflow during operations.

Uploaded by

Nazla Alwan P B
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)
145 views12 pages

Ktu Btech Module 2 Data Structures Semester 3

The document discusses stacks, which are linear data structures where elements are placed one above another. Stacks follow the LIFO (last-in, first-out) principle, meaning the last element added is the first removed. Basic stack operations are PUSH to insert at the top and POP to delete from the top. Stacks can be implemented using arrays or linked lists and must check for overflow or underflow during operations.

Uploaded by

Nazla Alwan P B
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/ 12

Stacks

Stack of Books
What is a Stack
– It is a linear data structure in which elements are placed one above
another.
– A stack is an ordered collection of homogeneous data elements where
the insertion and deletion operations take place only at one end called
Top of the stack.
– In stack elements are arranged in Last-In-First-Out manner (LIFO). So
it is also called LIFO lists.
– Stack principle is LIFO (last in, first out) i.e. the last element to be
added to a stack is the first item to be removed.
– Deletion and insertion of elements can be done only from one end
called the top of the stack (TOP)
Operations on stack:
● Two basic operations of stack:
1. PUSH : Insert an element at the top of stack
2. POP: Delete an element from the top of stack
While performing push and pop operations the following test must be
conducted on the Stack.

a) Stack is empty or not b) stack is full or not

1. Push: Push operation is used to add new elements in to the stack. At the time
of addition first check the stack is full or not. If the stack is full it generates an
error message "stack overflow".

2. Pop: Pop operation is used to delete elements from the stack. At the time of
deletion first check the stack is empty or not. If the stack is empty it generates
an error message "stack underflow".
Conditions:

Stack overflow - The condition resulting from trying to push


an element onto a full stack.

Stack underflow - The condition resulting from trying to pop


an element from an empty stack.
Representation of Stack (or) Implementation of stack

● Stack can either be a fixed size or dynamic.


● The stack can be implemented into two ways:
○ Using arrays (Static implementation)
○ Using pointer/Linked list (Dynamic implementation)
Stack using array

● An element in the stack is termed as ITEM.


● Initially top is set to -1, to indicate an empty stack. (Top = -1)
● The maximum no. of elements that a stack can accommodate is termed
MAX_SIZE.
● If stack is full Top = MAX_SIZE - 1
Algorithm: PUSH()

Let A be an array with Maximum size as MAX_SIZE. Initially, top= -1

Step 1: START
Step 2: if top>=size-1 then
Write “ Stack is Overflow”
Step 3: Else
3.1: read data value ‘x’
3.2: top=top+1;
3.3: stack[top]=x;
Step 4: END
Algorithm: POP()

Step 1: START

Step 2: if top==-1 then

Write “Stack is Underflow”

Step 3: otherwise

3.1: print “deleted element”

3.2: top=top-1;

Step 4: END
Algorithm: Display()

Step 1: START

Step 2: if top==-1 then

Write “Stack is Underflow”

Step 3: otherwise

3.1: print “Display elements are”

3.2: for top to 0

Print ‘stack[i]’

Step 4: END

You might also like