Lab Manual Ds - 2021
Lab Manual Ds - 2021
Lab Manual Ds - 2021
Autonomous status conferred by UGC under UGC act-1956, (2f), NAAC-A Grade
Formerly Known as
(Amritsar College of Engineering & Technology | Amritsar Pharmacy College)
Lab Manual
Data Structures
AGCS-21306
Table of Contents
2. Software Requirements 3
3. Books Recommended 3
Course Outcomes:
1. Implement different sorting and searching algorithms.
2. Performing different operations using arrays.
3. Performing different operations using linked lists.
4. Implement the Stacks, Queues and their applications.
5. Perform basic operations on trees.
6. Traversing of graph to calculate optimal and shortest path.
Part-A
List of Topics for Machine Exercises
1. Implementation of searching & sorting techniques using array.
2. Menu driven program that implements the following operations (using
separate functions)on a linear array:
• Insert a new element at end as well as at a given position
• Delete an element from a given whose value is given or whose position is
given
• To find the location of a given element
• To display the elements of the linear array
3. Menu driven program that maintains a linear linked list whose
elements are stored in an ascending order and implement the following
operations (using separate functions):
• Insert a new element
• Delete an existing element
• Search an element
• Display all the elements
4. Usage of Stack
• Push and Pop operations
• Converting an arithmetic expression from infix notation to postfix
notation
• Evaluating an arithmetic expression in postfix notation
10 | P a g e Department of CSE
for(i=0;i<=n-1;i++)
{
cout<<"\n\t"<<a[i];
cout<<"\n";
}
getch();
}
Output
11 | P a g e Department of CSE
MERGE SORT
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{clrscr();
int a[10];
int b[10];
int c[10];
int i,j,k;
int n,m,p=0,r=0,s=0;
cout<<"\n\n\tENTER THE NO. OF ELEMENTS YOU WANT IN 1 LIST:";
cin>>n;
cout<<"\n\tENTER IN SORTED ORDER\n";
for(i=0;i<n;i++)
{cout<<"\n\tENTER THE "<<i+1<<" ELEMENT OF 1 LIST:";
cin>>a[i];
}
cout<<"\n\n\tENTER THE NO. OF ELEMENTS YOU WANT IN 2 LIST:";
cin>>m;
cout<<"\n\tENTER IN SORTED ORDER";
for(j=0;j<m;j++)
{cout<<"\n\tENTER THE "<<j+1<<" ELEMENT OF 2 LIST:";
cin>>b[j];
}
while(r<=n && s<=m)
{
if(a[r]<b[s])
{
c[p]=a[r];
r++;
12 | P a g e Department of CSE
}
else
{
c[p]=b[s];
s++;
}
p++;
}
while(r<n)
{
c[p]=a[r];
r++;
p++;
}
while(s<m)
{
c[p]=b[s];
s++;
p++;
}
cout<<"\n\n\tTHE LIST ON MERGE SORTING IS\n\t";
for(k=0;k<m+n;k++)
{
cout<<" "<<c[k];
}
getch();
}
13 | P a g e Department of CSE
Output
14 | P a g e Department of CSE
QUICK SORT
#include<iostream.h>
#include<conio.h>
#define max 100
class quicksort{
int i,l,h;
public: void input();
void output(int *,int);
void quick_sort(int *,int,int);
};
void main()
{
int i,l,h,n,a[max];
clrscr();
quicksort qs;
cout<<"How many elements in the array: ";
cin>>n;
cout<<"Enter the elemennts:"<<endl;
for(i=0;i<=n-1;i++)
{
cin>>a[i];
}
l=0;
h=n-1;
qs.quick_sort(a,l,h);
cout<<"Sorted Array :";
qs.output(a,n);
getch();
}
15 | P a g e Department of CSE
void quicksort::quick_sort(int a[],int l, int h)
{
int temp,key,low,high;
low=l;
high=h;
key=a[(low+high)/2];
do
{
while(key>a[low])
{
low++;
}
while(key<a[high])
{
high--;
}
if(low<=high)
{
temp=a[low];
a[low++]=a[high];
a[high--]=temp;
}
} while(low<=high);
if(l<high)
quick_sort(a,l,high);
if(low<h)
quick_sort(a,low,h);
}
16 | P a g e Department of CSE
for(i=0;i<=n-1;i++)
{
cout<<endl<<a[i];
}
}
Output
17 | P a g e Department of CSE
MACHINE EXERCISE 2
Menu driven program that implements following operations (using separate
functions) on a linear array:
• Insert a new element at end as well as at a given position
• Delete an element from a given whose value is given or whose position is given
• To find the location of a given element
• To display the elements of the linear array
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void a_insert(int,int);
void a_delete(int);
void a_location(double);
void a_traverse();
int arr[10], n;
void main()
{int k;
cout<<"enter length";
cin>>n;
cout<<"array is:";
for(k=0;k<n;k++)
{cin>>arr[k];
}
int ch;
int num,pos;
double element;
while(ch!=5)
{
cout<<"1> Insert";cout<<"\n2> Delete";cout<<"\n3> location";cout<<"\n4>
Show";cout<<"\n5> Quit\n";
cin>>ch;
18 | P a g e Department of CSE
switch(ch)
{
case 1:
cout<<"enter element:";
cin>>num;
cout<<"enter pos.:";
cin>>pos;
a_insert(num,pos);
break;
case 2:
cout<<"enter pos.:";
cin>>pos;
a_delete(pos);
break;
case 3:
cout<<"enter element";
a_location(element);
break;
case 4:
cout<<"\nArray:";
a_traverse();
break;
case 5:
break;
default:
cout << "Invalid input" << endl;
}}
getch();
}
void a_insert(int num, int pos)
{
19 | P a g e Department of CSE
for(int i=5; i>=pos;i--)
arr[i]=arr[i-1];
arr[i]=num;n++;
}
void a_delete(int pos)
{
for(int i=pos; i<=n;i++)
arr[i-1]=arr[i];
arr[i-1]=0;
}
void a_traverse()
{
int i;
cout<<"array is:"<<endl;
for(i=0;i<n;i++)
cout<<arr[i]<<endl;
}
void a_location(double element)
{
cin>>element;
int Index;
for (int i=0;i<5; i++)
{if (element == arr[i])
{
Index = i;
}
cout<<Index;}}
20 | P a g e Department of CSE
Output
21 | P a g e Department of CSE
MACHINE EXERCISE 3
Menu driven program that maintains a linear linked list whose elements are stored
in on ascending order and implements the following operations (using separate
functions):
• Insert a new element
• Delete an existing element
• Search an element
• Display all the elements
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class list
{struct node
{int data;
node *link;
}*p;
public:
void inslast(int);
void insbeg(int);
void insnext(int,int);
void delelement(int);
void delbeg();
void dellast();
void disp();
int seek(int);
list(){p=NULL;}
~list();
};
void list::inslast(int x)
{node *q,*t;
if(p==NULL)
22 | P a g e Department of CSE
{p=new node;
p->data=x;
p->link=NULL;
}
else
{q=p;
while(q->link!=NULL)
q=q->link;
t=new node;
t->data=x;
t->link=NULL;
q->link=t;
}
cout<<"\n\tInserted successfully at the end..";
disp();
}
void list:: insbeg(int x)
{node *q;
q=p;
p=new node;
p->data=x;
p->link=q;
cout<<"\n\tInserted successfully at the begining..";
disp();
}
void list::delelement(int x)
{node *q,*r;
q=p;
if(q->data==x)
{p=q->link;
delete q;
23 | P a g e Department of CSE
return;
}
r=q;
while(q!=NULL)
{if(q->data==x)
{r->link=q->link;
delete q;
return;
}
r=q;
q=q->link;
}
cout<<"\n\tElement u entered "<<x<<" is not found..";
}
void list:: delbeg()
{cout<<"\n\tThe list before deletion:";
disp();
node *q;
q=p;
if(q==NULL)
{cout<<"\n\tNo data is present..";
return;
}
p=q->link;
delete q;
return;
}
void list:: dellast()
{cout<<"\n\tThe list before deletion:";
disp();
node *q,*t;
24 | P a g e Department of CSE
q=p;
if(q==NULL)
{cout<<"\n\tThere is no data in the list..";
return;
}
if(q->link==NULL)
{p=q->link;
delete q;
return;
}
while(q->link->link!=NULL)
q=q->link;
q->link=NULL;
return;
}
list::~list()
{node *q;
if(p==NULL) return;
while(p!=NULL)
{q=p->link;
delete p;
p=q;
}
}
void list::disp()
{node *q;
q=p;
if(q==NULL)
{cout<<"\n\tNo data is in the list..";
return;
}
25 | P a g e Department of CSE
cout<<"\n\tThe items present in the list are :";
while(q!=NULL)
{cout<<" "<<q->data;
q=q->link;
}
}
void list :: insnext(int value,int position)
{node *temp,*temp1;
temp=p;
if(temp1==NULL)
{temp1= new node;
temp1->data=value;
temp1->link=NULL;
p=temp1;
return;
}
for(int i=0;((i<position)&&(temp->link!=NULL)) ;i++)
{if(i==(position-1))
{temp1= new node;
temp1->data= value;
temp1->link=temp->link;
temp->link=temp1;
}
temp=temp->link;
}
//cout<<" Inserted successfully at the position.."<<position;
disp();
}
int list::seek(int value)
{node *temp;
temp=p;
26 | P a g e Department of CSE
int position=0;
while(temp!=NULL)
{if(temp->data==value)
return position+1;
else
{temp=temp->link;
position=position+1;
}
}
cout<<"\n\tElement "<<value<<" not found";
return 0;
}
void main()
{list l;
int ch,v,p,ps;
do
{clrscr();
cout<<"\n\n\n Operations on List..";
cout<<"\n\n\t1.Insertion \n\t2.Deletion\n\t3.Display\n\t4.Seek\n\t5.Exit";
cout<<"\n\n\tEnter ur choice:";
cin>>ch;
switch(ch)
{case 1:
cout<<"\n\n\t1.Insertion at begining\n\t2.Insertion at the end\n\t3.Insertion after the
mentioned position";
cout<<"\n\n\tEnter ur choice:";
cin>>ps;
cout<<"\n\n\tEnter the value to insert:";
cin>>v;
switch(ps)
{case 1:
27 | P a g e Department of CSE
l.insbeg(v);
break;
case 2:
l.inslast(v);
break;
case 3:
cout<<"\n\tEnter the position to insert the value:";
cin>>p;
l.insnext(v,p);
break;
default:
cout<<"\n\tThe choice is invalid";
return;
}
break;
case 2:
cout<<"\n\n\t1.Delete the first element\n\t2.Delete the last element";
cout<<"\n\t3.Enter the element to delete from the list";
cout<<"\n\n\tEnter ur choice:";
cin>>ps;
switch(ps)
{case 1:
l.delbeg();
cout<<"\n\tThe list after deletion:";l.disp();
break;
case 2:
l.dellast();
cout<<"\n\tThe list after deletion:";l.disp();
break;
case 3:
l.disp();
28 | P a g e Department of CSE
cout<<"\n\tEnter the element to delete : ";
cin>>v;
l.delelement(v);
cout<<"\n\tThe list after deletion:";l.disp();
break;
default:
cout<<"\n\tThe option is invalid...";
break;
}
break;
case 3:
l.disp();
break;
case 4:
l.disp();
cout<<"\n\n\tEnter the element to search:";
cin>>v;
cout<<"\n\tThe position of the element "<< v<<" is "<<l.seek(v);
getch();
break;
case 5:
exit(1);
default:
cout<<"\n\tThe option is invalid...";
return;
}
getch();
}while(ch!=5);
getch();
return;
}
29 | P a g e Department of CSE
Output
30 | P a g e Department of CSE
MACHINE EXERCISE 4
Usage of stack
PUSH AND POP ELEMENTS
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
clrscr();
int c,i;
int stack[10]={1,2,4,6,9,12};
int item,top=5;
back1:
for(i=top;i>=0;i--)
{
cout<<"\nstack"<<i<<" :"<<stack[i];
}
back:
cout<<"\npress 1 to push";
cout<<"\npress 2 to pop";
cout<<"\npress 3 to exit";
cout<<"\n\nEnter your choice:";
cin>>c;
switch(c)
{
case 1:
if(top==9){cout<<"stack is full";exit(0);}
cout<<"\nenter no. in stack:";
cin>>item;
top=top+1;
stack[top]=item;
31 | P a g e Department of CSE
goto back1;
case 2: if(top==0){cout<<"stack is empty";exit(0);}
item=stack[top];
top=top-1;
goto back1;
case 3:exit(0);
default:cout<<"\n wrong choice entered ";
goto back;}}
Output
32 | P a g e Department of CSE
EVALUATING ARITHMETIC EXPRESSION IN POSTFIX NOTATION
#include<iostream.h>
#include<ctype.h>
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
cout<<"Enter the expression :: ";
cin>>exp;
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
33 | P a g e Department of CSE
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
} }
push(n3);
} e++; }
cout<<"\nThe result of expression<<pop();
return 0;}
Output
34 | P a g e Department of CSE
CONVERTING AN ARITHMETIC EXPRESSION FROM INFIX NOTATION TO
POSTFIX NOTATION
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
char stack[20];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}
void main()
{
char exp[20];
35 | P a g e Department of CSE
char *e, x;
cout<<"Enter the expression :: ";
cin>>exp;
e = exp;
while(*e != '\0')
{
if(isalnum(*e))
cout<<*e;
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
cout<<x;
}
else
{
while(priority(stack[top]) >= priority(*e))
cout<<pop();
push(*e);
}
e++;
}
while(top != -1)
{ cout<<pop(); }
getch();
}
Output
36 | P a g e Department of CSE
TOWER OF HANOI USING RECURSION
#include <iostream.h>
#include<conio.h>
void towers(int, char, char, char);
int main()
{
int num;
cout<<"Enter the number of disks : ";
cin>>num;
cout<<"The sequence of moves involved in the Tower of Hanoi are :";
towers(num, 'A', 'C', 'B');
return 0;
}
void towers(int num, char frompeg, char topeg, char auxpeg)
{
if (num == 1)
{
cout<<"\n Move disk 1 from peg "<<frompeg<<" to peg "<<topeg;
return; }
towers(num - 1, frompeg, auxpeg, topeg);
cout<<"\n Move disk "<<num<<" from peg "<<frompeg<<" to peg "<<topeg;
towers(num - 1, auxpeg, topeg, frompeg);
}
Output
37 | P a g e Department of CSE
MACHINE EXERCISE 5
Implementation of Insert and Delete operations in Queues.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main ()
{
clrscr();
int c,a[10]={1,2,4,6},i,count=0,item,front=0,rear;
cout<<"\nqueue is:\n";
for(i=front;a[i]!='\0';i++)
{
cout<<a[i];
count++;
}
rear=count;
back:
cout<<"\n1.insertion\n2.deletion\n3.exit\nenter your choice:";
cin>>c;
switch(c)
{
case 1:
if(count==9){cout<<"\noverflow";getch();}
else
{
cout<<"\nenter value to insert:";
cin>>item;
a[count]=item;
count=count+1;
rear=count;
cout<<"\nqueue is:\n";
for(i=0;i<count;i++)
cout<<a[i];
cout<<"\nfront is :"<<front<<"\nrear is :"<<rear;
38 | P a g e Department of CSE
getch();
goto back;
}
case 2:
if(count==0){cout<<"\nunderflow";getch();}
else
{
item=a[0];
for(i=0;i<count;i++)
{
a[i]=a[i+1];
}
count--;
cout<<"\nqueue is:\n";
for(i=0;i<count;i++)
cout<<a[i];
front=front+1;
cout<<"\nfront is :"<<front<<"\nrear is :"<<rear;
getch();
goto back;
}
case 3:
exit(0);
default:
cout<<"\nwrong choice entered \n press any ke to enter again...";
getch();
goto back;
}
}
39 | P a g e Department of CSE
Output
40 | P a g e Department of CSE
MACHINE EXERCISE 6
Implementation of different operations on a binary search tree
#include <iostream.h>
#include <stdlib.h>
#include<conio.h>
struct node
{
int data;
node* left;
node* right;
};
void inorder(node* root)
{
if(root == NULL) return;
else
inorder(root->left);
cout<<root->data;
inorder(root->right);
}
void preorder(node* root)
{
if(root == NULL) return;
else
cout<<root->data;
preorder(root->left);
preorder(root->right);
}
void postorder(node* root)
{
if(root == NULL) return;
else
41 | P a g e Department of CSE
postorder(root->left);
postorder(root->right);
cout<<root->data;
}
struct node* createNode(int value)
{
node* newNode = new node;
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct node* insertLeft(node *root, int value)
{
root->left = createNode(value);
return root->left;
}
struct node* insertRight(struct node *root, int value){
root->right = createNode(value);
return root->right;
}
void main()
{
clrscr();
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);
insertLeft(root->left, 5);
insertRight(root->right, 6);
cout<<"Inorder traversal \n";
inorder(root);
42 | P a g e Department of CSE
cout<<"\nPreorder traversal \n";
preorder(root);
cout<<"\nPostorder traversal \n";
postorder(root);
getch();
}
Output
43 | P a g e Department of CSE
MACHINE EXERCISE 7
Traversal of graphs
BFS TRAVERSAL
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
int a[20][20], q[20], visited[20], n, i, j, f = 0, r = -1;
void bfs(int v) {
for(i = 1; i <= n; i++)
if(a[v][i] && !visited[i])
q[++r] = i;
if(f <= r) {
visited[q[f]] = 1;
bfs(q[f++]);
}
}
void main() {
clrscr();
int v;
cout<<"Enter the number of vertices: "<<endl;
cin>>n;
for(i=1; i <= n; i++) {
q[i] = 0;
visited[i] = 0;
}
cout<<"Enter graph data in matrix form:" << endl;
for(i=1; i<=n; i++) {
for(j=1;j<=n;j++) {
cin>>a[i][j];
}
}
44 | P a g e Department of CSE
cout<<"Enter the starting vertex: " <<endl;
cin>>v;
bfs(v);
cout<<"The node which are reachable are:" << endl;
for(i=1; i <= n; i++) {
if(visited[i])
cout<< i;
else {
cout<<"BFS is not possible. All nodes are not reachable!"<<endl;
break;
}
}
getch();
}
Output
45 | P a g e Department of CSE
DFS Traversal
#include <iostream>
int main()
{
//variable declaration
int cost[10][10], i, j, k, n, e, top, v, stk[10], visit[10], visited[10];
cout << "Enter the number of vertices in the Graph: ";
cin >> n;
cout << "\nEnter the number of edges in the Graph : ";
cin >> e;
cout << "\nEnter the start and end vertex of the edges: \n";
for (k = 1; k <= e; k++)
{
cin >> i >> j;
cost[i][j] = 1;
}
cout << "\nEnter the initial vertex to start the DFS traversal with: ";
cin >> v;
cout << "\nThe DFS traversal on the given graph is : \n";
cout << v << " ";
//As we start with the vertex v, marking it visited to avoid visiting again
visited[v] = 1;
k = 1;
//The DFS Traversal Logic
while (k < n)
{
for (j = n; j >= 1; j--)
{
if (cost[v][j] != 0 && visited[j] != 1 && visit[j] != 1)
{
visit[j] = 1;
46 | P a g e Department of CSE
//put all the vertices that are connected to the visited vertex into a stack
stk[top] = j;
top++;
} }
//output all the connected vertices one at a time
v = stk[--top];
cout << v << " ";
k++;
//as v is visited so it is not a valid candidate to visit in future so visit[v]=0 and
visited[v]=1
visit[v] = 0;
//to mark it visited
visited[v] = 1;
}
cout << "\n\n\n";
return 0;
}
Output
47 | P a g e Department of CSE
VIVA VOCE
1. What is data structure?
The logical and mathematical model of a particular organization of data is called data
structure. There are two types of data structure
[1] Linear
[2] Nonlinear
2. What are the goals of Data Structure?
It must rich enough in structure to reflect the actual relationship of data in real world. The
structure should be simple enough for efficient processing of data.
3. What does abstract Data Type Mean?
Data type is a collection of values and a set of operations on these values. Abstract data
type refer to the mathematical concept that define the data type. It is a useful tool for
specifying the logical properties of a data type. ADT consists of two parts
1.Values definition
2.Operation definition
4. What is the difference between a Stack and an Array?
Stack is a ordered collection of items Stack is a dynamic object whose size is constantly
changing as items are pushed and popped .Stack may contain different data types Stack is
declared as a structure containing an array to hold the element of the stack, and an integer
to indicate the current stack top within the array.
5. What do you mean by recursive definition?
The definition which defines an object in terms of simpler cases of itself is called
recursive definition.
6. What is sequential search?
In sequential search each item in the array is compared with the item being searched until
a match occurs. It is applicable to a table organized either as an array or as a linked list
7. What actions are performed when a function is called?
When a function is called
i) arguments are passed
ii) local variables are allocated and initialized
ii) transferring control to the function
48 | P a g e Department of CSE
8. What actions are performed when a function returns?
i) Return address is retrieved
ii) Function’s data area is freed
iii) Branch is taken to the return address
9. What is a linked list?
A linked list is a linear collection of data elements, called nodes, where the linear order is
given by pointers. Each node has two parts first part contain the information of the
element second part contains the address of the next node in the list.
10. What are the advantages of linked list over array (static data structure)?
The disadvantages of array are unlike linked list it is expensive to insert and delete
elements in the array One can’t double or triple the size of array as it occupies block of
memory space.
In linked list, each element in list contains a field, called a link or pointer which contains
the address of the next element successive element’s need not occupy adjacent space in
memory.
11. Can we apply binary search algorithm to a sorted linked list, why?
No, we cannot apply binary search algorithm to a sorted linked list, since there is no way
of indexing the middle element in the list. This is the drawback in using linked list as a
data structure.
12. What do you mean by free pool?
Pool is a list consisting of unused memory cells which has its own pointer.
13. What do you mean by garbage collection?
It is a technique in which the operating system periodically collects all the deleted space
onto the free storage list. It takes place when there is minimum amount of space left in
storage list or when “CPU” is ideal. The alternate method to this is to immediately
reinsert the space into free storage list which is time consuming.
14. What do you mean by overflow and underflow?
When new data is to be inserted into the data structure but there is no available space i.e.
free storage list is empty this situation is called overflow.
When we want to delete data from a data structure that is empty this situation is called
underflow.
49 | P a g e Department of CSE
15. What are the disadvantages array implementations of linked list?
1. The no of nodes needed can’t be predicted when the program is written.
2. The no of nodes declared must remain allocated throughout its execution
16. What is a queue?
A queue is an ordered collection of items from which items may be deleted at one end
(front end) and items inserted at the other end (rear end).It obeys FIFO rule there is no
limit to the number of elements a queue contains.
17. What is a priority queue?
The priority queue is a data structure in which the intrinsic ordering of the elements
(numeric or alphabetic) Determines the result of its basic operation. It is of two types
i) Ascending priority queue- Here smallest item can be removed (insertion is arbitrary)
ii) Descending priority queue- Here largest item can be removed (insertion is arbitrary)
18. What are the disadvantages of sequential storage?
1.Fixed amount of storage remains allocated to the data structure even if it contains less
element.
2.No more than fixed amount of storage is allocated causing overflow
19. What are the disadvantages of representing a stack or queue by a linked list?
i) A node in a linked list (info and next field) occupies more storage than a corresponding
element in an array.
ii) Additional time spent in managing the available list.
20. What is dangling pointer and how to avoid it?
After a call to free(p) makes a subsequent reference to *p illegal, i.e. though the storage
to p is freed but the value of p(address) remain unchanged .so the object at that address
may be used as the value of *p (i.e. there is no way to detect the illegality). Here p is
called dangling pointer.
To avoid this it is better to set p to NULL after executing free(p).The null pointer value
doesn’t reference a storage location it is a pointer that doesn’t point to anything.
21. What are the disadvantages of linear list?
i) We cannot reach any of the nodes that precede node (p)
ii) If a list is traversed, the external pointer to the list must be persevered in order to
reference the list again.
50 | P a g e Department of CSE
22. Define circular list?
In linear list the next field of the last node contain a null pointer, when a next field in the
last node contain a pointer back to the first node it is called circular list.
Advantages – From any point in the list it is possible to reach at any other point
23. What are the disadvantages of circular list?
i) We can’t traverse the list backward
ii) If a pointer to a node is given, we cannot delete the node
24. Define double linked list?
It is a collection of data elements called nodes, where each node is divided into three
parts
i) An info field that contains the information stored in the node
ii) Left field that contain pointer to node on left side
iii) Right field that contain pointer to node on right side
25. Is it necessary to sort a file before searching a particular item ?
If less work is involved in searching a element than to sort and then extract, then we don’t
go for sort If frequent use of the file is required for the purpose of retrieving specific
element, it is more efficient to sort the file. Thus it depends on situation.
26. What are the issues that hamper the efficiency in sorting a file?
The issues are
i) Length of time required by the programmer in coding a particular sorting program
ii) Amount of machine time necessary for running the particular program
iii)The amount of space necessary for the particular program .
27. Calculate the efficiency of sequential search?
The number of comparisons depends on where the record with the argument key appears
in the table
1. If it appears at first position then one comparison
2. If it appears at last position then n comparisons
3. Average=(n+1)/2 comparisons
4. Unsuccessful search n comparisons
5. Number of comparisons in any case is O (n).
28. Is any implicit arguments are passed to a function when it is called?
51 | P a g e Department of CSE
Yes, there is a set of implicit arguments that contain information necessary for the
function to execute and return correctly. One of them is return address which is stored
within the function’s data area, at the time of returning to calling program the address is
retrieved and the function branches to that location.
29. Parenthesis is never required in Postfix or Prefix expressions, why?
Parenthesis is not required because the order of the operators in the postfix /prefix
expressions determines the actual order of operations in evaluating the expression
30. List out the areas in which data structures are applied extensively?
Compiler Design
Operating System
Database Management System
Statistical analysis package
Numerical Analysis
Graphics
Artificial Intelligence
Simulation
31. What are the major data structures used in the following areas : network data model
&
Hierarchical data model?
RDBMS – Array (i.e. Array of structures)
Network data model – Graph
Hierarchical data model – Trees
32. If you are using C language to implement the heterogeneous linked list, what pointer
type will you use?
The heterogeneous linked list contains different data types in its nodes and we need a
link, pointer to connect them. It is not possible to use ordinary pointers for this. So we go
for void pointer. Void pointer is capable of storing pointer to any type as it is a generic
pointer type.
33. Minimum number of queues needed to implement the priority queue?
Two. One queue is used for actual storing of data and another for storing priorities.
34. What is the data structures used to perform recursion?
52 | P a g e Department of CSE
Stack. Because of its LIFO (Last In First Out) property it remembers its ‘caller’ so knows
whom to return when the function has to return. Recursion makes use of system stack for
storing the return addresses of the function calls.
Every recursive function has its equivalent iterative (non-recursive) function. Even when
such equivalent iterative procedures are written, explicit stack is to be used.
35. What are the notations used in Evaluation of Arithmetic Expressions using prefix and
postfix forms?
Polish and Reverse Polish notations.
36. Convert the expression ((A + B) * C – (D – E) ^ (F + G)) to equivalent Prefix and
Postfix
notations?
1.Prefix Notation:
^ – * +ABC – DE + FG
2.Postfix Notation:
AB + C * DE – – FG + ^
37. Sorting is not possible by using which of the following methods?
(a) Insertion
(b) Selection
(c) Exchange
(d) Deletion
(d) Deletion.
Using insertion we can perform insertion sort, using selection we can perform selection
sort, using exchange we can perform the bubble sort (and other similar sorting methods).
But no sorting method can be done just using deletion.
38. List out few of the Application of tree data-structure?
The manipulation of Arithmetic expression
Symbol Table construction
Syntax analysis
39. List out few of the applications that make use of Multilinked Structures?
Sparse matrix, Index generation.
40. In tree construction which is the suitable efficient data structure?
53 | P a g e Department of CSE
(A) Array (b) Linked list (c) Stack (d) Queue (e) none
41. What is the type of the algorithm used in solving the 8 Queens problem?
Backtracking
42. In an AVL tree, at what condition the balancing is to be done?
If the ‘pivotal value’ (or the ‘Height factor’) is greater than 1 or less than –1.
43. In RDBMS, what is the efficient data structure used in the internal storage
representation?
B+ tree. Because in B+ tree, all the data is stored only in leaf nodes, that makes searching
easier. This corresponds to the records that shall be stored in leaf nodes.
45. One of the following tree structures, which is, efficient considering space and time
complexities?
a) Incomplete Binary Tree
b) Complete Binary Tree
c) Full Binary Tree
b) Complete Binary Tree
By the method of elimination:
Full binary tree loses its nature when operations of insertions and deletions are done. For
incomplete binary trees,
extra property of complete binary tree is maintained even after operations like additions
and deletions are done on it.
46. What is a spanning Tree?
A spanning tree is a tree associated with a network. All the nodes of the graph appear on
the tree once. A minimum spanning tree is a spanning tree organized so that the total
edge weight between nodes is minimized.
47. Does the minimum spanning tree of a graph give the shortest distance between any 2
specified nodes?
No, Minimal spanning tree assures that the total weight of the tree is kept at its minimum.
But it doesn’t mean that the distance between any two nodes involved in the minimum-
spanning tree is minimum.
48. Whether Linked List is linear or Non-linear data structure?
According to Storage Linked List is a Non-linear one.
54 | P a g e Department of CSE
DO’S AND DON’TS OF LAB
Do’s
1. Maintain Discipline and Silence in Lab.
2. Listen carefully Instructions delivered by Instructor / Lecturer.
3. Practical performed in a Lab should be in written form in next Lab whenever it
occurs and should be duly signed by the respective subject Lecturer.
4. Ask your query to Lab instructor if you are having any confusion about list of
practical to be performed etc.
5. If you find any problems in your allotted computer system, immediately report to
Lab Instructor / Lecturer rather than troubleshooting yourself.
Don’ts
1. Don’t try to correct things by you own wish. If you are found handling any
hardware, you may be fined up to Rs 500.
2. Don’t write anything on this Lab Manual.
3. Don’t leave Lab Manual anywhere in Lab, return it to Instructor after its usage.
4. Don’t take this Lab Manual out of Lab.
55 | P a g e Department of CSE