DS Labmanual
DS Labmanual
Write a program that uses functions to perform the following operations on singly linked
List.:
i)Creation ii) Insertion iii) Deletion iv) Traversal
Source Code :
#include <stdio.h>
#include <stdlib.h>
/*Creating a node*/
struct node *create(int value)
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=value;
newnode->next=NULL;
return newnode;
}
/*Function Declaration*/
void insbeg();
void insend();
void insany();
void display();
void delbeg();
void delend();
void delany();
void insend()
{
struct node *newnode,*temp;
int value;
printf("Enter node data:");
1
scanf("%d",&value);
newnode=create(value);
if(head==NULL)
{
head=newnode;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
display();
}
void insany()
{
struct node *newnode,*temp1,*temp2;
temp1=head;
int value,pos,i;
printf("Enter node data:");
scanf("%d",&value);
printf("\nEnter Position:");
scanf("%d",&pos);
newnode=create(value);
if(head==NULL)
{
head=newnode;
}
else
{
if(pos==1)
{
insbeg();
}
else
{
for(i=0;i<pos-1;i++)
{
if(temp1->next==NULL)
{
insend();break;
}
temp2=temp1;
temp1=temp1->next;
}
temp2->next=newnode;
newnode->next=temp1;
}
}
display();
}
display();
}
else
{
head=head->next;
free(temp);
display();
}
}
void delend()
{
struct node *temp2,*temp1=head;
if(head==NULL)
{
printf("List empty");
}
else if(head->next==NULL)
{
head=NULL;
free(temp1);
display();
}
else
{
while(temp1->next!=NULL)
{
temp2=temp1;
temp1=temp1->next;
}
temp2->next=NULL;
free(temp1);
display();
}
}
void delany()
{
int pos,i;
struct node *temp1=head,*temp2;
printf("\nEnter Position:");
scanf("%d",&pos);
if(head==NULL)
{
printf("List empty\n");
}
3
else if(head->next==NULL)
{
head=NULL;
free(temp1);
}
else
{
if(pos==1)
{
delbeg();
}
else
{
for(i=1;i<pos;i++)
{
while(temp1->next==NULL)
{
delend();break;
}
temp2=temp1;
temp1=temp1->next;
}
temp2->next=temp1->next;
temp1->next=NULL;
free(temp1);
display();
}
}
}
printf("NULL\n\n");
}
}
5
2. Write a program that uses functions to perform the following operations on doubly linked
list.:
i)Creation ii) Insertion iii) Deletion iv) Traversal
Source Code :
#include <stdio.h>
#include <stdlib.h>
void insbeg();
void insend();
void insany();
void display();
void delbeg();
void delend();
void delany();
struct node
{
int data;
struct node *next;
struct node *prev;
}*head=NULL;
void insbeg()
{
struct node *newnode;
int value;
printf("Enter node data:");
scanf("%d",&value);
newnode=create(value);
if(head==NULL)
{
head=newnode;
}
else
{
newnode->next=head;
head->prev=newnode;
head=newnode;
}
display();
}
void insend()
{
struct node *newnode,*temp;
int value;
printf("Enter node data:");
scanf("%d",&value);
6
newnode=create(value);
if(head==NULL)
{
head=newnode;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
newnode->prev=temp;
}
display();
}
void insany()
{
struct node *newnode,*temp1,*temp2;
temp1=head;
int value,pos,i;
printf("Enter node data:");
scanf("%d",&value);
printf("\nEnter Position:");
scanf("%d",&pos);
newnode=create(value);
if(head==NULL)
{
head=newnode;
}
else
{
if(pos==0)
{
insbeg();
}
else
{
for(i=0;i<pos-1;i++)
{
if(temp1->next==NULL)
{
insend();break;
}
temp2=temp1;
temp1=temp1->next;
}
temp2->next=newnode;
newnode->prev=temp2;
newnode->next=temp1;
temp1->prev=newnode;
}
}
display();
}
7
void display()
{
struct node *temp;
temp=head;
if(temp==NULL)
{
printf("List Empty\n");
}
else
{
while(temp->next!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("%d->NULL\n",temp->data);
}
}
void delbeg()
{
struct node *temp=head;
if(head==NULL)
{
printf("List empty");
}
else if(head->next==NULL)
{
head=NULL;
free(temp);
display();
}
else
{
head=head->next;
head->prev=NULL;
temp->next=NULL;
free(temp);
display();
}
}
void delend()
{
struct node *temp2,*temp1=head;
if(head==NULL)
{
printf("List Empty");
}
else if(temp1->next==NULL)
{
head=NULL;
free(temp1);
display();
}
else
8
{
while(temp1->next!=NULL)
{
temp1=temp1->next;
}
temp2=temp1->prev;
temp1->next=NULL;
temp1->prev=NULL;
temp2->next=NULL;
free(temp1);
}
}
void delany()
{
int pos,i;
struct node *temp1=head,*temp2;
printf("Enter position:");
scanf("%d",&pos);
if(head==NULL)
{
printf("List empty\n");
}
else if(head->next==NULL)
{
head=NULL;
free(temp1);
display();
}
else
{
if(pos==1)
{
delbeg();
}
else
{
for(i=1;i<pos;i++)
{
while(temp1->next==NULL)
{
delend();break;
}
temp1=temp1->next;
}
temp2=temp1->prev;
temp2->next=temp1->next;
temp1->prev=NULL;
temp1->next=NULL;
free(temp1);
display();
}
}
}
9
void main()
{
int a;
while(1)
{
printf("\n\t\t1.Insert at beginning\n");
printf("\t\t2.Insert at end\n");
printf("\t\t3.Insert at specified position\n");
printf("\t\t4.Traversal\n");
printf("\t\t5.Delete at beginning\n");
printf("\t\t6.Delete at end\n");
printf("\t\t7.Delete at specified position\n");
printf("\t\t8.Exit\n");
printf("Enter your choice:");
scanf("%d",&a);
switch(a)
{
case 1:insbeg();break;
case 2:insend();break;
case 3:insany();break;
case 4:display();break;
case 5:delend();break;
case 6:delany();break;
case 7:display();break;
case 8:exit(0);
default:printf("\n******************( Invalid input )******************\n");
}
}
}
SAMPLE INPUT & OUTPUT
10
3. Write a program that uses functions to perform the following operations on circular linked
List.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
Source Code :
#include <stdio.h>
#include <stdlib.h>
void delbeg();
void delend();
void delany();
void insbeg();
void insend();
void insany();
void display();
struct node
{
int data;
struct node *next;
}*head=NULL;
void insbeg()
{
struct node *newnode,*temp;
temp=head;
int value;
printf("Enter node data:");
scanf("%d",&value);
newnode=create(value);
if(head==NULL)
{
head=newnode;
newnode->next=head;
}
else
{
newnode->next=head;
while(temp->next!=head)
{
temp=temp->next;
}
head=newnode;
temp->next=head;
}
display();
}
void insend()
{
struct node *newnode,*temp;
int value;
printf("Enter node data:");
scanf("%d",&value);
newnode=create(value);
if(head==NULL)
{
head=newnode;
newnode->next=head;
}
else
{
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=newnode;
newnode->next=head;
}
display();
}
void insany()
12
{
struct node *newnode,*temp1,*temp2;
temp1=head;
int value,pos,i;
printf("Enter node data:");
scanf("%d",&value);
printf("\nEnter Position:");
scanf("%d",&pos);
newnode=create(value);
if(head==NULL)
{
head=newnode;
newnode->next=head;
}
else
{
if(pos==0)
{
insbeg();
}
else
{
for(i=0;i<pos-1;i++)
{
if(temp1->next==head)
{
insend();break;
}
temp2=temp1;
temp1=temp1->next;
}
temp2->next=newnode;
newnode->next=temp1;
}
}
display();
}
void display()
{
struct node *temp;
temp=head;
if(temp==NULL)
{
printf("List Empty\n");
}
else
{
while(temp->next!=head)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("%d->",temp->data);
printf("NULL\n");
}
}
13
void delbeg()
{
struct node *temp=head;
if(head==NULL)
{
printf("List empty");
}
else if(head->next==NULL)
{
head=NULL;
free(temp);
display();
}
else
{
head=head->next;
head->prev=NULL;
temp->next=NULL;
free(temp);
display();
}
}
void delend()
{
struct node *temp2,*temp1=head;
if(head==NULL)
{
printf("List Empty");
}
else if(temp1->next==NULL)
{
head=NULL;
free(temp1);
display();
}
else
{
while(temp1->next!=NULL)
{
temp1=temp1->next;
}
temp2=temp1->prev;
temp1->next=NULL;
temp1->prev=NULL;
temp2->next=NULL;
free(temp1);
}
}
void delany()
{
int pos,i;
struct node *temp1=head,*temp2;
printf("Enter position:");
14
scanf("%d",&pos);
if(head==NULL)
{
printf("List empty\n");
}
else if(head->next==NULL)
{
head=NULL;
free(temp1);
display();
}
else
{
if(pos==1)
{
delbeg();
}
else
{
for(i=1;i<pos;i++)
{
while(temp1->next==NULL)
{
delend();break;
}
temp1=temp1->next;
}
temp2=temp1->prev;
temp2->next=temp1->next;
temp1->prev=NULL;
temp1->next=NULL;
free(temp1);
display();
}
}
}
int main()
{
int a;
while(1)
{
printf("\n\t\t1.Insert at beginning\n");
printf("\t\t2.Insert at end\n");
printf("\t\t3.Insert at specified position\n");
printf("\t\t4.Traversal\n");
printf("\t\t5.Delete at beginning\n");
printf("\t\t6.Delete at end\n");
printf("\t\t7.Delete at specified position\n");
printf("\t\t8.Exit\n");
printf("Enter your choice:");
scanf("%d",&a);
15
switch(a)
{
case 1:insbeg();break;
case 2:insend();break;
case 3:insany();break;
case 4:display();break;
case 5:delbeg();break;
case 6:delend();break;
case 7:delany();break;
case 8:exit(1);
default:printf("Invalid Input");
}
}
}
Source Code :
#include <stdio.h>
#include <stdlib.h>
#define size 100
void stkarr();
void stkpoi();
void push()
{
int value;
printf("Enter data:");
scanf("%d",&value);
if(top==-1)
stack[++top]=value;
else if(top==n-1)
printf("****Stack overflow****\n");
else
stack[++top]=value;
printf("stack elements:\n");
display();
}
void pop()
{
if(top==-1)
printf("****Stack underflow****\n");
else if(top==n)
top=-1;
else
top=--top;
display();
}
void display()
{
int i;
if(top==-1)
printf("Stack Empty\n");
else
for(i=top;i>=0;i--)
printf("\t\t_%d_\n",stack[i]);
}
}
17
void stkpoi() //STACK USING POINTER
{
int n,a,value;
printf("Enter the size of stack:");
scanf("%d",&n);
int stack[n];
while(1)
{
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\nEnter your choice:");
scanf("%d",&a);
switch(a)
{
case 1: printf("Enter data:");
scanf("%d",&value);
push(stack,value,n);break;
case 2: pop(stack,n);break;
case 3: display(stack,n);break;
case 4: return ; //returns to main function
default:printf("Invalid Input\n");
}
if(top==-1)
x[++top]=value;
else if(top==y-1)
printf("****Stack overflow****\n");
else
x[++top]=value;
printf("stack elements:\n");
display(x,y);
}
void pop(int*x,int y)
{
if(top==-1)
printf("****Stack underflow****\n");
else
top--;
display(x,y);
}
if(top==-1)
18
printf("Stack Empty\n");
else
for(i=top;i>=0;i--)
printf("\t\t_%d_\n",x[i]);
}
}
void main()
{
int s;
while(1)
{
printf("\n1.Stack using array");
printf("\n2.Stack using Pointers");
printf(“\n3.Exit”);
printf("\nEnter your choice :");
scanf("%d",&s);
switch(s)
{
case 1:stkarr();break;
case 2:stkpoi();break;
case 3:exit(0);
default:printf("Invalid Input");
}
}
}
Source Code :
#include <stdio.h>
#include <stdlib.h>
#define size 100
int rear=-1,front=-1;
void qarr();
19
void qpoi();
int a;
printf("Enter the size of queue:");
scanf("%d",&n);
while(1)
{
printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
printf("Enter your choice:");
scanf("%d",&a);
switch(a)
{
case 1:enqueue();break;
case 2:dequeue();break;
case 3:display();break;
case 4:return;
default:printf("Invalid choice\n");
}
}
void enqueue()
{
int value;
printf("Enter the data:");
scanf("%d",&value);
if(rear==-1 && front==-1)
rear=++rear,front=rear,queue[front]=value;
else if(rear==n-1)
printf("****queue overflow****\n");
else
queue[++rear]=value;
display();
}
void dequeue()
{
if(front==-1 && rear==-1)
printf("Queue underflow\n");
else if(front==rear)
front=-1,rear=-1;
else
front=++front;
display();
}
void display()
{
int i;
if(rear==-1 && front==-1)
printf("Queue Empty\n");
20
else
for(i=rear;i>=front;i--)
printf("->|%d|",queue[i]);
}
}
int a,n,value;
printf("Enter the size of queue:");
scanf("%d",&n);
int stack[n];
while(1)
{
printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
printf("Enter your choice:");
scanf("%d",&a);
switch(a)
{
case 1:
printf("Enter data:");
scanf("%d",&value);
enqueue(stack,n,value);break;
case 2:dequeue(stack);break;
case 3:display(stack);break;
case 4:return ;
default:printf("Invalid choice\n");
}
}
display(x);
}
display(x);
}
void main()
{
int s;
while(1)
{
printf("\n1.Queue using array");
printf("\n2.Queue using Pointers");
printf(“\n3.Exit”);
printf("\nEnter your choice :");
scanf("%d",&s);
switch(s)
{
case 1:qarr();break;
case 2:qpoi();break;
case 3:exit(0);
default:printf("Invalid Input");
}
}
}
SAMPLE INPUT & OUTPUT
6. Write a program that implements the following sorting methods to sort a given list of
integers in ascending order
i)Bubble sort ii) Selection sort iii) Insertion sort.
Source Code :
#include <stdio.h>
void bubble_sort(int a[],int n);
void selection_sort(int a[],int n);
void insertion_sort(int a[],int n);
void swap(int *a,int *b)
22
void swap(int *a,int *b)
{
*a=*a+*b;
*b=*a-*b;
*a=*a-*b;
}
void bubble_sort(int a[],int n)
{
int i,j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j+1]<a[j])
swap(&a[j+1],&a[j]);
}
}
printf("\nsorted Array using bubble sort :");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
void selection_sort(int a[],int n)
{
int i,j,min_idx;
for(i = 0; i < n - 1; i++)
{
min_idx=i;
for(j = i + 1; j < n; j++)
{
if(a[min_idx] > a[j])
min_idx=j;
}
if(min_idx != i)
swap(&a[i],&a[min_idx]);
}
printf("sorted array using selection sort :");
for(i=0;i<n;i++)
printf("%d ",a[i]);
23
}
void main()
{
int n,i;
printf("Enter the size of array :");
scanf("%d",&n);
int a[n];
printf("Enter array elements :");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
while(1)
{
int c;
printf("\n\n1.Bubble Sort");
printf("\n2.Selection Sort");
printf("\n3.Insertion Sort");
printf("\n4.Exit");
printf("\nEnter your choice :");
scanf("%d",&c);
switch(c)
{
case 1:bubble_sort(a,n);break;
case 2:selection_sort(a,n);break;
case 3:insertion_sort(a,n);break;
case 4:exit(0);
default:printf("\nInvalid Input");
}
}
}
7. Write a program that use both recursive and non recursive functions to perform the
Following searching operations for a Key value in a given list of integers:
i)Linear search ii) Binary search
Source Code :
#include<stdio.h>
24
#include<stdlib.h>
void linsearch(int a[],int size);
void binsearch(int a[],int size);
void insertion_sort(int a[],int n);
void lin_search_rec(int a[],int i,int key,int size);
void bin_search_rec(int a[],int beg,int key,int end);
void main()
{
int ch,n,i,key,t;
printf("Enter the size of list :");
scanf("%d",&n);
int a[n];
printf("Enter list elements :",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
insertion_sort(a,n);
while(1)
{
printf("\n 1.Linear Search\n 2.Binary Search\n");
printf("3.Recursive Linear Search");
printf("\n4.Recursive Binary Search");
printf("\n5.Exit");
printf("\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:linsearch(a,n);break;
case 2:binsearch(a,n);break;
case 3:printf("Enter the key to be searched :");
scanf("%d",&key);
lin_search_rec( a,0, key, n);
case 4:printf("Enter the key to be searched :");
scanf("%d",&key);
case 5:exit(0);
}
}
}
if(key==a[mid])
{
printf("\t\t---key found---");
}
else if(a[mid]>key)
{
for(i=0;i<mid;i++)
{
if(a[i]==key)
{
printf("\t\t---key found---\n");
break;
}
}
if(i==mid)
printf("\t\t---key not found---");
}
else
{
for(i=mid;i<n;i++ )
{
if(a[i]==key)
{
printf("\t\t---key found---\n");
break;
}
}
if(i==n)
printf("\t\t---key not found---\n");
26
}
}
if(a[i]==key)
{
printf("\t\t---KEY FOUND---");
}
else if(i+1!=size)
{
lin_search_rec( a,i+1,key, size);
}
else
{
printf("\t\t---KEY NOT FOUND");
}
}
27
8. Write a program to implement the tree traversal methods.
Source Code :
#include <stdio.h>
#include <stdlib.h>
struct bstnode
{
int data;
struct bstnode *lchild;
struct bstnode *rchild;
}*root=NULL;
void main()
{
int a,value,key;
struct bstnode *newnode;
while(1)
{
printf("\n");
printf("\t\t1.Insert element into bst\n");
printf("\t\t2.inorder\n");
printf("\t\t3.post-order\n");
printf("\t\t4.pre-order\n");
printf("\t\t5.Exit\n");
printf("\nEnter your choice:");
scanf("%d",&a);
switch(a)
{
case 1: printf("Enter Node data: ");
scanf("%d",&value);
newnode=bstcreate(value);
if(root==NULL)
{
root=newnode;
}
else
{
struct bstnode *temp;
temp=root;
insert(temp,newnode);
}break;
28
case 2:if(root==NULL)
{
printf("\n******************( TREE IS EMPTY )******************\n\n");
}
else
{
inorder(root);
}break;
case 3:if(root==NULL)
{
printf("\n******************( TREE IS EMPTY )******************\n\n");
}
else
{
postorder(root);
}break;
case 4:if(root==NULL)
{
printf("\n******************( TREE IS EMPTY )******************\n\n");
}
else
{
preorder(root);
}break;
case 5:exit(1);
default:printf("\n******************( Invalid input )******************\n");
}
}
}
}
}
}
}
if(temp==NULL)
{
printf("\n******************( KEY NOT FOUND )******************\n");
}
else if(key==temp->data)
{
printf("key found at node %d\n",parent->data);
}
else if(key>temp->data)
{
search(temp->rchild,key,temp);
}
else if(key<temp->data)
{
search(temp->lchild,key,temp);
}
}
void del(struct bstnode *temp,int key,struct bstnode *parent)
{
struct bstnode*temp1=temp;
if(key==temp->data)
{
if(temp->rchild==NULL && temp->lchild==NULL)
{
if(parent->data>key)
{
parent->lchild=NULL;
30
free(temp);
}
else if(parent->data<key)
{
parent->rchild=NULL;
free(temp);
}
else
{
root=NULL;
free(temp);
}
}
else if(temp->rchild==NULL || temp->lchild==NULL)
{
if(temp->rchild==NULL)
{
if(parent->data>key)
{
parent->lchild=temp->lchild;
temp->lchild=NULL;
free(temp);
}
else
{
parent->rchild=temp->lchild;
temp->lchild=NULL;
free(temp);
}
}
else
{
if(parent->data>key)
{
parent->lchild=temp->rchild;
temp->rchild=NULL;
free(temp);
}
else
{
parent->rchild=temp->rchild;
temp->rchild=NULL;
free(temp);
}
}
}
else if(temp->rchild!=NULL && temp->lchild!=NULL)
{
temp1=findmin(temp->rchild);
temp->data=temp1->data;
del(temp->rchild,temp1->data,temp);
}
}
else if(key<temp->data)
{ if(temp->lchild==NULL)
{
printf("\n******************( KEY NOT FOUND )******************\n");
}
31
else
{
del(temp->lchild,key,temp);
}
}
else if(key>temp->data)
{
if(temp->rchild==NULL)
{
printf("\n******************( KEY NOT FOUND )******************\n");
}
else
{
del(temp->rchild,key,temp);
}
}
}
void main()
{
int i,j,s,t;
while(1)
{
printf("1.BFS\n2.DFS\n3.Re-enter data\n4.Exit\n");
printf("Enter the traversal operation to be performed:");
scanf("%d",&t);
printf("Enter the no.of vertices:");
scanf("%d",&n);
printf("\nEnter Graph:\n");
for(i=1;i<=n;i++)
{
vis[i]=0;//intializing array
for(j=1;j<=n;j++)
{
scanf("%d",&dfs_gph[i][j]);
}
}
case 1:bfs(s);break;
case 2:dfs(s);break;
case 4:exit(1);
default:printf("Invalid choice \n");
}
void dfs(int v)
{
int i;
vis[v]=1;
for(i=1;i<=n;i++)
{
if( dfs_gph[v][i]==1 && vis[i]==0 )
{
printf("%d->%d\n",v,i);
dfs(i);
}
}
void bfs(int v)
{
int i,j=1;
vis[v]=1;
33
bfs_que[bfs_r+1]=v;
while(j<n)
{
for(i=1;i<=n;i++)
{
if(dfs_gph[v][i]==1&&vis[i]==0)
{
vis[i]=1;
bfs_que[bfs_r]=i;
printf("%d-->%d\n",v,bfs_que[bfs_r++]);
}
}
j++;
}
if(bfs_f<n)
bfs(bfs_que[bfs_f++]);
}
SAMPLE INPUT & OUTPUT
34