Data Structures and Algorithms in C|Set-2Stacks 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 user-defined, which means that when we say int arr[], the compiler can recognize that it is an array, but when we say Stack, the compiler can't recognize it without us defining it in the program. Stacks:Stack is a linear data structure. It is implemented on the principle "LIFO" abbreviation: Last in, first out. It means that the element that is last inserted into a stack will be the first one that gets deleted. A stack only has one opening, which means to insert or delete elements; we need to use the same end. When we insert elements into a stack, we insert elements on top of each other-new elements on the existing element. After inserting all the elements, if we want to delete elements from the Stack, the last element inserted will be the first to come out. Terminologies:
Functions for stacks:
Push and Pop:Push and pop are the two major operations on a stack. Hence, let us understand them elaborately: Suppose we have a stack called "Hundreds". It's capacity is 10*sizeof(integer) = 40 bytes. Top index = 3, top element = 400 It has four elements. Now let's perform push and pop on it: push(Hundreds, 550):
pop(): 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. If top_index = -1: stack is empty But here, top_index = 4. Hence Stack is not empty. 2. top = top - 1 Hundreds[top] Examples of Stack in Real-Life:
Implementation of a Stack:We can implement a stack in 2 ways in C:
1. Using Arrays: Output: 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
2. Using Linked lists: Output: 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 push and pop, to implement a stack. We need to make sure of two points:
Queues:A queue is a linear data structure like a stack, but the principle of queue implementation is FIFO-First in, first out. It means that the element inserted into the queue will be the first to come out of it, whereas, in a stack, the most recently pushed element will be the first to be popped out. Important points about a Queue:
Examples of Queues in Real-Life:
Terminology:
We can Implement a Queue in C:
1. Using Arrays: Output: 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 . . .
rear: (Inserting order): H - E - L - L - O(0 -> 1 -> 2 -> 3 -> 4) front: (Deleting order): O - L - L - E - H(4 -> 3 -> 2 -> 1 -> 0) 2. Using Linked lists: Output: 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 . . .
3. Using Stacks:
Let us take an example: 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. We can implement a queue using stacks in two ways: 1. Dequeue operation made costly 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). 2. enqueue operation made costly let us take an example: We have a stack "stack1" with one element-100:
Here is a program implementing using the first way - dequeue operation made costly: Output: 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 |