































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An introduction to stacks, a Last-In-First-Out (LIFO) linear data structure. the concept of a stack, its examples, stack implementation using static and dynamic methods, and basic operations on stacks. Additionally, the document discusses the application of stacks in recursion and the Tower of Hanoi problem. Polish notation is also briefly mentioned.
Typology: Slides
1 / 39
This page cannot be seen from the preview
Don't miss anything!
■ A stack is a non-primitive linear data structure.
■ It is an ordered list in which addition of new data item and deletion of already existing data items done from only one end, known as top tacks (TOP).
■ As all the deletion and insertion in a stack is done from top of the stack, the last added element will be the first to be removed from the stack.
■ That is the reason why stack is also called LAST-IN-FIRST-OUT (LIFO) type of list.
■ Whenever a stack is created the stack base remains fixed, as a new element is added to the stack from the top, the top goes on increasing. ■ Conversely as the top most elements of the stack is removed the stack top is decrementing. ■ Show the next slide figure to various stages of stack top during insertion and during deletion.
■ Stack top increases during insertion
Stack empty top=-
4 3 2 1 (^0 ) Insert first element
Top=
4 3 2 1 0
20 10 Top=
Insert second element
0
1
2
3
4
■ Stack can be implemented in two ways: ■ Static implementation ■ Dynamic implementation ■ Static implementation uses arrays to create stack. ■ Static implementation though a very simple technique but is not a flexible way of creation, as the size of stack has to be declared during program design, after that the size cannot be varied. ■ Moreover static implementation is not too efficient with respect to memory utilization.
■ As the declaration of array is done before the start of the operation, now if there are too few elements to be stored in the stack the statically allocated memory will be wasted. ■ On the other hand if there are more number of elements to be stored in the stack then we can’t be able to change the size of array to increase its capacity.
■ The basic operation that can be performed on stack are as follows: ■ PUSH : The process of adding a new element to the top of stack is called PUSH operation. when new element will be inserted at the top after every push operation that top is incremented by one. In case the array is full and no new element can be accommodated, it is called STACK-FULL condition. This condition is called STACK OVERFLOW.
■ POP: The process of deleting an element from the top of stack is called POP operation. After every pop operation the stack is decremented by one. If there is no element on the stack and the pop is performed then this will result into STACK UNDERFLOW condition.
■ PEEP: If one is interested only about an information stored at some location in a stack then peep operation is required. In short we can say extract any position information from the stack.
■ UPDATE : Update operation is required when the content of some location in a stack is to be changed.
■ STACK UNDERFLOW: This is the situation when the stack contains no element. At this point the top of stack is present at the bottom of the stack. ■ STACK OVERFLOW: This is the situation when the stack becomes full, and no more elements can be pushed onto the stack. At this point the stack top is present at the highest location of the stack.
■ There are various application of Stack: ■ Recursion: Recursion is an important facility in many programming language, such as PASCAL and C ect. ■ Some machine are also known which use built-in stack hardware called ‘stack machine’. ■ Representation of Polish Notation
■ There are three popular implementation of recursive computations: ■ Calculation of factorial value ■ Quick sort ■ Tower of Hanoi problem ■ Very simple example is to find Factorial value for an integer n: n!=n(n-1)(n-2)……..32 n!=n*(n-1)!
■ Another complex recursive problem is the tower of Hanoi problem.
■ This problem has a historical basis in the ritual of ancient Vietnam.
■ The problem can be described as below:
■ Suppose, there are three pillars A,B,C. there are N discs of decreasing size so that no two discs are of the same size.
■ Initially all the discs are stacked on one pillar in their decreasing order of size.
■ Let this pillar be A. other two pillars are empty. ■ The problem is to move all the discs from one pillar to other using as auxiliary so that ■ Only one disc may be moved at a time. ■ A disc may be moved from any pillar to another. ■ At no time can a larger disc be placed on a smaller disc.