stack
stack
CHAPTER 3: STACK
3.1 STACK
3.1.1 MEMORY REPRESENTATION OF STACKS (USING ARRAYS)
3.2 OPERATIONS ON STACK
3.2.1 PUSH OPERATION
3.2.2 POP OPERATION
3.3 APPLICATIONS OF STACK
3.3.1 EVALUATION OF POSTFIX-EXPRESSION
3.3.2 INFIX TO POSTFIX CONVERSION
3.3.3 RECURSION
STACK
3-1
DATA STRUCTURES AND APPLICATIONS
CHAPTER 3: STACK
3.1 STACK
Definition
• A stack is a linear data-structure.
• It follows the Last In, First Out (LIFO) principle.
Key Features
• Elements are added or removed only from one end called the top.
Operations
1) Insert: An element is inserted from top end. Insertion operation is called
push operation.
2) Delete: An element is deleted from top end. Deletion operation is called pop
operation.
3) Overflow: Check whether the stack is full or not.
4) Underflow: Check whether the stack is empty or not.
Applications
1) Converting infix expressions to postfix.
2) Evaluating postfix-expressions.
3) Managing function calls during recursion.
STACK
3-3
DATA STRUCTURES AND APPLICATIONS
STACK
3-4
DATA STRUCTURES AND APPLICATIONS
STACK
3-5
DATA STRUCTURES AND APPLICATIONS
}
}
while (true)
{
cout << "1. PUSH" << endl; cout
<< "2. POP" << endl; cout << "3.
DISPLAY" << endl; cout << "4.
EXIT" << endl; cout << "Enter
your choice: "; cin >> choice;
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return 0; default:
cout << "Invalid choice! Please try again." << endl;
}
}
STACK
3-6
DATA STRUCTURES AND APPLICATIONS
return 0;
}
Example 1: Postfix-expression 3 4 X 2 +
Example 2: Postfix-expression 7 8 2 * 4 /+
STACK
3-7
DATA STRUCTURES AND APPLICATIONS
Example 3: Postfix-expression 2 3 4 X +
STACK
3-8
DATA STRUCTURES AND APPLICATIONS
Example 4: Postfix-expression 3 4 X 2 5 X +
STACK
3-9
DATA STRUCTURES AND APPLICATIONS
float eval();
void push(float num);
float pop();
int main() {
int i;
float result;
i = 0;
while (postfix[i] != '\0') {
if (isalpha(postfix[i])) {
cout << "Enter value for operand " << postfix[i] << ": ";
cin >> value[i];
}
i++;
}
result = eval();
cout << "Result = " << result << endl;
return 0;
}
float eval() {
int i = 0;
float op1, op2, res;
STACK
3-10
DATA STRUCTURES AND APPLICATIONS
switch (postfix[i]) {
case '*':
push(op1 * op2);
break;
case '/':
push(op1 / op2);
break;
case '+':
push(op1 + op2);
break;
case '-':
push(op1 - op2);
break;
default:
break;
}
}
i++;
}
res = pop();
return res;
}
float pop() {
float num = s[top];
top = top - 1;
return num;
}
Output:
Enter the postfix-expression:
ab+
Enter value for operand a: 5
Enter value for operand b: 3
Result = 8
STACK
3-11
DATA STRUCTURES AND APPLICATIONS
STACK
3-12
DATA STRUCTURES AND APPLICATIONS
STACK
3-13
DATA STRUCTURES AND APPLICATIONS
STACK
3-14
DATA STRUCTURES AND APPLICATIONS
STACK
3-15
DATA STRUCTURES AND APPLICATIONS
}
if (F(s[top]) != G(symbol)) {
s[++top] = symbol;
} else {
top--;
}
}
// Main function
int main() {
char infix[20];
char postfix[20];
infix_postfix(infix, postfix);
return 0;
}
Output:
Enter a valid infix expression:
a+b*c
The postfix-expression is:
abc*+
STACK
3-16
DATA STRUCTURES AND APPLICATIONS
3.3.3 RECURSION
• The stack is used to store function call details during recursion in programming.
• Each function call is pushed onto the stack and popped when completed.
• The stack ensures function calls are stored and retrieved in a LIFO order.
Activation Record
• Each function call creates an activation record stored in the stack.
• An activation record contains:
1) Arguments: Values passed to the function.
2) Local Variables: Variables declared inside the function.
3) Return Address: Address of the calling function or where the control will
return after the function execution.
Working of Recursion
1) A recursive function keeps calling itself until a base case is reached.
2) Each recursive call creates a new activation record pushed onto the stack.
3) The stack ensures the most recent function call is solved first, adhering to the LIFO
principle.
4) After the base case is resolved, the program backtracks by solving the previous
function calls using the stored activation records.
Figure 3.4: Recursive call to find factorial Figure 3.5: Activation Record
STACK
3-17
DATA STRUCTURES AND APPLICATIONS
CHAPTER 4: QUEUE
4.1 QUEUE
4.1.1 MEMORY REPRESENTATION OF QUEUES (USING ARRAYS)
4.2 OPERATIONS ON QUEUE
4.2.1 INSERT OPERATION
4.2.2 DELETE OPERATION
4.3 TYPES OF QUEUES (VARIATIONS OF QUEUES)
4.3.1 CIRCULAR-QUEUE
4.3.1.1 MEMORY REPRESENTATION OF CIRCULAR-QUEUES
4.3.1.2 OPERATIONS ON CIRCULAR-QUEUE
4.3.2 DEQUEUE (DOUBLE-ENDED QUEUE)
4.3.3 PRIORITY-QUEUES
4.3 APPLICATIONS OF QUEUES
QUEUE
4-1
DATA STRUCTURES AND APPLICATIONS
CHAPTER 4: QUEUE
4.1 QUEUE
Definition
• A queue is a linear data-structure.
• It follows the FIFO (First In, First Out) principle.
Key Features
• Elements are added at one end called the rear (or tail).
• Elements are removed from the other end called the front (or head).
Operations
1) Insert: An element is added at the rear-end.
2) Delete: An element is removed from the front-end.
3) Overflow: Check whether the queue is full or not.
4) Underflow: Check whether the queue is empty or not.
Applications
1) Scheduling processes in operating systems.
2) Handling requests in printers.
3) Managing calls in call centers (customer service).
QUEUE
4-2
DATA STRUCTURES AND APPLICATIONS
Algorithm
1) Check for Overflow: Verify if the queue is full (rear == size - 1).
2) Insert the Element: If space is available:
o Increment rear by 1.
o Insert the new element at queue[rear].
Function to Add an Element to the Queue
void insert() {
if (rear == SIZE - 1) {
cout << "Queue Overflow" << endl;
} else {
rear = rear + 1;
cout << "Enter the element to be inserted: "; cin >>
queue[rear];
}
if (front == -1) { front = 0;
}
}
QUEUE
4-3
DATA STRUCTURES AND APPLICATIONS
Algorithm
1) Check for Underflow: Verify if the queue is empty (front == -1 or front > rear).
2) Remove the Element: If elements are present:
o Remove the value at queue[front].
o Increment front by 1.
3) Reset Queue (if Empty): If front == rear, reset both front and rear to -1.
Function to delete an element from the queue
void DeleteQ() {
if (front == -1) {
cout << "Queue Underflow" << endl;
} else {
cout << "The element deleted is " << queue[front] << endl; if (front == rear) {
front = -1;
rear = -1;
} else {
front = front + 1;
}
}
}
QUEUE
4-4
DATA STRUCTURES AND APPLICATIONS
void insert() {
if (rear == SIZE - 1) {
cout << "Queue Overflow" << endl;
} else {
rear = rear + 1;
cout << "Enter the element to be inserted: ";
cin >> queue[rear];
}
if (front == -1) {
front = 0;
}
}
void DeleteQ() {
if (front == -1) {
cout << "Queue Underflow" << endl;
} else {
cout << "The element deleted is " << queue[front] << endl;
if (front == rear) {
front = -1;
rear = -1;
} else {
front = front + 1;
}
}
}
void display() {
if (front == -1) {
cout << "Queue is empty" << endl;
} else {
for (int i = front; i <= rear; i++) {
cout << queue[i] << endl;
}
}
}
QUEUE
4-5
DATA STRUCTURES AND APPLICATIONS
while (ch) {
cout << "1. Insert" << endl; cout << "2. Delete" << endl; cout << "3. Display" <<
endl; cout << "4. Exit" << endl; cout << "Enter your choice: "; cin >> ch;
insert(); break;
case 2:
DeleteQ(); break;
case 3:
display(); break;
case 4:
exit(0); break;
default:
return 0;
QUEUE
4-6
DATA STRUCTURES AND APPLICATIONS
QUEUE
4-7
DATA STRUCTURES AND APPLICATIONS
void insert() {
if ((rear + 1) % SIZE == front) {
cout << "Queue Overflow" << endl;
} else {
rear = (rear + 1) % SIZE;
cout << "Enter the element to be inserted: "; cin >> queue[rear];
if (front == -1) { front = 0;
}
}
}
QUEUE
4-8
DATA STRUCTURES AND APPLICATIONS
DELETE OPERATION
Definition
• Removes the front element of the circular-queue.
Algorithm
1) Check for Underflow: If front == -1, the queue is empty.
2) Remove the Element: If elements are present:
o Remove the value at queue[front].
o Increment front as (front + 1) % size.
3) Reset Queue (if Empty): If front == rear, reset both front and rear to -1.
Function to Delete an Element from the Circular-queue
void DeleteQ() {
if (front == -1) {
cout << "Queue Underflow" << endl;
} else {
cout << "The element deleted is " << queue[front] << endl; if (front == rear) {
front = -1;
rear = -1;
} else {
front = (front + 1) % SIZE;
}
}
}
QUEUE
4-9
DATA STRUCTURES AND APPLICATIONS
QUEUE
4-10
DATA STRUCTURES AND APPLICATIONS
QUEUE
4-11
DATA STRUCTURES AND APPLICATIONS
void insert() {
if ((rear + 1) % SIZE == front) {
cout << "Queue Overflow" << endl;
} else {
rear = (rear + 1) % SIZE;
cout << "Enter the element to be inserted: ";
cin >> queue[rear];
if (front == -1) {
front = 0;
}
}
}
void DeleteQ() {
if (front == -1) {
cout << "Queue Underflow" << endl;
} else {
cout << "The element deleted is " << queue[front] << endl;
if (front == rear) {
front = -1;
rear = -1;
} else {
front = (front + 1) % SIZE;
}
}
}
void display() {
if (front == -1) {
cout << "Queue is empty" << endl;
} else {
for (int i = front; i <= rear; i++) {
cout << queue[i] << endl;
}
}
}
int main() { int ch = 1;
while (ch) {
QUEUE
4-12
DATA STRUCTURES AND APPLICATIONS
cout << "1. Insert" << endl; cout << "2. Delete" << endl; cout << "3. Display"
<< endl; cout << "4. Exit" << endl; cout << "Enter your choice: "; cin >> ch;
switch (ch) { case 1:
insert(); break;
case 2:
DeleteQ(); break;
case 3:
display(); break;
case 4:
exit(0); break;
default:
cout << "Invalid choice" << endl;
}
}
return 0;
}
Key Features
• It is not restricted to the FIFO principle.
• Supports insertion and deletion at both ends of the queue.
Types of Dequeues
1) Input-Restricted Dequeue: Insertion is allowed only at one end, but deletion
can be done from both ends.
2) Output-Restricted Dequeue: Deletion is allowed only at one end, but insertion can
be done from both ends.
QUEUE
4-13
DATA STRUCTURES AND APPLICATIONS
Applications
• Used in implementing sliding window algorithms.
• Helpful in managing tasks where operations at both ends are required, such as undo
functionality.
4.3.3 PRIORITY-QUEUES
Definition
• A priority-queue stores elements based on their priority instead of their arrival
order.
Key Features
• Elements with higher priority are processed before elements with lower priority.
• If two elements have the same priority, they are processed based on their order of
arrival (FIFO).
Operations
1) Insert: Add an element to the queue along with its priority.
2) Delete: Remove the element with the highest priority.
Types
1) Max-Priority-queue: Processes elements with the highest priority first.
2) Min-Priority-queue: Processes elements with the lowest priority first.
Applications
• Scheduling tasks in operating systems (e.g., job scheduling).
• Managing data in simulations and network routing.
QUEUE
4-14
DATA STRUCTURES AND APPLICATIONS
QUEUE
4-15
DATA STRUCTURES AND APPLICATIONS
QUEUE
4-16
DATA STRUCTURES AND APPLICATIONS
QUEUE
4-17