Dsu QB
Dsu QB
A stack is linear data structure or orderly collection of data in which data may be
inserted Or deleted from one end called „top of stack‟ for this reason stack is
referred as LIFO( Last In First Out).
It is the last element that was pushed onto the stack and will be the first element
to be popped off, following the Last-In-First-Out (LIFO) principle.
Purpose: The Pop operation removes and returns the top element from the stack.
PUSH
POP
5) .Write any two operations performed on stack
Initially stack top is set to -1.i.e stack is empty .When an element is to be inserted
first Increment the stack top by 1 and then insert new element into it.
Algorithm:
Algorithm:-
1. If TOP < 0, then
(a) Display “The Stack is empty”
(b) Exit
2. Else remove the Top most elements
3. DATA = STACK [TOP]
4. TOP = TOP – 1
5. Exit.
7) .Draw the diagram of Circular queue with front and rear pointers.
A circular queue is a linear data structure where it store all elements in a specific
order. It has Two ends front and rear where front is used to delete an element and
rear is used to insert an Element. The last location of circular queue is connected
to first location of the same. It Follows circular path while performing insertion
and deletion
• In computer system to maintain waiting list for single shared resources such as
printer, Disk, etc.
• Simulation
9) Differentiate between Stack and queue.
Stack Queue
1.Simple Queue
2.Circular Queue
3.Priority Queue
#include <stdio.h>
#define MAX 10 // You can change this value for different queue sizes
int queue[MAX];
void insert() {
int value;
if (rear == MAX - 1) {
printf("\nQueue is full!\n");
} else {
front = 0;
rear++;
queue[rear] = value;
}
}
void del() {
if (front == -1) {
printf("\nQueue is empty!\n");
} else {
front++;
}
}
void display() {
if (front == -1) {
printf("\nQueue is empty!\n");
} else {
printf("\n");
// Main function
int main() {
int choice;
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
printf("\nExiting program.\n");
return 0;
default:
return 0;
Output :-
1. Insert
2. Delete
3. Display
4. Exit
1. Insert
2. Delete
3. Display
4. Exit
1. Insert
2. Delete
3. Display
4. Exit
Queue elements: 10 20
1. Insert
2. Delete
3. Display
4. Exit
Deleted value: 10
1. Insert
2. Delete
3. Display
4. Exit
Queue elements: 20
1. Insert
2. Delete
3. Display
4. Exit
Exiting program.
#include <stdio.h>
#define MAX 5 // Maximum size of the queue
int queue[MAX];
void enqueue() {
int value;
if (rear == MAX - 1) {
scanf("%d", &value);
printf("\n");
int main() {
int choice;
// Infinite loop to display menu and handle user input
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
break;
case 2:
printf("\nExiting program.\n");
default:
return 0;
Output : -
1. Enqueue
2. Exit
Current queue: 10
1. Enqueue
2. Exit
Enqueued: 20
Current queue: 10 20
1. Enqueue
2. Exit
Enqueued: 30
Current queue: 10 20 30
1. Enqueue
2. Exit
Current queue: 10 20 30 40
1. Enqueue
2. Exit
Enqueued: 50
Current queue: 10 20 30 40 50
1. Enqueue
2. Exit
1. Enqueue
2. Exit
Exiting program.
3.Show the effect of Push & Pop operation on stack of size 10. The stack contains
10,20,30,40,50, and 60,begn at top of the stack
5) Write a menu driven 'c' program to implement Stack using array with the menu (i) PUSH
(ii) pop (iii) display lix) Exit.
#include <stdio.h>
int stack[MAX];
void push() {
int value;
if (top == MAX - 1) {
} else {
scanf("%d", &value);
top++;
stack[top] = value;
void pop() {
if (top == -1) {
top--;
void display() {
if (top == -1) {
printf("Stack is empty.\n");
} else {
printf("\n");
int main() {
int choice;
while (1) {
printf("\nMenu:\n");
printf("1. PUSH\n");
printf("2. POP\n");
printf("3. DISPLAY\n");
printf("4. EXIT\n");
scanf("%d", &choice);
switch (choice) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Exiting program.\n");
return 0;
default:
Output :-
Menu:
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Menu:
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Menu:
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Stack elements: 10 5
Menu:
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Menu:
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Stack elements: 15 10 5
Menu:
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Menu:
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Stack elements: 10 5
Menu:
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Exiting program.
A priority queue is a queue in which the intrinsic ordering among the elements
Decides the result of its basic operations i.e. the ordering among the elements
decides The manner in which Add and Delete operations will be performed. In a
priority Queue,
Above figure shows priority. Queue with 5 elements where B & C have same
Priority number.
An example where priority queue are used is in operating systems. The operating
System has to handle a large number of jobs. These jobs have to be properly
Scheduled. The operating system assigns priorities to each type of job. The jobs
are
Placed in a queue and the job with the highest priority will be executed first.
7) show the effect of INSERT and DELETE oporation onto the linear queue of size
10. The linear queue Sequential Contains 20, 20, 30, 40, and 50 where to is at front
queue.