## Data Structures and Algorithms in C|Set-2## Stacks and QueuesThis tutorial will teach two other linear data structures in C-stacks and queues. In the first part of this tutorial, we covered the basics of arrays, dynamic arrays, and linked lists. Both stacks and queues are ## Stacks:Stack is a linear data structure. It is implemented on the principle
- Inserting an element into the Stack:
**push** - Deleting an element from the Stack:
**pop** - The end/ opening of the Stack:
**top of the Stack**
## Functions for stacks:
## Push and Pop:Push and pop are the two major operations on a stack. Hence, let us understand them elaborately:
It has four elements. Now let's perform push and pop on it:
- Check if the Stack is full. If it is full, push operation is not possible. Total capacity = 40 bytes. Used capacity = 16 bytes. Hence, the Stack is not filled.
- top = top + 1;
Hundreds[top] = 550 Hundreds[4] = 550.
550 is not a multiple of 100. We mistakenly pushed it into the Stack. It is on the top of the Stack. Now, we need to pop it out. 1. Check if the Stack is empty. If it is empty, we cannot perform the pop operation.
But here, top_index = 4. Hence Stack is not empty. 2. top = top - 1 Hundreds[top] ## Examples of Stack in Real-Life:**Champagne Tower:** After arranging all the glasses, you can't take the glass from below the tower. It'll collapse the whole tower. Hence, the last glass arranged-the top glass should be the first to be taken-**LIFO principle**.**Plates:** After piling up plates in the kitchen, we won't take the last plate; we'll take them from the top end.
## Implementation of a Stack:We can implement a stack in 2 ways in C: - Using Arrays
- Using Linked lists
100 pushed into the Stack Elements in the Stack: 100 200 pushed into the Stack Elements in the Stack: 200 100 300 pushed into the Stack Elements in the Stack: 300 200 100 After the pop operation, elements in the Stack: Elements in the Stack: 200 100 - Like in implementing a linked list, we used a structure to represent Stack. Inside the structure, we created an array. size and top are the properties of the Stack.
Push operation: Enter a value to push:100 100 pushed Resultant Stack: The Stack: 100 Enter a value to push:200 200 pushed Resultant Stack: The Stack: 200 100 Enter a value to push:300 300 pushed Resultant Stack: The Stack: 300 200 100 Pop operation: Resultant Stack: The Stack: 200 100 -------------------------------- Process exited after 5.783 seconds with return value 0 Press any key to continue . . . We wrote two methods - When push is performed, we should always add the elements at the beginning of the linked list. -
**LI(first in)** - When pop is performed, the element from the beginning has to be deleted. -
**FO(first out)** - We created
**isEmpty()**and**peek()**to check if the Stack is empty because if a stack is empty, we can't perform pop.
## Queues:A queue is a linear data structure like a stack, but the principle of queue implementation is ## Important points about a Queue:- There will be two ends to a queue-front and rear ends.
- The elements are inserted from the front end and deleted from the rear end.
**Queues:**In our day-to-day lives, we keep seeing people standing in queues at different places like cinema theatres, KFC token counters, etc. The person who started the queue will be the first to get the ticket.**One-way road:**The car that enters the lane first will be the first one to exit the lane first.
- Inserting an element into a queue:
**enqueue** - Deleting an element from the queue:
**dequeue** - Element at the beginning:
**front** - Element at the end:
**rear**
- Using Arrays
- Using linked lists
- Using stacks
Enqueue operation: Enter an element to insert into the queue: 100 100 inserted The resultant queue:100 Enter an element to insert into the queue: 200 200 inserted The resultant queue:100 200 Enter an element to insert into the queue: 300 300 inserted The resultant queue:100 200 300 Dequeue operation: The resultant queue: 200 300 -------------------------------- Process exited after 5.774 seconds with return value 2 Press any key to continue . . . - Observe that two ends, "front" and "rear," are in the queue. Initially initialized to -1, the queue is empty.
- For every enqueue operation, we keep adding an index to the queue.
- Elements are inserted from the rear end and deleted from the front end.
- For every enqueue(): rear = rear + 1
- For every dequeue(): front = front + 1
Enqueue operation: Enter the value to insert: 100 Resultant queue: 100 Enter the value to insert: 200 Resultant queue: 100 200 Enter the value to insert: 300 Resultant queue: 100 200 300 Dequeue operation: Resultant queue: 200 300 -------------------------------- Process exited after 4.771 seconds with return value 0 Press any key to continue . . . - Implementation using arrays can't be used for large-scale applications. Hence, linked lists are used to implement queues.
- Front and rear are two pointers indicating the ends in the queue. If both front and rear = NULL, the queue is empty.
- With every insertion, rear points to the next index(NULL), and with every deletion, front points to the next index.
- When inserting elements into the queue, each element is inserted from the end of the linked -
**FI (first in)** - The elements are deleted from the beginning of the linked list. -
**FO (first out)**
- Stacks follow the LIFO principle and queues follow FIFO. So, how does it work?
There is a stack, and we pushed elements into it: Now, if we perform the pop operation, 300 will be popped. But, as per the queue principle, 100 should be deleted. We need to take another stack, say, stack2, and pop elements from our Stack to stack2: Now, we can pop the elements from stack2. It will be equivalent to the same operation as enqueue in the queue.
In the above example, when we insert the elements into the first Stack, it isn't altered-O(1). But, when we performed dequeue, first we needed to pop the elements to another stack, then from that stack-dequeue operation became costly-O(n).
let us take an example: We have a stack "stack1" with one element-100: - When we need to perform enqueue on the Stack, all the existing elements in stack1 are popped into stack2.
- Say 200 is inserted into stack1. Now, all the elements popped into stack2 are pushed back into stack1.
- This way, the first entered element is always kept at the top of stack1 using stack2
Here is a program implementing using the first way -
Enqueue operation-after inserting 10, 20 and 30:10 20 30 Dequeue operation: The deleted element is 10 Resultant queue:20 30 -------------------------------- Process exited after 0.8232 seconds with return value 1 Press any key to continue . . . ## Functions in the program:
Next TopicEmployee Record System in C |