[go: up one dir, main page]

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

Dsu QB

Notes

Uploaded by

vedantwalanj18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views23 pages

Dsu QB

Notes

Uploaded by

vedantwalanj18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

DSU QUESTION BANK

Q1] Attempt Any five ( 5X2= 10 M).

1). What is Stack ?

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).

2) .What is Top of stack ?

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.

3). Give purpose of (Stack) PUSH and pop operations?

PUSH: Push operation is used to insert an element at TOP of stack

Purpose:The Push operation adds an element to the top of the stack.

POP: Pop operation is used to remove an element from TOP of stack.

Purpose: The Pop operation removes and returns the top element from the stack.

4). Enlist operations on Stack.

PUSH

POP
5) .Write any two operations performed on stack

PUSH: Push operation is used to insert an element at TOP of stack

POP: Pop operation is used to remove an element from TOP of stack.

6) Explain push and pop operation on stack with Suitable Example


PUSH: Push operation is used to insert an element in 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:

1. If TOP = SIZE – 1, then:


(a) Display “The stack is in overflow condition”
(b) Exit
2. TOP = TOP + 1
3. STACK [TOP] = ITEM
4. Exit

POP: Pop operation is used to remove an element from stack.


When pop operation is called stack top is decremented by 1.

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

8). State any four applications of queue.

• In computer system to maintain waiting list for single shared resources such as
printer, Disk, etc.

• It is used as buffers on MP3 players, iPod playlist, etc.

• Used for CPU scheduling in multiprogramming and time sharing systems.


• Handling of interrupts in real-time systems.

• Simulation
9) Differentiate between Stack and queue.

Stack Queue

1. Stack is a data structure in which 1. Queue is a data structure in


insertion and deletion operations which insertion and deletion
are performed at same end. operations are performed at
different ends.
2. In stack an element inserted last 2. In Queue an element inserted
is deleted first so it is called Last first is deleted first so it is called
In First Out list. First In First Out list.
3.In stack only one pointer is used 3.In Queue two pointers are used
called as stack top called as front and rear
4. Example: Stack of books 4. Example: Students standing in
a line at fees counter
5.Application: 5. Application:
• Recursion • In computer system for
•Polish notation organizing processes.
• In mobile device for sending
receiving messages.

10) Explain Queue overflow and underflow conditions with Examples.

Queue Overflow: Inserting an element in a queue which is already full is known as


Queue Overflow condition (Rear = Max-1)

Queue Underflow: Deleting an element from queue which is already empty is


known as Queue Underflow condition (Front == Rear=-1)

11) List any four types of queues.

1.Simple Queue

2.Circular Queue

3.Priority Queue

4.Double ended queue (Deque).


Q.2] 4 Marks

1.Write a C program for insert & delete operation to be performed on Queue ?

#include <stdio.h>

#define MAX 10 // You can change this value for different queue sizes

int queue[MAX];

int front = -1, rear = -1;

// Function to insert an element into the queue

void insert() {

int value;

if (rear == MAX - 1) {

printf("\nQueue is full!\n");

} else {

if (front == -1) { // First element

front = 0;

printf("\nEnter value to insert: ");


scanf("%d", &value);

rear++;

queue[rear] = value;

}
}

// Function to delete an element from the queue

void del() {
if (front == -1) {

printf("\nQueue is empty!\n");

} else {

printf("\nDeleted value: %d\n", queue[front]);

front++;

if (front > rear) { // Reset the queue when empty

front = rear = -1;

}
}

// Function to display the queue elements

void display() {

if (front == -1) {

printf("\nQueue is empty!\n");

} else {

printf("\nQueue elements: ");

for (int i = front; i <= rear; i++) {

printf("%d ", queue[i]);


}

printf("\n");

// Main function

int main() {

int choice;
while (1) {

printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit\n");

printf("Enter your choice: ");

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:

printf("\nInvalid choice! Try again.\n");

return 0;

Output :-
1. Insert

2. Delete

3. Display

4. Exit

Enter your choice: 1

Enter value to insert: 10

1. Insert

2. Delete

3. Display

4. Exit

Enter your choice: 1

Enter value to insert: 20

1. Insert

2. Delete

3. Display
4. Exit

Enter your choice: 3

Queue elements: 10 20

1. Insert

2. Delete

3. Display
4. Exit

Enter your choice: 2

Deleted value: 10

1. Insert

2. Delete

3. Display
4. Exit

Enter your choice: 3

Queue elements: 20

1. Insert

2. Delete

3. Display

4. Exit

Enter your choice: 4

Exiting program.

2.Write a program for enqueue operation in queue ?

#include <stdio.h>
#define MAX 5 // Maximum size of the queue

int queue[MAX];

int rear = -1;


// Enqueue operation (insert an element into the queue)

void enqueue() {

int value;

// Check if the queue is full

if (rear == MAX - 1) {

printf("\nQueue is full! Cannot enqueue.\n");


} else {

printf("\nEnter value to enqueue: ");

scanf("%d", &value);

rear++; // Increment rear

queue[rear] = value; // Insert value at rear

printf("\nEnqueued: %d\n", value);

// Display the queue after enqueuing the element

printf("Current queue: ");

for (int i = 0; i <= rear; i++) {

printf("%d ", queue[i]);


}

printf("\n");

// Main function to test enqueue

int main() {

int choice;
// Infinite loop to display menu and handle user input

while (1) {

printf("\n1. Enqueue\n2. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {
case 1:

enqueue(); // Call enqueue function

break;

case 2:

printf("\nExiting program.\n");

return 0; // Exit the program

default:

printf("\nInvalid choice! Try again.\n");

return 0;

Output : -

1. Enqueue
2. Exit

Enter your choice: 1

Enter value to enqueue: 10


Enqueued: 10

Current queue: 10

1. Enqueue

2. Exit

Enter your choice: 1

Enter value to enqueue: 20

Enqueued: 20

Current queue: 10 20

1. Enqueue

2. Exit

Enter your choice: 1

Enter value to enqueue: 30

Enqueued: 30

Current queue: 10 20 30

1. Enqueue
2. Exit

Enter your choice: 1

Enter value to enqueue: 40


Enqueued: 40

Current queue: 10 20 30 40

1. Enqueue

2. Exit

Enter your choice: 1

Enter value to enqueue: 50

Enqueued: 50

Current queue: 10 20 30 40 50

1. Enqueue

2. Exit

Enter your choice: 1

Queue is full! Cannot enqueue.

1. Enqueue

2. Exit

Enter your choice: 2

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>

#define MAX 10 // Maximum size of the stack

int stack[MAX];

int top = -1;

// Function to push an element onto the stack

void push() {

int value;

if (top == MAX - 1) {

printf("Stack overflow! Cannot push more elements.\n");

} else {

printf("Enter the value to push: ");

scanf("%d", &value);

top++;

stack[top] = value;

printf("%d pushed onto the stack.\n", value);


}
}

// Function to pop an element from the stack

void pop() {

if (top == -1) {

printf("Stack underflow! No elements to pop.\n");


} else {

printf("%d popped from the stack.\n", stack[top]);

top--;

// Function to display the stack

void display() {
if (top == -1) {

printf("Stack is empty.\n");

} else {

printf("Stack elements: ");

for (int i = top; i >= 0; i--) {

printf("%d ", stack[i]);

printf("\n");

// Main function with menu-driven options

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");

printf("Enter your choice: ");

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:

printf("Invalid choice! Please try again.\n");


}

Output :-
Menu:

1. PUSH

2. POP

3. DISPLAY
4. EXIT

Enter your choice: 1

Enter the value to push: 5

5 pushed onto the stack.

Menu:

1. PUSH
2. POP

3. DISPLAY

4. EXIT

Enter your choice: 1

Enter the value to push: 10

10 pushed onto the stack.

Menu:

1. PUSH

2. POP
3. DISPLAY

4. EXIT

Enter your choice: 3

Stack elements: 10 5

Menu:

1. PUSH
2. POP

3. DISPLAY

4. EXIT

Enter your choice: 1

Enter the value to push: 15

15 pushed onto the stack.

Menu:

1. PUSH

2. POP

3. DISPLAY

4. EXIT

Enter your choice: 3

Stack elements: 15 10 5

Menu:

1. PUSH
2. POP

3. DISPLAY

4. EXIT

Enter your choice: 2

15 popped from the stack.

Menu:
1. PUSH

2. POP

3. DISPLAY

4. EXIT

Enter your choice: 3

Stack elements: 10 5

Menu:

1. PUSH

2. POP

3. DISPLAY

4. EXIT

Enter your choice: 4

Exiting program.

6) With a neat sketch Explain working of priority queue.

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,

1. Each element is assigning a priority.


2. The elements are processed according to, higher priority element is
processed Before lower priority element and two elements of same priority
are Processed according to the order of insertion.

Above figure shows priority. Queue with 5 elements where B & C have same
Priority number.

Each node in above priority queue contains three items.


i. Information fieldINFO
ii. A priority number PRNo
iii. LinkNext

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.

8) Define dequeue. How to implement it ? Explain with Example.

9) What are the operations performed on linear queue ?

You might also like