














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 22
This page cannot be seen from the preview
Don't miss anything!
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:
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*****
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
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
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 *****"
At any time the position of the element to be inserted will be calculated by the relation Rear = (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 *****