Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Laboratory Manual
Lab manual made by
PROF. S.S.KULKARNI
DATA STRUCTURE
For
FORWARD
It is my great pleasure to present this laboratory manual for Second year engineering students for the
subject of Data Structures & Numerical Computing keeping in view the vast coverage required for
visualization of concepts of Data Communication with simple language.
As a student, many of you may be wondering with some of the questions in your mind regarding the
subject and exactly what has been tried is to answer through this manual.
As you may be aware that MGM has already been awarded with ISO 9000 certification and it is our
endurance to technically equip our students taking the advantage of the procedural aspects of ISO
9000 Certification.
Faculty members are also advised that covering these aspects in initial stage itself, will greatly relived
them in future as much of the load will be taken care by the enthusiasm energies of the students once
they are conceptually clear.
Dr. S. D. Deshmukh
Principal
This manual is intended for the Second year students of IE branches in the subject of Data Structures
& Numerical Computing. This manual typically contains practical/Lab Sessions related Data
Structures covering various aspects related the subject to enhanced understanding.
We have made the efforts to cover various aspects of the subject covering computer hardware
components, software components networking aspects, Operating System concepts and programming
aspects will be complete in itself to make it meaningful, elaborative understandable concepts and
conceptual visualization.
Students are advised to thoroughly go though this manual rather than only topics mentioned in the
syllabus as practical aspects are the key to understanding and conceptual visualization of theoretical
aspects covered in the books.
SUBJECT INDEX
Lab Exercises 1
Program for merging of two arrays.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n1,n2,n3;
int a[10],b[10],c[20];
printf("\n enter no of elements of 1st array");
scanf("%d",&n1);
printf("\n now enter those elements");
for(i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("enter no of elements of 2nd array");
scanf("%d",&n2);
printf("now enter elements of 2nd array");
for(j=0;j<n2;j++);
scanf("%d",&b[j]);
n3=n1+n2;
for(i=0;i<n1;i++)
{
c[i]=a[i];
}
for(j=i;j<n3;j++)
{
c[j]=b[j-i];
}printf("\n the merged array is");
for(j=0;j<n3;j++)
printf("\n%d",c[j]);
}
Lab Exercises 2
Program using function and pointers.
#include<stdio.h>
#include<conio.h>
void swap (int *x,int*y);
void main()
{
inta,b;
a=10;
b=20;
printf("value of a before function call %d \n",a);
printf("value of b before function call %d \n",b);
swap(&a,&b);
printf("value of a after funtion call %d\n",b);
getch();
}
void swap(int*x,int*y)
{
int temp;
*x=*y;
*y=temp;
printf("value of a during function call &%d\n",*x);
printf("value of b during function call %d \n",*y);
}
Lab Exercises 3
Review:
1. A collection of data and its relationship among the elements forms a data structure.
2. A stack is ordered collection of items in which all insertion, deletion are done at the same end
called the top.
3. The basic operations on stack are push and pop. It is often called LIFO data structure.
4. A Queue is a non-primitive linear data structure. It is ordered collection of element in which
elements are added at one end called rear and elements are deleted at other end called front.
5. The basic operations on Queue are insert and delete. It is called as FIFO data structure.
6. A circular queue is implemented by visualizing the 1D array as circular queue. The circular
queue overcomes the problems associated when the deletion is made from the front, it gives
queue full condition even if in reality, it is empty.
1.2 Operations on Stack:
1.2.1 Algorithm to push an element:
1. Check whether stack is full or not. (i.e. if(top==MAXSIZE))
2. Otherwise increase the top by 1(else top=top+1)
3. Insert the element, elem.
stack[top]=elem;
1.2.2 Algorithm to pop an element:
1. Check whether stack is empty or not (if (top==-1))
2. Otherwise decrement top by 1.
1.2.3 To check whether the stack is empty or not (STACK EMPTY).
1.2.4 To check whether the stack is full or not (STACK FULL).
printf("\nSTACK IS FULL");
getch();
return(0);
}
else
{
printf("\nenter element");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
}
int pop()
{
int item;
if(top==-1)
{
printf("\nSTACK EMPTY");
getch();
return;
}
else
{
item=stack[top];
top=top-1;
return(item);
}
}
void display()
{
int i;
if(top==-1)
{
printf("\nstack is empty");
}
else
{
printf("\nthe elements are:");
for(i=top;i>=0;i--)
{
printf("\t %d",stack[i]);
}
}
}
Output:1.PUSH
2.POP
3.DISPLAY
4.EXIT
enter your choice 1
enter element 10
1.PUSH
2.POP
3.DISPLAY
4.EXIT
enter your choice 1
enter element 20
1.PUSH
2.POP
3.DISPLAY
4.EXIT
enter your choice 3
the elements are: 20
1.PUSH
2.POP
3.DISPLAY
4.EXIT
enter your choice 4
10
Lab Exercises 3
Program to implement queue.
1.3 Operations on Queue:
1.3.1 Algorithm to insert an element in Queue:
1. Initialize front=0, rear=-1 and MAXSIZE=5.
2. If rear>=MAXSIZE //condition for queue full
Print Queue Overflow and return
Else
Increment rear by 1.
3. Insert item, q[rear]=item;
4. For insertion of more elements goto step2.
1.3.2 Algorithm to delete an element from Queue:
1. If front<0 //condition for queue empty
Print Queue is empty and return
Else
Item=q[front]; //displaying item at the front of the queue
2. Increment front by 1.
int num;
if(front==(rear+1)%MAXSIZE)
{
printf("\nqueue overflow");
getch();
}
else
{
printf("\nenter element to be inserted");
scanf("%d",&num);
if(front==-1)
front=rear=0;
else
front=(rear+1)%MAXSIZE;
cqueue[rear]=num;
}
}
int cqdelete()
{
int num;
if(front==-1)
{
printf("queue underflow");
getch();
return 0;
}
else
{
num=cqueue[front];
front=rear+1;
}
}
void display()
{
int i;
if(front==-1)
{
printf("\nqueue underflow");
}
else
{
printf("\nelements of queue are");
for(i=front;i<=rear;i++)
{
printf("\t%d",cqueue[i]);
}
}
if(front>rear)
{
for(i=front;i<MAXSIZE;i++)
{
printf("%d",cqueue[i]);
}
for(i=0;i<=rear;i++)
{
printf("%d",cqueue[i]);
}
}
printf("\n");
}
Output:1.insert
2.delete
3.display
4.exit
enter your choice 1
enter element to be inserted 11
1.insert
2.delete
3.display
4.exit
enter your choice 1
enter element to be inserted 22
1.insert
2.delete
3.display
4.exit
enter your choice 3
elements of queue are 11 22
1.insert
2.delete
3.display
4.exit
enter your choice 4
Lab Exercises 5
Program to implement singly linked list.
Review
1. Static memory allocation may insert in wastage of memory and also shortage of memory.
(Like in array implementations)
2. To outcome these problems, dynamic memory allocation is used.
3. Linked list is a linear data structure which consists of number of nodes created dynamically.
4. A linked list has no fix size but grows or shrinks as elements are added or deleted from the
list.
5. A singly linked list is a dynamic data structure. It may grow or shrink. Growing or shrinking
depends on operation made.
6. Every node of list has two fields, information and next address. Next address field contains
memory address location of next node.
7. A doubly linked list maintains two links, to previous node and next node. We can delete or
insert a node given only a pointer.
2.2 Operations on Singly Linked List:
2.2.1 Algorithm for inserting a node at the beginning
1. Allocate memory for the new node, temp=malloc().
2. Assign the value of the data field of the new node, temp->info
3. Make the link field of the new node to point to the starting node of the linked list, temp>next=start.
4. Then, set the external pointer (which was pointing to the starting node) to point to the new
node, start=temp.
2.2.2 Algorithm for inserting a node at the end
1. If the list is empty then create the new node, temp=malloc ().
2. If the list is not empty, then go to the last node and then insert the new node after the last
node, r=start; go till last node then r->next=temp.
2.2.3 Algorithm for inserting node at the specified position
1.
2.
3.
4.
break;
case 5:printf("\n exit");
//break;
exit(0);
}
}while(ch!=5);
getch();
}
void insert()
{
struct node*temp,*r,*s;
int inf;
temp=(struct node*)malloc(sizeof(struct node*));
temp->next=NULL;
printf("\n enter information");
scanf("%d",&temp->info);
if(start==NULL)
{
start=temp;
}
else
{
printf("\n after which you want to insert");
scanf("%d",&inf);
r=start;
while(r->info!=inf)
{
r=r->next;
}
s=r->next;
r->next=temp;
temp->next=s;
}
}
void display()
{
struct node*temp;
temp=start;
if(temp==NULL)
{
printf("\n linked list is empty");
return;
}
else
{
do
{
printf("\n %d",temp->info);
temp=temp->next;
}while(temp!=NULL);
}
}
void delete1()
{
struct node*temp,*r;
int inf;
printf("\n which item you want to delete");
scanf("%d",&inf);
temp=start;
if(temp==NULL)
{
printf("\n linked list is empty");
return;
}else
{
while(temp->info!=inf);
{
temp=temp->next;
}
if(temp->next==NULL)
{
printf("\n deleted node is %d",temp->info);
r=start;
while(r->next!=temp);
{
r=r->next;
}
r->next=NULL;
}
else if(temp==start)
{
printf("\n deleted node is %d",temp->info);
start=start->next;
temp->next=NULL;
}
else
{
printf("\n deleted node is %d",temp->info);
r=start;
while(r->next!=temp);
{
r=r->next;
}
r->next=temp->next;
temp=temp=NULL;
}
free(temp);
}
}
void search()
{
int inf;
struct node*temp;
printf("\n enter item which we want to search");
scanf("%d",&inf);
temp=start;
do
{
if(temp->info==inf)
{
printf("\n item found");
return;
}
else
{
temp=temp->next;
}
}while(temp!=NULL);
printf("\n item not found");
}
Lab Exercises 6
Program to implement graph traversal.
Review:
1. A graph G consists of two sets V and E. Set V is a finite non empty set of vertices and E is
a set of pairs of vertices called edges.
2. There are two possible orders for visiting the vertices of the graph, Depth First Search
(DFS) and Breadth First Search (BFS).
3. DFS of an undirected graph is roughly analogous to preorder traversal of an ordered tree.
4. In DFS we traverse a single path of the graph as far as it can go i.e. until it lists a node
with no successors or a node all of whose successors have already been visited.
5. In BFS all unvisited vertices adjacent to V are visited after visiting the starting vertex V
and marking it as visited. Next the unvisited vertices adjacent to these vertices are visited
and so on until the entire graph has been traversed. BFS uses queue to store the nodes of
each level of the graph.
5.2 Algorithm fro Depth First Search
1. Select any unvisited node in the graph. Mark this node as visited, push this on to the stack.
2. Find the adjacent node on TOS (top of stack), and which is not yet visited, mark this new
node as visited & push on to stack.
3. Repeat step 2 until no new node to TOS node can b found, when no new adjacent node can be
found, pop TOS.
4. Repeat step 2 and 3 till stack becomes empty.
5. Repeat step 1 till any more nodes which are still unvisited- USE ADJANCY MATRIX for
finding adjacent nodes.
5.3 Algorithm for Breadth First Search
1. Select any unvisited node in the graph. Mark this node as visited. Insert this node into the
queue.
2. Find all the adjacent nodes (one by one) or node present in the front end of the queue & which
is not yet visited, mark this node as visited & insert it in to the queue.
3. Repeat step 2, till queue becomes empty.
4. Repeat step 1, till any more nodes which are still unvisited.
Lab Exercises 7
printf("%d\t",r->info);
inorder(r->rchild);
}
}
void postorder(struct node *r)
{
if(r!=NULL)
{
postorder(r->lchild);
postorder(r->rchild);
printf("%d\t",r->info);
}
}
0/*OUTPUT */
1.Insert 2.Preorder 3. Inorder 4. Postorder 5.Exit1
Enter the element55
1.Insert 2.Preorder 3. Inorder 4. Postorder 5.Exit1
Enter the element88
Element:88 is inserted on right of :55
1.Insert 2.Preorder 3. Inorder 4. Postorder 5.Exit1
Enter the element22
Element:22 is inserted on right of :55
1.Insert 2.Preorder 3. Inorder 4. Postorder 5.Exit1
Enter the element54
Element:54 is inserted on right of: 22
1.Insert 2.Preorder 3. Inorder 4. Postorder 5.Exit1
Enter the element54
Element :66 is inserted on left of :88
1.Insert 2.Preorder 3. Inorder 4. Postorder 5.Exit1
Enter the element10
Element :10 is inserted on left of:22
1.Insert 2.Preorder 3. Inorder 4. Postorder 5.Exit2
Tree traversed in preorder is 55 22 10 54 88 66
1.Insert 2.Preorder 3. Inorder 4. Postorder 5.Exit3
Tree traversed in preorder is 10 22 54 55 66 88
1.Insert 2.Preorder 3. Inorder 4. Postorder 5.Exit4
Tree traversed in preorder is 10 54 22 66 88 55
1.Insert 2.Preorder 3. Inorder 4. Postorder 5.Exit5
Lab Exercises 8:
Program to implement bubble sort.
Review:
1. The choice of particular algorithm depends upon following performance analysis and
measurements
a. Space Complexity.
b. Time Complexity.
2. Analysis of Space complexity of a program is the amount of memory it needs to run to
completion.
3. The time complexity of an algorithm of a program is the amount of time it needs to run to
completion.
4. Sorting and searching are among the most common topics for programming systems. Big Oh
(O), Omega () and theta () notations are most commonly used to describe the efficiency of
a program. It describes the behavior of time or space complexity for large instance
characteristics.
5. In bubble sort, each element is compared with its adjacent elements, if the first element is
larger than the second one then the positions of the elements are interchanged otherwise it is
not changed then next element is compared with its adjacent element and the same process is
repeated for all the elements in the array until we get a sorted array.
6. The total number of comparisons for bubble sort is n2 -2n+1, therefore, time complexity of
bubble sort is O(n2)
/* OUTPUT */
Enter the no. of elements: 4
Enter element 1: 101
Enter element 2: 98
Enter element 3: 212
Enter element 4: 301
Unsorted list is
101
98
212
301
After pass 1 elements are:
98
101
212
301
After pass 2 elements are:
98
101
212
301
After pass 3 elements are:
98
101
212
301
Sorted list is
98
101
212
301