0% found this document useful (0 votes)
2 views

Part Two Stack and Queue

Chapter 3 discusses data structures, focusing on stacks and queues. Stacks operate on a Last In, First Out (LIFO) principle with operations like push and pop, while queues follow a First In, First Out (FIFO) principle with enqueue and dequeue operations. The chapter also covers implementations of both data structures using arrays and linked lists, along with their attributes and applications.

Uploaded by

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

Part Two Stack and Queue

Chapter 3 discusses data structures, focusing on stacks and queues. Stacks operate on a Last In, First Out (LIFO) principle with operations like push and pop, while queues follow a First In, First Out (FIFO) principle with enqueue and dequeue operations. The chapter also covers implementations of both data structures using arrays and linked lists, along with their attributes and applications.

Uploaded by

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

Chapter 3: Data Structures

Stack and Queue


The Stack ADT
⚫ A stack is a list with the restriction
⚫ insertions and deletions can only be performed at the top of the list

Bottom

⚫ The other end is called bottom


⚫ Stacks are less flexible
 but are more efficient and easy to implement.
⚫ Stacks are known as LIFO (Last In, First Out) lists.

2 ⚫ The last element inserted will be the first to be retrieved


Stack ADT
⚫ Fundamental operations of stack:

⚫ Push: Equivalent to an insert

⚫ Add an element to the top of the stack

⚫ Pop: Equivalent to delete

⚫ Removes the most recently inserted element from the stack

⚫ In other words, removes the element at the top of the stack

⚫ Top/peek: Examines the most recently inserted element

3 ⚫ Retrieves the top element from the stack


Push and Pop
⚫ Example

empty push an push po


stack element another p
to
B
to p to
to p A A
p A
p
4
Implementation of Stacks

⚫ Any list implementation could be used to implement a stack

⚫ Arrays (static: the size of stack is given initially)

⚫ Linked lists (dynamic: never become full)

⚫ We will explore implementations based on array and linked list

⚫ Let’s see how to use an array to implement a stack first

5
Array Implementation
 Need to declare an array size ahead of time
 ​Associated with each stack is TopOfStack
 for an empty stack, set TopOfStack to -1
 Push
​ Increment TopOfStack by 1.
(1) Set Stack[TopOfStack] = X

(2)
Set return value to Stack[TopOfStack]
 Pop Decrement TopOfStack by 1


(1) ​These operations are performed in very fast constant time

6
(2)
Stack attributes and Operations
⚫ Attributes of Stack
⚫ maxTop: the max size of stack(the maximum value the top )
⚫ top: the index of the top element of stack
⚫ values: element/point to an array which stores elements of stack
⚫ Operations of Stack
⚫ IsEmpty: return true if stack is empty, return false otherwise
⚫ IsFull: return true if stack is full, return false otherwise
⚫ Top: return the element at the top of stack
⚫ Push: add an element to the top of stack
⚫ Pop: delete the element at the top of stack
7
⚫ DisplayStack: print all the data in the stack
Create Stack
⚫ Initialize the Stack

⚫ Allocate a stack array of size.

Example, size= 10.


⚫ Initially top is set to -1. It means

the stack is empty.


⚫ When the stack is full, top will

have value size – 1.

int Stack[size];
8
maxTop=size - 1;
Push Stack
⚫ void Push(int x);
⚫ Increment top by 1
⚫ Check if stack is not full
⚫ Push an element onto the stack
⚫ If the stack is full, print the error information.
⚫ Note top always represents the index of the top element.

void push(int int)


{ top = top+ 1;
if(top<= maxTop)
stack[top] = item; //Put the new element in the stack
else
9 cout<<"Stack Overflow";}
Pop Stack
⚫ Int Pop()--return the element at the top of the stack
⚫ If the stack is empty, print the error information. (In this case, the return value is
useless.)
⚫ Else, delete the top element
⚫ decrement top
int pop()
{
int del_val;
if(top==-1)
cout<<"Stack is empty";
else {
del_val= stack[top]; //Store the top most value in del_val
stack[top] = NULL; //Delete the top most value
top = top -1;
}
return(del_val); }
Stack Top
⚫ Top: Examines the most recently inserted element

⚫ double Top()

⚫ Return the top element of the stack

⚫ Unlike Pop, this function does not remove the top element
double Top() {
if (top==-1) {
cout << "Error: the stack is empty." << endl;
return -1;}
else
return stack[top];
}
Printing all the elements
⚫ void DisplayStack()
⚫ Print all the elements

void DisplayStack() {
cout << "top -->";
for (int i = top; i >= 0; i--)

cout << "\t|\t" << stack[i] << "\t|" << endl;


cout << "\t|---------------|" << endl;

12
Using Stack
int main(void) {
Push(5.0); result
Push(6.5);
Push(-3.0);
Push(-8.0);
DisplayStack();
cout << "Top: " <<Top() << endl;
stack.Pop();
cout << "Top: " <<Top() << endl;
while (top!=-1)
Pop();
DisplayStack();
return 0;
13
}
Linked-List implementation of stack
⚫ Need not know the maximum size

⚫ Add/Access/Delete in the beginning, O(1)

⚫ Need several memory access, deletions

Create the stack


struct node{
int item;
node *next;
};
14
node *topOfStack= NULL;
Linked List push Stacks
⚫ Algorithm

⚫ Step-1:Create the new node

⚫ Step-2: Check whether the top of Stack is empty or not if so, go

to step-3 else go to step-4


⚫ Step-3:Make your "topOfstack" pointer point to it and

quit.
⚫ Step-4:Assign the topOfstackpointer to the newly attached
15
element.
Push operation
push(node *newnode)
{
Cout<<“Add data”<<endl;
Cin>>newnode-> item ;
newnode-> next = NULL;
if( topOfStack = = NULL)
{ topOfStack = newnode;
}
else {
newnode-> next = topOfStack;
topOfStack = newnode;
}
16
}
The POP Operation
⚫ Algorithm:

⚫ Step-1:If the Stack is empty then give an alert message "Stack is

empty" and quit; else proceed


⚫ Step-2:Make "target" point to topOfstack next pointer

⚫ Step-3: Free the topOfstack node;

⚫ Step-4: Make the node pointed by "target" as your TOP most

element
17
Pop operation
int pop( )
{ int pop_val 0;
if(topOfStack = = NULL)
cout<<"Stack Underflow";
else {
node *temp= topOfStack;
pop_val= temp->data;
topOfStack =topOfStack-> next;
delete temp;
}return(pop_val);
}18
Application of stack Data Structure
1. Back and forward buttons in a web browser

19
Queue

20
Queue ADT
⚫ Like a stack, a queue is also a list.

⚫ However, with a queue, insertion is done at one end, while

deletion is performed at the other end.


⚫ Accessing the elements of queues follows a First In, First Out

(FIFO) order.
⚫ Like customers standing in a check-out line in a shop, the

21 first customer in is the first customer served.


The Queue ADT
 Basic operations:
 enqueue: insert an element at the rear of the list
 dequeue: delete the element at the front of the list

 Queue is First-in First-out (FIFO) list

22
Enqueue and Dequeue
⚫ A queue has a front and a rear.

Remove Insert
(Dequeue) front rear (Enqueue)
Implementation of Queue

⚫ Just as stacks ,queue can be implemented as arrays or

linked lists.
⚫ Dynamic queues have the same advantages over static

queues as dynamic stacks have over static stacks

32
Array Implementation of Queue
⚫ There are several different algorithms to implement Enqueue and
Dequeue
⚫ Naïve way
⚫ When enqueuing, the front index is always fixed and the rear index
moves forward in the array.
rear rear rear

3 3 6 3 6 9

front front front


Enqueue(3) Enqueue(6) Enqueue(9)
33
Array Implementation of Queue
⚫ Naïve way
⚫ When enqueuing, the front index is always fixed and the rear
index moves forward in the array.
⚫ When dequeuing, the element at the front of the queue is removed.
Move all the elements after it by one position. (Inefficient!!!)
Rear=1 Rear=0
rear = -1

6 9 9

front front front


Dequeue() Dequeue() Dequeue()
26
Array Implementation of Queue

⚫ Better way (Non-Naïve Way)

⚫ When an item is enqueued, make the rear index move forward.

⚫ When an item is dequeued, the front index moves by one element

towards the back of the queue (thus removing the front item, so no
copying to neighboring elements is needed).
The problem here is that the rear index cannot move beyond the last
element in the array.
27
Queue Class
⚫ Attributes of Queue
⚫ front/rear: front/rear index

⚫ counter: number of elements in the queue

⚫ maxSize: capacity of the queue

⚫ values: point to an array which stores elements of the queue

⚫ Operations of Queue
⚫ IsEmpty: return true if queue is empty, return false otherwise

⚫ IsFull: return true if queue is full, return false otherwise

⚫ Enqueue: add an element to the rear of queue

⚫ Dequeue: delete the element at the front of queue


28
Queue Implementation based on Linked List
 We can efficiently implement the queue ADT using linked list.

 For efficiency reasons, we choose the front of the queue to be at the head of the list,
and the rear of the queue to be at the tail of the list.

 In this way, we remove from the head and insert at the tail.

 To implement the queue using singly linked list, assume we have two pointers head
and tail, which point to the front and rear of the queue respectively.

 Hence, enqueue is inserting a node at the end of a linked list and dequeue is
deleting the first node in the list.


Enqueue
void Enqueue(double x) {
Node* n =new Node;
n->data = x;
n->next =NULL;
if (IsEmpty())
{
front=newNode;
Rear=newNode;
rear
8 5
}
else {
rear
rear->next= newNode;
8 5
rear= newNode;}
counter++; newNode
47 }
Dequeue
Dequeue(double & x) {
if (IsEmpty()) {
cout << "Error: the queue is empty." <<
endl; return false;
}
else {
x = front->data;
Node* nextNode = front->next;
delete front;
front = nextNode;
counter--;
3 8 5
}
} front
front
48 8 5
Applications of Queue
1. When jobs are submitted to a printer, they are arranged in order of arrival.Thus,
essentially, jobs sent to a printer are placed on a queue.
2. Virtually every real-life line is a queue. For instance, lines at ticket counters are queues,
because service is first-come first-served.
3.Another example concerns computer networks. There are many network setups of
personal computers in which the disk is attached to one machine, known as the file
server. Users on other machines are given access to files on a FIFO basis, so the data
structure is a queue.
3. In universities, where resources are limited, students must sign a waiting list if all
computers are occupied. The student who has been at a computer the longest is forced
off first, and the student who has been waiting the longest is the next user to be allowed
on.
4. Task scheduler in multiprocessing system - maintains priority queues of processes.
Telephone calls in a busy environment - maintains a queue of telephone calls.

You might also like