0% found this document useful (0 votes)
85 views

Data Structure Unit 2 Important Questions

The document describes a C program to implement a queue using a linked list. It includes functions to: 1. Create the queue and initialize front and rear pointers to NULL. 2. Enqueue an element by allocating a new node and adding it to the rear. 3. Dequeue an element by removing the front node and updating front. 4. Display the queue by traversing from front to rear and printing elements. 5. Check if the queue is empty by checking if front and rear are NULL. The functions implement basic queue operations of insertion and removal using a linked list to represent the queue.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views

Data Structure Unit 2 Important Questions

The document describes a C program to implement a queue using a linked list. It includes functions to: 1. Create the queue and initialize front and rear pointers to NULL. 2. Enqueue an element by allocating a new node and adding it to the rear. 3. Dequeue an element by removing the front node and updating front. 4. Display the queue by traversing from front to rear and printing elements. 5. Check if the queue is empty by checking if front and rear are NULL. The functions implement basic queue operations of insertion and removal using a linked list to represent the queue.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Write a C function for the array implementation of a stack.

Write all
primitive operations.
AKTU 2015-16, Marks 10
Answer:

void create()

char ch;

do{

top++;

printf(“Input Element”);

scanf(“%d”,&stack[top]);

}}

void push (int val,int n) //n is size of the stack

if (top == n )

printf("\n Overflow");

else

printf(‘Input new element to insert’);

scanf(‘%d’,&val);

top = top +1;

stack[top] = val;

int pop ()

if(top == -1)

{
printf("Underflow");

return 0;

else

return stack[top - - ];

void traverse()

int i;

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

printf(“%d\n”,stack[i]);

Ques 5 Write a C function for linked list implementation of stack.Write


all the primitive operations.
AKTU 2015-16, Marks 10
Answer:
Primitive Operations of a Stack:

1. Create
2. Push
3. Pop
4. Display

#include <stdio.h>

#include <stdlib.h>

struct node
{

int info;

struct node *ptr;

}*top,*top1,*temp;

void push(int data);

void pop();

void display();

void create();

int count = 0;

void create()

top = NULL;

void push(int data)

if (top == NULL)

top =(struct node *)malloc(1*sizeof(struct node));

top->ptr = NULL;

top->info = data;

else

temp =(struct node *)malloc(1*sizeof(struct node));

temp->ptr = top;
temp->info = data;

top = temp;

count++;

void display()

top1 = top;

if (top1 == NULL)

printf("Stack is empty");

return;

while (top1 != NULL)

printf("%d ", top1->info);

top1 = top1->ptr;

void pop()

top1 = top;

if (top1 == NULL)

printf("\n Error : Trying to pop from empty stack");

return;
}

else

top1 = top1->ptr;

printf("\n Popped value : %d", top->info);

free(top);

top = top1;

count--;

Ques 6 What is a stack? Implement stack with a singly linked list.


AKTU 2014-15, Marks 05
Answer:
Stack:A stack is a linear data structure in which insertion and
deletion of elements are done at only one end, which is known as the
top of the stack. Stack is called a last-in, first-out (LIFO) structure
because the last element which is added to the stack is the first
element which is deleted from the stack.

#include <stdio.h>

#include <stdlib.h>

struct node

int info;

struct node *ptr;

}*top,*top1,*temp;

void push(int data)

if (top == NULL)

{
top =(struct node *)malloc(1*sizeof(struct node));

top->ptr = NULL;

top->info = data;

else

temp =(struct node *)malloc(1*sizeof(struct node));

temp->ptr = top;

temp->info = data;

top = temp;

count++;

void pop()

top1 = top;

if (top1 == NULL)

printf("\n Error : Trying to pop from empty stack");

return;

else

top1 = top1->ptr;

printf("\n Popped value : %d", top->info);

free(top);

top = top1;

count--;
}

Ques 7 Write a function in C language to reverse a string using stack.


AKTU 2014-15, Marks 05
OR
What is a stack? Write a C program to reverse a string using stack.
AKTU 2017-18, Marks 07
Answer:

#include <stdio.h>

#include <string.h>

#define max 100

int top,stack[max];

void push(char x){

// Push(Inserting Element in stack) operation

if(top == max-1){

printf("stack overflow");

} else {

stack[++top]=x;

void pop(){

// Pop (Removing element from stack)


printf("%c",stack[top--]);

main()

char str[]="sri lanka";

int len = strlen(str);

int i;

for(i=0;i<len;i++)

push(str[i]);

for(i=0;i<len;i++)

pop();

What is circular queue? Write a C code to insert an element in circular


queue. Write all the condition for overflow.
AKTU 2014-15, Marks 05
Answer:
Circular Queue:
It is a very important data structure because it can store data in a
very practical way. The circular queue is a linear data structure. It
follows FIFO principle. In a circular queue, the last node is connected
back to the first node to make a circle.

Program Code:

# include<stdio.h>

# define MAX 5

int cqueue_arr[MAX];

int front = -1;

int rear = -1;

void insert(int item)

if((front == 0 && rear == MAX-1) || (front == rear+1))

printf("Queue Overflow \n");

return;

if (front == -1) /*If queue is empty */

{
front = 0;

rear = 0;

else

if(rear == MAX-1) /*rear is at last position of queue */

rear = 0;

else

rear = rear+1;

cqueue_arr[rear] = item ;

Ques 28 Write an algorithm to insert and delete an item from the circular
linked list.
Answer:
Inserting a node at the beginning:
The following steps are to be followed to insert a new node at the
beginning of the circular list:
• Get the new node using getnode().
newnode = getnode();
• If the list is empty, assign new node as start.
start = newnode;
newnode -> next = start;
• If the list is not empty, follow the steps given below:
last = start;
while(last -> next != start)
last = last -> next;
newnode -> next = start;
start = newnode;
last -> next = start;

Deleting a node at the beginning:


The following steps are followed, to delete a node at the beginning of
the list:
• If the list is empty, display a message ‘Empty List’.
• If the list is not empty, follow the steps given below:
last = temp = start;
while(last -> next != start)
last = last -> next;
start = start -> next;
last -> next = start;
• After deleting the node, if the list is empty then start = NULL.
The function cll_delete_beg(), is used for deleting the first node in
the list.

Ques 29 Write a C program to implement the array representation of


circular queue.
AKTU 2016-17, Marks 10
Answer:
Circular Queue:
It is a very important data structure because it can store data in a
very practical way. The circular queue is a linear data structure. It
follows FIFO principle. In a circular queue, the last node is connected
back to the first node to make a circle.

Program Code:

# include<stdio.h>

# define MAX 5

int cqueue_arr[MAX];

int front = -1;

int rear = -1;

void insert(int item)

if((front == 0 && rear == MAX-1) || (front == rear+1))

printf("Queue Overflow \n");

return;

if (front == -1) /*If queue is empty */

{
front = 0;

rear = 0;

else

if(rear == MAX-1) /*rear is at last position of queue */

rear = 0;

else

rear = rear+1;

cqueue_arr[rear] = item ;

void del()

if (front == -1)

printf("Queue Underflow\n");

return ;

printf("Element deleted from queue is : %d\n",cqueue_arr[front]);

if(front == rear) /* queue has only one element */

front = -1;

rear=-1;

else

if(front == MAX-1)

front = 0;
else

front = front+1;

void display()

int front_pos = front,rear_pos = rear;

if(front == -1)

printf("Queue is empty\n");

return;

printf("Queue elements :\n");

if( front_pos <= rear_pos )

while(front_pos <= rear_pos)

printf("%d ",cqueue_arr[front_pos]);

front_pos++;

else

while(front_pos <= MAX-1)

printf("%d ",cqueue_arr[front_pos]);

front_pos++;

front_pos = 0;

while(front_pos <= rear_pos)

{
printf("%d ",cqueue_arr[front_pos]);

front_pos++;

printf("\n");

int main()

int choice,item;

do

printf("1.Insert\n");

printf("2.Delete\n");

printf("3.Display\n");

printf("4.Quit\n");

printf("Enter your choice : ");

scanf("%d",&choice);

switch(choice)

case 1 :

printf("Input the element for insertion in queue : ");

scanf("%d", &item);

insert(item);

break;

case 2 :

del();

break;
case 3:

display();

break;

case 4:

break;

default:

printf("Wrong choice\n");

}while(choice!=4);

return 0;

Ques 30 Write a C program to implement a queue using a linked list.


Answer:
Program Code:

#include <stdio.h>

#include <stdlib.h>

struct node

int info;

struct node *ptr;

}*front,*rear,*temp,*front1;
int frontelement();

void enq(int data);

void deq();

void empty();

void display();

void create();

void queuesize();

int count = 0;

void create()

front = rear = NULL;

void queuesize()

printf("\n Queue size : %d", count);

void enq(int data)

if (rear == NULL)

rear = (struct node *)malloc(1*sizeof(struct node));

rear->ptr = NULL;

rear->info = data;

front = rear;

else
{

temp=(struct node *)malloc(1*sizeof(struct node));

rear->ptr = temp;

temp->info = data;

temp->ptr = NULL;

rear = temp;

count++;

void display()

front1 = front;

if ((front1 == NULL) && (rear == NULL))

printf("Queue is empty");

return;

while (front1 != rear)

printf("%d ", front1->info);

front1 = front1->ptr;

if (front1 == rear)

printf("%d", front1->info);

void deq()

{
front1 = front;

if (front1 == NULL)

printf("\n Error: Trying to display elements from empty queue");

return;

else

if (front1->ptr != NULL)

front1 = front1->ptr;

printf("\n Dequed value : %d", front->info);

free(front);

front = front1;

else

printf("\n Dequed value : %d", front->info);

free(front);

front = NULL;

rear = NULL;

count--;

int frontelement()

if ((front != NULL) && (rear != NULL))

return(front->info);

else
return 0;

void empty()

if ((front == NULL) && (rear == NULL))

printf("\n Queue empty");

else

printf("Queue not empty");

Ques 31 Explain how a circular queue can be implemented using arrays.


Write all functions for circular queue operations.
AKTU 2018-19, Marks 07
Answer:
Already Answered for Ques 29
Discuss queue.
Answer:

1. Queue is a linear data structure where the first element is


inserted from one end called REAR and deleted from the other
end called FRONT.
2. Front points to the beginning of the queue and Rear points to
the end of the queue.
3. Queue follows the FIFO (First - In - First Out) structure.
4. According to its FIFO structure, elements inserted first will
also be removed first.
5. In a queue, one end is always used to insert data (enqueue) and
the other is used to delete data (dequeue), because the queue
is open at both its ends.
6. The enqueue() and dequeue() are two important functions used
in a queue
Ques 2 Write the procedure for insertion, deletion, and traversal of a
queue.
AKTU 2014-15, Marks 05
OR

Discuss various algorithms for various operation of queue.


Answer:
Queue:
A queue is a first-in, first-out (FIFO) data structure in which the
element that is inserted first is the first one to be taken out. The
elements in a queue are added at one end called the rear and
removed from the other end called the front. Like stacks, queues can
be implemented by using either arrays or linked lists.
Every queue has front and rear variables that point to the position
from where deletions and insertions can be done, respectively

What is iteration? Explain.


Answer:
Iteration:

1. It explicitly uses a repetition structure.


2. Iteration terminates when the loop continues.
3. It keeps modifying the counter until the loop continuation
condition fails.
4. Iteration normally occurs within a loop so the extra memory
assigned is omitted.
5. It is the repetition of a process in order to generate a (possibly unbounded) sequence of
outcomes.
6. Each repetition of the process is a single iteration, and the outcome of each iteration is
then the starting point of the next iteration.

Ques 16 What is recursion? Explain.


Answer:

1. Recursion is the process which comes into existence when a function calls a copy of
itself to work on a smaller problem.
2. Any function which calls itself is called recursive function, and such function calls are
called recursive calls
3. The process in which a function calls itself directly or
indirectly is called recursion and the corresponding function is
called a recursive function.
4. Using recursive algorithms, certain problems can be solved
quite easily.
5. Examples of such problems are Towers of Hanoi
(TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc.

Ques 17 What is recursion? Write a recursive program to find sum of


digits of the given number. Also, calculate the time.
AKTU 2016-17, Marks 10
Answer:
Recursive Program:

int sum_of_digit(int n)

if (n == 0)

return 0;

return (n % 10 + sum_of_digit(n / 10));

int main()

int num = 12345;


int result = sum_of_digit(num);

printf("Sum of digits in %d is %d\n", num, result);

return 0;

Output:
Sum of digits in 12345 is 15
Time Complexity:
T(n)= O(length of digit of (n)) where n is the number whose sum of
individual digit is to be found.

Ques 18 Explain all types of recursion with example.


Answer:
Types of Recursion:
1.Tail Recursion:
A recursive function is tail recursive when recursive call is the last
thing executed by the function. It is a special form of recursion
where the last operation of a function is a recursive call. The
recursion may be optimized away by executing the call in the
current stack frame and returning its result rather than creating a
new stack frame.
Example:

#include <stdio.h>

void fun(int n)

if (n > 0) {

printf("%d ", n);

fun(n - 1);

}
int main()

int x = 3;

fun(x);

return 0;

Output:
321
Linear and Tree Recursion:
If a recursive function calls itself for one time then it’s known as
Linear Recursion. Otherwise if a recursive function calls itself for
more than one time then it’s known as Tree Recursion.
Example:

#include <stdio.h>

void fun(int n)

if (n > 0) {

printf("%d ", n);

// Calling once

fun(n - 1);

// Calling twice

fun(n - 1);

int main()

fun(3);
return 0;

Direct Recursion:
If a function calls itself, it's known as direct recursion. This results in
a one-step recursive call: the function makes a recursive call inside
its own function body.
Example:

int foo(int x){

if(x < =0)

return x;

return foo(x-1);

Indirect Recursion:
In this recursion, there may be more than one functions and they are
calling one another in a circular manner.

From the above diagram fun(A) is calling for fun(B), fun(B) is calling
for fun(C) and fun(C) is calling for fun(A) and thus it makes a cycle.
Example:

#include <stdio.h>

void funB(int n);


void funA(int n)

if (n > 0) {

printf("%d ", n);

// Fun(A) is calling fun(B)

funB(n - 1);

void funB(int n)

if (n > 1) {

printf("%d ", n);

// Fun(B) is calling fun(A)

funA(n / 2);

int main()

funA(20);

return 0;

}
Ques 19 Explain Tower of Hanoi.
Answer:
Tower of Hanoi:

In the game of Towers of Hanoi, there are three towers labeled 1, 2,


and 3. The game starts with n disks on tower A. For simplicity, let n is
3. The disks are numbered from 1 to 3, and without loss of generality
we may assume that the diameter of each disk is the same as its
number. That is, disk 1 has diameter 1 (in some unit of measure), disk
2 has diameter 2, and disk 3 has diameter 3. All three disks start on
tower A in the order 1, 2, 3. The objective of the game is to move all
the disks in tower 1 to entire tower 3 using tower 2. That is, at no
time can a larger disk be placed on a smaller disk.
Rules:
The rules to be followed in moving the disks from tower 1 tower 3
using tower 2 are as follows:
• Only one disk can be moved at a time.
• Only the top disc on any tower can be moved to any other tower.
• A larger disk cannot be placed on a smaller disk.
Ques 20 What is Tower of Hanoi problem? Write the recursive code in C
language for the problem.
AKTU 2014-15, Marks 05
Answer:
Recursive Code for Tower of Hanoi:

#include <stdio.h> #include <conio.h>

void towers_of_hanoi (int n, char *a, char *b, char *c);

int cnt=0;

int main (void) {

int n; printf("Enter number of discs: ");

scanf("%d",&n);

towers_of_hanoi (n, "Tower 1", "Tower 2", "Tower 3");

getch();

void towers_of_hanoi (int n, char *a, char *b, char *c) {

if (n == 1) {

++cnt; printf ("\n%5d: Move disk 1 from %s to %s", cnt, a, c); return; } else
{

towers_of_hanoi (n-1, a, c, b); ++cnt; printf ("\n%5d: Move disk %d from %s to


%s", cnt, n, a, c); towers_of_hanoi (n-1, b, a, c); return; } }

Ques 21 Write a recursive algorithm for solving the problem of Tower of


Hanoi and also explain its complexity. Illustrate the solution for four disks
and three pegs.
OR
Explain the Tower of Hanoi problem and write a recursive algorithm to
solve it.
AKTU 2018-19, Marks 07
OR
Write an algorithm for finding a solution to the Tower of Hanoi problem.
Explain the working of your algorithm (with 4 disks) with diagrams.
AKTU 2051-16, Marks 15
Answer:

Recursive Code:

#include <stdio.h> #include <conio.h>

void towers_of_hanoi (int n, char *a, char *b, char *c);

int cnt=0;

int main (void) {

int n; printf("Enter number of discs: ");

scanf("%d",&n);

towers_of_hanoi (n, "Tower 1", "Tower 2", "Tower 3");

getch();

void towers_of_hanoi (int n, char *a, char *b, char *c) {

if (n == 1) {

++cnt; printf ("\n%5d: Move disk 1 from %s to %s", cnt, a, c);

return;

else {

towers_of_hanoi (n-1, a, c, b);

++cnt;

printf ("\n%5d: Move disk %d from %s to %s", cnt, n, a, c);


towers_of_hanoi (n-1, b, a, c);

return;

Output:

Enter the number of discs: 4


1: Move disk 1 from tower 1 to tower 2.
2: Move disk 2 from tower 1 to tower 3.
3: Move disk 1 from tower 2 to tower 3.
4: Move disk 3 from tower 1 to tower 2.
5: Move disk 1 from tower 3 to tower 1.
6: Move disk 2 from tower 3 to tower 2.
7: Move disk 1 from tower 1 to tower 2.
8: Move disk 4 from tower 1 to tower 3.
9: Move disk 1 from tower 2 to tower 3.
10: Move disk 2 from tower 2 to tower 1.
11: Move disk 1 from tower 3 to tower 1.
12: Move disk 3 from tower 2 to tower 3.
13: Move disk 1 from tower 1 to tower 2.
14: Move disk 2 from tower 1 to tower 3.
15: Move disk 1 from tower 2 to tower 3.

Ques 22 Discuss the principle of recursion.


Answer:
Principle of Recursion:
The recursion is a process by which a function calls itself. We use
recursion to solve bigger problems into smaller sub-problems. One
thing we have to keep in mind, that if each sub-problem is following
the same kind of patterns, then only we can use the recursive
approach.
A recursive function has two different parts. The base case and the
recursive case. The base case is used to terminate the task of
recurring. If the base case is not defined, then the function will recur
an infinite number of times.

Ques 23 How recursion can be removed?


Answer:
This recursion can be removed by two ways:
1. Through Iteration.
2. Through Stack

The rules of Recursion Removal by iteration are:

1. Recursion can be removed by replacing the selection structure


with a loop.
2. If some data need to be stored for processing after the end of
the recursive step, a data structure is needed in addition to the
loop
3. The data structure varies from a simple string or an array to a
stack.

The Rules of Removal of Recursion by Stack:

1. A stack for each parameter, variable, (Label).


2. A variable to hold the return value.
3. if the stop condition is reached, check whether the stack is
empty.
4. If the stack is not empty, pop() and jump to the appropriate
label.
5. If the stack is empty, return the result.

Ques 24 Define the recursion. Write a recursive and non-recursive


program to calculate the factorial of the given number.,
AKTU 2017-18, Marks 07
Answer:
Recursion:
Recursion is a method of solving problems that involves breaking a
problem down into smaller and smaller subproblems until you get to
a small enough problem that it can be solved trivially. Usually
recursion involves a function calling itself
Recursive Code to calculate the factorial of a number:

int factorial (int n)

if (n < 1) return 1;

else return n * factorial(n-1);

Non-Recursive Code to calculate the factorial of a number:

int factorial(int n){

int r = 1;

while (true) {

if (n < 1)

return r;

r = r * n ;

n = n - 1;}}
Write a short note on the application of stack.
Answer:
Expression Evaluation:
While reading the expression from left to right, push the element in
the stack if it is an operand. Pop the two operands from the stack, if
the element is an operator and then evaluate it. Push back the result
of the evaluation. Repeat it till the end of the expression.
Expression Conversion:
One of the applications of Stack is in the conversion of arithmetic
expressions in high-level programming languages into machine
readable form.An expression can be represented in infix,postfix and
prefix and stack proves to be useful while converting one form to
another.
Syntax Parsing:
Conversion from one form of the expression to another form needs a
stack. Many compilers use a stack for parsing the syntax of
expressions, program blocks etc before translating into low level
code.
Parenthesis Checking:
One of the most important applications of stacks is to check if the
parentheses are balanced in a given expression. The compiler
generates an error if the parentheses are not matched.
String Reversal:
Reversing string is an operation of Stack by using it we can reverse
any string.
Function Call:
The function call stack (often referred to just as the call stack or the
stack) is responsible for maintaining the local variables and
parameters during function execution
Ques 9 Write down the algorithm to convert infix notation into postfix.

OR

Write down the algorithm to evaluate the infix expression.


Answer:
Algorithm for Infix To Postfix Conversion:

1. Create an empty stack and an empty postfix output


string/stream
2. Scan the infix input string/stream left to right
3. If the current input token is an operand, simply append it to the
output string (note the examples above that the operands
remain in the same order)
4. If the current input token is an operator, pop off all operators
that have equal or higher precedence and append them to the
output string; push the operator onto the stack. The order of
popping is the order in the output.
5. If the current input token is '(', push it onto the stack
6. If the current input token is ')', pop off all operators and append
them to the output string until a '(' is popped; discard the '('.
7. If the end of the input string is found, pop all operators and
append them to the output string.

Ques10 Convert following infix expression into postfix expression A +


(B*C + D)/E.
AKTU 2014-15, Marks 05
Answer:
Ques 11 Consider the following infix expression a convert into reverse
polish notation using stack.

Expression=A + (B*C -(D/E ^ F) * H)


AKTU 2018-19, Marks 07
Answer:
Ques 12 Write down the algorithm to evaluate the postfix expression.

OR

Write down the algorithm to convert postfix to infix.


Answer:
Postfix:If an operator appears before operand in the expression then
expression is known as Postfix operation.
Infix:If an operator is in between every pair of operands in the
expression then expression is known as Infix operation.
Algorithm to Convert Expression from Postfix to Infix:

1. If a character is operand, push it to stack.


2. If a character is an operator,
3. pop operand from the stack, say it’s s1.
4. pop operand from the stack, say it’s s2.
5. perform (s2 operator s1) and push it to stack.
6. Once the expression iteration is completed, initialize the result
string and pop out from the stack and add it to the result.
7. Return the result.

Ques 13 Consider the following arithmetic expression written in infix


notation :

i)E = (A + B) * C + D / (B + A * C) + D

ii)E = A / B ^ C + D * E - A * C

Convert the above expression into postfix and prefix notation.


Answer:
i)Postfix Expression:
i)Prefix Expression:
ii)Postfix Expression:

ii)Prefix Expression:
Answer Obtained:
-+/A^BC*DE*AC

Ques 14 Solve the following:

((A - (B + C) * D) / (E + F)) [Infix to postfix]

(A + B) + *C - (D - E) ∩ F [Infix to prefix]

752 + *415 - / - [Evaluate the given postfix expressions]


AKTU 2016-17, Marks 10
Answer:
a.Postfix Expression:

b.Prefix Expression:
c.Result from Postfix Expression:
Explain dequeue with its types.
Answer:
Dequeue(Double-Ended Queue) is an ordered collection of items
similar to the queue. It has two ends, a front and a rear, and the items
remain positioned in the collection
It is a special type of data structure in which insertions and
deletions will be done either at front end or at the rear end.
Operations performed:

1. Insert from Front


2. Insert from Rear
3. Delete from Front
4. Delete from Rear
5. Display

Types of Dequeue:
Input-restricted dequeue:It is the one where deletion can be made
from both ends, but insertion can be made at one end only.
Output-restricted dequeue:It is the one where insertion can be made
at both ends, but deletion can be made from one end only.

Ques 33 What do you mean by priority queue? Describe its applications.


Answer:
Priority Queue:

1. It is an abstract data type similar to regular queue or stack data structure in which each
element additionally has a priority associated with it.
2. In a priority queue, an element with high priority is served before an element with low
priority
3. The items are ordered by key value so that item with the lowest value of key is at front
and item with the highest value of key is at rear or vice versa.
4. Priority Queue is more specialized data structure than Queue.

Basic Operations:

1. insert / enqueue − add an item to the rear of the queue.


2. remove / dequeue − remove an item from the front of the
queue.

Applications:

1. Prim's algorithm implementation can be done using priority


queues.
2. Dijkstra's shortest path algorithm implementation can be done
using priority queues.
3. A eearch algorithm implementation can be done using priority
queues.
4. Priority queues are used to sort heaps.
5. Priority queues are used in operating system for load balancing
and interrupt handling.
6. Priority queues are used in huffman codes for data
compression.
7. In traffic light, depending upon the traffic, the colors will be
given priority.

What do you mean by stack? Explain all its operations with a suitable
example.
Answer:
Stack is a linear data structure which follows a particular order in which the
operations are performed. The order may be LIFO(Last In First Out) or FILO(First In
Last Out).

Mainly the following three basic operations are performed in the


stack:

• Create Stack:To create an empty stack s.


• Push: Adds an item in the stack. If the stack is full, then it is
said to be an Overflow condition.
• Pop: Removes an item from the stack. The items are popped in
the reversed order in which they are pushed. If the stack is
empty, then it is said to be an Underflow condition.
• Peek or Top: Returns top element of stack.
• isEmpty: Returns true if stack is empty, else false.

Ques 2 Write a short note on the abstract data type.


Answer:
An abstract data type (ADT) is the way we look at a data structure,
focusing on what it does and ignoring how it does its job. For
example, stacks and queues are perfect examples of an ADT. We can
implement both these ADTs using an array or a linked list. This
demonstrates the ‘abstract’ nature of stacks and queues.
To further understand the meaning of an abstract data type, we will
break the term into ‘data type’ and ‘abstract’, and then discuss their
meanings.

Ques 3 Discuss PUSH and POP operations in a stack and write down their
algorithm.
Answer:
PUSH:The push operation adds an element to the top of the stack.
Algorithm:
if stack is full(full means if FULL = MAX-1)
return null
endif
top ← top + 1
stack[top] ← data end procedure

POP:The pop operation removes the element from the top of the
stack
Algorithm:
if stack is empty
return null
endif
data ← stack[top]
top ← top - 1
return data
end procedure

You might also like