Ktu Btech Module 2 Data Structures Semester 3
Ktu Btech Module 2 Data Structures Semester 3
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.
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:
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 3: otherwise
3.2: top=top-1;
Step 4: END
Algorithm: Display()
Step 1: START
Step 3: otherwise
Print ‘stack[i]’
Step 4: END