Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Week 5 Stack and Its Implementation

Uploaded by

Muneeb Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Week 5 Stack and Its Implementation

Uploaded by

Muneeb Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

The Stack ADT and its

Implementation
CSC-211 Data Structures and Algorithms
Lecture outline

• The Stack ADT


• Operations supported on stack
• Applications of Stack
• Implementing a Stack using a Linked List
• Implementing the stack using an Array
• Using Stacks for different computing applications
• Printing binary notation of numbers
• Towers of Hanoi
• Infix to Postfix conversion

2
What is a stack?
• It is an ordered group of homogeneous items of elements.
• Elements are added to and removed from the top of the stack (the most recently
added items are at the top of the stack).
• The last element to be added is the first to be removed (LIFO: Last In, First Out).
The Stack Abstract Data Type
A stack is a basic data structure that
can be logically thought of as a linear
structure represented by a real physical
stack or pile
• Insertion and deletion takes place at
one end called the top of the stack.
• The basic implementation of a stack is
also called a LIFO (Last In First Out)

4
Supported Operations on a Stack
• Push()
• Pop()
• Peek()
• IsEmpty()
• IsFull()

5
Stack ADT Interface
• The main functions in the Stack ADT are (S is the stack)

boolean isEmpty();// return true if empty

boolean isFull(S); // return true if full

void push(S, item); // insert item into stack

void pop(S); // remove most recent item

void clear(S); // remove all items from stack

Item top(S); // retrieve most recent item

Item topAndPop(S); // return & remove most recent item


Sample Operation
Stack S = malloc(sizeof(stack));

push(S, “a”);
s
push(S, “b”);
d
push(S, “c”); top
d=top(S);

e
pop(S);

c
push(S, “e”);

b
a
pop(S);
Implementing a Stack using a Linked List
• Can use a Linked List as implementation of stack
StackLL

lst Top of Stack = Front of Linked-List


LinkedListItr

head

a1 a2 a3 a4
}

//make
printf("NULL\n");
} the head node points to the next node.
Stack Example using
//logically removing the node
//make theheadnew =node
int main()
//so that head
as head node
head->next;
will always point the last inserted data
Link List
#include<stdio.h> {
head = newNode;
#include<stdlib.h> //free the poped element's memory
push(10);
} free(temp);
push(20); Connected Code for Reference
struct Node } push(30);
{ void pop()
} printf("Linked List\n");
{
int data; display();
//temp
struct Node is
//print
*next;used
the to free list
linked
pop(); the head node Output
}; struct Node
void *temp;
display()
printf("After the pop, the new linked list\n");
{ display();
if(head == NULL)
struct= pop();
struct Node *head Node
NULL;*temp = head;
printf("Stack is Empty\n");
printf("After the pop, the new linked list\n");
else //iterate
void push(int val) the entire linked list and print the data
display();
{ { while(temp != NULL)
//createprintf("Poped
new{ nodereturn element
0; = %d\n", head->data);
printf("%d->",
struct Node *newNode
} temp->data); Node));
= malloc(sizeof(struct
//backup
newNode->data =the
tempval;=head node
temp->next;
temp = head;
//make the new node points to the head node
9
newNode->next = head;
Stack Example using Link List with
Dropdown Menu

Connected Code for Reference

10
Implementing a Stack using an Array
• use Array with a top index pointer as an implementation of stack

StackAr

arr
0 1 3 4 6 7 8 9
2 5

A A B C D E F

top
Implementing a Stack using an Array

Implementation Code for Reference

12
Applications of stack
Stacks are used for a variety of applications in the computing world:
• For storing return addresses and local variables during function
calls.
• For implementing various computing algorithms:
• line editing
• The Quick Sort algorithm
• Evaluating mathematical expressions
• Solving Towers of Hanoi problem
• Etc

13
Using Stacks: Tower of Hanoi

1. You can move only one disk at


a time.
2. For temporary storage, a third
pole may be used.
3. You cannot place a disk of
larger diameter on a disk of
smaller diameter.

14
Using Stacks: Checking Balance of Brackets
• Ensures that pairs of brackets are properly matched

• An Example: {a,(b+f[4])*3,d+f[5]}

• Bad Examples:

(..)..) // too many closing brackets

(..(..) // too many open brackets

[..(..]..) // mismatched brackets


Informal Procedure
Initialize the stack to empty
For every char read
if open bracket then push onto stack
if close bracket, then
return & remove most recent item
from the stack
if doesn’t match then flag error
if non-bracket, skip the char read

Example [ ]
([ ])
{a,(b+f[4])*3,d+f[5]}
{ }
Stack
Using Stacks: Infix to Postfix Conversion
• Computation of arithmetic expressions can be efficiently carried out in Postfix
notation with the help of a stack.
Infix - Num1 op Num2
Prefix - op Num1 Num2
Postfix - Num1 Num2 op
postfix
(2*3)+4 2 3 * 4 +
infix

2*3+4

2*(3+4) 2 3 4 + *
Informal Procedure
Initialise stack S
For each item read.
If it is an operand,
push on the stack
If it is an operator,
pop arguments from stack;
perform operation;
push result onto the stack
Expr
2 push(S, 2)
3 push(S, 3)
4 push(S, 4)
+ Num2=topAndPop(S)
Num1=topAndPop(S) 4
push(S, Num1+Num2) 3+4=7
3
Num2=topAndPop(S)
* 2*7=14
2
Num1=topAndPop(S)
push(S, Num1*Num2) Stack
References

1. https://en.wikibooks.org/wiki/Data_Structures/Stacks_and_Queues#Stacks_and_Queues
2. https://en.wikipedia.org/wiki/Stack_(abstract_data_type)
3. https://www.geeksforgeeks.org/stack-data-structure/
4. https://www.programmersought.com/article/93075200781/
5. https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm
6. https://slideplayer.com/slide/3591689/
7. Infix To Postfix Conversion Using Stack [with C program] (includehelp.com)

19

You might also like