DS Lab
DS Lab
DS Lab
SREYAS
SYLLABUS
Week1:
Write a C program that uses functions to perform the following:
a) Create a singly linked list of integers.
b) Delete a given integer from the above linked list.
c) Display the contents of the above list after deletion.
Week 2:
Write a C program that uses functions to perform the following:
a) Create a doubly linked list of integers.
b) Delete a given integer from the above doubly linked list.
c) Display the contents of the above list after deletion.
Week 3:
Write a C program that uses stack operations to convert a given infix expressio
n into its postfix Equivalent, Implement the stack using an array.
Week 4:
Write C programs to implement a double ended queue ADT using i)array and ii)doubly
linked list respectively.
Week 5:
Write a C program that uses functions to perform the following:
a) Create a binary search tree of characters.
b) Traverse the above Binary search tree recursively in Postorder.
Week 6:
Write a C program that uses functions to perform the following:
a) Create a binary search tree of integers.
b) Traverse the above Binary search tree non recursively in inorder.
Week 7:
Write C programs for implementing the following sorting methods to arrange a list of integers in
Ascending order :
a)Insertion sort b) Merge sort
Week 8:
Write C programs for implementing the following sorting methods to arrange a list of integers in
ascending order:
a)Quick sort b) Selection sort
Week 9:
i) Write a C program to perform the following operation:
a)Insertion into a B-tree.
ii) Write a C program for implementing Heap sort algorithm for sorting a given list of integers in
ascending order.
Week 10:
Department of Computer Science and Engineering, SREYAS
Page 1
Source Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head,*var,*trav;
void insert_at_begning(int value)
{
var=(struct node *)malloc(sizeof (struct node));
var->data=value;
if(head==NULL)
{
head=var;
head->next=NULL;
}
else
{
var->next=head;
head=var;
Department of Computer Science and Engineering, SREYAS
Page 2
}
}
void insert_at_end(int value)
{
struct node *temp;
temp=head;
var=(struct node *)malloc(sizeof (struct node));
var->data=value;
if(head==NULL)
{
head=var;
head->next=NULL;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
var->next=NULL;
temp->next=var;
}
}
void insert_at_middle(int value, int loc)
{
struct node *var2,*temp;
var=(struct node *)malloc(sizeof (struct node));
var->data=value;
temp=head;
if(head==NULL)
{
head=var;
head->next=NULL;
}
else
{
while(temp->data!=loc)
{
temp=temp->next;
}
var2=temp->next;
temp->next=var;
var->next=var2;
}
}
Department of Computer Science and Engineering, SREYAS
Page 3
void display()
{
trav=head;
if(trav==NULL)
{
printf("\nList is Empty");
}
else
{
printf("\nElements in the List: ");
while(trav!=NULL)
{
printf(" -> %d ",trav->data);
trav=trav->next;
}
printf("\n");
}
}
void main()
{
int i=0;
head=NULL;
printf("insertion at begning of linked list - 1");
printf("\ninsertion at the end of linked list - 2");
printf("\ninsertion at the middle where you want - 3");
printf("\ndeletion from the end of linked list - 4");
printf("\ndeletion of the data that you want - 5");
printf("\nexit - 6\n");
while(1)
{
printf("\n enter the choice of operation to perform on linked list");
scanf("%d",&i);
switch(i)
{
case 1:
{
int value;
printf("\n enter the value to be inserted");
scanf("%d",&value);
insert_at_begning(value);
display();
break;
}
case 2:
{
int value;
printf("\n enter value to be inserted");
scanf("%d",&value);
Department of Computer Science and Engineering, SREYAS
Page 5
insert_at_end(value);
display();
break;
}
case 3:
{
int value,loc;
printf("\n after which data you want to insert the data");
scanf("%d",&loc);
printf("\n enter the value to be inserted");
scanf("%d",&value);
insert_at_middle(value,loc);
display();
break;
}
case 4:
{
delete_from_end();
display();
break;
}
case 5:
{
int value;
display();
printf("\n enter the data that you want to delete from the list shown above");
scanf("%d",&value);
delete_from_middle(value);
display();
break;
}
case 6:
{
exit(0);
}
}
}
}
Output:
Insertion at beginning of linked list-1
Insertion at end of linked list-2
Insertion at middle of linked list-3
Deletion from the end of linked list-4
Deletion of the data that u want-5
Exit-6
Enter the choice of operation to perform on linked list
1
Department of Computer Science and Engineering, SREYAS
Page 6
temp->previous=NULL;
temp->next=head;
head->previous=temp;
head=temp;
}
}
void insert_end(int value)
{
struct node *var,*temp;
var=(struct node *)malloc(sizeof(struct node));
var->data=value;
if(head==NULL)
{
head=var;
head->previous=NULL;
head->next=NULL;
last=head;
}
else
{
last=head;
while(last!=NULL)
{
temp=last;
last=last->next;
}
last=var;
temp->next=last;
last->previous=temp;
last->next=NULL;
}
}
int insert_after(int value, int loc)
{
struct node *temp,*var,*temp1;
var=(struct node *)malloc(sizeof(struct node));
var->data=value;
if(head==NULL)
{
head=var;
head->previous=NULL;
head->next=NULL;
}
else
{
temp=head;
while(temp!=NULL && temp->data!=loc)
Department of Computer Science and Engineering, SREYAS
Page 9
{
temp=temp->next;
}
if(temp==NULL)
{
printf("\n%d is not present in list ",loc);
}
else
{
temp1=temp->next;
temp->next=var;
var->previous=temp;
var->next=temp1;
temp1->previous=var;
}
}
last=head;
while(last->next!=NULL)
{
last=last->next;
}
}
int delete_from_end()
{
struct node *temp;
temp=last;
if(temp->previous==NULL)
{
free(temp);
head=NULL;
last=NULL;
return 0;
}
printf("\nData deleted from list is %d \n",last->data);
last=temp->previous;
last->next=NULL;
free(temp);
return 0;
}
int delete_from_middle(int value)
{
struct node *temp,*var,*t, *temp1;
temp=head;
while(temp!=NULL)
{
if(temp->data == value)
{
if(temp->previous==NULL)
Department of Computer Science and Engineering, SREYAS
Page 10
{
free(temp);
head=NULL;
last=NULL;
return 0;
}
else
{
var->next=temp1;
temp1->previous=var;
free(temp);
return 0;
}
}
else
{
var=temp;
temp=temp->next;
temp1=temp->next;
}
}
printf("data deleted from list is %d",value);
}
void display()
{
struct node *temp;
temp=head;
if(temp==NULL)
{
printf("List is Empty");
}
while(temp!=NULL)
{
printf("-> %d ",temp->data);
temp=temp->next;
}
}
int main()
{
int value, i, loc;
head=NULL;
printf("Select the choice of operation on link list");
printf("\n1.) insert at begning\n2.) insert at at\n3.) insert at middle");
printf("\n4.) delete from end\n5.) reverse the link list\n6.) display list\n7.)exit");
while(1)
{
printf("\n\nenter the choice of operation you want to do ");
Department of Computer Science and Engineering, SREYAS
Page 11
scanf("%d",&i);
switch(i)
{
case 1:
{
printf("enter the value you want to insert in node ");
scanf("%d",&value);
insert_begning(value);
display();
break;
}
case 2:
{
printf("enter the value you want to insert in node at last ");
scanf("%d",&value);
insert_end(value);
display();
break;
}
case 3:
{
printf("after which data you want to insert data ");
scanf("%d",&loc);
printf("enter the data you want to insert in list ");
scanf("%d",&value);
insert_after(value,loc);
display();
break;
}
case 4:
{
delete_from_end();
display();
break;
}
case 5:
{
printf("enter the value you want to delete");
scanf("%d",value);
delete_from_middle(value);
display();
break;
}
case 6 :
{
display();
break;
}
case 7 :
Department of Computer Science and Engineering, SREYAS
Page 12
{
exit(0);
break;
}
}
}
printf("\n\n%d",last->data);
display();
getch();
}
Output:
Output:
Select the choice of operation on link list
1.) insert at begning
2.) insert at at
3.) insert at middle
4.) delete from end
5.) reverse the link list
6.) display list
7.)exit
enter the choice of operation you want to do 1
enter the value you want to insert in node 4
-> 4
enter the choice of operation you want to do 1
enter the value you want to insert in node 3
-> 3 -> 4
enter the choice of operation you want to do 1
enter the value you want to insert in node 6
-> 6 -> 3 -> 4
enter the choice of operation you want to do 2
enter the value you want to insert in node at last 3
-> 6 -> 3 -> 4 -> 3
enter the choice of operation you want to do 3
after which data you want to insert data 4
enter the data you want to insert in list 5
-> 6 -> 3 -> 4 -> 5 -> 3
enter the choice of operation you want to do 4
Data deleted from list is 3
-> 6 -> 3 -> 4 -> 5
enter the choice of operation you want to do
4
Department of Computer Science and Engineering, SREYAS
Page 13
/*POP FUNCTION*/
int pop (int stack[])
{
Department of Computer Science and Engineering, SREYAS
Page 14
int ret;
if (top == -1)
{ ret = 0;
status = 0;
}
else
{ status = 1;
ret = stack [top];
--top;
}
return ret;
}
/*FUNCTION TO DISPLAY STACK*/
void display (int stack[])
{ int i;
printf ("\nThe Stack is: ");
if (top == -1)
printf ("empty");
else
{ for (i=top; i>=0; --i)
printf ("\n--------\n|%3d |\n--------",stack[i]);
}
printf ("\n");
}
/*MAIN PROGRAM*/
void main()
{
int stack [MAX], item;
int ch;
clrscr ();
top = -1;
do
{ do
{ printf ("\n MAIN MENU");
printf ("\n1.PUSH (Insert) in the Stack");
printf ("\n2.POP (Delete) from the Stack");
printf ("\n3.Exit (End the Execution)");
printf ("\nEnter Your Choice: ");
scanf ("%d", &ch);
if (ch<1 || ch>3)
printf ("\nInvalid Choice, Please try again");
}while (ch<1 || ch>3);
switch (ch)
{case 1:
printf ("\nEnter the Element to be pushed : ");
scanf ("%d", &item);
printf (" %d", item);
push (stack, item);
if (status)
Department of Computer Science and Engineering, SREYAS
Page 15
Output:
1.push
2.pop
3.exit.
Enter yourchoice:1
1.insertion
2.deletion
3.display stack contents
4.display top most element
5.exit
Enter your choice:1
Enter the element to be inserted:10
element is inserted
1.insertion
2.deletion
3.display stack contents
4.display top most element
5.exit
Enter your choice:1
Enter the element to be inserted:20
element is inserted
1.insertion
Department of Computer Science and Engineering, SREYAS
Page 16
2.deletion
3.display stack contents
4.display top most element
5.exit
Enter your choice:1
Enter the element to be inserted:30
element is inserted
1.insertion
2.deletion
3.display stack contents
4.display top most element
5.exit
Enter your choice:4
top of the stack element is:30
1.insertion
2.deletion
3.display stack contents
4.display top most element
5.exit
Enter your choice:3
stack contains as follows:
30 20 10
1.insertion
2.deletion
3.display stack contents
4.display top most element
5.exit
Enter your choice:2
deleted element is:30
1.insertion
2.deletion
3.display stack contents
4.display top most element
5.exit
Enter your choice:5
1.integer stack
2.float stack
3.char stack.
4.exit.
Enter your choice:4
Source Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
int q[MAX],front=0,rear=0;
void add_rear();
void add_front();
void delete_rear();
void delete_front();
void display();
void main()
{
int ch;
clrscr();
do
{
//clrscr();
printf("\n DQueue Menu");
printf("\n--------------");
printf("\n 1. AddRear");
printf("\n 2. AddFront");
printf("\n 3. DeleteRear");
printf("\n 4. DeleteFront");
printf("\n 5. Display");
printf("\n 6. Exit");
printf("\n--------------");
printf("\n Enter your choice:-");
scanf("%d",&ch);
switch(ch)
{
case 1:
Department of Computer Science and Engineering, SREYAS
Page 18
{
add_rear();
printf("\n Queue after insert at rear");
display();
break;
}
case 2:
{
add_front();
printf("\n Queue after insert at front");
display();
break;
}
case 3:
{
delete_rear();
printf("\n Queue after delete at rear");
display();
break;
}
case 4:
{
delete_front();
printf("\n Queue after delete at front");
display();
break;
}
case 5:
{
display();
break;
}
case 6:
{
exit(0);
break;
}
default:
{
printf("\n Wrong Choice\n");
}
}
} while(ch!=6);
}
void add_rear()
Department of Computer Science and Engineering, SREYAS
Page 19
{
int no;
printf("\n Enter value to insert : ");
scanf("%d",&no);
if(rear==MAX)
{
printf("\n Queue is Overflow");
return;
}
else
{
rear++;
q[rear]=no;
if(rear==0)
rear=1;
if(front==0)
front=1;
}
}
void add_front()
{
int no;
printf("\n Enter value to insert:-");
scanf("%d",&no);
if(front<=1)
{
printf("\n Cannot add value at front end");
return;
}
else
{
front--;
q[front]=no;
}
}
void delete_front()
{
int no;
if(front==0)
{
printf("\n Queue is Underflow\n");
return;
}
else
{
no=q[front];
printf("\n Deleted element is %d\n",no);
Department of Computer Science and Engineering, SREYAS
Page 20
if(front==rear)
{
front=0;
rear=0;
}
else
{
front++;
}
}
}
void delete_rear()
{
int no;
if(rear==0)
{
printf("\n Cannot delete value at rear end\n");
return;
}
else
{
no=q[rear];
if(front==rear)
{
front=0;
rear=0;
}
else
{
rear--;
printf("\n Deleted element is %d\n",no);
}
}
}
void display()
{
int i;
if(front==0)
{
printf("\n Queue is Underflow\n");
return;
}
else
{
printf("\n Output");
for(i=front;i<=rear;i++)
{
Department of Computer Science and Engineering, SREYAS
Page 21
printf("\n %d",q[i]);
}
}
}
Output:
DQueue Menu
-------------1. AddRear
2. AddFront
3. DeleteRear
4. DeleteFront
5. Display
6. Exit
-------------Enter your choice:-1
Enter value to insert : 3
Queue after insert at rear
Output
3
DQueue Menu
-------------1. AddRear
2. AddFront
3. DeleteRear
4. DeleteFront
5. Display
6. Exit
-------------Enter your choice:-1
Enter value to insert : 5
Queue after insert at rear
Output
3
5
DQueue Menu
-------------1. AddRear
2. AddFront
3. DeleteRear
4. DeleteFront
5. Display
6. Exit
-------------Enter your choice:-4
Deleted element is 3
Queue after delete at front
Output
5
Department of Computer Science and Engineering, SREYAS
Page 23
DQueue Menu
-------------1. AddRear
2. AddFront
3. DeleteRear
4. DeleteFront
5. Display
6. Exit
-------------Enter your choice:-5
Output
5
DQueue Menu
-------------1. AddRear
2. AddFront
3. DeleteRear
4. DeleteFront
5. Display
6. Exit
-------------Enter your choice:Algorithm:
ii)doubly linked list respectively.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
struct node *previous;
int data;
struct node *next;
}*head, *last;
void insert_begning(int value)
{
struct node *var,*temp;
var=(struct node *)malloc(sizeof(struct node));
var->data=value;
Department of Computer Science and Engineering, SREYAS
Page 24
if(head==NULL)
{
head=var;
head->previous=NULL;
head->next=NULL;
last=head;
}
else
{
temp=var;
temp->previous=NULL;
temp->next=head;
head->previous=temp;
head=temp;
}
}
void insert_end(int value)
{
struct node *var,*temp;
var=(struct node *)malloc(sizeof(struct node));
var->data=value;
if(head==NULL)
{
head=var;
head->previous=NULL;
head->next=NULL;
last=head;
}
else
{
last=head;
while(last!=NULL)
{
temp=last;
last=last->next;
}
last=var;
temp->next=last;
last->previous=temp;
last->next=NULL;
}
}
int insert_after(int value, int loc)
{
struct node *temp,*var,*temp1;
var=(struct node *)malloc(sizeof(struct node));
var->data=value;
Department of Computer Science and Engineering, SREYAS
Page 25
if(head==NULL)
{
head=var;
head->previous=NULL;
head->next=NULL;
}
else
{
temp=head;
while(temp!=NULL && temp->data!=loc)
{
temp=temp->next;
}
if(temp==NULL)
{
printf("\n%d is not present in list ",loc);
}
else
{
temp1=temp->next;
temp->next=var;
var->previous=temp;
var->next=temp1;
temp1->previous=var;
}
}
last=head;
while(last->next!=NULL)
{
last=last->next;
}
}
int delete_from_end()
{
struct node *temp;
temp=last;
if(temp->previous==NULL)
{
free(temp);
head=NULL;
last=NULL;
return 0;
}
printf("\nData deleted from list is %d \n",last->data);
last=temp->previous;
last->next=NULL;
free(temp);
return 0;
}
Department of Computer Science and Engineering, SREYAS
Page 26
int main()
{
int value, i, loc;
head=NULL;
printf("Select the choice of operation on link list");
printf("\n1.) insert at begning\n2.) insert at at\n3.) insert at middle");
printf("\n4.) delete from end\n5.) reverse the link list\n6.) display list\n7.)exit");
while(1)
{
printf("\n\nenter the choice of operation you want to do ");
scanf("%d",&i);
switch(i)
{
case 1:
{
printf("enter the value you want to insert in node ");
scanf("%d",&value);
insert_begning(value);
display();
break;
}
case 2:
{
printf("enter the value you want to insert in node at last ");
scanf("%d",&value);
insert_end(value);
display();
break;
}
case 3:
{
printf("after which data you want to insert data ");
scanf("%d",&loc);
printf("enter the data you want to insert in list ");
scanf("%d",&value);
insert_after(value,loc);
display();
break;
}
case 4:
{
delete_from_end();
display();
break;
}
case 5:
{
printf("enter the value you want to delete");
scanf("%d",value);
Department of Computer Science and Engineering, SREYAS
Page 28
delete_from_middle(value);
display();
break;
}
case 6 :
{
display();
break;
}
case 7 :
{
exit(0);
break;
}
}
}
printf("\n\n%d",last->data);
display();
getch();
}
output:-
Source Code:
AIM: b)Traverse the above Binary search tree recursively in Postorder.
Algorithm:
Source Code:
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
typedef struct BST
{
int data;
struct BST *lchild,*rchild;
}node;
void insert(node *,node *);
void inorder(node *);
void preorder(node *);
void postorder(node *);
node *search(node *,int,node **);
Department of Computer Science and Engineering, SREYAS
Page 31
void main()
{
int choice;
char ans='N';
int key;
node *new_node,*root,*tmp,*parent;
node *get_node();
root=NULL;
printf("nProgram For Binary Search Tree ");
do
{
printf("\n1.Create");
printf("\n2.Search");
printf("\n3.Recursive Traversals");
printf("\n4.Exit");
printf("\nEnter your choice :");
scanf("%d",&choice);
switch(choice)
{
case 1:
do
{
new_node=get_node();
printf("\nEnter The Element ");
scanf("%d",&new_node->data);
if(root==NULL) /* Tree is not Created */
root=new_node;
else
insert(root,new_node);
printf("\nWant To enter More Elements?(y/n)");
ans=getch();
}while(ans=='y');
break;
case 2:
printf("\nEnter Element to be searched :");
scanf("%d",&key);
tmp = search(root,key,&parent);
printf("\nParent of node %d is %d",
tmp->data,parent->data);
break;
case 3:
if(root==NULL)
printf("Tree Is Not Created");
else
{
printf("\nThe Inorder display : ");
inorder(root);
printf("\nThe Preorder display : ");
preorder(root);
Department of Computer Science and Engineering, SREYAS
Page 32
{
printf("\n The %d Element is Present",temp->data);
return temp;
}*parent=temp;
if(temp->data>key)
temp=temp->lchild;
else
temp=temp->rchild;
}
return NULL;
}
/*
This function displays the tree in inorder fashion
*/
void inorder(node *temp)
{
if(temp!=NULL)
{
inorder(temp->lchild);
printf("%d",temp->data);
inorder(temp->rchild);
}
}
/*
This function displays the tree in preorder fashion
*/
void preorder(node *temp)
{
if(temp!=NULL)
{
printf("%d",temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
}
}
/*
This function displays the tree in postorder fashion
*/
void postorder(node *temp)
{
if(temp!=NULL)
{
postorder(temp->lchild);
postorder(temp->rchild);
printf("%d",temp->data);
}
}
new_node=get_node();
printf("nEnter The Element ");
scanf("%d",&new_node->data);
if(root==NULL) /* Tree is not Created */
root=new_node;
else
insert(root,new_node);
printf("nWant To enter More Elements?(y/n)");
ans=getch();
}while(ans=='y');
break;
case 2:
printf("nEnter Element to be searched :");
scanf("%d",&key);
tmp = search(root,key,&parent);
printf("nParent of node %d is %d",
tmp->data,parent->data);
break;
case 3:
if(root==NULL)
printf("Tree Is Not Created");
else
{
printf("nThe Inorder display : ");
inorder(root);
printf("nThe Preorder display : ");
preorder(root);
printf("nThe Postorder display : ");
postorder(root);
}
break;
}
}while(choice!=4);
}
/*
Get new Node
*/
node *get_node()
{
node *temp;
temp=(node *)malloc(sizeof(node));
temp->lchild=NULL;
temp->rchild=NULL;
return temp;
}
/*
This function is for creating a binary search tree
*/
void insert(node *root,node *new_node)
Department of Computer Science and Engineering, SREYAS
Page 36
{
if(new_node->data < root->data)
{
if(root->lchild==NULL)
root->lchild = new_node;
else
insert(root->lchild,new_node);
}
if(new_node->data > root->data)
{
if(root->rchild==NULL)
root->rchild=new_node;
else
insert(root->rchild,new_node);
}
}
/*
This function is for searching the node from
binary Search Tree
*/
node *search(node *root,int key,node **parent)
{
node *temp;
temp=root;
while(temp!=NULL)
{
if(temp->data==key)
{
printf("n The %d Element is Present",temp->data);
return temp;
}
*parent=temp;
if(temp->data>key)
temp=temp->lchild;
else
temp=temp->rchild;
}
return NULL;
}
/*
This function displays the tree in inorder fashion
*/
void inorder(node *temp)
{
if(temp!=NULL)
{
inorder(temp->lchild);
printf("%d",temp->data);
inorder(temp->rchild);
Department of Computer Science and Engineering, SREYAS
Page 37
}
}
/*
This function displays the tree in preorder fashion
*/
void preorder(node *temp)
{
if(temp!=NULL)
{
printf("%d",temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
}
}
/*
This function displays the tree in postorder fashion
*/
void postorder(node *temp)
{
if(temp!=NULL)
{
postorder(temp->lchild);
postorder(temp->rchild);
printf("%d",temp->data);
}
}
Output:
Source Code:
/* a)Insertion sort */
#include>stdio.h>
#include>conio.h>
void main()
Department of Computer Science and Engineering, SREYAS
Page 38
{
int arr[5]={25,17,31,13,2};
int i,j,k,temp;
printf(insertion sort \n);
printf(array before sorting \n);
for(i=0;i<=4;i++)
printf(%d,arr[i]);
for(i=1;i<=4;i++)
{
for(j=0;j<=4;j++)
{
if(arr[j]>arr[i])
{
temp=arr[j];
arr[j]=arr[i];
for(k=I;k>j;k--)
arr[k]=arr[k-1];
arr[k+1]=temp;
}
}
}
printf(\n);
printf(array after sorting \n);
for(i=0;i<=4;i++)
printf(%d\t,arr[i]);
}
Output:
31
AIM:
b) To implement the Merge sort
Source Code:
/* b) Merge sort */
#include<stdio.h>
#include<conio.h>
void mergesort(int a[],int,int);
void merge(int [],int,int,int);
void main()
{
int a[20],i,n;
clrscr();
printf("Enter the number of elements");
scanf("%d",&n);
printf("Enter the elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
mergesort(a,0,n-1);
printf("Data After Merge Sort");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}
void mergesort(int a[],int lb,int ub)
{
int mid;
if(lb<ub)
{
mid=(lb+ub)/2;
mergesort(a,lb,mid);
mergesort(a,mid+1,ub);
merge(a,lb,mid+1,ub);
Department of Computer Science and Engineering, SREYAS
Page 40
}
}
void merge(int a[],int lb,int mid,int ub)
{
int k,p1,p2,p3,b[20];
p1=lb;
p3=lb;
2=mid;
while((p1<mid)&&(p2<=ub))
{
if(a[p1]<=a[p2])
b[p3++]=a[p1++];
else
b[p3++]=a[p2++];
}
while(p1<mid)
{
b[p3++]=a[p1++];
}
while(p2<=ub)
{
b[p3++]=a[p2++];
}
for(k=lb;k<p3;k++)
{
a[k]=b[k];
}
}
Output:
Enter 10 numbers for list1:0 1 2 3 4 5 6 7 8 9
Enter 10 numbers for list1:0 1 2 3 4 5 6 7 8 9
Department of Computer Science and Engineering, SREYAS
Page 41
array[low++]=array[high];
array[high--]=temp;
}
}
while(low<=high);
if(first<high)
qsort(array,first,high);
if(low<last)
qsort(array,low,last);
}
void display(int list[],int n)
{ int i;
printf("\n List after sorting the elements\n\n");
for(i=1;i<=n;i++)
{
printf("%d",list[i]);
}
}
Output:
how many numbers do you want to Enter:6
Enter numbers:9 8 7 5 3 4
elements after quicksort:3 4 5 7 8 9
how many numbers do you want to Enter:10
Enter numbers:9 8 7 5 3 4 1 -3 -2 0
elements after quicksort:-2 -3 0 1 3 4 5 7 8 9
b) Selection sort
AIM :
To imlementing the selection sort to arrange elements in ascending order.
Source Code:
#include<stdio.h>
Department of Computer Science and Engineering, SREYAS
Page 43
#include<conio.h>
void select(int [],int);
void bubble(int [],int);
int min(int [],int,int);
void main()
{
int a[20],i,n;
clrscr();
printf("Enter the number of items in the array");
anf("%d",&n);
printf("Enter the data in the array");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
bubble(a,n);
select(a,n);
getch();
}
void bubble(int a[],int n)
{
int i,temp,p;
for(i=1;i<n;i++)
{
for(p=0;p<n-i;p++)
{
if(a[p]>a[p+1])
{
temp=a[p];
a[p]=a[p+1];
a[p+1]=temp;
}
}
}
printf("\nData After Bubble Sort");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
}
void select(int a[],int n)
{
int i,loc,temp;
loc=0;
temp=0;
for(i=0;i<n;i++)
{
loc=min(a,i,n);
temp=a[loc];
a[loc]=a[i];
a[i]=temp;
Department of Computer Science and Engineering, SREYAS
Page 44
}
printf("\nData After Selection Sort");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
}
int min(int a[],int lb,int ub)
{
int m=lb;
while(lb<ub)
{
if(a[lb]<a[m])
{
m=lb;
}
lb++;
}
return m;
}
Output:
Output:
Enter the number of items in the array
5
Enter the data in the array
11
3
9
77
5
Data After Bubble Sort
3
5
9
11
77
Data After Selection Sort
3
5
9
11
77
Source Code:
/*Heap Sort.*/
#include<iostream.h>
#include<conio.h>
#define max 20;
int heap[21];
void insert(int,int,int);
void makeheap(int);
void heapsort(int);
void main()
{
int i,j,n;
clrscr();
cout<<"how many numbers are there for sorting?:";
cin>>n;
for(i=1;i<=n;i++)
cin>>heap[i];
makeheap(n);
cout<<"after heapsort";
heapsort(n);
for(i=1;i<=n;i++)
cout<<heap[i]<<" ";
Department of Computer Science and Engineering, SREYAS
Page 46
cout<<"\n";
getch();
}
void makeheap(int size)
{
int k,kmax;
kmax=size/2;
for(k=kmax;k>=1;k--)
insert(heap[k],k,size);
}
void insert(int i,int n,int s)
{
int c,t;
c=n*2;
while(c<=s)
{
if(c<s && heap[c]<heap[c+1])
c++;
if(i>=heap[c])
break;
else
{
t=heap[n];
heap[n]=heap[c];
heap[c]=t;
Department of Computer Science and Engineering, SREYAS
Page 47
n=c;
c=n*2;
}
}
}
void heapsort(int s)
{
int i,j,t;
for(i=s;i>1;i--)
{
t=heap[i];
heap[i]=heap[1];
heap[1]=t;
insert(heap[1],1,i-1);
}
}
Output:
how many numbers do you want to Enter:5
Enter numbers:6 4 3 2 1
After sorted elements:1 2 3 4 6
how many numbers do you want to Enter:6
Enter numbers:5 3 4 1 -3 0
After sorted elements:-3 0 1 3 4 5
Department of Computer Science and Engineering, SREYAS
Page 48
Source Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
* finds the position of the pattern in the given target string
* target - str, patter - word
*/
int kmpSearch(char *str, char *word, int *ptr) {
int i = 0, j = 0;
while ((i + j) < strlen(str)) {
/* match found on the target and pattern string char */
if (word[j] == str[i + j]) {
if (j == (strlen(word) - 1)) {
printf("%s located at the index %d\n",
word, i + 1);
return;
}
j = j + 1;
} else {
/* manipulating next indices to compare */
i = i + j - ptr[j];
if (ptr[j] > -1) {
j = ptr[j];
} else {
j = 0;
}
}
}
}
/* find the overlap array for the given pattern */
void findOverlap(char *word, int *ptr) {
int i = 2, j = 0, len = strlen(word);
ptr[0] = -1;
ptr[1] = 0;
while (i < len) {
if (word[i - 1] == word[j]) {
j = j + 1;
ptr[i] = j;
Department of Computer Science and Engineering, SREYAS
Page 50
i = i + 1;
} else if (j > 0) {
j = ptr[j];
} else {
ptr[i] = 0;
i = i + 1;
}
}
return;
}
int main() {
char word[256], str[1024];;
int *ptr, i;
/* get the target string from the user */
printf("Enter your target string:");
fgets(str, 1024, stdin);
str[strlen(str) - 1] = '\0';
/* get the pattern string from the user */
printf("Enter your pattern string:");
fgets(word, 256, stdin);
word[strlen(word) - 1] = '\0';
/* dynamic memory allocation for overlap array */
ptr = (int *)calloc(1, sizeof(int) * (strlen(word)));
/* finds overlap array */
findOverlap(word, ptr);
/* find the index of the pattern in target string */
kmpSearch(str, word, ptr);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter your target string: abc abcdabcdabd
Enter your pattern string: abcdabd
abcdabd located at the index 9
AIM:
Write C programs for implementing the following graph traversal algorithms:
a)Depth first traversal b)Breadth first traversal.
Source Code:
/*a) Depth First Traversal*/
#include<stdio.h>
#include<conio.h>
char stack[20];
int top=-1, n;
char arr[20];
char dfs(int );
char ajMat[20][20];
char b[20];
void display();
int p=0;
void main()
{
char v,k;
int l=0,i,j;
printf("Enter the number of nodes in a graph");
scanf("%d",&n);
printf("Enter the value of node of graph");
for(i=0; i<n; i++)
{
scanf("%s",&b[i]);
}
k=b[0];
printf("Enter the value in adjancency matrix in from of 'Y' or 'N'\n");
printf("\nIf there is an edge between the two vertices then enter 'Y' or 'N'\n");
for(i=0; i<n; i++)
printf(" %c ",b[i]);
for(i=0;i<n; i++)
{
printf("\n%c ",b[i]);
for(j=0; j<n; j++)
{
printf("%c ",v=getch());
ajMat[i][j]=v;
}
printf("\n\n");
Department of Computer Science and Engineering, SREYAS
Page 52
}
for(i=0;i<n;i++)
{
l=0;
while(k!=b[l])
l++;
k=dfs(l);
}
display();
getch();
}
void display()
{ int i;
printf(" DFS of Graph : ");
for(i=0; i<n; i++)
printf("%c ",arr[i]);
}
void push(char val)
{
top=top+1;
stack[top]=val;
}
char pop()
{
return stack[top];
}
int unVisit(char val)
{
int i;
for(i=0; i<p; i++)
if(val==arr[i])
return 0;
for(i=0; i<=top; i++)
if(val==stack[top])
return 0;
return 1;
}
char dfs(int i)
{
int k,j;
char m;
if(top==-1)
{
push(b[i]);
}
m=pop();
top--;
Department of Computer Science and Engineering, SREYAS
Page 53
arr[p]=m;
p++;
for(j=0; j<n; j++)
{
if(ajMat[i][j]=='y')
{
if(unVisit(b[j]))
{
push(b[j]);
}
}
}
return stack[top];
}
Output:
int bfs(int i)
{
int j;
char m;
if(front==0)
{
insert(b[i]);
}
for(j=0; j<n; j++)
{
if(ajMat[i][j]=='y')
{
if(unVisit(b[j]))
{
insert(b[j]);
}
}
}
m=del();
arr[p]=m;
p++;
return 0;
}
Output: