stack_operationss_documentation_file.ppt

l228296 24 views 24 slides May 26, 2024
Slide 1
Slide 1 of 24
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24

About This Presentation

A linked list is a data structure consisting of nodes where each node contains data and a reference (or pointer) to the next node in the sequence. The key features are:

1. **Node Structure**: Each node has two parts: data and a next pointer.
2. **Head**: The first node in the list is called the hea...


Slide Content

Stack Data Structure

What is a Data Structure Data Structure is a way to store and organize data so that it can be used efficiently. It is a special format for organizing and storing data. Data Structures Linear Structure Linked List Stack Queue Non Linear Structure Graph Tree

Abstract Data Types To simplify process of problem solving, we combine data structure with their operations and call this Abstract Data Types (ADT) It consists of 2 parts Declaration of Data Declaration of Operations

What is Stack A stack is an ordered list in which insertion and deletion are done at one end, called top . The last element inserted is the first one to be deleted. Hence it is called Last In First Out(LIFO) or First In Last Out(FILO).

Stack ADT

Stack Operations Operation Description push(int data) inserts data into stack int pop() removes and returns last inserted element int top() returns last inserted element int size() returns number of elements in stack int isEmpty() indicate if any element is stored or not int isFull() indicates if stack is full or not

Stack Applications Balancing of Symbols Infix to Postfix conversion Evaluation of postfix expression Implementing function calls History of a web browser Undo Sequence Tree Traversal

Working TOP = 0 MAX = 5 10 TOP = 1 MAX = 5 CREATE STACK SIZE 5 PUSH(10)

Working 10 20 TOP = 2 MAX = 5 10 20 30 TOP = 3 MAX = 5 PUSH(30) PUSH(20)

Working 10 20 30 40 TOP = 4 MAX = 5 10 20 30 TOP = 3 MAX = 5 POP() PUSH(40)

Working 10 20 TOP = 2 MAX = 5 10 TOP = 1 MAX = 5 POP() POP()

Working TOP = 0 MAX = 5 FULLSTACK() EMPTYSTACK() 10 20 30 40 50 TOP = 4 MAX = 5

Implementation by Array Advantages Best Performance Disadvantages Fixed Size Basic Implementation Initially Empty Array Field to record where next data is placed if array is full, push(item) else return false if array is empty, pop() return item on top else NULL

Implementation by Array CREATING STRUCTURE struct ArrayStruct{ int top; //Keep track of top Element int capacity; //capacity of the stack int *array; //pointer to address of first index } Space Complexity (for n operations)– O (n)

Implementation by Array isEmpty() { return (S->top == -1); } Time Complexity O(1) isFull(){ return(S->top==S->capacity-1); } Time Complexity O(1)

Implementation by Array void push(struct Array *S,int data){ if(isFull(S)) cout<<“FULL”; else S->array[++S->top]=data; } 
 Time Complexity O(1) int pop(struct Array *S){ if(isEmpty(S)){ cout<<“EMPTY”; return;} else return (S->array[S->top]); } Time Complexity O(1)

Linked List Implementation Advantages Always constant time to push and pop can grow to an infinite size Disadvantages can grow to infinite size common case is slowest of all Basic Implementation Initially Empty List push() add item on top pop() removes item on top

Linked List Implementation void push(struct node **top,int d){ struct node *temp; temp->data=d; temp->link=(*top); *temp = temp; } 
 Time Complexity O(1) void pop(struct node **top){ struct node *temp; int i; temp=(*top); i=temp->data; *top = (*top)->link; return i; } Time Complexity O(1)

Use of stack EVALUATING POSTFIX = 123*+5- Stack Expression STEP 1 2 3 1 Stack Expression STEP 2

Use of stack EVALUATING POSTFIX = 123*+5- 1 Stack Expression 2*3=6 STEP 3 6 1 Stack Expression STEP 4

Use of stack EVALUATING POSTFIX = 123*+5- Stack Expression 1+6=7 STEP 5 7 Stack Expression STEP 6

Use of stack EVALUATING POSTFIX = 123*+5- 5 7 Stack Expression STEP 7 Stack Expression 7-5=2 STEP 8

Use of stack EVALUATING POSTFIX = 123*+5- 2 Stack Expression STEP 9 123*+5- RESULT POSTFIX STRING 2

References Data Structures and Algorithms, Narsimha Karumanchi https://www.geeksforgeeks.org/stack-data-structure/ https://www.geeksforgeeks.org/tag/data-structures-stack/