2023 Slot07 Stack Queue
2023 Slot07 Stack Queue
Stack - Queue
Instructor:
Nguyen Tran Duy
Minh
CSC10002 – Programming Techniques Page 1
Content
1 Review
2 Stack
3 Queue
top = -1 top = 2
3
pTop 2 3 5
5
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
3
}
5
// 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
// 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) {
bool Queue::isFull() {
bool Queue::isFull() {
return rear == MAX_SIZE - 1;
}