Sei sulla pagina 1di 32

Jawaharlal Nehru Engineering College

Laboratory Manual
Lab manual made by
PROF. S.S.KULKARNI

DATA STRUCTURE

For

Second Year Students


(Electronics And Telecommunication)

Author JNEC, Aurangabad

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

LABORATORY MANUAL CONTENTS

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.

Good Luck for your Enjoyable Laboratory Sessions.

SUBJECT INDEX

1. Lab Exercises 1: Program for merging of two arrays.


2. Lab Exercises 2: Program using function and pointers.
3. Lab Exercises 3: Program to implement stack.
4. Lab Exercises 4: Program to implement queue.
5. Lab Exercises 5: Program to implement singly linked list.
6. Lab Exercises 6: Program to implement graph traversal.
7. Lab Exercises 7: Program to implement binary search tree.
8. Lab Exercises 8: Program to implement bubble sort.

.Conduction of Viva-Voce Examinations


.Submission
.Evaluation and marking system

DOs and DONTs in Laboratory:


1. Do not handle any equipment before reading the instructions/Instruction manuals
2. Read carefully the power ratings of the equipment before it is switched on whether ratings 230
V/50
Hz or 115V/60 Hz. For Indian equipments, the power ratings are normally 230V/50Hz. If you have
equipment with 115/60 Hz ratings, do not insert power plug, as our normal supply is 230V/50 Hz,
which will damage the equipment.
3. Observe type of sockets of equipment power to avoid mechanical damage
4. Do not forcefully place connectors to avoid the damage
5. Strictly observe the instructions given by the teacher/Lab Instructor
Instructions for Laboratory Teachers:
1. Submission related to whatever lab work has been completed should be done during the next lab
session. The immediate arrangements for printouts related to submission on the day of practical
assignments.
2. Students should be taught for taking the printouts under the observation of lab teacher.
3. The promptness of submission should be encouraged by way of marking and evaluation patterns
that will benefit the sincere students.

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

Program to implement stack.

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).

/*Program demonstrate the operations performed on stack*/


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAXSIZE 10
void push ();
int pop ();
void display ();
int top=-1;
int stack[MAXSIZE];
void main()
{
int choice;
clrscr();
do
{
printf("\n1.PUSH");
printf("\n2.POP");
printf("\n3.DISPLAY");
printf("\n4.EXIT");
printf("\nenter your choice");
scanf("\n%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:printf("\ndeleted item is %d",pop());
break;
case 3:display();
break;
case 4:printf("\nexit");
exit(0);
break;
}
}while(choice!=4);
getch();
}
void push()
{
int item;
if(top==MAXSIZE-1)
{

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.

/*Program to demonstrate operations on queue*/


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAXSIZE 10
int front=-1,rear=0;
void cqinsert();
int cqdelete();
void display();
int cqueue[MAXSIZE];
int option;
char ch;
void main()
{
clrscr();
do
{
printf("\n1.insert");
printf("\n2.delete");
printf("\n3.display");
printf("\n4exit");
printf("\nenter your choice");
scanf("%d",&option);
switch (option)
{
case 1:cqinsert();
break;
case 2:printf("\ndeleted item is %d",cqdelete());
break;
case 3:display();
break;
case 4:printf("\nexit");
exit(0);
break;
}
}while(option!=4);
getch();
}
void cqinsert()
{

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.

Allocate memory for the new node, temp=malloc ().


Assign value to the data field of the new node, temp->info.
Go till node R, also mark next node as S.
Make the next field of node temp to node S and next field of node R to point to new node.

2.2.4 Algorithm for deleting the first node


1.
2.
3.
4.

Mark the first node as temp.


Then, make start point to the next node.
Make next field of temp, null.
Free temp.

2.2.5 Algorithm for deleting the last node


1. Mark the last node as temp & last but one node as R.
2. Make the next field of R as NULL.
3. Free temp.
2.2.6 Algorithm for deleting a middle node
1.
2.
3.
4.
5.

Mark the node to be deleted as temp.


Mark the previous node as R & next nodes as S.
Make the next field of R to point S.
Make the next field of temp, NULL.
Free Temp.

/*Program to demonstrate operation on singly linked list*/


#include<stdio.h>
#include<malloc.h>
lude<malloc.h>
#include<conio.h>
#include<process.h>
struct node
{
int info;
struct node*next;
}*start;
void insert();
void display();
void delete1();
void search();
void main()
{
int ch;
start=NULL;
clrscr();
do
{
printf("\n1.insert");
printf("\n2.delete");
printf("\n3.display");
printf("\n4.search");
printf("\n5.exit");
printf("\n enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n insert node at any position");
insert();
break;
case 2:printf("\n delete a node");
delete1();
break;
case 3:printf("\n display list");
display();
break;
case 4:printf("\n search for an node");
search();

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.

/**Program to traverse a graph using DFS**/


#include<stdio.h>
#include<conio.h>
void creatadjm(int adj[10][10],int);
void dfs(int,int visited[10],int adj[10][10],int);
void main()
{
int adj[10][10],node,n;
int i,visited[10];
clrscr();
printf("\n Enter the no. of nodes in graph,max=10");
scanf("%d",&n);
creatadjm(adj,n);
for(i=0;i<n;i++)
{
if(visited[i]==0)
dfs(i,visited,adj,n);
}
getch();
}
void creatadjm(int adj[][10],int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\n Enter 1 if there is edge from %d to %d otherwise 0",i,j);
scanf("%d",&adj[i][j]);
}
}
}
void dfs(int x,int visited[10],int adj[][10],int n)
{int j;
visited[x]=1;
printf("\n The visited node is :%d\t",x);
for(j=0;j<n;j++)
{
if(adj[x][j]==1 && visited[j]==0)
dfs(j,visited,adj,n);
}
}

Lab Exercises 7

Program to implement binary search tree.


Review:
1. Trees are non linear data structures that can be used to represent data items processing
hierarchical relationship between them.
2. Binary search tree may be used to store ordered information which can reduce search time for
any particular element.
3. Trees can be traversed in inorder, preorder and postorder.
4. Inorder traversal of binary search tree will result in elements arranged in ascending order.
5. Binary trees can be used to evaluate mathematical expressions
4.2 Algorithm for creation of binary tree and inserting an element
1. Read an element, make it root node.
2. Read next element, elm, compare it with the root node, if it is less then insert it in left else
insert right.
3. Repeat step 2 till you insert all elements.
4.3 Traversal Methods:
4.3.1 Algorithm for Preorder Traversal
1. Visit the node.
2. Traverse the left sub-tree in preorder.
3. Traverse the right sub-tree in preorder.
4.3.2 Algorithm for Inorder Traversal
1. Traverse the left sub-tree in inorder.
2. Visit the node.
3. Traverse the right sub-tree in inorder.
4.3.3 Algorithm for Postorder Traversal
1. Traverse the left sub-tree in postorder.
2. Traverse the right sub-tree in postorder.
3. Visit the node.

/*prgram to create binary tree n traverse it*/


#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void insert(struct node *r,struct node *p);
void preorder(struct node *r);
void inorder(struct node *r);
void postorder(struct node *r);
struct node
{
int info;
struct node *lchild,*rchild;
}*root,*p;
void main()
{
int i,ch=0;
clrscr();
root=NULL;
while(ch!=5)
{
printf("\n1:Insert 2:Preorder 3:Inorder 4:Postorder 5:Exit");
scanf("%d",&ch);
switch(ch)
{
case 1:p=(struct node*)malloc(sizeof(struct node));
printf("Enter the element");
scanf("%d",&i);
p->info=i;
p->lchild=NULL;
p->rchild=NULL;
if(root==NULL)
{
root=p;
}
else
insert(root,p);
break;
case 2:printf("%s","tree traversal is preorder is");
preorder(root);
break;
case 3:printf("%s","tree traversal is inorder is");
inorder(root);
break;

case 4:printf("%s","tree traversal is postorder is");


postorder(root);
break;
}
}
}
void insert(struct node *r,struct node *q)
{
if(q->info < r->info)
{
if(r->lchild==NULL)
{
r->lchild=p;
printf("\n element:%d is inserted on left of:%d",p->info,r->info);
}
else
insert (r->lchild,p);
}
else
{
if(r->rchild==NULL)
{
r->rchild=p;
printf("\n element:%d is inserted on right of:%d",p->info,r->info);
}
else
insert(r->rchild,p);
}
}
void preorder(struct node *r)
{
if(r!=NULL)
{
printf("%d\t",r->info);
preorder(r->lchild);
preorder(r->rchild);
}
}
void inorder(struct node *r)
{
if(r!=NULL)
{
inorder(r->lchild);

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)

6.1.1 Bubble Sort Algorithm:


Let A be a linear array of n numbers. Temp is a temporary variable for swapping (or interchange) the
position of the numbers.
1. Input n numbers to an array A
2. Initialize i=0 and repeat through step 4 if (i<n)
3. Initialize j=0 and repeat through step 4 if (j<n-i-1)
4. If (A[j]>A[j+1])
a. Temp=A[j]
b. A[j]=A[j+1]
c. A[j+1]=Temp
5. Display the sorted numbers of array A
6. Exit.

/** PROGRAM FOR BUBBLE SORT **/


#include<stdio.h>
#include<conio.h>
#define max 30
void main()
{
int arr[max],i,j,k,temp,n;
clrscr();
printf("Enter the number of elements:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the elements %d:",i+1);
scanf("%d",&arr[i]);
}
printf("Unsorted list is\n");
for(i=0;i<n;i++)
{
printf("%d",arr[i]);
printf("\n");
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
printf("\n After pass %d elements are:",i+1);
for(k=0;k<n;k++)
printf("\n %d",arr[k]);
printf("\n");
}
printf("\n Sorted list is \n");
for(i=0;i<n;i++)
printf("\n%d",arr[i]);
getch();
}

/* 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

Potrebbero piacerti anche