Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Stacks, Queues, and Deques: Data Structures in Computer Science, Schemes and Mind Maps of Computer Science

Fundamental data structures in computer science, focusing on stacks, queues, and deques. It provides definitions, operations, and array representations of these structures, illustrating their applications in various scenarios. The document also delves into circular queues and priority queues, highlighting their unique characteristics and uses. suitable for introductory computer science courses, providing a foundation for understanding data structures and their role in algorithm design.

Typology: Schemes and Mind Maps

2023/2024

Available from 11/11/2024

dipika-bhosale
dipika-bhosale 🇮🇳

5 documents

1 / 22

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 03
Stacks and Queues
01 Stack:
01.1 Definition:
A stack is one of the most important and useful non-primitive linear data structure in computer
science. It is an ordered collection of items into which new data items may be added/inserted and from
which items may be deleted at only one end, called the top of the stack. As all the addition and deletion in a
stack is done from the top of the stack, the last added element will be first removed from the stack. That is
why the stack is also called Last-in-First-out (LIFO).
Note that the most frequently accessible element in the stack is the top most elements, whereas the least
accessible element is the bottom of the stack.
01.2 Operations:
The primitive operations performed on the stack are as follows:
PUSH: The process of adding (or inserting) a new element to the top of the stack is called PUSH operation.
Pushing an element to a stack will add the new element at the top. After every push operation the top is
incremented by one. If the array is full and no new element can be accommodated, then the stack overflow
condition occurs.
POP: The process of deleting (or removing) an element from the top of stack is called POP operation. After
every pop operation the stack is decremented by one. If there is no element in the stack and the pop
operation is performed then the stack underflow condition occurs.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Stacks, Queues, and Deques: Data Structures in Computer Science and more Schemes and Mind Maps Computer Science in PDF only on Docsity!

Chapter 03

Stacks and Queues

01 Stack: 01.1 Definition: A stack is one of the most important and useful non-primitive linear data structure in computer science. It is an ordered collection of items into which new data items may be added/inserted and from which items may be deleted at only one end, called the top of the stack. As all the addition and deletion in a stack is done from the top of the stack, the last added element will be first removed from the stack. That is why the stack is also called Last-in-First-out (LIFO). Note that the most frequently accessible element in the stack is the top most elements, whereas the least accessible element is the bottom of the stack. 01.2 Operations: The primitive operations performed on the stack are as follows: PUSH: The process of adding (or inserting) a new element to the top of the stack is called PUSH operation. Pushing an element to a stack will add the new element at the top. After every push operation the top is incremented by one. If the array is full and no new element can be accommodated, then the stack overflow condition occurs. POP: The process of deleting (or removing) an element from the top of stack is called POP operation. After every pop operation the stack is decremented by one. If there is no element in the stack and the pop operation is performed then the stack underflow condition occurs.

The insertion (or addition) operation is referred to as push , and the deletion (or remove) operation as pop. A stack is said to be empty or underflow , if the stack contains no elements. At this point the top of the stack is present at the bottom of the stack. And it is overflow when the stack becomes full, i.e. , no other elements can be pushed onto the stack. At this point the top pointer is at the highest location of the stack. 01.3 Array representation of stack: In the computer’s memory, stacks can be represented as a linear array. Every stack has a variable called TOP associated with it, which is used to store the address of the topmost element of the stack. It is this position where the element will be added to or deleted from. There is another variable called MAX, which is used to store the maximum number of elements that the stack can hold. If TOP = NULL, then it indicates that the stack is empty and if TOP = MAX–1, then the stack is full. Algorithm: Push_Operation_on_stack Input: VALUE: Insert element from user Output: TOP Algorithm: Pop_operation_on_stack Input: -- Output: TOP

**5. Conversion of an infix expression into a prefix expression:

  1. Evaluation of a prefix expression** : For example, The infix expression 9 – ((3 * 4) + 8) / 4 can be consider the prefix expression + – 9 2 7 * 8 / 4 12. 7. Recursion : A recursive function is defined as a function that calls itself to solve a smaller version of its task until a final call is made which does not require a call to itself. Since a recursive function repeatedly calls itself, it makes use of the system stack to temporarily store the return address and local variables of the calling function. 8. Tower of Hanoi : Look at Fig. 7.33 which shows three rings mounted on pole A. The problem is to move all these rings from pole A to pole C while maintaining the same order. The main issue is that the smaller disk must always come above the larger disk.

Write a program to perform Push, Pop, and Peek operations on a stack. #include <stdio.h> #include <stdlib.h> #include <conio.h> #define MAX 3 // Altering this value changes size of stack created int st[MAX], top=-1; void push(int st[], int val); int pop(int st[]); int peek(int st[]); void display(int st[]); int main() { int val, option; do { printf("\n *****MAIN MENU*****"); printf("\n 1. PUSH"); printf("\n 2. POP"); printf("\n 3. PEEK"); printf("\n 4. DISPLAY"); printf("\n 5. EXIT"); printf("\n Enter your option: "); scanf("%d", &option);

void display(int st[]) { int i; if(top == - 1) printf("\n STACK IS EMPTY"); else { for(i=top;i>=0;i--) printf("\n %d",st[i]); printf("\n"); // Added for formatting purposes } } int peek(int st[]) { if(top == - 1) { printf("\n STACK IS EMPTY"); return - 1; } else return (st[top]); } Output: *****MAIN MENU*****

  1. PUSH
  2. POP
  3. PEEK
  4. DISPLAY
  5. EXIT Enter your option : 1 Enter the number to be pushed on stack : 500 02 Queue: 02.1 Definition: A queue is logically a first in first out (FIFO or first come first serve) linear data structure. The concept of queue can be understood by our real life problems. For example a customer come and join in a queue to take the train ticket at the end (rear) and the ticket is issued from the front end of queue. That is, the customer who arrived first will receive the ticket first. It means the customers are serviced in the order in which they arrive at the service centre.

02.2 Operations: It is a homogeneous collection of elements in which new elements are added at one end called rear , and the existing elements are deleted from other end called front. The basic operations that can be performed on queue are

  1. Insert (or add) an element to the queue (push)
  2. Delete (or remove) an element from a queue (pop) Push operation will insert (or add) an element to queue, at the rear end, by incrementing the array index. Pop operation will delete (or remove) from the front end by decrementing the array index and will assign the deleted value to a variable. Total number of elements present in the queue is front-rear+1, when implemented using arrays.

02.3 Array representation of queue: Queues can be easily represented using linear arrays. As stated earlier, every queue has front and rear variables that point to the position from where deletions and insertions can be done, respectively. In Fig. 8.1, FRONT = 0 and REAR = 5. Suppose we want to add another element with value 45, then REAR would be incremented by 1 and the value would be stored at the position pointed by REAR. The queue after addition would be as shown in Fig. 8.2. Here, FRONT = 0 and REAR = 6. Every time a new element has to be added, we repeat the same procedure. If we want to delete an element from the queue, then the value of FRONT will be incremented. Deletions are done from only this end of the queue. The queue after deletion will be as shown in Fig. 8.3. Here, FRONT = 1 and REAR = 6. However, before inserting an element in a queue, we must check for overflow conditions. An overflow will occur when we try to insert an element into a queue that is already full. When REAR = MAX

  • 1, where MAX is the size of the queue, we have an overflow condition. Note that we have written MAX – 1 because the index starts from 0.

Similarly, before deleting an element from a queue, we must check for underflow conditions. An underflow condition occurs when we try to delete an element from a queue that is already empty. If FRONT = – 1 and REAR = – 1, it means there is no element in the queue. Let us now look at Figs 8.4 and 8.5 which show the algorithms to insert and delete an element from a queue. Algorithm: insert an element in a queue Input: NUM: Input from user Output: FRONT and REAR Algorithm: Delete element from a queue Input: -- Output: FRONT and REAR 02.4 Applications of Queue: 1 Queues are widely used as waiting lists for a single shared resource like printer, disk, CPU. 2 Queues are used to transfer data asynchronously (data not necessarily received at same rate as sent) between two processes (IO buffers), e.g., pipes, file IO, sockets.

case 4: display(); break; } }while(option != 5); getch(); return 0; } void insert() { int num; printf(“\n Enter the number to be inserted in the queue : “); scanf(“%d”, &num); if(rear == MAX-1) printf(“\n OVERFLOW”); else if(front == - 1 && rear == - 1) front = rear = 0; else rear++; queue[rear] = num; } int delete_element() { int val; if(front == - 1 || front>rear) { printf(“\n UNDERFLOW”); return - 1; } else { val = queue[front]; front++; if(front > rear) front = rear = - 1; return val; } } int peek() { if(front==-1 || front>rear) { printf(“\n QUEUE IS EMPTY”); return - 1; } else { return queue[front]; } }

void display() { int i; printf(“\n”); if(front == - 1 || front > rear) printf(“\n QUEUE IS EMPTY”); else { for(i = front;i <= rear;i++) printf(“\t %d”, queue[i]); } } Output ***** MAIN MENU *****"

  1. Insert an element
  2. Delete an element
  3. Peek
  4. Display the queue
  5. EXIT Enter your option : 1 Enter the number to be inserted in the queue : 50 02.5 Circular queue: Suppose a queue Q has maximum size 5, say 5 elements pushed and 2 elements popped. Now if we attempt to add more elements, even though 2 queue cells are free, the elements cannot be pushed. Because in a queue, elements are always inserted at the rear end and hence rear points to last location of the queue array Q[4]. That is queue is full (overflow condition) though it is empty. This limitation can be overcome if we use circular queue. In circular queues the elements Q[0],Q[1],Q[2] .... Q[ n – 1] is represented in a circular fashion with Q[1] following Q[n]. A circular queue is one in which the insertion of a new element is done at the very first location of the queue if the last location at the queue is full. Suppose Q is a queue array of 6 elements. Push and pop operation can be performed on circular. The following figures will illustrate the same.

At any time the position of the element to be inserted will be calculated by the relation Rear = (Rear

    1. % SIZE After deleting an element from circular queue the position of the front end is calculated by the relation Front= (Front + 1) % SIZE After locating the position of the new element to be inserted, rear , compare it with front. If (rear = front), the queue is full and cannot be inserted anymore. Algorithm: Algorithm to insert an element in a circular queue Input: VAL: data from user Output: FRONT & REAR

Algorithm: Algorithm to delete an element from a circular queue Input:-- Output: FRONT & REAR Write a program to implement a circular queue. #include <stdio.h> #include <conio.h> #define MAX 10 int queue[MAX]; int front=–1, rear=–1; void insert(void); int delete_element(void); int peek(void); void display(void); int main() { int option, val; clrscr(); do { printf("\n ***** MAIN MENU *****"); printf("\n 1. Insert an element"); printf("\n 2. Delete an element"); printf("\n 3. Peek"); printf("\n 4. Display the queue"); printf("\n 5. EXIT"); printf("\n Enter your option : "); scanf("%d", &option); switch(option) {

val = queue[front]; if(front==rear) front=rear=–1; else { if(front==MAX–1) front=0; else front++; } return val; } int peek() { if(front==–1 && rear==–1) { printf("\n QUEUE IS EMPTY"); return – 1; } else { return queue[front]; } } void display() { int i; printf("\n"); if (front ==–1 && rear= =–1) printf ("\n QUEUE IS EMPTY"); else { if(front<rear) { for(i=front;i<=rear;i++) printf("\t %d", queue[i]); } else { for(i=front;i<MAX;i++) printf("\t %d", queue[i]); for(i=0;i<=rear;i++) printf("\t %d", queue[i]); } } } Output ***** MAIN MENU *****

  1. Insert an element
  2. Delete an element
  1. Peek
  2. Display the queue
  3. EXIT Enter your option : 1 Enter the number to be inserted in the queue : 25 Enter your option : 2 The number deleted is : 25 Enter your option : 3 QUEUE IS EMPTY Enter your option : 5 02.6 Priority queue: A priority queue is a data structure in which each element is assigned a priority. The priority of the element will be used to determine the order in which the elements will be processed. The general rules of processing the elements of a priority queue are
  4. An element with higher priority is processed before an element with a lower priority.
  5. Two elements with the same priority are processed on a first-come-first-served (FCFS) basis. A priority queue can be thought of as a modified queue in which when an element has to be removed from the queue, the one with the highest-priority is retrieved first. The priority of the element can be set based on various factors. Priority queues are widely used in operating systems to execute the highest priority process first. The priority of the process may be set based on the CPU time it requires to get executed completely. For example, if there are three processes, where the first process needs 5 ns to complete, the second process needs 4 ns, and the third process needs 7 ns, then the second process will have the highest priority and will thus be the first to be executed. However, CPU time is not the only factor that determines the priority, rather it is just one among several factors. Another factor is the importance of one process over another. In case we have to run two processes at the same time, where one process is concerned with online order booking and the second with printing of stock details, then obviously the online booking is more important and must be executed first. Array Representation of a Priority Queue When arrays are used to implement a priority queue, then a separate queue for each priority number is maintained. Each of these queues will be implemented using circular arrays or circular queues. Every individual queue will have its own FRONT and REAR pointers.