0% found this document useful (0 votes)
23 views13 pages

Stack and Queue

The document defines the Push() and Pop() operations for stacks, explaining how elements are added and removed while checking for overflow and underflow conditions. It also describes the enqueue() and dequeue() operations for queues, highlighting their FIFO principle and applications in task scheduling and resource management. Additionally, it compares stacks and queues, discusses circular queues, priority queues, and introduces double-ended queues (deques).
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
23 views13 pages

Stack and Queue

The document defines the Push() and Pop() operations for stacks, explaining how elements are added and removed while checking for overflow and underflow conditions. It also describes the enqueue() and dequeue() operations for queues, highlighting their FIFO principle and applications in task scheduling and resource management. Additionally, it compares stacks and queues, discusses circular queues, priority queues, and introduces double-ended queues (deques).
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
3. Define Push() and Pop() operation. 2/5 Marks Ans: PUSH: PUSH operation implies the insertion of a new element into a Stack. A new element is always inserted from the topmost position of the Stack; thus, we always need to check if the top is empty or not, i.e., TOP=Max-1 if this condition goes false, it means the Stack is full, and no more elements can be inserted, and even if we try to insert the element, a Stack overflow message will be displayed. POP: POP means to delete an element from the Stack. Before deleting an element, make sure to check if the Stack Top is NULL, i.e., TOP=NULL. If this condition goes true, it means the Stack is empty, and no deletion operation can be performed, and even if we try to delete, then the Stack underflow message will be generated. void push(int value) { if (top == MAX - 1) { printf("Stack Overflow! Cannot push %d\n", value); } else { topt+; stack[top] = value; printf("Pushed %d\n", value) i } int pop() { if (top == -1) { printf("Stack Underflow! Cannot Pop\n"); return -1; } else { int popped = stack[top]; top--; printf("Popped %d\n", popped); return popped; 1; + void display() { if (top == -1) { printf("Stack is empty.\n"); } else { printf("Stack elements: ") for (int i = 0; i <= top; i++) { printf("%d ", stack[i]); a printf("\n"); 4. Define enqueue() and dequeue() operation. 2/5 Marks Ans: In data structure, enqueue is the operation used to insert an element at the rear end of a queue. It follows the First-In, First-Out (FIFO) principle, ensuring that new elements join the end of the line. This operation is commonly used in applications like job scheduling, buffering, and resource management. Dequeue is the operation used to remove an element from the front end of a queue. It returns and deletes the oldest element that was inserted first, maintaining the FIFO order. This operation is useful in processing tasks in the order they arrive, such as in queues for printers, CPU scheduling, or customer service systems. void enqueue(int value) { if (rear == MAX - 1) { printf("Queue Overflow! Cannot enqueue %d\n", value); } else { if (front reart+; queue[rear] = value; printf("Enqueued %d\n", value); -1) front = + int dequeue() { if (front == -1 || front > rear) ¢ printf("Queue Underflow! Cannot dequeue\n"); return -1; } else { int deleted = queue[front]; front+ printf("Dequeued %d\n", deleted); return deleted; + + void display() { if (front == -1 || front > rear) { printf("Queue is empty.\n"); } else { printf("Queue elements: "); for (int i = front; i <= rear; it+) { printf("%d ", queuelil); + printf("\n"); A circular queue is a type of linear data structure that uses a circular array to efficiently manage elements in a queue format. It works on the First-In-First-Out (FIFO) principle, but unlike a simple queue, it connects the end of the queue back to the beginning, forming a circle. void enqueue(int value) { if ((front == 0 && rear == MAX - 1) || (rear + 1) % MAX == front) { printf("Queue Overflow! Cannot enqueue %d\n", value); } else { if (front == -1) front = 0; rear = (rear + 1) % MAX; queue[rear] = value printf("Enqueued %d\n", value); } int dequeue() { if (front printf("Queue Underflow! Cannot dequeue\n"); return -1; } else int deleted = queue[front]; if (front == rear) { front = -1; rear } else { front = (front + 1) % MAX; } printf("Dequeued %d\n", deleted); return deleted; } void display() { if (front == -1) { printf("Queue is empty.\n"); } else { printf("Queue elements: "); int i = front; while (1) ¢ printf("%d ", queue[i]); if (i == rear) break; i= (i + 1) % MAX; t printf("\n"); LINEAR QUEUE A linear data structure that stores data as a sequence of element similar to a real world queue. Possible to enter new items from the rear end and remove the items from the front. Requires more memory. Less efficient. CIRCULAR QUEUE A linear data structure in which the last item connects back to the first item forming a circle. Possible to enter and remove elements from any position. Requires less memory. More efficient. Definition: A priority queue is an abstract data structure in which each element has a priority associated with it, and elements are served based on their priority. The element with the highest (or lowest) priority is processed first. 1. Write down the application of stack. 2/5 Marks Ans: Applications of Stacks: + Function calls: Stacks are used to keep track of the return addresses of function calls, allowing the program to return to the correct location after a function has finished executing. + Recursion: Stacks are used to store the local variables and return addresses of recursive function calls, allowing the program to keep track of the current state of the recursion. + Expression evaluation: Stacks are used to evaluate expressions in postfix notation (Reverse Polish Notation). + Syntax parsing: Stacks are used to check the validity of syntax in programming languages and other formal languages. + Memory management: Stacks are used to allocate and manage memory in some operating systems and programming languages. 2. Write down the application of queue? 2/5 Marks Ans: + Task Scheduling: Queues can be used to schedule tasks based on priority or the order in which they were received. + Resource Allocation: Queues can be used to manage and allocate resources, such as printers or CPU processing time. + Batch Processing: Queues can be used to handle batch processing jobs, such as data analysis or image rendering. + Event Handling: Queues can be used to handle events in event- driven systems, such as GUI applications or simulation systems. * Traffic Management: Queues can be used to manage traffic flow in transportation systems, such as airport control systems or road networks. ‘* Operating systems: Operating systems often use queues to manage processes and resources. 5. Write down the difference between stack and queue? 2/5 Marks Ans: ‘Comparing Stack and Queue Parameter | Stack Data Structure Queue Data Structure Design ‘A Stack is a linear data ‘A Queue is also a linear data structure where removal | structure, but removal and and insertion occur at the | insertion happen at different same end. ends. Principle | A Stack follows the Last In,| A Queue follows the First In, First) First Out (LIFO) principle, | Out (FIFO) principle, meaning the| ‘meaning the most recently | earliest inserted element is inserted element is removed] removed first. first. Pointers A Stack uses a single A Queue uses two pointers, pointer, the top, to keep | the front and the rear, to keep track of the most recently | track of the first and last inserted added element. elements respectively. Operations | A Stack A Queue uses push and pop operati | uses enqueue and dequeue oper ons for insertion and ations for insertion and deletion deletion respectively. respectively. Structure | Ina Stack, both insertion | Ina Queue, insertion happens at land deletion happen at the | the rear end and deletion same end, known as the | happens at the front end. top. Full ‘A Stack is considered full | A Queue is considered full when Condition | when top equals max-1. rear equals max-1 Check Empty ‘A Stack is considered ‘A Queue is considered empty Condition | empty when top equals -1. | _ when front equals rear+! or front Check equals -1 Variants | A Stack does not have any | A Queue has three variants - variants, circular queue, priority queue, and double-ended queue. Visualizatio| A Stack can be visualized | A Queue can be visualized as a a ‘as a vertical arrangement of) horizontal arrangement of data, data elements. elements, Implementa| Implementation is generally Implementation is usually more tion impler in a Stack. complex in a Queue than in a Stack. Overflow in Stack Stack overflow occurs when you try to push an element onto a full stack, exceeding its maximum size. Underflow in Stack Stack underflow occurs when you try to pop an element from an empty stack, where no elements are available to remove. @ Definition of Double-Ended Queue (Deque): A Double-Ended Queue, or Deque (pronounced "deck’), is a linear data structure that allows insertion and deletion of elements from both the front and rear ends.

You might also like