0% found this document useful (0 votes)
11 views41 pages

2023 Slot07 Stack Queue

This document covers the concepts of stacks and queues, including their definitions, basic operations, and implementations using arrays and linked lists. It details how to perform operations such as push, pop, enqueue, and dequeue, along with applications for each data structure. Additionally, it includes exercises for reversing a queue and sorting values in a stack.

Uploaded by

minh le
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views41 pages

2023 Slot07 Stack Queue

This document covers the concepts of stacks and queues, including their definitions, basic operations, and implementations using arrays and linked lists. It details how to perform operations such as push, pop, enqueue, and dequeue, along with applications for each data structure. Additionally, it includes exercises for reversing a queue and sorting values in a stack.

Uploaded by

minh le
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Session 07 -

Stack - Queue
Instructor:
Nguyen Tran Duy
Minh
CSC10002 – Programming Techniques Page 1
Content

1 Review
2 Stack
3 Queue

CSC10002 – Programming Techniques Page 2


Stack

CSC10002 – Programming Techniques Page 3


Stack
 A stack is a linear data structure that follows the principle of
Last In First Out (LIFO)
 That is, elements can be added to and removed from a stack
only at the top

CSC10002 – Programming Techniques Page 4


Stack
 The following are the basic operations served by stacks.
 push(): Adds an element to the top of the stack
 pop(): Removes the topmost element from the stack.
 isEmpty(): Checks whether the stack is empty.
 peek()/top(): Displays the topmost element of the stack.
 size(): returns the size of stack
 isFull(): Checks whether the stack is full.

CSC10002 – Programming Techniques Page 5


Implement a Stack
 Using an array:
 An array of at most n elements: S[0…n-1]
 An attribute top that indexes the most recently inserted
element
 Empty stack: top = -1
0 1 2 3 4 0 1 2 3 4
2 3 5

top = -1 top = 2

CSC10002 – Programming Techniques Page 6


Implement a Stack
// Define the Stack structure
struct Stack {
int top; // Index of the top element
int data[MAX_SIZE]; // Array to store stack elements
// Constructor to initialize the stack
Stack(){
top = -1;
}
void push(int value);
int pop();
int peek();
bool isEmpty();
bool isFull();
};

CSC10002 – Programming Techniques Page 7


Implement a Stack
// Add an element to the top of the stack
void Stack::push(int value) {

CSC10002 – Programming Techniques Page 8


Implement a Stack
// Add an element to the top of the stack
void Stack::push(int value) {
if(isFull()) {
std::cout << "Stack is full!" << std::endl;
} else {
top = top + 1;
data[top] = value;
}
}

CSC10002 – Programming Techniques Page 9


Implement a Stack
// Remove and return the top element of the stack
int Stack::pop() {

CSC10002 – Programming Techniques Page 10


Implement a Stack
// Remove and return the top element of the stack
int Stack::pop() {
if(isEmpty()) {
std::cout << "Stack is empty!" <<
std::endl;
return -1; // Indicate error
} else {
return data[top--];
}
}

CSC10002 – Programming Techniques Page 11


Implement a Stack
 Using a singly-linked list: pop
push
 A pointer pTop points to the top of the
stack
 Empty stack: pTop = NULL
2

3
pTop 2 3 5
5

CSC10002 – Programming Techniques Page 12


Implement a Stack
struct Node { pop
int data; push
Node* next;
};

struct Stack {
2
Node* top;
void push(int value);
3
int pop();
int peek(); 5
bool isEmpty();
void clear();
};
CSC10002 – Programming Techniques Page 13
Implement a Stack
pop
push

void Stack::push(int value) {


2

3
}
5

CSC10002 – Programming Techniques Page 14


Implement a Stack
pop
push

void Stack::push(int value) {


Node* newNode = new Node(value); 2
newNode->next = top;
3
top = newNode;
} 5

CSC10002 – Programming Techniques Page 15


Implement a Stack
int Stack::pop() { pop
push

CSC10002 – Programming Techniques Page 16


Implement a Stack
int Stack::pop() { pop
if (isEmpty()) { push
std::cout << "Stack is
empty!";
return -1; // Indicate error
} else { 2
Node* temp = top;
3
int poppedValue = top->data;
top = top->next;
5
delete temp;
return poppedValue;
}
CSC10002 – Programming Techniques Page 17
Stack Applications
 Delimiter Matching (part of any compiler)
 C++: ( ) { } /* */ [ ]
 Expression: ((a + b) * (c - d + e))
 Delimiters can be nested
 Idea:
 Opening Delimiters: When an opening delimiter ((, {, [, /*) is
encountered, push it onto the stack
 Closing Delimiters: When a closing delimiter (), }, ], */) is
encountered, perform the following checks:
 If the stack is empty, the expression is invalid.
 Pop the top element from the stack. If the popped element does
not match the corresponding opening delimiter for the closing
delimiter encountered, the expression is invalid.
CSC10002 – Programming Techniques Page 18
Stack
// Function to check if the delimiters in the expression are correctly
matched
bool checkDelimiters(const std::string& expression) {
Stack stack;
for (int i = 0; i < expression.length(); i++) {
char ch = expression[i];
// Push the ASCII value of opening delimiters onto the stack
if (ch == '(' || ch == '{' || ch == '[‘) stack.push(ch);

// For closing delimiters, check if they match the top of the stack
else if (ch == ')' || ch == '}' || ch == ']') {
if (stack.isEmpty() || !isMatch(stack.pop(), ch)) {
// No matching opening delimiter or mismatched pair
return false;
}
}
}
// If the stack is empty, all delimiters were matched correctly
return stack.isEmpty();
} CSC10002 – Programming Techniques Page 19
Stack Applications
 Adding two large number
 Treat these numbers as strings of numerals, store the numbers
corresponding to these numerals on 2 stacks
 Perform addition by popping numbers from the stacks

CSC10002 – Programming Techniques Page 20


Queue

CSC10002 – Programming Techniques Page 21


Queue
 A queue is a data structure that stores and retrieves elements
in a first-in-first-out (or FIFO) manner

CSC10002 – Programming Techniques Page 22


Queue
 The following are the basic operations served by stacks.
 enqueue(): adding an element to the end of the queue
 dequeue(): removing an element from the front of the
queue
 front(): get the element at the front of the queue without
removing it
 initialize(): Creates an empty queue
 isEmpty(): Check if the queue is empty
 isFull(): Checks if the queue is full

CSC10002 – Programming Techniques Page 23


Implement a Queue
 Using Array

CSC10002 – Programming Techniques Page 24


Implement a Queue
 Using Array

CSC10002 – Programming Techniques Page 25


Implement a Queue
struct Queue {
int arr[MAX_SIZE];
int front, rear;

// Constructor
Queue(){
front = -1;
rear = -1;
}
void enqueue(int value);
int dequeue();
int peek();
bool isEmpty();
bool isFull();
};
CSC10002 – Programming Techniques Page 26
Implement a Queue
void Queue::enqueue(int value) {

CSC10002 – Programming Techniques Page 27


Implement a Queue
void Queue::enqueue(int value) {
if (isFull()) {
std::cout << "Queue is full!\n";
} else {
if (front == -1) front = 0;
// If inserting the first element, set
front to 0
arr[++rear] = value;
}
}

CSC10002 – Programming Techniques Page 28


Implement a Queue
int Queue::dequeue() {

CSC10002 – Programming Techniques Page 29


Implement a Queue
int Queue::dequeue() {
if (isEmpty()) {
std::cout << "Queue is empty!\n";
return -1; // Indicate error
} else {
int value = arr[front];
if (front >= rear) {
// Reset the queue if the last element is dequeued
front = -1;
rear = -1;
} else {
front++;
}
return value;
}
}
CSC10002 – Programming Techniques Page 30
Implement a Queue
bool Queue::isEmpty() {

bool Queue::isFull() {

CSC10002 – Programming Techniques Page 31


Implement a Queue
bool Queue::isEmpty() {
return front == -1;
}

bool Queue::isFull() {
return rear == MAX_SIZE - 1;
}

CSC10002 – Programming Techniques Page 32


Implement a Queue
 Using Linked List:

CSC10002 – Programming Techniques Page 33


Implement a Queue
void Queue::enqueue(int value) {

CSC10002 – Programming Techniques Page 34


Implement a Queue
void Queue::enqueue(int value) {
Node* newNode = new Node(value);
if (rear == nullptr) {
// If the queue is empty,
// new node becomes both front and rear
front = rear = newNode;
} else {
// Attach the new node at the end and update
rear
rear->next = newNode;
rear = newNode;
}
} CSC10002 – Programming Techniques Page 35
Implement a Queue
int Queue::dequeue() {

CSC10002 – Programming Techniques Page 36


Implement a Queue
int Queue::dequeue() {
if (isEmpty()) {
std::cout << "Queue is empty!\n"; return -1; // Indicate
error
} else {
Node* temp = front;
int value = front->data;
front = front->next;
if (front == nullptr) {
// If the queue becomes empty,
// rear is also set to nullptr
rear = nullptr;
}
delete temp; return value;
}
}
CSC10002 – Programming Techniques Page 37
Queue Applications
 Task Scheduling
 Resource Allocation
 Message buffering
 Traffic Management
 Print jobs, procedures management
 Download jobs in browsers

CSC10002 – Programming Techniques Page 38


Exercises
 Reverse a Queue
 Objective: Implement a function in C++ that reverses the
elements of a queue. You may use a stack as an auxiliary data
structure to hold the elements of the queue while you perform
the reversal.

CSC10002 – Programming Techniques Page 39


Exercises
 Sort Values in a Stack
 Objective: Write a program in C++ that sorts the values in a
stack in ascending order. You may use additional stacks but no
other data structures like arrays, lists, etc.

CSC10002 – Programming Techniques Page 40


THANK YOU
for YOUR ATTENTION

CSC10002 – Programming Techniques Page 41

You might also like