Download presentation
Presentation is loading. Please wait.
Published byIsaac Glenn Modified over 9 years ago
1
Stack Data Structure By Marwa M. A. Elfattah
2
Stack - What A stack is one of the most important non- primitive linear data structure in computer science. It is an ordered collection of items into which new data items may be added/inserted and from which items may be deleted at only one end, called the top of the stack. Last-in-First-out (LIFO) So, the stack is called Last-in-First-out (LIFO)
3
Stack - What Add (20) Add( 5) Add(30) Delete Add(0) Add( -3) 20 5 30 -3 0
4
N() System Stack M() O() Q(1) Q(2)
5
OPERATIONS PERFORMED ON STACK Create the stack, leaving it empty. Determine whether the stack is empty or not. Determine whether the stack is full or not. Push a new entry onto the top of the stack Pop the entry off the top of the stack.
6
Stack Contiguous Implementation Each stack item is adjacent in memory to the next stack item, and so stack items are kept in an array. The top position is kept in an integer field. The top and the data array are grouped in a struct. #define MAX 10 typedef char EntryType; typedef struct{ int top; EntryType entry[ MAX ]; } StackType; char10
7
Stack Contiguous Implementation
14
Using of Stack Assume that we need to read a line of text and write it back in a reverse order. StackType stack; CreateStack(&stack);//Initialize the stack //to be empty item = getchar(); while (!StackFull(stack) && item != '\n'){ Push(item, &stack);//Push each item onto //the stack item = getchar(); } while (!StackEmpty(stack)){ Pop(&item, &stack); //Pop an item from the //stack putchar(item); }
15
The use of functions. You use the structure at the “User Level” without caring about the details at the “Implementation Level”. Your program, i.e., the user level, does not change even if the implementation of the used structure is changed. Your program is clear from the logical point of view. Information hiding (Encapsulation)
16
Exercise As a user for the stack ADT, write the StackTop function which return the top element of the stack and left the stack unchanged Rewrite the previouse function as a part of stack ADT
17
Exercise
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.