Ds Lab Manual
Ds Lab Manual
Ds Lab Manual
MANUAL
LAB MANUAL
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
VSSUT, Burla, Odisha
Department of Computer Science and Engineering
Lab Exercises
DATA STRUCTURES LABORATORY
1. Represent the given sparse matrix using one dimensional array and linked list.
2. Create a Stack and do the following operations using arrays and linked lists
(i)Push (ii) Pop (iii) Peep
3. Create a Queue and do the following operations using arrays and linked lists
(i)Add (ii) Remove
4. Implement the operations on singly linked list, doubly linked list and circular
linked list.
7. Sort the given list of numbers using heap and quick sort.
*****
Data Structures Lab Manual Dept. of Computer Science and Engineering VSSUT, Burla
Ex. No: 1
Represent the given sparse matrix using Arrays
Date:
Aim: To write a program to represent Sparse Matrix using one dimensional Array
Algorithm:
1. Start
2. Declare a two-dimensional array variable
3. Get the order from the user
4. If the order is 1 then perform row-major order and display the result
5. If the order is 2 then perform column-major order and display the result
6. If the order is 3 then exit
7. Stop
Logical Description
A matrix that has relatively few non-zero entries. It may be represented in much less than
n × m space. An n × m matrix with k non-zero entries is sparse if k << n × m. It may be
faster to represent the matrix compactly as a list of the non-zero indexes and associated
entries, as a list of lists of entries (one list for each row), coordinate format (the values
and their row/column positions), or by a point access method.
Program Code
#include<stdio.h>
void main()
{
int a[10][10],s[10][10];
int i,j,m,n,k=0,l=0;
clrscr();
printf("\n Enter the no of rows: ");
scanf("%d",&n);
printf("\n Enter the no of columns: ");
scanf("%d",&m);
printf("\n Enter the elements in the array : ");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
printf("\n Elements of the matrix \n : ");
for(i=0;i<n;i++)
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
{
for(j=0;j<m;j++)
printf("\t %d",a[i][j]);
printf("\n");
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(a[i][j]!=0)
{
s[k][l]=i;
s[k][++l]=j;
s[k][++l]=a[i][j];
k++;
l=0;
}
}
}
printf("\n Elements of sparse matrix \n: ");
for(i=0;i<k;i++)
{
for(j=0;j<3;j++)
printf("\t %d",s[i][j]);
printf("\n");
}
getch();
}
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Expected Output:
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Ex. No: 2 Create a Stack and do the basic operations using
Date: Arrays
Aim: To write a program to create a Stack and manipulate it using one dimensional
Array
Algorithm:
1. Start
2. Declare an one dimensional array variable
3. Using Switch case statement select one appropriate operation
a. PUSH:
i. Read an element
ii. Store it at the top of the stack
iii. Increment the TOS
b. POP
i. Read the element from the top of the stack
ii. Display it
iii. Decrement the TOS
c. PEEK
i. Read the element from the top of the stack
ii. Display it
4. Stop
Logical Description
The program implements the Stack using one dimensional array. Initially an array
variable is declared, and variable TOS is to point the top of the stack is also declared.
PUSH operation is done by reading an element from the user and storing it at TOS
position. POP operation retrieves the value at TOS and removes the element; TOS now
points the next element. PEEK operation retrieves the element but it is not deleted from
the stack.
Program Code
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
void peek();
void main()
{
clrscr();
printf("\n\t\tStack Using Arrays");
printf("\n\t\t------------------");
printf("\n\t\t 1.Create");
printf("\n\t\t 2.Push");
printf("\n\t\t 3.Pop");
printf("\n\t\t 4.Peek");
printf("\n\t\t 5.Exit\n");
do
{
printf("\t\tEnter Your Choice :");
scanf("%d",&ch);
switch(ch)
{
case 1: create();break;
case 2: push();break;
case 3: pop();break;
case 4: peek();break;
default: printf("\t\tExit");
break;
}
}while(ch!=5);
getch();
}
void create()
{
if(top==0)
{
printf("\t\tNumber of Element:");
scanf("%d",&n);
if(n>SIZE)
printf("\t\tSize of Stack is Large\n");
else
{
printf("\t\tPushing Element :");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
top=n;
display();
}
}
else
{
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
printf("\tStack Already Exists");
}
}
void push()
{
if(top==SIZE)
printf("\t\tStack Overflow\n");
else
{
printf("\t\tPushing Element :");
scanf("%d",&a[++top]);
display();
}
}
void pop()
{
if(top==0)
printf("\t\tStack Underflow\n");
else
{
printf("\t\tPop Element :%d\n",a[top--]);
display();
}
}
void display()
{
if(top==0)
printf("\t\tStack is Empty");
else
{
printf("\t\tStack Contains :");
for(i=top;i>=1;i--)
printf("%d ",a[i]);
}
printf("\n");
}
void peek()
{
if(top==0)
printf("\t\tStack Empty\n");
else
printf("\t\tTop Element is %d\n",a[top]);
}
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Expected Output
1. Push
2. Pop
3. Peek
4. Display
5. Exit
1. Push
2. Pop
3. Peek
4. Display
5. Exit
1. Push
2. Pop
3. Peek
4. Display
5. Exit
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Enter your option: 4
The elements in the stack are: 12
15
34
1. Push
2. Pop
3. Peek
4. Display
5. Exit
1. Push
2. Pop
3. Peek
4. Display
5. Exit
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Ex. No : 3 Create a Queue and do the basic operations using
Date : Arrays
Algorithm:
1. Start
2. Read ch and n value
3. check (ch!=4) and if (rear =n) then read value
4. Assign value to q(rear) and increment the rear, else Display as queue if full
5. if (front=rear) then print front value of queue and increment front
Else Display as queue is empty
6. if (rear=0) then assign front to I and write q[i].
Else Display as queue is empty
7. Go to step 3
8. stop
Logical Description
A collection of items in which only the earliest added item may be accessed. Basic
operations are add (to the tail) or enqueue and delete (from the head) or dequeue. Delete
returns the item removed. Also known as "first-in, first-out" or FIFO.
Program Code
#include<stdio.h>
#include<conio.h>
void main()
{
int q[50],i,j,n,op,front,rear,no;
clrscr();
printf("Enter the size of the queue:");
scanf("%d",&n);
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
printf("QUEUE OPERATIONS:\n1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter the operation to be performed:");
scanf("%d",&op);
front=0;rear=-1;
while(op!=4){
switch(op)
{
case 1:{
if(rear==n-1)
printf("\nQueue is full!\n");
else
{
printf("Enter the element to be inserted:");
scanf("%d",&no);
q[++rear]=no;
printf("\n%d is inserted!",no);
}
break; }
case 2: {
if(front==0 && rear==-1)
printf("\nQueue is empty!");
else
{
printf("\n%d is deleted!",q[front]);
q[front]=0;
for(j=front;j<rear;j++)
q[j]=q[j+1];
rear=rear-1;
}
break; }
case 3: {
printf("\nQueue is:\n");
for(i=front;i<=rear;i++)
printf("%d\t",q[i]);
break; }
}
printf("\nEnter the operation to be performed:");
scanf("%d",&op);
}
getch(); }
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Expected Output
12 is inserted!
Enter the operation to be performed:1
Enter the element to be inserted:32
32 is inserted!
Enter the operation to be performed:3
Queue is:
12 32
Enter the operation to be performed:2
12 is deleted!
Enter the operation to be performed:3
Queue is:
32
Enter the operation to be performed:2
32 is deleted!
Enter the operation to be performed:2
Queue is empty!
Enter the operation to be performed:3
Queue is:
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Ex. No : 4 Implement the basic operations on Singly Linked
Date : List
Aim: To write a program to implement the basic operations on Singly Linked List
Algorithm:
1. Start
2. Read the value of ch
3. If ch =1 then read item of number
set n as malloc to size, set n of into as num and next as list
4. If ch=2 to check if list =0 then print “Empty list”
else if n is list then delete the node n
5. If ch=3 then read position value and set n as malloc size and read num value and
store in n.
6. If ch =4 then check list as NULL or not. If NULL then print “ Empty list”
Else check I not equal to pos then print info of p then delete the node p
7. If ch=5 then set n and allocate malloc size and read num.
Else next not as Null then n as next of p
8. If ch=6 then check pos Null and print “Empty list”
Else delete the node p
9. stop
Logical Description
The singly-linked list is the most basic of all the linked data structures. A singly-linked
list is simply a sequence of dynamically allocated objects, each of which refers to its
successor in the list. Despite this obvious simplicity, there are myriad implementation
variations.
Program Code
struct node
{
int data;
struct node *link;
}*head;
typedef struct node node;
void maintain(void);
void create(node *);
void insertion(node *);
void deletion(node *);
void display(node *);
int count=0;
#include"stdio.h"
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
#include"alloc.h"
main()
{
head=malloc(sizeof(node));
create(head);
maintain();
getch();
return;
}
void maintain()
{
int n;
clrscr();
printf(" MAINTANENCE\n\n1. Insertion\n2. Deletion\n3. Display\n4. Exit");
printf("\n\nYour choice: ");
scanf("%d",&n);
switch(n)
{
case 1:
{
insertion(head);
break;
}
case 2:
{
deletion(head);
break;
}
case 3:
{
display(head);
break;
}
case 4:
{
exit();
}
}
}
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
printf("\nCreation of Linked List\n");
printf("\nEnter the data('0' to terminate)\n");
scanf("%d",&i);
p->data=i; count++;
scanf("%d",&i);
while(i!=0)
{
q=malloc(sizeof(node));
q->data=i; count++;
p->link=q;
scanf("%d",&i);
if(i!=0)
{
p=malloc(sizeof(node));
p->data=i; count++;
q->link=p;
}
else
{
q->link=NULL;
return;
}
scanf("%d",&i);
}
p->link=NULL;
maintain();
}
void insertion(node *p)
{
int n,m,i=1;
node *new,*next,*tail;
clrscr();
while(i==1)
{
clrscr();
p=head;
printf("\tINSERTION\n\n1. Insertion at first\n2. Insertion at last\n3. Insertion
inbetween\n\nYour choice: ");
scanf("%d",&n);
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
case 1:
{
new->link=head;
head=new;
count++; break;
}
case 2:
{
while(p->link!=NULL)
p=p->link;
p->link=new;
new->link=NULL;
count++; break;
}
case 3:
{
printf("\nEnter the location: ");
scanf("%d",&n);
if(n<2 || n>=count)
{
clrscr();
printf("Sorry!!! Location doesn't fall within the limit");
break;
}
m=1;
while(m<n-1)
{
p=p->link;
m++;
}
next=p->link;
p->link=new;
new->link=next;
count++; break;
}
}
printf("\n\nDo you want to continue insertion(1/0): ");
scanf("%d",&i);
}
maintain();
}
void deletion(node *p)
{
int n,m,i=1;
node *next,*tail;
clrscr();
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
while(i==1)
{
clrscr();
p=head;
printf("\tDELETION\n\n1. Deletion at first\n2. Deletion at last\n3. Deletion
inbetween\n\nYour choice: ");
scanf("%d",&n);
switch(n)
{
case 1:
{
head=head->link;
count--; break;
}
case 2:
{
while((p->link)->link!=NULL)
p=p->link;
p->link=NULL;
count--; break;
}
case 3:
{
printf("\nEnter the location: ");
scanf("%d",&n);
if(n<2 || n>=count)
{
clrscr();
printf("Sorry!!! Location doesn't fall within the limit");
break;
}
m=1;
while(m<n-1)
{
p=p->link;
m++;
}
next=p->link;
p->link=next->link;
count--; break;
}
}
printf("\n\nDo you want to continue deletion(1/0): ");
scanf("%d",&i);
}
maintain();
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
}
void display(node *p)
{
clrscr();
printf("\nCreated List");
printf("\n\nTotal no of elements %d\n",count);
while(p!=NULL)
{
printf("%d ",p->data);
p=p->link;
}
getch();
maintain();
}
Expected Output
MAINTANENCE
1. Insertion
2. Deletion
3. Display
4. Exit
Your choice: 1
INSERTION
1. Insertion at first
2. Insertion at last
3. Insertion inbetween
Your choice: 1
Created List
Total no of elements 7
12346789
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Ex. No : 5
Represent the given sparse matrix using Linked List
Date :
Algorithm:
1. start
2. Enter the row and column matrix
3. Read the value of m and n
4. Set for loop i< row as i=1 and another loop i < col as j=1
5. Read value of x
Next j and i
6. If we choose insert then check if (sparse == Null ) then sparse =p
Else check while (q-> next != Null ) then q = q-> next; q-> next =p
7. If we choose display than for i=1 to r then check p=p->next
8. stop
Logical Description
A matrix that has relatively few non-zero entries. It may be represented in much less than
n × m space.
Program Code
#include<stdio.h>
struct node
{
int row;
int col;
int data;
struct node *link;
}*head=NULL,*curr=NULL,*p=NULL;
void main()
{
int i,j,m,n,a[50][50];
clrscr();
printf("\nSparse Matrix using Linked List\n");
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
printf("\nEnter the number of rows of the matrix:");
scanf("%d",&m);
printf("Enter the number of columns of the matrix:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]!=0)
{
curr=(struct node*)malloc(sizeof(struct node));
curr->row=i;
curr->col=j;
curr->data=a[i][j];
curr->link=NULL;
if(head==NULL)
head=curr;
else
{
p=head;
while(p->link!=NULL)
p=p->link;
p->link=curr;
}
}
}
}
printf("\nSparse matrix using linked list:\nRow\tColumn\tElement\t\n");
p=head;
if(head==NULL)
printf("\nSparse matrix empty!\n");
else
{
while(p->link!=NULL)
{
printf("%d\t%d\t%d\n",p->row,p->col,p->data);
p=p->link;
}
printf("%d\t%d\t%d\n",p->row,p->col,p->data);
}
getch();
}
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Expected Output:
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Ex. No : 6 Create a Stack and do the basic operations using
Date : Linked List
Algorithm :
1. Start
2. Read the value of ch
3. Allocate the malloc size to a variable
4. Get the item and store in data then link to Null value
5. Link the temp to top. Then check the top is Null then display as “ Stack is Empty”
6. If not as Null then print the top value
7. Print data as temp becomes Null
8. Stop
Logical Description
The stack is a common data structure for representing things that need to maintain in a
particular order. Conceptually, a stack is simple: a data structure that allows adding and
removing elements in a particular order. Every time an element is added, it goes on the
top of the stack; the only element that can be removed is the element that was at the top
of the stack. Consequently, a stack is said to have "first in last out" behavior (or "last in,
first out"). The first item added to a stack will be the last item removed from a stack.
Program Code
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node* link;
}*head,*top,*curr;
void push(int data)
{
curr=(struct node*)malloc(sizeof(struct node));
curr->data=data;
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
curr->link=NULL;
if(top==NULL)
{
head=curr;
top=curr;
}
else
{
top->link=curr;
top=curr;
}
}
int pop()
{
int d=0;
d=top->data;
curr=head;
if(curr->link==NULL)
{
head=NULL;
top=NULL;
}
else
{
while(curr->link->link!=NULL)
{
curr=curr->link;
}
curr->link=NULL;
top=curr;
}
return d;
}
int peep()
{
return top->data;
}
void main()
{
int op,data;
clrscr();
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
do
{
printf("\nSTACK OPERATIONS:\n1.Push\n2.Pop\n3.Peep\n4.Exit\n");
printf("Enter the operation to be performed:");
scanf("%d",&op);
switch(op)
{
case 1:
{
printf("Enter the elemen to b pushed:");
scanf("%d",&data);
push(data);
break;
}
1 case 2:
{
printf("Popped element: %d",pop());
break;
}
case 3:
{
printf("Peeped element: %d",peep());
break;
}
case 4:
break;
default:
{
printf("\nEnter a proper option!\n");
}
}
}while(op!=4);
getch();
}
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Expected Output
STACK OPERATIONS:
1.Push
2.Pop
3.Peep
4.Exit
Enter the operation to be performed:1
Enter the elemen to b pushed:12
STACK OPERATIONS:
1.Push
2.Pop
3.Peep
4.Exit
Enter the operation to be performed:1
Enter the elemen to b pushed:70
STACK OPERATIONS:
1.Push
2.Pop
3.Peep
4.Exit
Enter the operation to be performed:3
Peeped element: 70
STACK OPERATIONS:
1.Push
2.Pop
3.Peep
4.Exit
Enter the operation to be performed:2
Popped element: 70
STACK OPERATIONS:
1.Push
2.Pop
3.Peep
4.Exit
Enter the operation to be performed:3
Peeped element: 12
STACK OPERATIONS:
1.Push
2.Pop
3.Peep
4.Exit
Enter the operation to be performed:4
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Ex. No : 7 Create a Queue and do the basic operations using
Date : Linked List
Algorithm:
1. Start
2. Read the value of ch
3. Get the variable temp and set malloc for size
4. Get item and store in data and link as Null
5. Get link of rear to temp. If front as Null and print Queue as Empty
6. Else print front element
7. print the data until the temp get Null
8. Stop
Logical Description
Queues are data structures that, like the stack, have restrictions on where you can add
and remove elements. To understand a queue, think of a cafeteria line: the person at the
front is served first, and people are added to the line at the back. Thus, the first person in
line is served first, and the last person is served last. This can be abbreviated to First In,
First Out (FIFO).
Program Code
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
struct node* link;
}*head,*rear,*front,*curr;
void insert(int data)
{
curr=(struct node*)malloc(sizeof(struct node));
curr->data=data;
curr->link=NULL;
if(rear==NULL)
{
rear=curr;
front=curr;
}
else
{
rear->link=curr;
rear=curr;
}
}
int delete()
{
int d=0;
d=front->data;
curr=front;
front=front->link;
free(curr);
return d;
}
void display()
{
curr=front;
if(front==NULL)
printf("\nQueue empty!\n");
else if(front==rear)
printf("\nQueue is:%d",curr->data);
else {
printf("\nQueue is:\n");
while(curr->link!=NULL)
{
printf("%d\t",curr->data);
curr=curr->link;
}
printf("%d\n",curr->data); }
}
void main()
{
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
int op,data;
clrscr();
do
{
printf("\nQUEUE OPERATIONS:\n1.Insert\n2.Delete\n3.Exit\n");
printf("Enter the operation to be performed:");
scanf("%d",&op);
switch(op)
{
case 1:
{
printf("Enter the element to be inserted:");
scanf("%d",&data);
insert(data);
break;
}
case 2:
{
data=delete();
if(data)
printf("Deleted element: %d",data);
break;
}
case 3:
break;
default:
printf("\nEnter a proper option!\n");
}
display();
}while(op!=3);
getch();
}
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Expected Output
QUEUE OPERATIONS:
1.Insert
2.Delete
3.Exit
Enter the operation to be performed:1
Enter the element to be inserted:34
Queue is:34
QUEUE OPERATIONS:
1.Insert
2.Delete
3.Exit
Enter the operation to be performed:1
Enter the element to be inserted:78
Queue is:
34 78
QUEUE OPERATIONS:
1.Insert
2.Delete
3.Exit
Enter the operation to be performed:1
Enter the element to be inserted:56
Queue is:
34 78 56
QUEUE OPERATIONS:
1.Insert
2.Delete
3.Exit
Enter the operation to be performed:2
Deleted element: 34
Queue is:
78 56
QUEUE OPERATIONS:
1.Insert
2.Delete
3.Exit
Enter the operation to be performed:3
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Ex. No : 8 Implement the basic operations on Doubly Linked
Date : List
Algorithm:
1. Start
2. Read the value of ch1, ch2
3. If ch=1 then read the value of num n malloc size. Assign left and right as Null
4. If ch=2 then get data n->right =1:st1 list1->left=n; list1=n;
5. If ch=3 p=list1 then p->info list1=p->right list of left as null and delete p
6. If ch=4 then read the position then get data() p=list1 then assign value of n as
q->right
7. If ch=5 then check if (list1==null) then list1=n;
8. If ch=6 then check if (list1=null) then print (“list empty”);
9. else check while (p->right!=null) then print p->info
10. stop
Logical Description
A more sophisticated kind of linked list is a doubly-linked list or two-way linked list.
Each node has two links: one points to the previous node, or points to a null value or
empty list if it is the first node; and one points to the next, or points to a null value or
empty list if it is the final node.
Program Code
#include<stdio.h>
#include<conio.h>
struct node
{
struct node *prev;
int data;
struct node *nxt;
}*head=NULL,*curr=NULL,*curr1=NULL,*p;
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
p=p->nxt;
}
p=head;
if(pos<=count+1)
{
curr=(struct node*)malloc(sizeof(struct node));
printf("Enter the node:");
scanf("%d",&curr->data);
curr->nxt=NULL;
curr->prev=NULL;
if(head==NULL)
{
head=curr;
}
else if(pos==1)
{
head->prev=curr;
curr->nxt=head;
head=curr;
}
else
{
for(i=1;i<(pos-1);i++)
p=p->nxt;
curr->prev=p;
curr->nxt=p->nxt;
p->nxt->prev=curr;
p->nxt=curr;
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
free(curr);
}
else
{
curr=curr->nxt;
while(curr->nxt!=NULL)
{
if(curr->data==data)
{
found=1;
break;
}
else
curr=curr->nxt;
}
if(found==1 || curr->data==data)
{
curr1=curr->prev;
curr1->nxt=curr->nxt;
(curr->nxt)->prev=curr1;
printf("\n%d deleted!\n",curr->data);
free(curr);
}
else
printf("\n%d is not present in the list!\n",data);
}
}
void display()
{
curr=head;
if(head==NULL)
printf("\nList is empty!\n");
else {
printf("\nList:\n");
while(curr->nxt!=NULL) {
printf("%d\t",curr->data);
curr=curr->nxt;
}
printf("%d\n",curr->data);
}
}
void main()
{
int op,data;
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
clrscr();
printf("Creation of Doubly Linked List\n");
curr=(struct node*)malloc(sizeof(struct node));
curr->nxt=NULL;
curr->prev=NULL;
printf("Enter the first node:");
scanf("%d",&curr->data);
head=curr;
head->nxt=NULL;
head->prev=NULL;
do
{
printf("\nDOUBLY LINKED LIST OPERATIONS:\n1.Insert a node
\n2.Delete a node\n3.Display\n4.Exit\n");
printf("\nSelect an operation:");
scanf("%d",&op);
switch(op)
{
case 1: {
printf("Enter the position where you want to insert the
node:");
scanf("%d",&data);
insert(data);
break; }
case 2:{
printf("Enter the node to be deleted:");
scanf("%d",&data);
deletenode(data);
break; }
case 3: {
display();
break; }
case 4:
break;
default:
printf("\nEnter a valid option!");
}
}while(op!=4);
getch();
}
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Expected Output
Select an operation:1
Enter the position where you want to insert the node:1
Enter the node:80
80 inserted at pos:1!
Select an operation:1
Enter the position where you want to insert the node:2
Enter the node:60
Select an operation:2
Enter the node to be deleted:80
80 deleted!
Select an operation:4
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Ex. No : 9 Implement the basic operations on Circular Linked
Date : List
Aim: To write a program to implement the basic operations on Circular Linked List
Algorithm:
1. Start
2. Read the values of ch1, ch2
3. If ch=1 then check if (p==null) then n-> next =n;
Else then check while(p->next!=list1) then
p->=p->next; p->next=n;
n->next =list1
4. If ch=2 then check if (p=p->next) then list1=Null
Else
While(p->next !=list1)then
P=p->next; p->next=list1->next;
Q=list1
Free(q)
5. If ch=3 then while(i++!=post) then
Q=p; p=p->next
6. If ch=4 then while(i++=pos) then
Q=p; p=p->next
Free(p)
7. If ch=5 then check if list1 is Null then list1=n
8. If ch=6 then check if (p=p-> next) list1=Null
9. if ch=7 then check if list1==Null then print “List Empty”
10. Stop
Logical Description
In a circularly-linked list, the first and final nodes are linked together. This can be done
for both singly and doubly linked lists. To traverse a circular linked list, you begin at any
node and follow the list in either direction until you return to the original node. Viewed
another way, circularly-linked lists can be seen as having no beginning or end. This type
of list is most useful for managing buffers for data ingest, and in cases where you have
one object in a list and wish to see all other objects in the list.
Program Code
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
struct node *next;
}*head=NULL,*curr=NULL,*p=NULL;
void insert(int pos)
{
int count=1,i;
p=head;
while(p->next!=head)
{
count++;
p=p->next;
}
p=head;
if(pos<=count+1)
{
curr=(struct node*)malloc(sizeof(struct node));
printf("Enter the node:");
scanf("%d",&curr->data);
curr->next=NULL;
if(head==NULL)
{
head=curr;
head->next=head;
}
else if(pos==1)
{
while(p->next!=head)
p=p->next;
curr->next=head;
head=curr;
p->next=head;
}
else
{
for(i=1;i<(pos-1);i++)
p=p->next;
curr->next=p->next;
p->next=curr;
}
printf("\n%d inserted at pos:%d!\n",curr->data,pos);
}
else
printf("\nEnter a valid position!");
}
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
int flag=0;
p=head;
if(p->data==data)
{
if(p->next==head)
{
p=head;
head=NULL;
}
else
{
curr=head;
p=head;
while(curr->next!=head)
curr=curr->next;
head=head->next;
curr->next=head;
}
printf("\n%d deleted!\n",data);
free(p);
}
else
{
while(p->next->next!=head)
{
if(p->next->data==data)
{
flag=1;
break;
}
else
p=p->next;
}
if(p->next->data==data)
flag=1;
if(flag==1)
{
curr=p->next;
p->next=curr->next;
free(curr);
printf("\n%d deleted!\n",data);
}
else
printf("%d could not be found!\n",data);
}
}
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
void display()
{
p=head;
if(head!=NULL)
{
while(p->next!=head)
{
printf("%d\t",p->data);
p=p->next;
}
printf("%d\n",p->data);
}
else
printf("\nList empty!\n");
}
void search(int data)
{
int flag=0;
p=head;
while(p->next!=head)
{
if(p->data==data)
{
flag=1;
break;
}
else
p=p->next;
}
if(p->data==data)
flag=1;
if(flag==1)
printf("\n%d is present in the list\n",data);
else
printf("\n%d is not present in the list\n",data);
}
void main()
{
int op=0,data;
clrscr();
printf("CIRCULAR LINKED LIST\n");
printf("Creation of circular list\n");
curr=(struct node*)malloc(sizeof(struct node));
printf("Enter the first node:");
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
scanf("%d",&curr->data);
head=curr;
head->next=head;
printf("\nMENU\n1.Insert node\n2.Delete a node\n3.Search a
node\n4.Display\n5.Exit\n");
do{
printf("Select an operation:");
scanf("%d",&op);
switch(op)
{
case 1: {
printf("Enter the position where u want to insert:");
scanf("%d",&data);
insert(data);
break;}
case 2: {
printf("Enter the node to delete:");
scanf("%d",&data);
deletenode(data);
break; }
case 3: {
printf("Enter the node to be searched:");
scanf("%d",&data);
search(data);
break; }
case 4: {
display();
break; }
case 5:
break;
default:
printf("\nEnter a correct option!\n");
}
}while(op!=5);
getch();
}
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Expected Output
MENU
1.Insert node
2.Delete a node
3.Search a node
4.Display
5.Exit
Select an operation:1
Enter the position where u want to insert:1
Enter the node:40
40 inserted at pos:1!
Select an operation:1
Enter the position where u want to insert:4
Select an operation:1
Enter the position where u want to insert:3
Enter the node:60
60 inserted at pos:3!
Select an operation:4
40 12 60
Select an operation:2
Enter the node to delete:12
12 deleted!
Select an operation:2
Enter the node to delete:45
45 could not be found!
Select an operation:3
Enter the node to be searched:60
60 is present in the list
Select an operation:5
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Ex. No : 10
Binary Tree Traversals
Date :
Aim : To write a program to Create a binary search tree and do the following traversals
(i)In-order (ii) Pre order (iii) Post order
Algorithm :
1. Start
2. Read the values of ch
3. If ch=1 then it traverse through Inorder
Inorder(T-left)
Print(T->data)
Inorder(T->right)
4. if ch=2 then it traverse through
Postorder Postorder(T->left)
Postorder(T->right)
Preorder(T->right)
5. if ch=2 then it traverse through
Post order postorder(T->left)
Postorder(T->right)
Print(T->data)
6. Print the element
7. Stop the process.
Logical Description
The tree structures can be traversed in many different ways. Starting at the root of a
binary tree, there are three main steps that can be performed and the order in which they
are performed define the traversal type. These steps (in no particular order) are:
performing an action on the current node (referred to as "visiting" the node), traversing to
the left child node, and traversing to the right child node. Thus the process is most easily
described through recursion.
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
2. Visit the node.
3. Traverse the right subtree.
Program Code
# include<stdio.h>
# include <conio.h>
# include <malloc.h>
struct node
{
struct node *left;
int data;
struct node *right;
};
void main()
{
void insert(struct node **,int);
void inorder(struct node *);
void postorder(struct node *);
void preorder(struct node *);
struct node *ptr;
int will,i,num;
ptr = NULL;
ptr->data=NULL;
clrscr();
printf("Tree Traversal\n");
printf("\nEnter the number of terms to add ");
scanf("%d",&will);
for(i=0;i<will;i++)
{
printf("\nEnter the item");
scanf("%d",&num);
insert(&ptr,num);
}
getch();
printf("\nINORDER TRAVERSAL");
inorder(ptr);
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
getch();
printf("\nPREORDER TRAVERSAL");
preorder(ptr);
getch();
printf("\nPOSTORDER TRAVERSAL");
postorder(ptr);
getch();
}
void insert(struct node **p,int num)
{
if((*p)==NULL)
{
(*p)=malloc(sizeof(struct node));
(*p)->left = NULL;
(*p)->right = NULL;
(*p)->data = num;
return;
}
else
{
if(num==(*p)->data)
{
printf("\nREPEATED ENTRY ERROR VALUE REJECTED");
return;
}
if(num<(*p)->data)
{
insert(&((*p)->left),num);
}
else
{
insert(&((*p)->right),num);
}
}
return;
}
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
return;
}
void preorder(struct node *p)
{
if(p!=NULL)
{
printf(" %d ",p->data);
preorder(p->left);
preorder(p->right);
}
else
return;
}
void postorder(struct node *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf(" %d ",p->data);
}
else
return;
}
Expected Output
Tree Traversal
INORDER TRAVERSAL 2 3 4 5 7 9
PREORDER TRAVERSAL 7 5 3 2 4 9
POSTORDER TRAVERSAL 2 4
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Ex. No : 11
Binary Search Tree implementation
Date :
Aim: To write a program to implement Binary Search Tree
Algorithm:
1. Start
2. Read the option value
3. If option=1 then read x
And check if root is Null then assign root as t
4. Else store current data as x and print it
If (curr->data=x) then assign left child to curr and check if(curr=null)
Prev->lchild=t
5. Else Prev=curr, curr=curr->rchild then check if(curr==Null)then pre-> rchild=t
6. Print the value of x
7. Enter the no of deleted x
Check p is not null and then assign lchild as p
8. If p is null then print x
Check P as root then assign c as root. Then delete the node p
9. Stop
Logical Description
Insertion
Insertion begins as a search would begin; if the root is not equal to the value, we search
the left or right subtrees as before. Eventually, we will reach an external node and add the
value as its right or left child, depending on the node's value. In other words, we examine
the root and recursively insert the new node to the left subtree if the new value is less
than the root, or the right subtree if the new value is greater than or equal to the root.
Deletion
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Once we find either the in-order successor or predecessor, swap it with N, and then delete
it. Since both the successor and the predecessor must have fewer than two children, either
one can be deleted using the previous two cases. A good implementation avoids
consistently using one of these nodes, however, because this can unbalance the tree.
Program Code
/*BINARY SEARCH TREE*/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *left,*right;
}*tree;
void create();
void insert();
void del();
struct node *maketree(int);
void display(struct node*);
void setleft(struct node *,int);
void setright(struct node *,int);
void insert()
{
struct node *p,*q;
int num;
printf("\n\t\tEnter The Node Value : ");
scanf("%d",&num);
p=q=tree;
while((num!=p->data)&&(q!=NULL))
{
p=q;
if(num<p->data)
q=p->left;
else
q=p->right;
}
if(num==p->data)
printf("\n\t\tDuplicate Value");
else if(num<p->data)
setleft(p,num);
else
setright(p,num);
}
void main()
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
{
int ch;
clrscr();
printf("\n\t\t\tBINARY SEARCH TREE");
printf("\n\t\t\t~~~~~~~~~~~~~~~~~~~~~");
printf("\n\t\t\t 1.CREATION");
printf("\n\t\t\t 2.INSERTION");
printf("\n\t\t\t 3.DELETION");
printf("\n\t\t\t 4.DISPLAY");
printf("\n\t\t\t 5.EXIT\n");
do
{
printf("\n\t\tEnter The Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: create(); break;
case 2: insert(); break;
case 3: if(tree==NULL)
printf("\n\t Tree Empty");
else
del();
break;
case 4: printf("\n\t\tTREE CONTAINS : ");
display(tree);
printf("\n");
break;
default:printf("\n\t\tEXIT");
}
}while(ch<=4);
getch();
}
struct node *maketree(int val)
{
struct node *t=(struct node *)malloc(sizeof(struct node));
t->data=val;
t->left=t->right=NULL;
return(t);
}
void create()
{
int n,i,num;
printf("\n\t\tEnter The No Of Node :");
scanf("%d",&n);
printf("\n\t\tEnter The No Of Node :");
scanf("%d",&num);
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
tree=maketree(num);
for(i=2;i<=n;i++)
insert();
}
void display(struct node *p1)
{
if(p1==NULL)
return;
display(p1->left);
printf("%d",p1->data);
display(p1->right);
}
void setleft(struct node *p1,int val)
{
if(p1==NULL)
printf("\n\t\tVoid Insertion");
else if(p1->left!=NULL)
printf("\n\t\tInvalid Insertion");
else
p1->left=maketree(val);
}
void setright(struct node *p1,int val)
{
if(p1==NULL)
printf("\n\t\tVoid Insertion");
else if(p1->right!=NULL)
printf("\n\t\tInvalid Insertion");
else
p1->right=maketree(val);
}
void del()
{
struct node *p,*q,*rp,*f,*s;
int num;
p=tree;
q=NULL;
printf("\n\t\tEnter The Node To Be Deleted : ");
scanf("%d",&num);
while((p!=NULL)&&(p->data!=num))
{
q=p;
p=(num<p->data)?p->left:p->right;
}
if(p==NULL)
{
printf("\n\t\tVoid Deletion");
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
return;
}
if(p->left==NULL)
rp=p->right;
else if(p->right==NULL)
rp=p->left;
else
{
f=p;
rp=p->right;
s=rp->left;
while(s!=NULL)
{
f=rp;
rp=s;
s=rp->left;
}
if(f!=p)
{
f->left=rp->right;
rp->right=p->right;
}
rp->left=p->left;
}
if(q==NULL)
tree=rp;
else
p=(q->left)?(q->left=rp):(q->right=rp);
}
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Expected Output
TREE CONTAINS : 3 4 6 7 9
TREE CONTAINS : 3 4 5 6 7 9
TREE CONTAINS : 3 5 6 7 9
EXIT
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Ex. No: 12
Sort the given list of numbers using Heap Sort
Date:
Aim: To write a program to sort the given list of numbers using heap sort.
Algorithm:
1. Start
2. Read the value of n
3. check while(n>=2) then item =delete heap(a,n)
a[n]=item, n--
4. Insert heap(assigning int var) while (i>1) && (item>a[par]) then
A[i]=a[par], i=par, par=i/2
5. Check if (par<1) then par=1
Delete heap (assign value)
6. Check while (right<=n) then if (Last ) = a[left]&&
[last]=a[right] then a[i]=last
7. Else if [a(left)]=a[right] then
A[i]=a[left], i=left; else a[i]=a[right]
8. Stop
Logical Description
Heap sort inserts the input list elements into a heap data structure. The largest
value (in a max-heap) or the smallest values (in a min-heap) are extracted until none
remain, the values having been extracted in sorted order. The heap's invariant is
preserved after each extraction, so the only cost is that of extraction.
During extraction, the only space required is that needed to store the heap. In
order to achieve constant space overhead, the heap is stored in the part of the input array
that has not yet been sorted.
Program Code
#include<stdio.h>
#include<math.h>
void heapsort(int x[],int n);
void main()
{
int a[100],n,i;
clrscr();
printf("\n\tHEAP SORT");
printf("\nEnter the number of elements:");
scanf("%d",&n);
printf("Enter the elements:\n");
for(i=1;i<=n;i++)
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
scanf("%d",&a[i]);
heapsort(a,n);
printf("\n The sorted elements in the array are:");
for(i=1;i<=n;i++)
printf("\n%d",a[i]);
getch();
}
void cheap(int x[20],int n)
{
int s,f,key,q;
for(q=2;q<=n;++q)
{
s=q;
key=x[q];
f=(int)(s/2);
while((s>1)&&(key>x[f]))
{
x[s]=x[f];
s=f;
f=(int)(s/2);
if(f<1)
f=1;
}
x[s]=key;
}
}
void heapsort(int x[20],int n)
{
int i,temp,s,f,key;
cheap(x,n);
for(i=n;i>=2;--i)
{
temp=x[1];
x[1]=x[i];
x[i]=temp;
s=1;
key=x[s];
f=2;
if((f+1)<i)
if(x[f+1]>x[f])
f=f+1;
while((f<=(i-1))&&(x[f]>key))
{
x[s]=x[f];
s=f;
f=2*s;
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
if((f+1)<i)
if(x[f+1]>x[f])
f=f+1;
else
if(f>n)
f=n;
x[s]=key;
}
}
}
Expected Output
HEAP SORT
Enter the number of elements:5
Enter the elements:
5
6
4
2
8
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Ex. No: 13
Sort the given list of elements using Quick Sort
Date:
Aim: To write a program to sort the given list of elements using Quick Sort.
Algorithm:
Logical Description
Quick sort sorts by employing a divide and conquer strategy to divide a list into two sub-
lists. In quicksort, we divide the array of items to be sorted into two partitions and then
call the quicksort procedure recursively to sort the two partitions, ie we divide the
problem into two smaller ones and conquer by solving the smaller ones.
Program Code
/*QUIC SORT*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int i,j,n,pivot,a[20];
void quick(int a[],int left, int right);
void swap(int a[],int i, int j);
void main()
{
int a[20],n,i;
clrscr();
printf("\n\tQUICK SORT");
printf("\nEnter the number of elements:");
scanf(" %d",&n);
printf("Enter the elements:");
for(i=1;i<=n;i++)
scanf(" %d",&a[i]);
quick(a,1,n);
printf("\n The sorted elements in the array are:");
for(i=1;i<=n;i++)
printf(" %d",a[i]);
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
getch();
}
Expected Output
QUICK SORT
Enter the number of elements:5
Enter the elements:7
9
4
3
1
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Ex. No: 14
Depth First Search
Date:
Aim: To write a program to traverse the tree using Depth first search.
Algorithm:
1. Start
2. Read the value of n
3. Set for loop as i<n as i=0
4. Set for loop as j>n as j=0
5. Read adjacency of adj[i][j]
Read dfs(s)
6. Set for loop i<n and execute check whether (!vis[i]) then
Dfs(i)
7. Stop
Logical Description
DFS is an uninformed search that progresses by expanding the first child node of the
search tree that appears and thus going deeper and deeper until a goal node is found, or
until it hits a node that has no children. Then the search backtracks, returning to the most
recent node it hadn't finished exploring. In a non-recursive implementation, all freshly
expanded nodes are added to a LIFO stack for exploration.
Program Code
#include<stdio.h>
#include<conio.h>
#define max 50
int adj[max][max];
int vertex[max];
int visited[max];
void creategraph(int);
void dfs(int,int);
void depthfirst(int);
void main()
{
int ch,n,v;
clrscr();
do
{
printf("\n1.create graph"
"\n2.dfs"
"\n3.exit"
"\n Enter your choice");
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
scanf("%d",&ch);
switch(ch)
{
case 1:{ printf("\n Enter no. of vertices to be created");
scanf("%d",&v);
creategraph(v);
break; }
case 2: depthfirst(v);
break;
case 3:printf("end of the program");
break;
}
}while(ch!=3);
getch();
}
void creategraph(int v)
{
int i,j,n,m,k;
for(i=0;i<v;i++)
{
printf("\n Enter the node value");
scanf("%d",&vertex[i]);
visited[i]=0;
}
for(j=0;j<v;j++)
adj[i][j]=0;
printf("Enter the adjacency vertices list");
printf("for each vertex of the graph");
for(i=0;i<v;i++)
{
printf("\n Enter the no.of adjancey vertices for the vertex %d",vertex[i]);
scanf("%d",&n);
for(j=0;j<n;j++)
{
printf("\n Enter adjacency vertex");
scanf("%d",&m);
for(k=0;k<v;k++)
{
if(vertex[k]==m)
adj[i]]k]=1;
}
}
}
printf("\n graph created with no. of vertices");
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
printf("the adjacency matrix of graph\n");
for(i=0;i<v;i++)
{
for(j=0;j<v;j++)
printf("%d\t",adj[i][j]);
printf("\n");
}
}
void depthfirst(int v)
{
int i;
for(i=0;i<v;i++)
visited[i]=0;
visited[0]=1;
printf("Depth first serach \n");
printf("%d",vertex[0]);
dfs(0,v);
}
void dfs(int ad,int v)
{
int j,k;
for(k=ad;k<v;k++)
for(j=0;j<v;j++)
if(adj[k][j]==1)
{
if(visited[j]==0)
{
visited[j]=1;
printf("%d",vertex[j]);
dfs(j,v);
}
}
}
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Expected Output
1.create graph
2.dfs
3.exit
Enter your choice1
Enter the adjacency vertices list for each vertex of the graph
Enter the no.of adjancey vertices for the vertex 1
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Ex. No: 15
Breadth First Search
Date:
Aim: To write a program to traverse the tree using breadth first search.
Algorithm:
1. Start
2. Read the size value in choice=1 and get the vertex value
3. Set for loop of condition i=1 and i<n
4. Read the number of edges
5. Set for loop of condition j<= no. of edges and j=1
6. Read the edge position
7. if Ch=2 then check a[i][j]==1
then check v[j]=0
printf v[j]
8. If ch==3 for display set for loop i<=n and print vt[i]
9. Set for loop j<=n and then print the a[i][j]
10. Stop
Logical Description
BFS is an uninformed search method that aims to expand and examine all nodes of a
graph systematically in search of a solution. In other words, it exhaustively searches the
entire graph without considering the goal until it finds it. It does not use a heuristic.
From the standpoint of the algorithm, all child nodes obtained by expanding a node are
added to a FIFO queue. In typical implementations, nodes that have not yet been
examined for their neighbors are placed in some container (such as a queue or linked list)
called "open" and then once examined are placed in the container "closed".
Program Code
#include<stdio.h>
void insert(int c);
int del();
int front=-1,rear=-1;
int q[30],ans[10],k=0;
void main()
{
int i,j,n,no,a[10][10];
clrscr();
printf("\n Enter the number of Nodes: ");
scanf("%d",&n);
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
printf("\n Enter the adjacency matrix of the graph:\n ");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("\n Adjacency is: \n ");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("\t %d",a[i][j]);
printf("\n");
}
insert(0);
printf("\bBreadth First Search of the nodes is:\n");
while(rear!=front)
{
i=del();
printf("\t:%d",i+1);
for(j=0;j<n;j++)
{
if (a[i][j]==1)
insert(j);}
}
getch();
}
int del()
{
return q[++front];
}
void insert(int c)
{
int i,count=0;
for(i=0;i<k;i++)
{
if(ans[i]==c)
{
count=1;
break;
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
}
}
if(count==0)
{
q[++rear]=c;
ans[k++]=c;
}
Expected Output
Adjacency is:
0 1 1 0
1 0 1 0
1 1 0 1
1 0 1 0
Breadth First Search of the nodes is:
:1 :2 :3 :4
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Ex. No: 16
Dijkstra’s Algorithm
Date:
Aim: To write a program to implement Dijkstra’s algorithm to find the shortest path.
Algorithm:
1. Start
2. Read the value of n
3. Read small dist, newdist, maxnode
4. set for loop of i< 1 to n-2
5. set for loop of i< 1 to n
6. Check whether dist[i] =newdist then if n-1 then calculate smalldist=dist[i]
Assign the value of i<j
7. print current
8. check whether dist[i] <small dist if it is true print current value otherwise print
newdist[i]
9. Stop
Logical Description
Dijkstra's algorithm is a graph search algorithm that solves the single-source shortest path
problem for a graph with non negative edge path costs, outputting a shortest path tree.
This algorithm is often used in routing.
For a given source vertex (node) in the graph, the algorithm finds the path with lowest
cost (i.e. the shortest path) between that vertex and every other vertex. It can also be used
for finding costs of shortest paths from a single vertex to a single destination vertex by
stopping the algorithm once the shortest path to the destination vertex has been
determined. For example, if the vertices of the graph represent cities and edge path costs
represent driving distances between pairs of cities connected by a direct road, Dijkstra's
algorithm can be used to find the shortest route between one city and all other cities.
Program Code
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define MAX 30
#define UNVISITED -1
#define VISITED 1
#define INFINITY 32767
int adjMat[MAX][MAX],n;
void viewAdjMat();
void viewPathMat(int pm[MAX],int n,int len);
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
int searchPath(int src,int des,int pathMat[MAX],int *minLen);
typedef struct
{
int previous,len,status;
}node;
void main()
{
char ch,s,d;
int i,j,k,src,des,minLen,tot,pathMat[MAX];
clrscr();
printf(“\n DIJKSTRA’S SHORTEST PATH ALGORITHM”);
printf("\n\N Enter total number of vertex:");
scanf("%d",&n);
printf("\n\n************ADJACENCY MATRIX********\n\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&adjMat[i][j]);
while(1)
{
printf("\n\n Enter the source node :");
fflush(stdin);
scanf("%c",&s);
printf("\n\n Enter the destination node :");
fflush(stdin);
scanf("%c",&d);
src=toupper(s)-64;
des=toupper(d)-64;
tot=searchPath(src,des,pathMat,&minLen);
viewPathMat(pathMat,tot,minLen);
printf("\n Do you want to continue(y/n):");
ch=getche();
if(ch!='y' && ch!='Y')
break;} }
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
printf("%d ",adjMat[pm[k]] [pm[k-1]]);
}
else
printf("\n NO path from source to destination node \n");
}
int searchPath(int src,int des,int pathMat[MAX],int *minLen)
{
node graph[MAX];
int i,k,min,tot=0,curVertex,newLen,u,v;
*minLen=0;
for(i=1;i<=n;i++)
{
graph[i].previous=0;graph[i].len=INFINITY;
graph[i].status=UNVISITED;
}
graph[src].previous=0;graph[src].len=0;
graph[src].status=VISITED;curVertex=src;
while(curVertex!=des){
for(k=1;k<=n;k++){
if(adjMat[curVertex][k]>0 && graph[k].status==UNVISITED){
newLen=graph[curVertex].len+adjMat[curVertex][k];
if(newLen<graph[k].len){
graph[k].previous=curVertex;
graph[k].len=newLen;
}
}
}
min=INFINITY;curVertex=0;
for(i=1;i<=n;i++)
if(graph[i].status==UNVISITED && graph[i].len<min){
min=graph[i].len;curVertex=i;
}
if(curVertex==0) return 0;
graph[curVertex].status=VISITED;
}
while(curVertex!=0){
pathMat[++tot]=curVertex;
curVertex=graph[curVertex].previous;
}
for(i=tot;i>1;i--){
u=pathMat[i];v=pathMat[i-1];
*minLen=*minLen+adjMat[u][v];
}
return(tot); }
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla
Expected Output
Data Structures Lab Manual, Dept. of Computer Science and Engineering VSSUT, Burla