0% found this document useful (0 votes)
129 views12 pages

Dsu Imp Paper

Data structure

Uploaded by

patilchhakuli3
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)
129 views12 pages

Dsu Imp Paper

Data structure

Uploaded by

patilchhakuli3
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

Define the term algorithm.

Algorithm is a stepwise set of instructions written to perform a specific task.


List any 4 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.
● In real life, Call Center phone systems will use Queues, to hold people calling them in
an order, until a service representative is free.
● Handling of interrupts in real-time systems.
● Simulation
Define the terms: Linear data structure and non-linear data structure
Linear Data Structure: A data structure in which all data elements are stored in a particular
sequence is known as linear data structure.
Example: stack, queue
Non-Linear data structure: A data structure in which all data elements are not stored in any
particular sequence is known as nonlinear data structure.
Example: graph and tree.
convert infix expression into prefix expression: (A+B)*(C/G)+F
Infix expression Read Stack contents Prefix expression
Character
(A+B)*(C/G)+F F - F
(A+B)*(C/G)+ + + F
(A+B)*(C/G) ) +) F
(A+B)*(C/G G +) GF
(A+B)*(C/ / +)/ GF
(A+B)*(C C +)/ CGF
(A+B)*( ( + /CGF
(A+B)* * +* /CGF
(A+B) ) +*) /CGF
(A+B B +*) B/CGF
(A+ + +*)+ B/CGF
(A A +*)+ AB/CGF
( ( +* +AB/CGF
*+AB/CGF
+*+AB/CGF
Describe following terms [Link] tree:
(i) Leaf node (ii) Level of node
A Level 0

B C
Level 1
Leaf node:A node without any child node is called as leaf node
Nodes B and C are leaf node as shown in above example.
Level of node: Position of a node in the hierarchy of a tree is called as level of node.
Level of node B is 1 as shown in above example.
Write algorithm for performing push and pop operations on stack.
Push algorithm:-Max is maximum size of
[Link] 1: [Check for stack full/ overflow]
If stack_top is equal to max-1 then
Display output as “Stack Overflow” and return to calling function
Otherwise
Go to step 2
Step 2: [Increment stack_top] Increment stack top pointer by one
stack_top=stack_top +1;
Step 3: [Insert element] stack [stack_top] = item;
Step 4: return to calling function
Pop algorithm: - Max is maximum size of stack
Step 1: [Check for stack empty/underflow]
If stack_top is equal to -1 then
Display output as “Stack Underflow” and return to calling function
Otherwise
Go to step 2
Step 2: [delete element] stack [stack_top] = item;
Step 3: [Decrement stack_top] Decrement stack top pointer by one.
stack_top=stack_top -1;
Step 4: return to calling function
Convert following expression into postfix form. Give stepwise procedure
A+B↑C*(D/E)-F/G.
Scanned Operation Postfix Expression
Symbol stack
( (
A ( A
+ (+ A
B (+ AB
↑ (+↑ AB
C (+↑ ABC
* (+* ABC↑
( (+*( ABC↑
D (+*( ABC↑D
/ (+*(/ ABC↑D
E (+*(/ ABC↑DE
) (+* ABC↑DE/
- (- ABC↑DE/*+
F (- ABC↑DE/*+F
/ (-/ ABC↑DE/*+F
G (-/ ABC↑DE/*+FG
) EMPTY ABC↑DE/*+FG/-

POSTFIX EXPRESSION: ABC↑DE/*+FG/-

Describe the concept of linked list with the terminologies: node, next Pointer, null
pointer and empty list.
Node: Each data element in a linked list is represented as a node. Node contains two parts-
one is info (data) and other is next pointer (address). Info part stores data and next pointer
stores address of next node.

Next pointer: It is a pointer that holds address of next node in the list i.e. next pointer points
to next node in the list

Null pointer: It is a pointer that does not hold any memory address i.e. it is pointing to
nothing. It is used to specify end of the list. The last element of list contains NULL pointer
to specify end of list.

Empty list: Each linked list has a header node. When header node contains NULL value,
then that list is said to be empty list.

Stack Queue
1. Stack is a data structure in which 1. Queue is a data structure in which
insertion and deletion operations are insertion and deletion operations are
performed at same end. performed at different ends.
2. In stack an element inserted last is 2. In Queue an element inserted first is
deleted first so it is called Last In First deleted first so it is called First In First
Out list. Out list.
[Link] stack only one pointer is used called [Link] Queue two pointers are used called
as stack top 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 organizing
Polish notation processes.
In mobile device for sending receiving
messages.

Describe working of linear search with example.


In linear search, search element is compared with each element from the list in a sequence.
Comparison starts with first element from the list and continues till number is found or
comparison reaches to the last element of the list.
As each element is checked with search element, the process of searching requires more
time. Time complexity of linear search is O (n) where n indicates number of elements in list.
Linear search on sorted array:-On sorted array search takes place till element is found or
comparison reaches to an element greater than search element.
Example:- Using array representation
Input list 10, 20, 30, 40, 50 and Search element 30, Index =0
Iteration 1

10 ! = 30
Index = Index + 1
Iteration 2

20 ! = 30
Index = Index + 1
Iteration 3

30 = 30
Number found
General Tree Binary Tree
A general tree is a data structure in which A Binary tree is a data structure in which
each node can have infinite number of each node has at most two nodes i.e. left and
children right
In general tree, root has in- degree 0 and In binary tree, root has in- degree 0 and
maximum out- degree n. maximum out- degree 2.
In general tree, each node have in-degree one In binary tree, each node have in-degree one
and maximum out-degree n. and maximum out-degree 2.
Height of a general tree is the length of Height of a binary tree is : Height(T) = {
longest path from root to the leaf of tree. max (Height(Left Child) , Height(Right
Height(T)={max(height(child1), Child) + 1}
height(child2) , … height(child-n) ) +1}
Subtree of general tree are not ordered Subtree of binary tree is ordered.

Write a C program for deletion of an element from an array.


#include
<stdio.h> int
main(){
int array[100], position, c, n;
printf("Enter number of elements in
array\n"); scanf("%d", &n);
printf("Enter %d elements\n",
n); for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the location where you wish to delete
element\n"); scanf("%d", &position);
if (position >= n+1)
printf("Deletion not possible.\n");
else
{
for (c = position - 1; c < n - 1; c++)
array[c] = array[c+1];

printf("Resultant array:\n");

for (c = 0; c < n - 1; c++)


printf("%d\n", array[c]);
}
return 0;
}
Describe queue full and queue empty operation conditions on linear queue with
suitable diagrams.
Queue full:-A queue is full when its rear pointer points to max -1 position. Max is
maximum number of elements in a queue. If rear pointer is not equal to max-1 then a new
element can be added to a queue. If queue is full then new element cannot be added to a
queue.
Example:-Consider max=4. First element is stored at 0th position and last element is stored at
3rd position in queue. In the diagram given below rear pointer is pointing to max-1 (3)
position so queue is full
Queue empty: A queue is empty when its front pointer points to -1 position. When front
pointer is -1 then one cannot delete any data from a queue.
Example:-In the diagram given below front pointer points to -1 value i.e. it points no location
inside queue so queue is empty

Write a program in ‘C’ to insert an element in a linear queue


#include<stdio.h>
#include<conio.h>
#define n 5
void main(){
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
//clrscr();
printf("Queue using Array");
printf("\[Link] \[Link]\[Link]");
while(ch){
printf("\nEnter theChoice:");
scanf("%d",&ch);
switch(ch){
case 1:
if(rear==x)
printf("\n Queue isFull");
else
{
printf("\n Enter no%d:",j++);
scanf("%d",&queue[rear++];
}
brea;
case 2:
printf("\n Queue Elements are:\n");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 3:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
getch();
}
Define the term recursion. Write a program in C to display factorial of an entered
number using recursion
Definition: Recursion is the process of calling function by itself. A recursive function body
contains function call statement that calls itself repeatedly.
Program:
#include<stdio.h>
#include<conio.h>
int fact(int n);
void main(){
int n;
clrscr();
printf("\nThe factorial of % is = %d",n,fact(n));
getch();}
int fact(int n){
if(n==1)
return 1;
else
return(n*fact(n-1));}
Describe working of bubble sort with example.
Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based
algorithm in which each pair of adjacent elements is compared and the elements are
swapped if they are not in order. This algorithm is not suitable for large data sets as its
average and worst case complexity is of Ο (n2) where n is the number of items.
Bubble Sort Working:We take an unsorted array for our example as A[]={19, 2, 27,
3, 7, 5, 31}. Bubble sort takes Ο(n2) time so we're keeping it short and precise.
Pass 1: 2,19,27,3,7,5,31
2,19,27,3,7,5,31
2,19,3,27,7,5,31
2,19,3,7,27,5,31
2,19,3,7,5,27,31
Pass 1 Completed
Pass 2: 2,19,3,7,5,27,31
2,3,19,7,5,27,31
2,3,7,19,5,27,31
2,3,7,5,19,27,31
2,3,7,5,19,27,31
Pass 2 Completed
Pass 3: 2,3,7,5,19,27,31
2,3,7,5,19,27,31
2,3,5,7,19,27,31
Pass 3 Completed
Pass 4: 2,3,5,7,19,27,31
Pass 4 Completed
Pass 5: 2,3,5,7,19,27,31
Pass 5 Completed
Pass 6: 2,3,5,7,19,27,31
Pass 6 Completed
For given binary tree write in-order, pre-order and post-order traversal.

Inorder Traversal: Q,E,F,R,D,H,B,A,I,J,K,C,L,P


Preorder Traversal: A,B,D,E,Q,F,R,H,C,I,J,K,L,P
Postorder Traversal: Q,R,F,E,H,D,B,K,J,I,P,L,C,A
Write an algorithm to count number of nodes in singly linked list.
Function to count number of nodes in a given singly linked list.

For example, the function should return 5 for linked list 1->3->1->2->1.
Algorithm: Using Iterative Solution1)Initialize count as 0
2)Initialize a node pointer, current = head.
3)Do following while current is not NULL
A)current = current -> next
B)count++;
4)Return count
Write an algorithm to insert an element at the beginning and end of linked list
Algorithm to insert an element at the beginning of linked list:
1. Start
2. Create the node pointer *temp Struct node * temp
3. Allocate address to temp using malloc temp = malloc(sizeof(struct node));
4. Check whether temp is null, if null then Display “Overflow”
else
temp-> info=data
temp-> next=start
5. Start=temp
6. stop
Algorithm to insert an element at the end of linked list:
1. Start
2. Create two node pointers *temp,*q struct node * temp, *q;
3. q= start
4. Allocate address to temp using malloc temp = malloc(sizeof(struct node));
5. Check whether temp is null, if null then Display “Overflow”
else
temp-> info=data
temp-> next=null
6. While(q->next!=null)
q= q-> next
7. q->next= temp
8. stop
Construct a binary search tree for following elements: 30,100,90,15,2,25,36,72,78,10
show each step of construction of BST.

Describe undirected graph with suitable example.


Undirected graph: A graph in which the edges do not have any direction associated with
them is known as undirected graph.
In undirected graph, if an edge exists between two nodes A and B then the nodes can
traverse from A to B as well as from B to A. Each edge is bidirectional.
Example:-

A B

D C
In the above example, each edge is bidirectional
Describe working of selection sort method. Also sort given input list in ascending order
using selection sort input list:- 55, 25, 5, 15, 35.
Working of Selection sort: Selection Sort algorithm is used to arrange a list of elements in a
particular order (Ascending or Descending). In selection sort, the first element in the list is
selected and it is compared repeatedly with remaining all the elements in the list. If any
element is smaller than the selected element (for ascending order), then both are swapped.
Then we select the element at second position in the list and it is compared with remaining
all elements in the list. If any element is smaller than the selected element, then both are
swapped. This procedure is repeated till the entire list is sorted.

Describe procedure to delete an element from singly linked list using diagram.
In a linear linked list, a node can be deleted from the beginning of list, from in between
positions and from end of the list.
Delete a node from the beginning:-

Node to be deleted is [Link] a temporary node as ‘temp’. Set ‘temp’ node with the
address of first node. Store address of node 2 in header pointer ‘start’ and then delete ‘temp’
pointer with free function. Deleting temp pointer deletes the first node from the list.
OR
Step 1: Create temporary node ‘temp’.
Step 2: Assign address of first node to ‘temp’ pointer.
Step 3: Store address of second node (temp->next) in header pointer ‘start’.
Step 4: Free temp.
Delete a node from in between position:-

Node to be deleted is [Link] a temporary node as ‘temp’ and ‘q’. Set ‘temp’ node with
the address of first node. Traverse the list up to the previous node of node 3 and mark the
next node (node3) as ‘q’. Store address from node ‘q’ into address field of ‘temp’ node. Then
delete ‘q’ pointer with free function. Deleting ‘q’ pointer deletes the node 3 from the list.
OR
Step 1: Create temporary node ‘temp’, ’q’.
Step 2: Assign address of first node to ‘temp’ pointer.
Step 3: Traverse list up to previous node of node to be deleted.
Step 4: Mark the node to be deleted ‘q’.
Step 5: Store address from node ‘q’ in address field of ‘temp’ node (temp- >next=q->next).
Step 6: Free q.
Delete a node from the end:-

Node to be deleted is node [Link] a temporary node as ‘temp’ and ‘q’. Set ‘temp’ node with
the address of first node. Traverse the list up to the second last node and mark the last node as
‘q’. Store NULL value in address field of ‘temp’ node and then delete ‘q’ pointer with free
function. Deleting q pointer deletes the last node from the list.
Find the position of element 29 using binary search method in an array ‘A’ given below.
Show each step.A={11,5,21,3,29,17,2,43}
An array which is given A[ ]= {11,5,21,3,29,17,2,43} is not in sorted manner, first we need
to sort them in order;
So an array will be A[ ]={2,3,5,11,17,21,29,43} and the value to be searched is VAL = 29.
The binary search algorithm will proceed in the following manner.
Iteration 1:
BEG = 0, END = 7, MID = (0 + 7)/2

Now, VAL = 29 and A[MID] = A[3] =11

A[3] is less than VAL, therefore, we now search for the value in the second half of the
array.
So, we change the values of BEG and MID.
Iteration 2:
Now, BEG = MID + 1 = 4, END = 7, MID = (4 + 7)/2 =11/2 = 5; VAL = 29 and A [MID] =
A [5] = 21
A[5] is less than VAL, therefore, we now search for the value in the second half of the
segment.
So, again we change the values of BEG and MID.
Iteration 3:
Now, BEG = MID + 1 = 6, END = 7, MID = (6 + 7)/2 = 6 Now, VAL = 29 and A [MID] =
A [6]=29
So, Element 29 is found at 6th location in given array A[]={2,3,5,11,17,21,29,43}.

You might also like