0% found this document useful (0 votes)
23 views98 pages

Dsuc Program

Uploaded by

anupk6347
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views98 pages

Dsuc Program

Uploaded by

anupk6347
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 98

Data Structure Using C

Experiment No:1

Object: Calculate factorial of a number using recursion

int fact(int x)
{
return (x<=1?1:x*fact(x-1));
}
void main()
{
int n,ans;
printf("\nInput number to calculate factorial");
scanf("%d",&n);
ans=fact(n);
printf("\nFactorial value is %d",ans);
getch();
}

Signature

(Rajeev Ranjan Kumar Tripathi)


Data Structure Using C
Experiment No:2

Object: Print Fibonacci series using recursion

int fib(int x)
{
if(x==1)
return 0;
else if(x==2)
return 1;
else
return (fib(x-1)+fib(x-2));
}
void main()
{
int i,n;
clrscr();
printf("\nInput number upto that you want to print fibonacci series");
scanf("%d",&n);
for(i=1;i<=n;i++)
printf("\n%d",fib(i));
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:3

Object: Implementing Ackermann function

int ackermann(int m,int n)


{
if(m==0)
return (n+1);
else if(m!=0&&n==0)
return ackermann(m-1,1);
else if(m!=0&&n!=0)
return ackermann(m-1,ackermann(m,n-1));
}
void main()
{
int m,n;
clrscr();
printf("\nInput value of m and n");
scanf("%d%d",&m,&n);
printf("\nValue is=%d",ackermann(m,n));
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:4

Object: Calculating GCD of a given number using recursion

int GCD(int a,int b)


{
if(a<b)
return GCD(b,a);
else if(b==0)
return a;
else
return GCD(b,a%b);
}
void main()
{
int x,y;
clrscr();
printf("\nInput x and y for calculation of GCD");
scanf("%d%d",&x,&y);
printf("\nGCD is=%d",GCD(x,y));
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:5

Object: Implementing Tower Of Hanoi.

void tower_of_hanoi(int disc_no ,char from_peg,char to_peg,char aux_peg)


{
if(disc_no==1)
printf("\n%s%c%s%c","Move disk 1 from peg ",from_peg,"to peg ",to_peg);
else
{
tower_of_hanoi(disc_no-1,from_peg,aux_peg,to_peg);
printf("\n%s%d%s%c%s%c","Move disk ",disc_no,"frompeg ",from_peg,"to peg
",to_peg);
tower_of_hanoi(disc_no-1,aux_peg,to_peg,from_peg);
}
}
void main()
{
int n;
char from_peg,to_peg,aux_peg;
clrscr();
printf("\nInput number of disc");
scanf("%d",&n);
printf("\n Demonstrating Tower Of Hanoi For %d Disk ",n);
printf("\n A is the label of starting peg");
printf("\n B is the label of auxillary peg");
printf("\n C is the label of ending or final peg");
tower_of_hanoi(n,'A','B','C');
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:6

Object: Implementing Tower Of Hanoi and finding time for


solution.

#include<dos.h>
#include<time.h>
static long unsigned int i=0;
void tower_of_hanoi(int disc_no ,char from_peg,char to_peg,char aux_peg)
{
if(disc_no==1)
{
printf("\n%s%c%s%c","Move disk 1 from peg ",from_peg,"to peg ",to_peg);
i=i+1;
}
else
{
i=i+1;
tower_of_hanoi(disc_no-1,from_peg,aux_peg,to_peg);
printf("\n%s%d%s%c%s%c","Move disk ",disc_no,"frompeg ",from_peg,"to peg
",to_peg);
tower_of_hanoi(disc_no-1,aux_peg,to_peg,from_peg);
}
}
void main()
{
int n;
char from_peg,to_peg,aux_peg;
clock_t start, end;
clrscr();
printf("\nInput number of disc");
scanf("%d",&n);
printf("\n Demonstrating Tower Of Hanoi For %d Disk ",n);
printf("\n A is the label of starting peg");
printf("\n B is the label of auxillary peg");
printf("\n C is the label of ending or final peg");
start = clock();
Data Structure Using C
tower_of_hanoi(n,'A','B','C');
end = clock();
printf("The time was: %f\n", (end - start) / CLK_TCK);
printf("\nTotal Number of Iteration=%lu",i);
getch();

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:7

Object: Implementing stack using array.

#define MAXSIZE 5
int top=-1;
int stack[MAXSIZE];
void push(int x)
{
if(top==MAXSIZE-1)
printf("\nOverflow occurs ");
else
stack[++top]=x;
}
void pop()
{
if(top==-1)
printf("\nUnderflow occurs");
else
top=top-1;
}
void display()
{
int i=0;
printf("\nDisplaying stack");
if(top==-1)
printf("\nThere is no item to display");
else
for(i=0;i<=top;i++)
printf("\n%d",stack[i]);
}
void main()
{
clrscr();
push(10);
push(20);
push(30);
Data Structure Using C
push(40);
push(50);
display();
push(60);
pop();
display();
pop();
pop();
pop();
pop();
display();
pop();
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:8

Object: Validating expression using sack.

#define SIZE 10
char stack[SIZE];
int top=-1;
void push(char a)
{
stack[++top]=a;
}
void pop(char a)
{
if((stack[top]=='('&&a==')')||(stack[top]=='{'&&a=='}')||(stack[top]=='['&&a==']'))
top-=1;
}
int peep()
{
return (top==-1?0:1);
}
void main()
{
char *a;
int i=0;
clrscr();
printf("\nInput erxpression to validate");
gets(a);
for(i=0;i<strlen(a);i++)
{
if(a[i]=='{'||a[i]=='['||a[i]=='(')
push(a[i]);
else if(a[i]=='}'||a[i]==']'||a[i]==')')
pop(a[i]);
}
if(!peep())
printf("\nExpression is valid");
else
Data Structure Using C
printf("\nExpression is invalid");
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:9

Object: Implementing queue using array.

#define MAXSIZE 5
int FRONT=-1;
int REAR=-1;
int QUEUE[MAXSIZE];
void push(int item)
{
if(REAR==MAXSIZE-1)
printf("\nOverflow");
if(FRONT==-1&&REAR==-1)
{
FRONT=FRONT+1;
REAR=REAR+1;
QUEUE[REAR]=item;
}
else
{
REAR=REAR+1;
QUEUE[REAR]=item;
}
}
void pop()
{
if(FRONT==-1)
printf("\nUnderflow");
else if(FRONT==REAR)
{
FRONT=-1;
REAR=-1;
}
else
FRONT=FRONT+1;
}
void display()
Data Structure Using C
{
int i;
printf("\nDisplaying items of queue");
if(FRONT==-1&&REAR==-1)
printf("\nNo item in queue to display");
else
{
for(i=FRONT;i<=REAR;i++)
printf("\n%d",QUEUE[i]);
}
}
void main()
{
clrscr();
push(10);
push(20);
push(30);
push(40);
push(50);
display();
pop();
printf("\nDeleting Data from stack and displaying the stack ");
display();
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:10

Object: Implementing circular queue using array.

#define MAXSIZE 5
int FRONT=-1;
int REAR=-1;
int CQUEUE[MAXSIZE];
void push(int item)
{
if(FRONT==0&&(REAR==MAXSIZE-1))
printf("\nOverflow occurs");
if(FRONT==-1&&REAR==-1)
{
FRONT=FRONT+1;
REAR=REAR+1;
CQUEUE[REAR]=item;
}
else
{
REAR=(REAR+1);
CQUEUE[REAR]=item;
}
}
void pop()
{
if(FRONT==-1)
printf("\nUnderflow occurs");
if(FRONT==REAR)
{
FRONT=-1;
REAR=-1;
}
else if(FRONT==MAXSIZE-1)
FRONT=0;
else
FRONT=(FRONT+1);
Data Structure Using C
}
void display()
{
int i;
if(FRONT==-1)
printf("\nThere is no item to display");
else
{
for(i=FRONT;i<=REAR;i++)
printf("\n%d",CQUEUE[i]);
}
}
void main()
{
clrscr();
push(10);
push(20);
push(30);
push(40);
push(50);
printf("\nDispalying Circular List");
display();
printf("\nCircular List is full and we want to push another data");
push(100);
printf("\nDeleting data from circular list");
pop();
printf("\nDispalying Circular List after pop");
display();
printf("\nDeleting two data items from circular list");
pop();
pop();
printf("\nDispalying Circular List after pop");
display();
getch();
}
Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:11

Object: Implementing stack using dynamic memory


allocation.

#include<alloc.h>
struct stack
{
int data;
struct stack *link;
}*top=NULL;
void push(int item)
{
struct stack *temp;
temp=(struct stack *)malloc(sizeof(struct stack));
temp->data=item;
temp->link=top;
top=temp;
}
void display()
{
struct stack *temp;
temp=top;
if(top==NULL)
printf("\nThere is no item to display");
else
{
printf("\nDisplaying item of Stack");
while(temp!=NULL)
{
printf("\n%d",temp->data);
temp=temp->link;
}
}
}
void pop()
{
Data Structure Using C
struct stack *temp,*temp1;
if(top==NULL)
printf("\nStack is in Underflow Condition");
else
{
printf("\nPop operation is called");
printf("\n%d is deleted",top->data);
temp=top;
temp1=top->link;
top=temp1;
free(temp);
}
}

void main()
{
clrscr();
push(10);
push(20);
push(30);
push(40);
push(50);
push(60);
push(70);
push(80);
push(90);
push(100);
display();
pop();
display();
pop();
display();
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:12

Object: Implementing queue using dynamic memory


allocation.

#include<alloc.h>
struct queue
{
int data;
struct queue *link;
};
void push(struct queue **f,struct queue **r ,int item)
{
struct queue *temp;
temp=(struct queue*)malloc(sizeof(struct queue));
temp->data=item;
temp->link=NULL;
if(*f==NULL)
*f=temp;
else
(*r)->link=temp;
*r=temp;
}
void pop(struct queue **f,struct queue **r)
{
struct queue *temp;
int item;
if(*f==NULL)
printf("\nQueue is in Underflow Condition");
else
{
temp=*f;
item=temp->data;
*f=temp->link;
printf("\n%d is deleted",item);
free(temp);
if(*f==NULL)
Data Structure Using C
*r=NULL;
}
}
void display(struct queue *q)
{
while(q!=NULL)
{
printf("\n%d",q->data);
q=q->link;
}
}
void main()
{
struct queue *front,*rear;
front=NULL;
rear=NULL;
clrscr();
push(&front,&rear,10);
push(&front,&rear,20);
push(&front,&rear,30);
push(&front,&rear,40);
push(&front,&rear,50);
printf("\nDisplaying items of queue");
display(front);
printf("\nPop operation is called");
pop(&front,&rear);
printf("\nDisplaying items of queue");
display(front);
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:13

Object: Implementing linked list.

#include<alloc.h>
struct node
{
int data;
struct node *link;
};
struct node *start=NULL;
void insert(int item)
{
struct node *temp1,*temp2;
if(start==NULL)
{
temp1=(struct node*)malloc(sizeof(struct node));
temp1->data=item;
temp1->link=NULL;
start=temp1;
}
else
{
temp1=start;
while(temp1->link!=NULL)
temp1=temp1->link;
temp2=(struct node*)malloc(sizeof(struct node));
temp2->data=item;
temp2->link=NULL;
temp1->link=temp2;
}
}
void display()
{
struct node *temp;
temp=start;
while(temp!=NULL)
Data Structure Using C
{
printf("\n%d",temp->data);
temp=temp->link;
}
}
int count()
{
int c=0;
struct node *temp;
temp=start;
while(temp!=NULL)
{
c=c+1;
temp=temp->link;
}
return c;
}

void main()
{
clrscr();
insert(10);
insert(20);
insert(30);
insert(40);
insert(50);
insert(60);
insert(70);
insert(80);
insert(90);
printf("\nNumber of node created =%d",count());
display();
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:14

Object: Adding item at desired location in the linked list.

#include<alloc.h>
struct node
{
int data;
struct node *link;
};
struct node *start=NULL;
void insert(int item)
{
struct node *temp1,*temp2;
if(start==NULL)
{
temp1=(struct node*)malloc(sizeof(struct node));
temp1->data=item;
temp1->link=NULL;
start=temp1;
}
else
{
temp1=start;
while(temp1->link!=NULL)
temp1=temp1->link;
temp2=(struct node*)malloc(sizeof(struct node));
temp2->data=item;
temp2->link=NULL;
temp1->link=temp2;
}
}
void display()
{
struct node *temp;
temp=start;
while(temp!=NULL)
Data Structure Using C
{
printf("\n%d",temp->data);
temp=temp->link;
}
}
int count()
{
int c=0;
struct node *temp;
temp=start;
while(temp!=NULL)
{
c=c+1;
temp=temp->link;
}
return c;
}

void addatbegin(int item)


{
struct node *temp,*temp1;
temp1=start;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=item;
temp->link=temp1;
start=temp;
}
void addatloc(int loc,int item)
{
struct node *temp1,*temp2;
int i;
temp1=start;
for(i=1;i<loc-1;i++)
temp1=temp1->link;
if(temp1==NULL)
printf("\nThis Location Does not exist");
else
Data Structure Using C
{
temp2=(struct node *)malloc(sizeof(struct node));
temp2->data=item;
temp2->link=temp1->link;
temp1->link=temp2;
}
}
void addatlast(int num)
{
struct node *temp1,*temp2;
temp1=start;
while(temp1->link!=NULL)
temp1=temp1->link;
temp2=(struct node *)malloc(sizeof(struct node));
temp2->data=num;
temp2->link=NULL;
temp1->link=temp2;
}
void main()
{
clrscr();
insert(10);
insert(20);
insert(30);
insert(40);
insert(50);
printf("\nNumber of node created =%d",count());
display();
addatbegin(0);
printf("\nNumber of node created =%d",count());
display();
addatloc(2,500);
printf("\nNumber of node created =%d",count());
display();
addatlast(5000);
printf("\nNumber of node created =%d",count());
display();
Data Structure Using C
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No: 15

Object: Deleting item in the linked list.

#include<alloc.h>
struct node
{
int data;
struct node *link;
};
struct node *start=NULL;
void insert(int item)
{
struct node *temp1,*temp2;
if(start==NULL)
{
temp1=(struct node*)malloc(sizeof(struct node));
temp1->data=item;
temp1->link=NULL;
start=temp1;
}
else
{
temp1=start;
while(temp1->link!=NULL)
temp1=temp1->link;
temp2=(struct node*)malloc(sizeof(struct node));
temp2->data=item;
temp2->link=NULL;
temp1->link=temp2;
}
}
void display()
{
struct node *temp;
temp=start;
while(temp!=NULL)
Data Structure Using C
{
printf("\n%d",temp->data);
temp=temp->link;
}
}
void delnode(struct node **q,int n)
{
struct node *old,*temp;
temp=*q;
while(temp!=NULL)
{
if(temp->data==n)
{
if(temp==*q)
{
*q=temp->link;
free(temp);
}
else
{
old->link=temp->link;
free(temp);
}
}
else
{
old=temp;
temp=temp->link;
}
}
}

void main()
{
clrscr();
insert(10);
insert(20);
Data Structure Using C
insert(30);
insert(40);
insert(50);
printf("\nDispalying list");
display();
printf("\nDeleting 10");
delnode(&start,10);
display();
printf("\nDeleting 30");
delnode(&start,30);
display();
printf("\nDeleting 50");
delnode(&start,50);
display();
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No: 16

Object: Displaying and deleting item in the linked list.

#include<alloc.h>

struct node
{
int data;
struct node *link;
}; struct node *start=NULL;

void insert(int item)


{
struct node *temp1, *temp2;
if(start==NULL)
{
temp1=(struct node*)malloc(sizeof(struct node));
temp1->data=item;
temp1->link=NULL;
start=temp1;
}
else
{
temp1=start;
while(temp1->link!=NULL)
temp1=temp1->link;
temp2=(struct node*)malloc(sizeof(struct node));
temp2->data=item;
temp2->link=NULL;
temp1->link=temp2;
}
}

void display( )
{
struct node *temp;
Data Structure Using C
temp=start;
while(temp!=NULL)
{
printf("\n %d", temp->data);
temp=temp->link;
}
}

void dis_del( )
{
struct node *temp, *temp1;
temp=start;
while(temp!=NULL)
{
printf("\n %d", temp->data);
temp1=temp;
temp=temp->link;
free (temp1);

}
}

void main( )
{
clrscr( );
insert(10);
insert(20);
insert(30);
insert(40);
insert(50);
printf("\n Dispalying list");
display( );
dis_del( );
getch( );
}
Data Structure Using C
Experiment No: 17

Object: Searching an item in the linked list.

#include<alloc.h>
struct node
{
int data;
struct node *link;
};
struct node *start=NULL;
void insert(int item)
{
struct node *temp1,*temp2;
if(start==NULL)
{
temp1=(struct node*)malloc(sizeof(struct node));
temp1->data=item;
temp1->link=NULL;
start=temp1;
}
else
{
temp1=start;
while(temp1->link!=NULL)
temp1=temp1->link;
temp2=(struct node*)malloc(sizeof(struct node));
temp2->data=item;
temp2->link=NULL;
temp1->link=temp2;
}
}
void display()
{
struct node *temp;
temp=start;
Data Structure Using C
while(temp!=NULL)
{
printf("\n%d",temp->data);
temp=temp->link;
}
}
int search(int item)
{
int c=0;
struct node *temp;
temp=start;
while(temp!=NULL)
{
if(temp->data==item)
c=c+1;
temp=temp->link;
}
return c;
}

void main()
{
clrscr();
insert(10);
insert(20);
insert(30);
insert(40);
insert(50);
printf("\nDisplaying linked list ");
display();
printf("\nSearching list for 10");
if(search(10))
printf("\nSearch is successful");
printf("\nSearching list for 100");
if(!search(100))
printf("\nSearch is unsuccessful");
getch();
Data Structure Using C
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No: 18

Object: Sorting the linked list.


#include<alloc.h>
struct node
{
int data;
struct node *link;
};
struct node *start=NULL;
void insert(int item)
{
struct node *temp1,*temp2;
if(start==NULL)
{
temp1=(struct node*)malloc(sizeof(struct node));
temp1->data=item;
temp1->link=NULL;
start=temp1;
}
else
{
temp1=start;
while(temp1->link!=NULL)
temp1=temp1->link;
temp2=(struct node*)malloc(sizeof(struct node));
temp2->data=item;
temp2->link=NULL;
temp1->link=temp2;
}
}
void display()
{
struct node *temp;
temp=start;
while(temp!=NULL)
{
Data Structure Using C
printf("\n%d",temp->data);
temp=temp->link;
}
}

int count()
{
struct node *temp;
int c=0;
temp=start;
while(temp!=NULL)
{
c=c+1;
temp=temp->link;
}
return c;
}

void sort(int num)


{
int i,j,temp;
struct node *temp1,*temp2;
temp1=start;
for(i=1;i<num-1;i++)
{
temp2=temp1->link;
for(j=i+1;j<num;j++)
{
if(temp1->data>temp2->data)
{
temp=temp1->data;
temp1->data=temp2->data;
temp2->data=temp;
}
temp2=temp2->link;
}
temp1=temp1->link;
Data Structure Using C
}
}
void main()
{
clrscr();
insert(100);
insert(20);
insert(300);
insert(40);
insert(500);
printf("\nDisplaying linked list ");
display();
printf("\nSorting the list using selection sort");
sort(count());
display(); Signature
getch(); (Rajeev Ranjan Kumar Tripathi)
}
Data Structure Using C
Experiment No: 19

Object: Implementing doubly linked list.


# include <stdio.h>
# include <malloc.h>
struct Double {
int info;
struct Double *next;
struct Double *previous;
};
int i;
struct Double start, *new1;
void Doubly_insertion_Last (struct Double *);
void Doubly_Create_Last (struct Double *);
void Display (struct Double *);
void Doubly_Create_Last (struct Double *node)
{
int i = 0;
int ch=1;
start.next = NULL;
start.previous = NULL;
node = &start;
while (ch!= 0)
{
node->next = (struct Double *) malloc(sizeof(struct Double ));
node->next->previous = node;
node = node->next;
printf("\n Input the value for: %d: ", i+1);
scanf("%d", &node->info);
node->next = NULL;
i++;
fflush(stdin);
printf("\n Input choice 0 for break: ");
scanf("%d",&ch);
}
}
void Display (struct Double *node)
Data Structure Using C
{
node = start.next;
while (node)
{
printf("\n%d", node->info);
node = node->next;
}
}
void main()
{
struct Double *node = (struct Double *) malloc(sizeof(struct Double));
clrscr();
Doubly_Create_Last (node);
printf("\n Created list is as follows\n");
Display(node);
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No: 20

Object: Implementing circular linked list.

#include<alloc.h>
struct circular
{
int data;
struct circular *link;
};
void addtocircularlist(struct circular **f,struct circular **r,int num)
{
struct circular *q;
q=(struct circular*)malloc(sizeof(struct circular ));
q->data=num;
if(*f==NULL)
*f=q;
else
(*r)->link=q;
*r=q;
(*r)->link=*f;
}
void deletenodefromcircularlist(struct circular **f,struct circular **r)
{
struct circular *q;
if(*f==NULL)
printf("\nCircular list is empty");
else
{
if(*f==*r)
{
free(*f);
*f=NULL;
*r=NULL;
}
else
Data Structure Using C
{
q=*f;
*f=(*f)->link;
(*r)->link=*f;
free(q);
}
}
}
void display(struct circular *f)
{
struct circular *q=f,*p=NULL;
while(q!=p)
{
printf("\n%d",q->data);
q=q->link;
p=f;
}
}
void main()
{
struct circular *front, *rear;
clrscr();
front=NULL;
rear=NULL;
addtocircularlist(&front,&rear,10);
addtocircularlist(&front,&rear,20);
addtocircularlist(&front,&rear,30);
addtocircularlist(&front,&rear,40);
addtocircularlist(&front,&rear,50);
printf("\nDispaling item of circular list");
display(front);
printf("\nDeleting item of circular list");
deletenodefromcircularlist(&front,&rear);
display(front);
getch();
} Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No: 21

Object: Implementing Binary Search Tree for numerals.

#include<alloc.h>
struct bst
{
int data;
struct bst *lchild;
struct bst *rchild;
}*root=NULL;
void insert(struct bst **bstroot,int item)
{
if(*bstroot==NULL)
{
*bstroot=(struct bst*)malloc(sizeof(struct bst));
(*bstroot)->lchild=NULL;
(*bstroot)->rchild=NULL;
(*bstroot)->data=item;
}
else
{
if(item<(*bstroot)->data)
insert(&((*bstroot)->lchild),item);
if(item>(*bstroot)->data)
insert(&((*bstroot)->rchild),item);
}
}
void inorder(struct bst *bstroot)
{
if(bstroot!=NULL)
{
inorder(bstroot->lchild);
printf("\n%d",bstroot->data);
inorder(bstroot->rchild);
}
}
Data Structure Using C
void preorder(struct bst *bstroot)
{
if(bstroot!=NULL)
{
printf("\n%d",bstroot->data);
preorder(bstroot->lchild);
preorder(bstroot->rchild);
}
}
void postorder(struct bst *bstroot)
{
if(bstroot!=NULL)
{
postorder(bstroot->lchild);
postorder(bstroot->rchild);
printf("\n%d",bstroot->data);
}
}
void main()
{
clrscr();
insert(&root,100);
insert(&root,110);
insert(&root,90);
insert(&root,10);
insert(&root,101);
insert(&root,120);
insert(&root,103);
insert(&root,105);
insert(&root,105);
clrscr();
printf("\nTraversing bst into inorder");
inorder(root);
printf("\nTraversing bst into preorder");
preorder(root);
printf("\nTraversing bst into postorder");
postorder(root);
Data Structure Using C
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No: 22

Object: Implementing Binary Search Tree for alphabets.

#include<alloc.h>
struct bst
{
char data;
struct bst *lchild;
struct bst *rchild;
}*root=NULL;
void insert(struct bst **bstroot,char item)
{
if(*bstroot==NULL)
{
*bstroot=(struct bst*)malloc(sizeof(struct bst));
(*bstroot)->lchild=NULL;
(*bstroot)->rchild=NULL;
(*bstroot)->data=item;
}
else
{
if(item<(*bstroot)->data)
insert(&((*bstroot)->lchild),item);
if(item>(*bstroot)->data)
insert(&((*bstroot)->rchild),item);
}
}
void inorder(struct bst *bstroot)
{
if(bstroot!=NULL)
{
inorder(bstroot->lchild);
printf("\n%c",bstroot->data);
inorder(bstroot->rchild);
}
}
Data Structure Using C
void preorder(struct bst *bstroot)
{
if(bstroot!=NULL)
{
printf("\n%c",bstroot->data);
preorder(bstroot->lchild);
preorder(bstroot->rchild);
}
}
void postorder(struct bst *bstroot)
{
if(bstroot!=NULL)
{
postorder(bstroot->lchild);
postorder(bstroot->rchild);
printf("\n%c",bstroot->data);
}
}
void main()
{
clrscr();
insert(&root,'A');
insert(&root,'B');
insert(&root,'a');
insert(&root,'b');
insert(&root,'C');
insert(&root,'c');
insert(&root,'D');
insert(&root,'d');
insert(&root,'E');
clrscr();
printf("\nTraversing bst into inorder");
inorder(root);
printf("\nTraversing bst into preorder");
preorder(root);
printf("\nTraversing bst into postorder");
postorder(root);
Data Structure Using C
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No: 23

Object: Implementing Binary Search Tree for strings where


case does not matter.

#include<alloc.h>
#include<string.h>
struct bst
{
struct bst *lchild;
char name[20];
struct bst *rchild;
}*root=NULL;
void insert(struct bst **st,char *n)
{
if(*st==NULL)
{
(*st)=(struct bst *)malloc(sizeof(struct bst));
(*st)->lchild=NULL;
strcpy((*st)->name,n);
(*st)->rchild=NULL;
}
else
{
if(strcmpi((*st)->name,n)<0)
insert(&((*st)->rchild),n);
if(strcmpi((*st)->name,n)>0)
insert(&((*st)->lchild),n);
}
}
void inorder(struct bst *st)
{
if(st!=NULL)
{
inorder(st->lchild);
puts(st->name);
inorder(st->rchild);
Data Structure Using C
}
}
void preorder(struct bst *st)
{
if(st!=NULL)
{
puts(st->name);
preorder(st->lchild);
preorder(st->rchild);
}
}
void postorder(struct bst *st)
{
if(st!=NULL)
{
postorder(st->lchild);
postorder(st->rchild);
puts(st->name);
}
}
void main()
{
clrscr();
insert(&root,"hEENA");
insert(&root,"DaNnY");
insert(&root,"BrijesH");
insert(&root,"aJiT");
insert(&root,"FIzzA");
insert(&root,"LoveLY");
insert(&root,"jaSSI");
insert(&root,"ImraN");
insert(&root,"NaViN");
insert(&root,"pRiTy");
printf("\nTraversing tree in inorder\n");
inorder(root);
printf("\nTraversing tree in preorder\n");
preorder(root);
Data Structure Using C
printf("\nTraversing tree in postorder\n");
postorder(root);
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:24

Object: Implementing Binary Search Tree for strings where


case matters.

#include<alloc.h>
struct bst
{
struct bst *lchild;
char name[20];
struct bst *rchild;
}*root=NULL;
void insert(struct bst **st,char *n)
{
if(*st==NULL)
{
(*st)=(struct bst *)malloc(sizeof(struct bst));
(*st)->lchild=NULL;
strcpy((*st)->name,n);
(*st)->rchild=NULL;
}
else
{
if(strcmp((*st)->name,n)<0)
insert(&((*st)->rchild),n);
if(strcmp((*st)->name,n)>0)
insert(&((*st)->lchild),n);
}
}
void inorder(struct bst *st)
{
if(st!=NULL)
{
inorder(st->lchild);
puts(st->name);
inorder(st->rchild);
}
Data Structure Using C
}
void preorder(struct bst *st)
{
if(st!=NULL)
{
puts(st->name);
preorder(st->lchild);
preorder(st->rchild);
}
}
void postorder(struct bst *st)
{
if(st!=NULL)
{
postorder(st->lchild);
postorder(st->rchild);
puts(st->name);
}
}
void main()
{
clrscr();
insert(&root,"HEENA");
insert(&root,"DANNY");
insert(&root,"BRIJESH");
insert(&root,"AJIT");
insert(&root,"FIZZA");
insert(&root,"LOVELY");
insert(&root,"JASSI");
insert(&root,"IMRAN");
insert(&root,"NAVIN");
insert(&root,"PRITY");
printf("\nTraversing tree in inorder\n");
inorder(root);
printf("\nTraversing tree in preorder\n");
preorder(root);
printf("\nTraversing tree in postorder\n");
Data Structure Using C
postorder(root);
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No: 25

Object: Implementing Binary Search Tree and traverse in


Inorder, without using recursion.

#include<alloc.h>
#define MAX 10
int req, i=1;

struct bst
{
int data;
struct bst *lchild;
struct bst *rchild;
}*root=NULL;

void insert(struct bst **bstroot, int item)


{
if(*bstroot==NULL)
{
*bstroot=(struct bst*)malloc(sizeof(struct bst));
(*bstroot)->lchild=NULL;
(*bstroot)->rchild=NULL;
(*bstroot)->data=item;
}
else
{
if(item<(*bstroot)->data)
insert(&((*bstroot)->lchild), item);
if(item>(*bstroot)->data)
insert(&((*bstroot)->rchild), item);
}
}

void inorder(struct bst *currentnode)


{
int top = 0;
Data Structure Using C
struct bst *nodestack[MAX];
while(1)
{
while(currentnode != NULL)
{
top++;
if(top>MAX)
{
printf(“\n stack is full”);

exit();
}
else
{
nodestack[top] = currentnode;
currentnode = currentnode -> leftchild;
}
}
if(top != 0)
{
currentnode = nodestack[top];
top--;
print(“%d”, currentnode -> data);
currentnode = currentnode->rchild;
}
else
break;
}
}

void main( )
{
clrscr( );
insert(&root, 100);
insert(&root, 110);
insert(&root, 90);
insert(&root, 10);
Data Structure Using C
insert(&root, 101);
insert(&root, 120);
insert(&root, 103);
insert(&root, 105);
insert(&root, 105);
clrscr( );
printf("\n Traversing bst into inorder");
inorder(root);
getch( );
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No: 26

Object: Implementing Binary Search Tree and traverse in


Preorder, without using recursion.

#include<alloc.h>
#define MAXSIZE 10
int i, top=0,stack[MAXSIZE];

struct bst
{
int data;
struct bst *lchild;
struct bst *rchild;
}*root=NULL;

void insert(struct bst **bstroot, int item)


{
if(*bstroot==NULL)
{
*bstroot=(struct bst*)malloc(sizeof(struct bst));
(*bstroot)->lchild=NULL;
(*bstroot)->rchild=NULL;
(*bstroot)->data=item;
}
else
{
if(item<(*bstroot)->data)
insert(&((*bstroot)->lchild), item);
if(item>(*bstroot)->data)
insert(&((*bstroot)->rchild), item);
}
}

void preorder(struct bst *node)


{
int top = 1;
Data Structure Using C
stack[0] = NULL;
node = root
while(node != NULL)
{
print(“%d”, node -> data);
if (node->rchild!=NULL)
{
top++;
stack[top] = node;

node = node -> lchild;


}
if(node->lchild!=NULL)
node =node->rchild;
else
{
node = stack[top];
top--;
}

}
}

void main( )
{
clrscr( );
insert(&root, 100);
insert(&root, 110);
insert(&root, 90);
insert(&root, 10);
insert(&root, 101);
insert(&root, 120);
insert(&root, 103);
insert(&root, 105);
insert(&root, 105);
clrscr( );
printf("\n Traversing bst into preorder");
Data Structure Using C
prerder(root);
getch( );
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No: 27

Object: Implementing Binary Search Tree and traverse in


Postorder, without using recursion.

#include<alloc.h>
#define MAXSIZE 10
int i, top=0,stack[MAXSIZE];

struct bst
{
int data;
struct bst *lchild;
struct bst *rchild;
}*root=NULL;

void insert(struct bst **bstroot, int item)


{
if(*bstroot==NULL)
{
*bstroot=(struct bst*)malloc(sizeof(struct bst));
(*bstroot)->lchild=NULL;
(*bstroot)->rchild=NULL;
(*bstroot)->data=item;
}
else
{
if(item<(*bstroot)->data)
insert(&((*bstroot)->lchild), item);
if(item>(*bstroot)->data)
insert(&((*bstroot)->rchild), item);
}
}

void postorder(struct bst *node)


{
top = 1;
Data Structure Using C
stack[0] = NULL;
node = root
level 1:
while(node != NULL)
{
top++;
stack[top]=node;
if (node->rchild!=NULL)
{

top++;
stack[top] = -node->rchild;
node=node->lchild;
}
}
node =stack[top];
top--;
while(node>0)
{
print(“%d”, node -> data);
node = stack[top]
top--;
}
if(node<0)
{
node = -node
goto level 1;
}
}

void main( )
{
clrscr( );
insert(&root, 100);
insert(&root, 110);
insert(&root, 90);
insert(&root, 10);
Data Structure Using C
insert(&root, 101);
insert(&root, 120);
insert(&root, 103);
insert(&root, 105);
insert(&root, 105);
clrscr( );
printf("\n Traversing bst into postorder");
postorder(root);
getch( );
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:28

Object: Deleting a node from Binary Search Tree .

#include<stdio.h>
#include<conio.h>
#include<malloc.h>

struct nodetype
{
int info;
struct nodetype *left;
struct nodetype *right;
struct nodetype *father;
};
typedef struct nodetype* NODEPTR;
NODEPTR maketree(int);
void setleft(NODEPTR,int);
void setright(NODEPTR,int);
void intrav(NODEPTR,int);
NODEPTR tree_min(NODEPTR);
NODEPTR tree_max(NODEPTR);
NODEPTR tree_succ(NODEPTR);
NODEPTR tree_pred(NODEPTR);
void insert(NODEPTR,int);
NODEPTR search(NODEPTR,int);
void delete(NODEPTR,NODEPTR);

main()
{
NODEPTR p,q,btree;
int number, n=1;
char another=’y’;
printf(“ends nos from binary tree”);
scanf(“%d”,&number);
btree=maketree(number);
while(n)
Data Structure Using C
{
printf(“Enter the number”);
scanf(“%d”,&number);
p=q=bree;
while(q!=NULL)
{
p=q;
if(number>=p->info)
q=p->right;
if(p)
delete(btree,p);
break;
}
}
returen 0;
}

NODEPTR maketree(int x)
{
NODEPTR p;
p=malloc(sizeof(struct nodetype));
p->info=x;
p->left=NULL
p->right=NULL
p->father=NULL
return p;
}

void setleft(NODEPTR p, int x)


{
if(p==NULL)
printf(“\nvoid insertion”);
else if(p->left!=NULL)
printf(‘\ninvalid insertion”);
else
{
p->left=maketree(x);
Data Structure Using C
(p->left)->father=p;
}
}

void setright(NODEPTR p, int x)


{
if(p==NULL)
printf(“\nvoid insertion”);
else if(p->right!=NULL)
printf(‘\ninvalid insertion”);
else
{
p->right=maketree(x);
(p->right)->father=p;
}
}

void intrav(NODEPTR p)
{
if(p==NULL)
{
intrav(p->left);
printf(“\n%d”,p->info);
intrav(p->right);
}
}

NODEPTR tree_min(NODEPTR p)
{
while(p->left!=NULL)
p=p->left;
return p;
}

NODEPTR tree_max(NODEPTR p)
{
while(p->right!=NULL)
Data Structure Using C
p=p->right;
return p;
}

NODEPTR tree_succ(NODEPTR x)
{
NODEPTR y;
if(x->right!=NULL)
return tree_min(x->right);
y=x->father;
while(x==y->right && y!=NULL)
{
x=y;
y=y->father;
}
return y;
}

NODEPTR tree_pred(NODEPTR x)
{
NODEPTR y;
if(x->left!=NULL)
return tree_min(x->left);
y=x->father;
while(x==y->left && y!=NULL)
{
x=y;
y=y->father;
}
return y;
}

void insert(NODEPTR btree, int number);


{
NODEPTR p,q;
p=b=btree;
while(q!=NULL)
Data Structure Using C
{
p=q;
if(number>=p->info)
q=p->right;
else
q=p->left;
}
if(number>=p->info)
setright(p,number)
else
setleft(p,number);
}

NODEPTR search(NODEPTR btree, int number)


{
NODEPTR p,q;
p=q=btree;
while(q!=NULL && number!=p->info)
{
p=q;
if(number>p->info)
q=p->right;
else
q=p->left;
}
if(number==p->info)
{
printf(“\n%d is present in the tree”, number);
return p;
}
else if(q==NULL)
{
printf(“\n%d is not present in the tree”,number);
return p;
}
}
Data Structure Using C
void delete(NODEPTR btree, NODEPTR z)
{
NODEPTR x,y;
if(z->left==NULL||z->right==NULL)
y=z;
else
y=tree_succ(z);
if(y->left!=NULL)
x=y->left;
else
x=y->right;
if(x!=NULL)
x->father=y->father;
if(y->father==NULL)
btree=y;
else if(y==(y->father)->left)
(y->father)->left=x;
else(y->father)->right=x;
if(y!=z)
z->info=y->info;
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:29

Object: Performing linear search.

# include<stdio.h>
int search;
int flag;
int input( int *, int , int);
int list[200];
void linear_search(int * , int , int );
void display(int *, int);
void linear_search(int l[], int n, int element)
{
int k;
flag = 1;
for(k = 0; k< n; k++)
{
if(l[k] == element)
{
printf("\n Search is successful \n");
printf("\n Element: %i Found at Location: %i", element, k+1);
flag=0;
}
}
if(flag)
printf("\n Search is unsuccessful");
}
void display(int list[], int n)
{
int i;
for(i=0;i<n;i++)
printf(" %d", list[i]);
}
input(int list[], int number, int key)
{
int i;
key=30;
Data Structure Using C
printf("Input the number of elements in the list:");
number=20;
for(i=0;i<20;i++)
{
list[i] = rand() %100;
}
printf("\n Element to be searched: %d", key);
search = key;
return number;
}
void main()
{
int number, key, list[200];
number = input( list, number, key);
key = search ;
printf("\n Entered list as follows:\n");
display(list,number);
linear_search(list, number, key);
printf("\n In the following list\n");
display(list,number);
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:30

Object: Performing binary search.

#include <stdio.h>
int binary_search(int array[], int value, int size)
{
int found = 0;
int high = size, low = 0, mid;
mid = (high + low) / 2;
printf("\n\nLooking for %d\n", value);
while ((! found) && (high >= low))
{
printf("Low %d Mid %d High %d\n", low, mid, high);
if(value == array[mid])
found=1;
else if (value < array[mid])
high = mid - 1;
else
low = mid + 1;
mid = (high + low) / 2;
}
return((found) ? mid: -1);
}
void main(void)
{
int array[100], i;
clrscr();
for (i = 0; i < 100; i++)
array[i] = i+20;
printf("Result of search %d\n", binary_search(array, 33, 100));
printf("Result of search %d\n", binary_search(array, 75, 100));
printf("Result of search %d\n", binary_search(array, 1, 100));
printf("Result of search %d\n", binary_search(array, 1001, 100));
getch();
} Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:31

Object: Performing selection sort on array (for numerals).

#include <stdio.h>
#include <stdlib.h>
void selection_sort(int array[], int size)
{
int temp, current, j;
for (current = 0; current < size; current++)
for (j = current + 1; j < size; j++)
if (array[current] > array[j])
{
temp = array[current];
array[current] = array[j];
array[j] = temp;
}
}
void main(void)
{
int values[30], i;
clrscr();
printf("\n Unsorted list is as follows \n");
for (i = 0; i < 30; i++)
{
values[i]= rand()%100;
printf("\t%d", values[i]);
}
selection_sort(values, 30);
printf("\n Sorted list is as follows \n");
for (i = 0; i < 30; i++)
printf("\t%d", values[i]);
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:32

Object: Performing Bubble sort on array (for numerals).

#include <stdio.h>
#include <stdlib.h>
void bubble_sort(int array[], int size)
{
int temp, i, j;
for (i = 0; i < size; i++)
for (j = 0; j < size; j++)
if (array[i] < array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
void main()
{
int values[30], i;
clrscr();
printf("\n Unsorted list is as follows\n");
for (i = 0; i < 10; i++)
{
values[i] = rand() % 100;
printf("\t%d", values[i]);
}
bubble_sort(values, 10);
printf("\n Sorted list is as follows\n");
for (i = 0; i < 10; i++)
printf("\t%d", values[i]);
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:33

Object: Performing Bubble sort on array (for strings).

# include <stdlib.h>
# include <string.h>
# include <malloc.h>
# include <stdio.h>
void bubble_sort(char *array[], int );
void display(char *list[], int);
void bubble_sort(char *array[], int size)
{
char *temp;
int i, j;
for (i = 0; i < size; i++)
for (j = 0; j < size; j++)
if (strcmp(array[i], array[j]) < 0)
{
temp =(char *) malloc(sizeof(array[i]));
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
void display(char *list[], int n)
{
int i;
for(i = 0 ; i < n; i++)
{
printf("\n %s", list[i]);
}
}

void main(void)
{
char *list[100] = {"MONDAY", "TUESDAY", "WEDNESDAY",
"THURSDAY","FRIDAY", "SATARDAY","SUNDAY"};
Data Structure Using C
clrscr();
printf("\n Input list is as follows:");
display(list, 7);
bubble_sort(list, 7);
printf("\n Sorted list is as follows:\n");
display(list, 7);
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:34

Object: Performing insertion sort on array.

# include<stdio.h>
# include<stdlib.h>
void insertion_sort(int *, int);
void display(int*, int);
void insertion_sort(int l[], int n)
{
int pointer, temp;
int i, k;
l[0] = -0;
for(i = 1 ; i <= n; i++)
{
pointer = i -1;
temp = l[i];
while(temp < l[pointer])
{
l[pointer+1] = l[pointer];
pointer --;
}
l[pointer+1] = temp;
printf("Step %d=", i);
for(k = 0; k <= n; k++)
printf(" %d", l[k]);
printf("\n");
}
}
void display(int l[], int n)
{
int i;
printf("\n Sorted list is as follows\n");
for(i = 1; i <= n; i++)
printf(" %d", l[i]);
}
void main()
Data Structure Using C
{
int number = 10;
int list[100];
int i;
clrscr();
printf("\n Number of elements in the list: %i", number);
printf("\n Unsorted list is as follows \n");
for(i = 1; i <= number; i++)
{
list[i] = rand() %100;
printf(" %d", list[i]);
}
printf("\n Step wise result is as follows \n\n");
insertion_sort(list,number);
display(list, number);
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:35

Object: Performing quick sort on array .

#include <stdio.h>
#include <stdlib.h>
void quick_sort(int array[], int first, int last)
{
int temp, low, high, list_separator;
low = first;
high = last;
list_separator = array[(first + last) / 2];
do
{
while (array[low] < list_separator)
low++;
while (array[high] > list_separator)
high--;
if (low <= high)
{
temp = array[low];
array[low++] = array[high];
array[high--] = temp;
}
}while (low <= high);
if (first < high)
quick_sort(array, first, high);
if (low < last)
quick_sort(array, low, last);
}
void main()
{
int values[100], i;
clrscr();
printf("\n Unsorted list is as follows \n");
for (i = 0; i < 20; i++)
{
Data Structure Using C
values[i] = rand() % 100;
printf("\t%d", values[i]);
}
quick_sort(values, 0, 19);
printf("\n Sorted list as follows\n");
for (i = 0; i < 20; i++)
printf("\t%d ", values[i]);
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:36

Object: Performing heap sort on array .

# include<stdio.h>
void heap_sort(int *, int );
void create_heap(int *, int);
void display(int *, int);
void create_heap(int list[], int n )
{
int k, j, i, temp;
for(k = 2 ; k <= n; ++k)
{
i=k;
temp = list[k];
j=i/2;
while((i > 1) && (temp > list[j]))
{
list[i] = list[j];
i=j;
j=i/2;
if ( j < 1 )
j=1;
}
list[i] = temp ;
}
}
void heap_sort(int list[], int n)
{
int k, temp, value, j, i, p;
int step = 1;
for(k = n ; k >= 2; --k)
{
temp = list[1] ;
list[1] = list[k];
list[k] = temp ;
i=1;
Data Structure Using C
value = list[1];
j=2;
if((j+1) < k)
if(list[j+1] > list[j])
j ++;
while((j <= ( k-1)) && (list[j] > value))
{
list[i] = list[j];
i=j;
j = 2*i ;
if((j+1) < k)
if(list[j+1] > list[j])
j++;
else
if( j > n)
j=n;
list[i] = value;
}
printf("\n Step %d =", step);
step++;
for(p = 1; p <= n; p++)
printf("\t%d", list[p]);
}
}
void display(int list[], int n)
{
int i;
for(i = 1 ; i <= n; ++ i)
{
printf("\t%d", list[i]);
}
}
void main()
{
int list[100];
int i, size = 13 ;
clrscr();
Data Structure Using C
printf("\n Size of the list: %d", size);
for(i = 1 ; i <= size ; ++i)
{
list[i] = rand() % 100;
}
printf("\n Entered list is as follows:\n");
display(list, size);
create_heap(list, size);
printf("\n Heap\n");
display(list, size);
printf("\n\n");
heap_sort(list,size);
printf("\n\n Sorted list is as follows :\n\n");
display(list,size);
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:37

Object: Performing 2-Way Merge sort on an array .

# include<stdio.h>
# include<stdlib.h>
void merge_sort(float *, int , int , int );
void merge_pass(float *, int , int );
void merge_sort(float l[], int top, int size, int bottom)
{
float temp[1000];
int f = top;
int s = size +1 ;
int t = top ;
int upper;
while(( f <= size)&&(s <=bottom))
{
if(l[f] <= l[s])
{
temp[t] = l[f] ;
f ++;
}
else
{
temp[t] = l[s] ;
s ++;
}
t ++;
}
if(f <=size)
{
for(f = f ; f<=size; f++)
{
temp[t] = l[f];
t++;
}
}
Data Structure Using C
else
{
for(s = s ; s<= bottom ; s++)
{
temp[t]=l[s];
t++;
}
}
for(upper = top; upper <= bottom ; upper++)
{
l[upper]=temp[upper];
}
}
void merge_pass( float append[], int m, int n )
{
if( m!= n)
{
int mid = (m+n)/2;
merge_pass( append, m, mid );
merge_pass( append, mid+1, n );
merge_sort( append, m, mid, n );
}
}
void main()
{
float list[1000];
int i, n = 30;
clrscr();
printf("\n Size of the list: %d", n);
for(i = 0 ; i < n ; i++)
{
list[i] = (float) (rand() %100);
}
printf("\n Entered list as follows:\n");
for( i = 0 ; i<n ; i++)
printf(" %d ", (int) list[i]);
i=0;
Data Structure Using C
merge_pass(list, i, n-1);
printf("\n Merge sorted list is as follows:\n");
for( i = 0 ; i<n ; i++)
printf(" %d", (int) list[i]);
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:38

Object: Performing 2-Way Merge sort on two arrays .

# include<stdio.h>
int merge_sort( int , int *, int , int *, int * );
int bubble_sort(int , int *);
bubble_sort(int n, int l[])
{
int flag = 1 ;
int i, j, k, temp;
for(j = 0 ; j< n - 1; j++)
{
for(k = 0 ; k< n - j - 1 ; k++)
{
if(l[k] > l[k+1])
{
temp = l[k];
l[k] = l[k+1];
l[k+1] = temp ;
flag = 0;
}
}
if(flag)
break ;
else
flag = 1;
}
printf("\n Entered list as follows in");
printf("\n Ascending order: ");
for(i = 0 ; i < n ; i++)
printf(" %d", l[i]);
return 0 ;
}
merge_sort( int n, int list_a[], int m ,
int list_b[], int result_list[] )
{
Data Structure Using C
int i = 0;
int j = 0;
int k = 0;
int ch, l;
while((i < n) &&(j<m))
{
if(list_a[i] < list_b[j])
{
result_list[k] = list_a[i];
i++;
k++;
}
else
if(list_a[i] > list_b[j])
{
result_list[k] = list_b[j];
j++;
k++;
}
else
{
result_list[k] = list_a[i];
i++;
j++;
k++;
}
printf("\n");
for(ch = 0; ch < k; ch++)
printf(" %d", result_list[ch]);
}
if(i <n )
{
for(l = i ; l <n ; l++)
{
result_list[k] = list_a[i];
i++;
k++;
Data Structure Using C
printf("\n");
for(ch = 0; ch < k; ch++)
printf(" %d", result_list[ch]);
}
}
else
if(j < m)
{
for(l = j; l<m; l++)
{
result_list[k] = list_b[j];
j++;
k++;
printf("\n");
for(ch = 0; ch < k; ch++)
printf(" %d", result_list[ch]);
}
}
return (k);
}
void main()
{
int list_a[100],list_b[100];
int result[200];
int n, m, k, i;
clrscr();
printf("\n Input the number of elements of list_a: ");
scanf("%d", &n);
for(i = 0; i<n ; i++)
{
printf("\n Input the element: %d: ", i+1);
scanf("%d", &list_a[i]);
}
bubble_sort(n, list_a);
printf("\n Input the number of elements of list_b:");
scanf("%d", &m);
for(i = 0 ; i< m ; i++)
Data Structure Using C
{
printf("\n Input the element: %d: ", i+1);
scanf("%d", &list_b[i]);
}
bubble_sort(m, list_b);
k = merge_sort( n, list_a, m , list_b, result);
printf("\n Duplicates are : %d", m+n-k);
printf("\n");
printf("\n Sorted list is as follows:\n");
for(i = 0 ; i< k ; i++)
{
printf(" %d", result[i]);
}
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:39

Object: Traversing the graph using DFS Technique.

# include<stdio.h>
# define size 20
# define T 1
# define F 0
struct Edge
{
int terminal;
struct Edge *next;
};
struct Vertex
{
int visit;
int vertex_no;
char info;
int path_length;
struct Edge *Edge_Ptr;
};
void Table(int , int matrix [size][size], struct Vertex vert[size]);
struct Edge *Insert_Vertex (int , struct Edge *);
void DFS ( int , int *dist, struct Vertex vert [size]);
void Input(int, int a [size][size]);
void Output(int, int a [size][size]);
struct Edge * Insert_Vertex (int vertex_no, struct Edge *first)
{
struct Edge *new1, *current;
new1 = (struct Edge *) malloc(sizeof(struct Edge));
new1->terminal = vertex_no;
new1->next = NULL;
if (!first)
return (new1);
for (current = first; current->next; current = current->next);
current->next = new1;
return (first);
Data Structure Using C
}
void Table(int vertex_num, int matrix [size][size],
struct Vertex vert[size])
{
int i, j;
for (i = 0; i < vertex_num; i++)
{
vert [i].visit = F;
vert [i].vertex_no = i+1;
vert [i].info = 'A'+ i;
vert [i].path_length = 0;
vert [i].Edge_Ptr = NULL;
}
for (i =0; i < vertex_num ; i++)
for (j =0; j < vertex_num ; j++)
if (matrix [i][j] > 0)
vert [i].Edge_Ptr = Insert_Vertex (j, vert [i].Edge_Ptr);
}
void DFS ( int index, int *dist,
struct Vertex vert [size])
{
struct Edge *Link;
vert [index].visit = T;
vert [index].path_length = *dist;
*dist += 1;
for ( Link = vert [index].Edge_Ptr; Link; Link = Link->next)
if (vert [Link->terminal].visit == F)
DFS (Link->terminal, dist, vert);
}
void Input(int number, int a [size][size])
{
int i, j;
printf("\n Input the adjacency matrix \n");
for (i =0; i < number; i++)
{
for (j=0; j < number; j ++)
{
Data Structure Using C
scanf("%d", &a [i][j]);
}
printf("\n");
}
}
void Output(int number, int a [size][size])
{
int i, j;
printf("\n Adjacency matrix \n");
for (i = 0; i < number; i++)
{
for (j = 0; j < number; j ++)
{
printf(" %d", a [i][j]);
}
printf("\n");
}
}
void main()
{
int i;
int number, index, dist;
int a [size][size];
struct Vertex vert [size];
struct Edge *List;
clrscr();
printf("\n Input the number of vertices in the graph: ");
scanf("%d", &number);
Input(number, a);
Output(number, a);
Table(number, a, vert);
printf("\n Input the starting vertex 0- %d:", number-1);
scanf("%d", &index);
dist = 0;
DFS (index, &dist, vert);
printf("\n Path length of the vertex from %c", vert[index].info);
printf("\n Vertex Length Vertex Connectivity \n ");
Data Structure Using C
for (i = 0; i < number; i++)
{
printf("\n %c %d ", vert[i].info, vert[i].path_length);
for (List= vert[i].Edge_Ptr; List; List = List->next)
{
printf(" ");
putchar(List->terminal+'A');
}
}
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C
Experiment No:40

Object: Traversing the graph using BFS Technique.

# include<stdio.h>
# define size 20
# define T 1
# define F 0
struct Edge
{
int terminal;
struct Edge *next;
};
struct Vertex
{
int visit;
int vertex_no;
char info;
int path_length;
struct Edge *Edge_Ptr;
};
struct Q
{
int info;
struct Q *next;
};
void Table(int , int matrix [size][size], struct Vertex vert[size]);
struct Edge *Insert_Vertex (int , struct Edge *);
void BFS ( int , struct Vertex vert [size]);
void Input(int, int mat [size][size]);
void Output(int number, int mat [size][size]);
struct Q *Insert_Queue(int vertex_no, struct Q *first);
struct Q *Delete_Queue(int *vertex_no, struct Q *first);
struct Edge * Insert_Vertex (int vertex_no, struct Edge*first)
{
struct Edge *new1, *current;
new1 = (struct Edge *) malloc(sizeof(struct Edge));
Data Structure Using C
new1->terminal = vertex_no;
new1->next = NULL;
if (!first)
return (new1);
for (current = first; current->next; current = current->next);
current->next = new1;
return (first);
}
struct Q * Insert_Queue(int vertex_no, struct Q *first)
{
struct Q *new1, *current;
new1 =(struct Q *) malloc(sizeof(struct Q));
new1->info = vertex_no;
new1->next = NULL;
if (!first)
return (new1);
for (current = first; current->next; current = current->next);
current->next = new1;
return (first);
}
struct Q * Delete_Queue(int *vertex_no, struct Q *first)
{
struct Q *previous;
if (!first)
return (NULL);
*vertex_no = first->info;
previous = first;
first = first->next;
free(previous);
return (first);
}
void Table(int vertex_num, int matrix [size][size],
struct Vertex vert[size])
{
int i, j;
for (i = 0; i < vertex_num; i++)
{
Data Structure Using C
vert [i].visit = F;
vert [i].vertex_no = i+1;
vert [i].info = 'A'+ i;
vert [i].path_length = 0;
vert [i].Edge_Ptr = NULL;
}
for (i =0; i < vertex_num ; i++)
for (j =0; j < vertex_num ; j++)
if (matrix [i][j] > 0 )
vert [i].Edge_Ptr = Insert_Vertex (j, vert [i].Edge_Ptr);
}
void BFS ( int index, struct Vertex vert [size])
{
struct Q *queue = NULL;
struct Edge *Link;
vert [index].visit = T;
queue = Insert_Queue(index, queue);
while(queue)
{
queue = Delete_Queue(&index, queue);
for ( Link = vert [index].Edge_Ptr; Link; Link = Link->next)
{
if (vert [Link->terminal].visit == F)
{
vert[Link->terminal].visit = T;
vert[Link->terminal].path_length=vert[index].path_length+1;
queue = Insert_Queue(Link->terminal, queue);
}
}
}
}
void Input(int number, int mat [size][size])
{
int i, j;
printf("\n Input the adjacency matrix \n");
for (i =0; i < number; i++)
{
Data Structure Using C
for (j=0; j < number; j ++)
{
scanf("%d", &mat [i][j]);
}
printf("\n");
}
}
void Output(int number, int mat [size][size])
{
int i, j;
printf("\n Adjacency matrix \n");
for (i =0; i < number; i++)
{
for (j=0; j < number; j ++)
{
printf(" %d", mat [i][j]);
}
printf("\n");
}
}
void main()
{
int i, number, index;
int mat [size][size];
struct Vertex vert [size];
struct Edge *List;
clrscr();
printf("\n Input the number of vertices in the graph: ");
scanf("%d", &number);
Input(number, mat);
Output(number, mat);
Table(number, mat, vert);
printf("\n Input the starting vertex 0- %d :",number-1);
scanf("%d", &index);
BFS (index, vert);
printf("\n Path length of the vertex from %c", vert[index].info);
printf("\n Vertex Length Vertex Connectivity \n ");
Data Structure Using C
for (i = 0; i < number; i++)
{
printf("\n %c %d ",vert[i].info, vert[i].path_length);
for (List= vert[i].Edge_Ptr; List; List = List->next)
{
printf(" ");
putchar(List->terminal+'A');
}
}
getch();
}

Signature
(Rajeev Ranjan Kumar Tripathi)
Data Structure Using C

You might also like