Data Structure Unit 2 Important Questions
Data Structure Unit 2 Important Questions
Write all
primitive operations.
AKTU 2015-16, Marks 10
Answer:
void create()
char ch;
do{
top++;
printf(“Input Element”);
scanf(“%d”,&stack[top]);
}}
if (top == n )
printf("\n Overflow");
else
scanf(‘%d’,&val);
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]);
1. Create
2. Push
3. Pop
4. Display
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
}*top,*top1,*temp;
void pop();
void display();
void create();
int count = 0;
void create()
top = NULL;
if (top == NULL)
top->ptr = NULL;
top->info = data;
else
temp->ptr = top;
temp->info = data;
top = temp;
count++;
void display()
top1 = top;
if (top1 == NULL)
printf("Stack is empty");
return;
top1 = top1->ptr;
void pop()
top1 = top;
if (top1 == NULL)
return;
}
else
top1 = top1->ptr;
free(top);
top = top1;
count--;
#include <stdio.h>
#include <stdlib.h>
struct node
int info;
}*top,*top1,*temp;
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
else
temp->ptr = top;
temp->info = data;
top = temp;
count++;
void pop()
top1 = top;
if (top1 == NULL)
return;
else
top1 = top1->ptr;
free(top);
top = top1;
count--;
}
#include <stdio.h>
#include <string.h>
int top,stack[max];
if(top == max-1){
printf("stack overflow");
} else {
stack[++top]=x;
void pop(){
main()
int i;
for(i=0;i<len;i++)
push(str[i]);
for(i=0;i<len;i++)
pop();
Program Code:
# include<stdio.h>
# define MAX 5
int cqueue_arr[MAX];
return;
{
front = 0;
rear = 0;
else
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;
Program Code:
# include<stdio.h>
# define MAX 5
int cqueue_arr[MAX];
return;
{
front = 0;
rear = 0;
else
rear = 0;
else
rear = rear+1;
cqueue_arr[rear] = item ;
void del()
if (front == -1)
printf("Queue Underflow\n");
return ;
front = -1;
rear=-1;
else
if(front == MAX-1)
front = 0;
else
front = front+1;
void display()
if(front == -1)
printf("Queue is empty\n");
return;
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
else
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
front_pos = 0;
{
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");
scanf("%d",&choice);
switch(choice)
case 1 :
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;
#include <stdio.h>
#include <stdlib.h>
struct node
int info;
}*front,*rear,*temp,*front1;
int frontelement();
void deq();
void empty();
void display();
void create();
void queuesize();
int count = 0;
void create()
void queuesize()
if (rear == NULL)
rear->ptr = NULL;
rear->info = data;
front = rear;
else
{
rear->ptr = temp;
temp->info = data;
temp->ptr = NULL;
rear = temp;
count++;
void display()
front1 = front;
printf("Queue is empty");
return;
front1 = front1->ptr;
if (front1 == rear)
printf("%d", front1->info);
void deq()
{
front1 = front;
if (front1 == NULL)
return;
else
if (front1->ptr != NULL)
front1 = front1->ptr;
free(front);
front = front1;
else
free(front);
front = NULL;
rear = NULL;
count--;
int frontelement()
return(front->info);
else
return 0;
void empty()
else
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.
int sum_of_digit(int n)
if (n == 0)
return 0;
int main()
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.
#include <stdio.h>
void fun(int n)
if (n > 0) {
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) {
// 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:
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>
if (n > 0) {
funB(n - 1);
void funB(int n)
if (n > 1) {
funA(n / 2);
int main()
funA(20);
return 0;
}
Ques 19 Explain Tower of Hanoi.
Answer:
Tower of Hanoi:
int cnt=0;
scanf("%d",&n);
getch();
if (n == 1) {
++cnt; printf ("\n%5d: Move disk 1 from %s to %s", cnt, a, c); return; } else
{
Recursive Code:
int cnt=0;
scanf("%d",&n);
getch();
if (n == 1) {
return;
else {
++cnt;
return;
Output:
if (n < 1) return 1;
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
OR
i)E = (A + B) * C + D / (B + A * C) + D
ii)E = A / B ^ C + D * E - A * C
ii)Prefix Expression:
Answer Obtained:
-+/A^BC*DE*AC
(A + B) + *C - (D - E) ∩ F [Infix to prefix]
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:
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.
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:
Applications:
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).
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