Вы находитесь на странице: 1из 53

2010

-11

DATA
STRUCTURE
LAB
Computer Science And Engineering dept.

Vivek Gupta
Roll No. 0905210064
2|Page

INDEX
S.NO. PROGRAM PAGE NO. SIGNATURE
1. Array Implementation of Stack 3

2. Array Implementation of Queues 5

3. Array implementation of Circular Queue 8

4. Array Implementation of List 11

5. Implementation of Stack Using Dynamic 15


Memory Allocation
6. Implementation Of Queue Using Dynamic 18
Memory Allocation
7. Implementation of circular Queue usng 21
Dynamic Memory Allocation
8. Implementation of List Using Dynamic Memory 24
Allocation
9. Insertion,Deletion And traversal In Binary 30
search Tree

10. Implementation of Binary Search 37

11. Heapsort 38

12. Quicksort 41

13. Insertion Sort 44

14. Traversing Of Graph Through BFS and DFS 45

15. Implementation Of Shortest Path Algorithm 50

VIVEK GUPTA ROLL NO. 0905210064


3|Page

ARRAY IMPLEMENTATION OF STACK


/* Program of stack using array*/
#include<stdio.h>
#define MAX 5

int top = -1;


int stack_arr[MAX];

main()
{
int choice;
while(1)
{
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/

push()
{

VIVEK GUPTA ROLL NO. 0905210064


4|Page

int pushed_item;
if(top == (MAX-1))
printf("Stack Overflow\n");
else
{
printf("Enter the item to be pushed in stack : ");
scanf("%d",&pushed_item);
top=top+1;
stack_arr[top] = pushed_item;
}
}/*End of push()*/

pop()
{
if(top == -1)
printf("Stack Underflow\n");
else
{
printf("Popped element is : %d\n",stack_arr[top]);
top=top-1;
}
}/*End of pop()*/

display()
{
int i;
if(top == -1)
printf("Stack is empty\n");
else
{
printf("Stack elements :\n");
for(i = top; i >=0; i--)
printf("%d\n", stack_arr[i] );
}
}/*End of display()*/

VIVEK GUPTA ROLL NO. 0905210064


5|Page

ARRAY IMPLEMENTAION OF QUEUE


/*Program of queue using array*/
# include<stdio.h>
# define MAX 5

int queue_arr[MAX];
int rear = -1;
int front = -1;

main()
{
int choice;
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1 :
insert();
break;
case 2 :
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/

VIVEK GUPTA ROLL NO. 0905210064


6|Page

insert()
{
int added_item;
if (rear==MAX-1)
printf("Queue Overflow\n");
else
{
if (front==-1) /*If queue is initially empty */
front=0;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
rear=rear+1;
queue_arr[rear] = added_item ;
}
}/*End of insert()*/

del()
{
if (front == -1 || front > rear)
{
printf("Queue Underflow\n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_arr[front]);
front=front+1;
}
}/*End of del() */

display()
{
int i;
if (front == -1)
printf("Queue is empty\n");
else
{
printf("Queue is :\n");
for(i=front;i<= rear;i++)
printf("%d ",queue_arr[i]);
printf("\n");

VIVEK GUPTA ROLL NO. 0905210064


7|Page

}
}/*End of display() */

VIVEK GUPTA ROLL NO. 0905210064


8|Page

ARRAY IMPLENTATION OF CIRCULAR QUEUES


/* Program of circular queue using array*/
# include<stdio.h>
# define MAX 5

int cqueue_arr[MAX];
int front = -1;
int rear = -1;

main()
{
int choice;
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1 :
insert();
break;
case 2 :
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while */
}/*End of main()*/

VIVEK GUPTA ROLL NO. 0905210064


9|Page

insert()
{
int added_item;
if((front == 0 && rear == MAX-1) || (front == rear+1))
{
printf("Queue Overflow \n");
return;
}
if (front == -1) /*If queue is empty */
{
front = 0;
rear = 0;
}
else
if(rear == MAX-1)/*rear is at last position of queue */
rear = 0;
else
rear = rear+1;
printf("Input the element for insertion in queue : ");
scanf("%d", &added_item);
cqueue_arr[rear] = added_item ;
}/*End of insert()*/

del()
{
if (front == -1)
{
printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is : %d\n",cqueue_arr[front]);
if(front == rear) /* queue has only one element */
{
front = -1;
rear=-1;
}
else
if(front == MAX-1)
front = 0;
else
front = front+1;

VIVEK GUPTA ROLL NO. 0905210064


10 | P a g e

}/*End of del() */

display()
{
int front_pos = front,rear_pos = rear;
if(front == -1)
{
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
if( front_pos <= rear_pos )
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
else
{
while(front_pos <= MAX-1)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
}/*End of else */
printf("\n");
}/*End of display() */

VIVEK GUPTA ROLL NO. 0905210064


11 | P a g e

ARRAY IMPLEMENTAION OF LIST


/* Program of list using array */
# include<stdio.h>
# define MAX 20

int arr[MAX];
int n; /*Total number of elements in the list */

main()
{
int choice,item,pos;
while(1)
{
printf("1.Input list\n");
printf("2.Insert\n");
printf("3.Search\n");
printf("4.Delete\n");
printf("5.Display\n");
printf("6.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("Enter the number of elements to be entered : ");
scanf("%d",&n);
input(n);
break;
case 2:
insert();
break;
case 3:
printf("Enter the element to be searched : ");
scanf("%d", &item);
pos = search(item);
if(pos >= 1)
printf("%d found at position %d\n",item,pos);
else
printf("Element not found\n");

VIVEK GUPTA ROLL NO. 0905210064


12 | P a g e

break;
case 4:
del();
break;
case 5:
display();
break;
case 6:
exit();
break;
default:
printf("Wrong choice\n");
} /*End of switch */
}/*End of while */
}/*End of main() */

input()
{
int i;
for(i = 0; i< n ; i++)
{
printf("Input value for element %d : ", i+1);
scanf("%d", &arr[i]);
}
}/*End of input()*/

int search(int item)


{
int i;
for(i=0; i < n; i++)
{
if(item == arr[i])
return(i+1);
}
return(0); /* If element not found */
}/*End of search()*/

insert()
{
int temp,item,position;
if(n == MAX)

VIVEK GUPTA ROLL NO. 0905210064


13 | P a g e

{
printf("List overflow\n");
return;
}
printf("Enter position for insertion : ");
scanf("%d", &position);
printf("Enter the value : ");
scanf("%d",&item);
if(position > n+1 )
{
printf("Enter position less than or equal to %d\n",n+1);
return;
}
if( position == n+1 ) /*Insertion at the end */
{
arr[n] = item;
n = n+1;
return;
}
/* Insertion in between */
temp=n-1;
while( temp >= position-1)
{
arr[temp+1] = arr[temp]; /* shifting right */
temp --;
}
arr[position-1] = item;
n = n +1 ;
}/*End of insert()*/

del()
{
int temp,position,item;
if(n == 0)
{
printf("List underflow\n");
return;
}
printf("Enter the element to be deleted : ");
scanf("%d",&item);
if(item==arr[n-1]) /*Deletion at the end*/

VIVEK GUPTA ROLL NO. 0905210064


14 | P a g e

{
n = n-1;
return;
}
position=search(item);
if(position==0)
{
printf("Element not present in array\n");
return;
}
/*Deletion in between */
temp=position-1;
while(temp <= n-1)
{
arr[temp] = arr[temp+1]; /* Shifting left */
temp ++;
}
n=n-1;
}/*End of del()*/

display()
{
int i;
if(n==0)
{
printf("List is empty\n");
return;
}
for(i = 0; i< n; i++)
printf("Value at position %d : %d\n", i+1, arr[i]);
}/*End of display()*/

VIVEK GUPTA ROLL NO. 0905210064


15 | P a g e

IMPLEMENTATION OF STACK USING DYNAMIC


MEMOTY ALLOCATION
/* Program of stack using linked list*/
# include<stdio.h>
# include<malloc.h>

struct node
{
int info;
struct node *link;
} *top=NULL;

main()
{
int choice;
while(1)
{ printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ") ;
scanf("%d", &choice);

switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(1);
default :
printf("Wrong choice\n");
}/*End of switch */

VIVEK GUPTA ROLL NO. 0905210064


16 | P a g e

}/*End of while */
}/*End of main() */

push()
{
struct node *tmp;
int pushed_item;
tmp = (struct node *)malloc(sizeof(struct node));
printf("Input the new value to be pushed on the stack : ");
scanf("%d",&pushed_item);
tmp->info=pushed_item;
tmp->link=top;
top=tmp;
}/*End of push()*/

pop()
{
struct node *tmp;
if(top == NULL)
printf("Stack is empty\n");
else
{ tmp=top;
printf("Popped item is %d\n",tmp->info);
top=top->link;
free(tmp);
}

}/*End of pop()*/

display()
{ struct node *ptr;
ptr=top;
if(top==NULL)
printf("Stack is empty\n");
else
{
printf("Stack elements :\n");
while(ptr!= NULL)
{
printf("%d\n",ptr->info);
ptr = ptr->link;

VIVEK GUPTA ROLL NO. 0905210064


17 | P a g e

}/*End of while */
}/*End of else*/
}/*End of display()*/

VIVEK GUPTA ROLL NO. 0905210064


18 | P a g e

IMPLEMENTATION OF QUEUE USING DYNAMIC


MEMOTY ALLOCATION
/* Program of queue using linked list*/
# include<stdio.h>
# include<malloc.h>

struct node
{
int info;
struct node *link;
}*front=NULL,*rear=NULL;

main()
{
int choice;
while(1)
{ printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d", &choice);

switch(choice)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default :
printf("Wrong choice\n");
}/*End of switch*/

VIVEK GUPTA ROLL NO. 0905210064


19 | P a g e

}/*End of while*/
}/*End of main()*/

insert()
{
struct node *tmp;
int added_item;
tmp = (struct node *)malloc(sizeof(struct node));
printf("Input the element for adding in queue : ");
scanf("%d",&added_item);
tmp->info = added_item;
tmp->link=NULL;
if(front==NULL) /*If Queue is empty*/
front=tmp;
else
rear->link=tmp;
rear=tmp;
}/*End of insert()*/

del()
{
struct node *tmp;
if(front == NULL)
printf("Queue Underflow\n");
else
{
tmp=front;
printf("Deleted element is %d\n",tmp->info);
front=front->link;
free(tmp);
}
}/*End of del()*/

display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
printf("Queue is empty\n");
else
{

VIVEK GUPTA ROLL NO. 0905210064


20 | P a g e

printf("Queue elements :\n");


while(ptr != NULL)
{
printf("%d ",ptr->info);
ptr = ptr->link;
}
printf("\n");
}/*End of else*/
}/*End of display()*/

VIVEK GUPTA ROLL NO. 0905210064


21 | P a g e

IMPLENTAION OF CIRCULAR QUEUE USING


DYNAMIC MEMORY ALLOCATION
/* Program of queue using circular linked list*/
# include <stdio.h>
# include <malloc.h>

struct node
{
int info;
struct node *link;
}*rear=NULL;

main()
{
int choice;
while(1)
{
printf("1.Insert \n");
printf("2.Delete \n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit();
default:
printf("Wrong choice\n");

VIVEK GUPTA ROLL NO. 0905210064


22 | P a g e

}/*End of switch*/
}/*End of while*/
}/*End of main()*/

insert()
{
int num;
struct node *q,*tmp;
printf("Enter the element for insertion : ");
scanf("%d",&num);
tmp= malloc(sizeof(struct node));
tmp->info = num;

if(rear == NULL) /*If queue is empty */


{
rear = tmp;
tmp->link = rear;
}
else
{
tmp->link = rear->link;
rear->link = tmp;
rear = tmp;
}
}/*End of insert()*/

del()
{
struct node *tmp,*q;
if(rear==NULL)
{
printf("Queue underflow\n");
return;
}
if( rear->link == rear ) /*If only one element*/
{
tmp = rear;
rear = NULL;
free(tmp);
return;
}

VIVEK GUPTA ROLL NO. 0905210064


23 | P a g e

q=rear->link;
tmp=q;
rear->link = q->link;
printf("Deleted element is %d\n",tmp->info);
free(tmp);
}/*End of del()*/

display()
{
struct node *q;
if(rear == NULL)
{
printf("Queue is empty\n");
return;
}
q = rear->link;
printf("Queue is :\n");
while(q != rear)
{
printf("%d ", q->info);
q = q->link;
}
printf("%d\n",rear->info);
}/*End of display()*/

VIVEK GUPTA ROLL NO. 0905210064


24 | P a g e

IMPLENTATION OF LIST USING DYNAMIC


MEMORY ALLOCATION
/* Program of single linked list*/
# include <stdio.h>
# include <malloc.h>

struct node
{
int info;
struct node *link;
}*start;

main()
{
int choice,n,m,position,i;
start=NULL;
while(1)
{
printf("1.Create List\n");
printf("2.Add at begining\n");
printf("3.Add after \n");
printf("4.Delete\n");
printf("5.Display\n");
printf("6.Count\n");
printf("7.Reverse\n");
printf("8.Search\n");
printf("9.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("How many nodes you want : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the element : ");
scanf("%d",&m);
create_list(m);

VIVEK GUPTA ROLL NO. 0905210064


25 | P a g e

}
break;
case 2:
printf("Enter the element : ");
scanf("%d",&m);
addatbeg(m);
break;
case 3:
printf("Enter the element : ");
scanf("%d",&m);
printf("Enter the position after which this element is inserted : ");
scanf("%d",&position);
addafter(m,position);
break;
case 4:
if(start==NULL)
{
printf("List is empty\n");
continue;
}
printf("Enter the element for deletion : ");
scanf("%d",&m);
del(m);
break;
case 5:
display();
break;
case 6:
count();
break;
case 7:
rev();
break;
case 8:
printf("Enter the element to be searched : ");
scanf("%d",&m);
search(m);
break;
case 9:
exit();
default:

VIVEK GUPTA ROLL NO. 0905210064


26 | P a g e

printf("Wrong choice\n");
}/*End of switch */
}/*End of while */
}/*End of main()*/

create_list(int data)
{
struct node *q,*tmp;
tmp= malloc(sizeof(struct node));
tmp->info=data;
tmp->link=NULL;

if(start==NULL) /*If list is empty */


start=tmp;
else
{ /*Element inserted at the end */
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
}/*End of create_list()*/

addatbeg(int data)
{
struct node *tmp;
tmp=malloc(sizeof(struct node));
tmp->info=data;
tmp->link=start;
start=tmp;
}/*End of addatbeg()*/

addafter(int data,int pos)


{
struct node *tmp,*q;
int i;
q=start;
for(i=0;i<pos-1;i++)
{
q=q->link;
if(q==NULL)

VIVEK GUPTA ROLL NO. 0905210064


27 | P a g e

{
printf("There are less than %d elements",pos);
return;
}
}/*End of for*/

tmp=malloc(sizeof(struct node) );
tmp->link=q->link;
tmp->info=data;
q->link=tmp;
}/*End of addafter()*/

del(int data)
{
struct node *tmp,*q;
if(start->info == data)
{
tmp=start;
start=start->link; /*First element deleted*/
free(tmp);
return;
}
q=start;
while(q->link->link != NULL)
{
if(q->link->info==data) /*Element deleted in between*/
{
tmp=q->link;
q->link=tmp->link;
free(tmp);
return;
}
q=q->link;
}/*End of while */
if(q->link->info==data) /*Last element deleted*/
{
tmp=q->link;
free(tmp);
q->link=NULL;
return;
}

VIVEK GUPTA ROLL NO. 0905210064


28 | P a g e

printf("Element %d not found\n",data);


}/*End of del()*/

display()
{
struct node *q;
if(start == NULL)
{
printf("List is empty\n");
return;
}
q=start;
printf("List is :\n");
while(q!=NULL)
{
printf("%d ", q->info);
q=q->link;
}
printf("\n");
}/*End of display() */

count()
{
struct node *q=start;
int cnt=0;
while(q!=NULL)
{
q=q->link;
cnt++;
}
printf("Number of elements are %d\n",cnt);
}/*End of count() */

rev()
{
struct node *p1,*p2,*p3;
if(start->link==NULL) /*only one element*/
return;
p1=start;
p2=p1->link;
p3=p2->link;

VIVEK GUPTA ROLL NO. 0905210064


29 | P a g e

p1->link=NULL;
p2->link=p1;
while(p3!=NULL)
{
p1=p2;
p2=p3;
p3=p3->link;
p2->link=p1;
}
start=p2;
}/*End of rev()*/

search(int data)
{
struct node *ptr = start;
int pos = 1;
while(ptr!=NULL)
{
if(ptr->info==data)
{
printf("Item %d found at position %d\n",data,pos);
return;
}
ptr = ptr->link;
pos++;
}
if(ptr == NULL)
printf("Item %d not found in list\n",data);
}/*End of search()*/

VIVEK GUPTA ROLL NO. 0905210064


30 | P a g e

INSERTION, DELETION AND TRAVERSAL IN


BINARY SEARCH TREE
/*Insertion ,Deletion and Traversal in Binary Search Tree*/
# include <stdio.h>
# include <malloc.h>

struct node
{
int info;
struct node *lchild;
struct node *rchild;
}*root;
void case_a(struct node *par,struct node *loc )

void find(int item,struct node **par,struct node **loc)


{
struct node *ptr,*ptrsave;

if(root==NULL) /*tree empty*/


{
*loc=NULL;
*par=NULL;
return;
}
if(item==root->info) /*item is at root*/
{
*loc=root;
*par=NULL;
return;
}
/*Initialize ptr and ptrsave*/
if(item<root->info)
ptr=root->lchild;
else
ptr=root->rchild;
ptrsave=root;

while(ptr!=NULL)
{

VIVEK GUPTA ROLL NO. 0905210064


31 | P a g e

if(item==ptr->info)
{ *loc=ptr;
*par=ptrsave;
return;
}
ptrsave=ptr;
if(item<ptr->info)
ptr=ptr->lchild;
else
ptr=ptr->rchild;
}/*End of while */
*loc=NULL; /*item not found*/
*par=ptrsave;
}/*End of find()*/

void insert(int item)

{ struct node *tmp,*parent,*location;


find(item,&parent,&location);
if(location!=NULL)
{
printf("Item already present");
return;
}

tmp=(struct node *)malloc(sizeof(struct node));


tmp->info=item;
tmp->lchild=NULL;
tmp->rchild=NULL;

if(parent==NULL)
root=tmp;
else
if(item<parent->info)
parent->lchild=tmp;
else
parent->rchild=tmp;
}/*End of insert()*/

void del(int item)

VIVEK GUPTA ROLL NO. 0905210064


32 | P a g e

{
struct node *parent,*location;
if(root==NULL)
{
printf("Tree empty");
return;
}

find(item,&parent,&location);
if(location==NULL)
{
printf("Item not present in tree");
return;
}

if(location->lchild==NULL && location->rchild==NULL)


case_a(parent,location);
if(location->lchild!=NULL && location->rchild==NULL)
case_b(parent,location);
if(location->lchild==NULL && location->rchild!=NULL)
case_b(parent,location);
if(location->lchild!=NULL && location->rchild!=NULL)
case_c(parent,location);
free(location);
}/*End of del()*/

void case_a(struct node *par,struct node *loc )


{
if(par==NULL) /*item to be deleted is root node*/
root=NULL;
else
if(loc==par->lchild)
par->lchild=NULL;
else
par->rchild=NULL;
}/*End of case_a()*/

case_b(struct node *par,struct node *loc)


{
struct node *child;

VIVEK GUPTA ROLL NO. 0905210064


33 | P a g e

/*Initialize child*/
if(loc->lchild!=NULL) /*item to be deleted has lchild */
child=loc->lchild;
else /*item to be deleted has rchild */
child=loc->rchild;

if(par==NULL ) /*Item to be deleted is root node*/


root=child;
else
if( loc==par->lchild) /*item is lchild of its parent*/
par->lchild=child;
else /*item is rchild of its parent*/
par->rchild=child;
}/*End of case_b()*/

case_c(struct node *par,struct node *loc)


{
struct node *ptr,*ptrsave,*suc,*parsuc;

/*Find inorder successor and its parent*/


ptrsave=loc;
ptr=loc->rchild;
while(ptr->lchild!=NULL)
{
ptrsave=ptr;
ptr=ptr->lchild;
}
suc=ptr;
parsuc=ptrsave;

if(suc->lchild==NULL && suc->rchild==NULL)


case_a(parsuc,suc);
else
case_b(parsuc,suc);

if(par==NULL) /*if item to be deleted is root node */


root=suc;
else
if(loc==par->lchild)
par->lchild=suc;
else

VIVEK GUPTA ROLL NO. 0905210064


34 | P a g e

par->rchild=suc;

suc->lchild=loc->lchild;
suc->rchild=loc->rchild;
}/*End of case_c()*/

preorder(struct node *ptr)


{
if(root==NULL)
{
printf("Tree is empty");
return;
}
if(ptr!=NULL)
{
printf("%d ",ptr->info);
preorder(ptr->lchild);
preorder(ptr->rchild);
}
}/*End of preorder()*/

inorder(struct node *ptr)


{
if(root==NULL)
{
printf("Tree is empty");
return;
}
if(ptr!=NULL)
{
inorder(ptr->lchild);
printf("%d ",ptr->info);
inorder(ptr->rchild);
}
}/*End of inorder()*/

postorder(struct node *ptr)


{
if(root==NULL)
{
printf("Tree is empty");

VIVEK GUPTA ROLL NO. 0905210064


35 | P a g e

return;
}
if(ptr!=NULL)
{
postorder(ptr->lchild);
postorder(ptr->rchild);
printf("%d ",ptr->info);
}
}/*End of postorder()*/

display(struct node *ptr,int level)


{
int i;
if ( ptr!=NULL )
{
display(ptr->rchild, level+1);
printf("\n");
for (i = 0; i < level; i++)
printf(" ");
printf("%d", ptr->info);
display(ptr->lchild, level+1);
}/*End of if*/
}/*End of display()*/main()
{
int choice,num;
root=NULL;
while(1)
{
printf("\n");
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Inorder Traversal\n");
printf("4.Preorder Traversal\n");
printf("5.Postorder Traversal\n");
printf("6.Display\n");
printf("7.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{

VIVEK GUPTA ROLL NO. 0905210064


36 | P a g e

case 1:
printf("Enter the number to be inserted : ");
scanf("%d",&num);

insert(num);
break;
case 2:
printf("Enter the number to be deleted : ");
scanf("%d",&num);
del(num);
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
display(root,1);
break;
case 7:
exit();
default:
printf("Wrong choice\n");
}/*End of switch */
}/*End of while */
}/*End of main()*/

VIVEK GUPTA ROLL NO. 0905210064


37 | P a g e

IMPLENTATION OF BINARY SEARCHING


/*Program for binary search*/
#include <stdio.h>

main()
{
int arr[20],start,end,middle,n,i,item;

printf("How many elements you want to enter in the array : ");


scanf("%d",&n);
for(i=0; i < n; i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&arr[i]);
}
printf("Enter the element to be searched : ");
scanf("%d",&item);
start=0;
end=n-1;
middle=(start+end)/2;
while(item != arr[middle] && start <= end)
{
if(item > arr[middle])
start=middle+1;
else
end=middle-1;
middle=(start+end)/2;
}
if(item==arr[middle])
printf("%d found at position %d\n",item,middle+1);
if(start>end)
printf("%d not found in array\n",item);
}/*End of main()*/

VIVEK GUPTA ROLL NO. 0905210064


38 | P a g e

IMPLENTATION OF HEAPSORT
* Program of sorting through heapsort*/
# include <stdio.h>

int arr[20],n;

main()
{
int i;
printf("Enter number of elements : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&arr[i]);
}
printf("Entered list is :\n");
display();

create_heap();

printf("Heap is :\n");
display();

heap_sort();
printf("Sorted list is :\n");
display();
}/*End of main()*/

display()
{ int i;
for(i=0;i<n;i++)
printf("%d ",arr[i]);
printf("\n");
}/*End of display()*/

create_heap()
{
int i;
for(i=0;i<n;i++)

VIVEK GUPTA ROLL NO. 0905210064


39 | P a g e

insert(arr[i],i);
}/*End of create_heap()*/

insert(int num,int loc)


{
int par;
while(loc>0)
{
par=(loc-1)/2;
if(num<=arr[par])
{
arr[loc]=num;
return;
}
arr[loc]=arr[par];
loc=par;
}/*End of while*/
arr[0]=num;
}/*End of insert()*/

heap_sort()
{
int last;
for(last=n-1; last>0; last--)
del_root(last);
}/*End of del_root*/

del_root(int last)
{
int left,right,i,temp;
i=0; /*Since every time we have to replace root with last*/
/*Exchange last element with the root */
temp=arr[i];
arr[i]=arr[last];
arr[last]=temp;

left=2*i+1; /*left child of root*/


right=2*i+2;/*right child of root*/

while( right < last)

VIVEK GUPTA ROLL NO. 0905210064


40 | P a g e

{
if( arr[i]>=arr[left] && arr[i]>=arr[right] )
return;
if( arr[right]<=arr[left] )
{
temp=arr[i];
arr[i]=arr[left];
arr[left]=temp;
i=left;
}
else
{
temp=arr[i];
arr[i]=arr[right];
arr[right]=temp;
i=right;
}
left=2*i+1;
right=2*i+2;
}/*End of while*/
if( left==last-1 && arr[i]<arr[left] )/*right==last*/
{
temp=arr[i];
arr[i]=arr[left];
arr[left]=temp;
}
}/*End of del_root*/

VIVEK GUPTA ROLL NO. 0905210064


41 | P a g e

IMPLEMENTATION OF QUICK SORT


/*Program of sorting using quick sort through recursion*/
#include<stdio.h>
#define MAX 30

enum bool { FALSE,TRUE };


main()
{
int array[MAX],n,i;
printf("Enter the number of elements : ");
scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&array[i]);
}

printf("Unsorted list is :\n");


display(array,0,n-1);
printf("\n");

quick(array,0,n-1);

printf("Sorted list is :\n");


display(array,0,n-1);
printf("\n");

}/*End of main() */

quick(int arr[],int low,int up)


{
int piv,temp,left,right;
enum bool pivot_placed=FALSE;
left=low;
right=up;
piv=low; /*Take the first element of sublist as piv */

if(low>=up)
return;

VIVEK GUPTA ROLL NO. 0905210064


42 | P a g e

printf("Sublist : ");
display(arr,low,up);

/*Loop till pivot is placed at proper place in the sublist*/


while(pivot_placed==FALSE)
{
/*Compare from right to left */
while( arr[piv]<=arr[right] && piv!=right )
right=right-1;
if( piv==right )
pivot_placed=TRUE;
if( arr[piv] > arr[right] )
{
temp=arr[piv];
arr[piv]=arr[right];
arr[right]=temp;
piv=right;
}
/*Compare from left to right */
while( arr[piv]>=arr[left] && left!=piv )
left=left+1;
if(piv==left)
pivot_placed=TRUE;
if( arr[piv] < arr[left] )
{
temp=arr[piv];
arr[piv]=arr[left];
arr[left]=temp;
piv=left;
}
}/*End of while */

printf("-> Pivot Placed is %d -> ",arr[piv]);


display(arr,low,up);
printf("\n");

quick(arr,low,piv-1);
quick(arr,piv+1,up);
}/*End of quick()*/
display(int arr[],int low,int up)
{

VIVEK GUPTA ROLL NO. 0905210064


43 | P a g e

int i;
for(i=low;i<=up;i++)
printf("%d ",arr[i]);
}

VIVEK GUPTA ROLL NO. 0905210064


44 | P a g e

IMPLEMENTATION OF INSERTION SORT


/* Program of sorting using insertion sort */
#include <stdio.h>
#define MAX 20

main()
{
int arr[MAX],i,j,k,n;
printf("Enter the number of elements : ");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
printf("Enter element %d : ",i+1);
scanf("%d", &arr[i]);
}
printf("Unsorted list is :\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
/*Insertion sort*/
for(j=1;j<n;j++)
{
k=arr[j]; /*k is to be inserted at proper place*/
for(i=j-1;i>=0 && k<arr[i];i--)
arr[i+1]=arr[i];
arr[i+1]=k;
printf("Pass %d, Element inserted in proper place: %d\n",j,k);
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
printf("Sorted list is :\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}/*End of main()*/

VIVEK GUPTA ROLL NO. 0905210064


45 | P a g e

TRAVERSING OF GRAPH THROUGH BFS AND DFS


/* Program for traversing a graph through BFS and DFS */
#include<stdio.h>
#define MAX 20

typedef enum boolean{false,true} bool;


int adj[MAX][MAX];
bool visited[MAX];
int n; /* Denotes number of nodes in the graph */
main()
{
int i,v,choice;

create_graph();
while(1)
{
printf("\n");
printf("1. Adjacency matrix\n");
printf("2. Depth First Search using stack\n");
printf("3. Depth First Search through recursion\n");
printf("4. Breadth First Search\n");
printf("5. Adjacent vertices\n");
printf("6. Components\n");
printf("7. Exit\n");
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("Adjacency Matrix\n");
display();
break;
case 2:
printf("Enter starting node for Depth First Search : ");
scanf("%d",&v);
for(i=1;i<=n;i++)
visited[i]=false;
dfs(v);
break;

VIVEK GUPTA ROLL NO. 0905210064


46 | P a g e

case 3:
printf("Enter starting node for Depth First Search : ");
scanf("%d",&v);
for(i=1;i<=n;i++)
visited[i]=false;
dfs_rec(v);
break;
case 4:
printf("Enter starting node for Breadth First Search : ");
scanf("%d", &v);
for(i=1;i<=n;i++)
visited[i]=false;
bfs(v);
break;
case 5:
printf("Enter node to find adjacent vertices : ");
scanf("%d", &v);
printf("Adjacent Vertices are : ");
adj_nodes(v);
break;
case 6:
components();
break;
case 7:
exit(1);
default:
printf("Wrong choice\n");
break;
}/*End of switch*/
}/*End of while*/
}/*End of main()*/

create_graph()
{
int i,max_edges,origin,destin;

printf("Enter number of nodes : ");


scanf("%d",&n);
max_edges=n*(n-1);

for(i=1;i<=max_edges;i++)

VIVEK GUPTA ROLL NO. 0905210064


47 | P a g e

{
printf("Enter edge %d( 0 0 to quit ) : ",i);
scanf("%d %d",&origin,&destin);

if((origin==0) && (destin==0))


break;

if( origin > n || destin > n || origin<=0 || destin<=0)


{
printf("Invalid edge!\n");
i--;
}
else
{
adj[origin][destin]=1;
}
}/*End of for*/
}/*End of create_graph()*/

display()
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%4d",adj[i][j]);
printf("\n");
}
}/*End of display()*/

dfs_rec(int v)
{
int i;
visited[v]=true;
printf("%d ",v);
for(i=1;i<=n;i++)
if(adj[v][i]==1 && visited[i]==false)
dfs_rec(i);
}/*End of dfs_rec()*/

dfs(int v)

VIVEK GUPTA ROLL NO. 0905210064


48 | P a g e

{
int i,stack[MAX],top=-1,pop_v,j,t;
int ch;

top++;
stack[top]=v;
while (top>=0)
{
pop_v=stack[top];
top--; /*pop from stack*/
if( visited[pop_v]==false)
{
printf("%d ",pop_v);
visited[pop_v]=true;
}
else
continue;

for(i=n;i>=1;i--)
{
if( adj[pop_v][i]==1 && visited[i]==false)
{
top++; /* push all unvisited neighbours of pop_v */
stack[top]=i;
}/*End of if*/
}/*End of for*/
}/*End of while*/
}/*End of dfs()*/

bfs(int v)
{
int i,front,rear;
int que[20];
front=rear= -1;

printf("%d ",v);
visited[v]=true;
rear++;
front++;
que[rear]=v;

VIVEK GUPTA ROLL NO. 0905210064


49 | P a g e

while(front<=rear)
{
v=que[front]; /* delete from queue */
front++;
for(i=1;i<=n;i++)
{
/* Check for adjacent unvisited nodes */
if( adj[v][i]==1 && visited[i]==false)
{
printf("%d ",i);
visited[i]=true;
rear++;
que[rear]=i;
}
}
}/*End of while*/
}/*End of bfs()*/

adj_nodes(int v)
{
int i;
for(i=1;i<=n;i++)
if(adj[v][i]==1)
printf("%d ",i);
printf("\n");
}/*End of adj_nodes()*/

components()
{
int i;
for(i=1;i<=n;i++)
visited[i]=false;
for(i=1;i<=n;i++)
{
if(visited[i]==false)
dfs_rec(i);
}
printf("\n");
}/*End of components()*/

VIVEK GUPTA ROLL NO. 0905210064


50 | P a g e

IMPLENTATION OF SHORTEST PATH ALGORITHM


/* Program of shortest path between two node in graph using Djikstra algorithm */
#include<stdio.h>

#define MAX 10
#define TEMP 0
#define PERM 1
#define infinity 9999

struct node
{
int predecessor;
int dist; /*minimum distance of node from source*/
int status;
};

int adj[MAX][MAX];
int n;
void main()
{
int i,j;
int source,dest;
int path[MAX];
int shortdist,count;

create_graph();
printf("The adjacency matrix is :\n");
display();

while(1)
{
printf("Enter source node(0 to quit) : ");
scanf("%d",&source);
printf("Enter destination node(0 to quit) : ");
scanf("%d",&dest);

if(source==0 || dest==0)
exit(1);

count = findpath(source,dest,path,&shortdist);

VIVEK GUPTA ROLL NO. 0905210064


51 | P a g e

if(shortdist!=0)
{
printf("Shortest distance is : %d\n", shortdist);
printf("Shortest Path is : ");
for(i=count;i>1;i--)
printf("%d->",path[i]);
printf("%d",path[i]);
printf("\n");
}
else
printf("There is no path from source to destination node\n");
}/*End of while*/
}/*End of main()*/

create_graph()
{
int i,max_edges,origin,destin,wt;

printf("Enter number of vertices : ");


scanf("%d",&n);
max_edges=n*(n-1);

for(i=1;i<=max_edges;i++)
{
printf("Enter edge %d(0 0 to quit) : ",i);
scanf("%d %d",&origin,&destin);
if((origin==0) && (destin==0))
break;
printf("Enter weight for this edge : ");
scanf("%d",&wt);
if( origin > n || destin > n || origin<=0 || destin<=0)
{
printf("Invalid edge!\n");
i--;
}
else
adj[origin][destin]=wt;
}/*End of for*/
}/*End of create_graph()*/

display()

VIVEK GUPTA ROLL NO. 0905210064


52 | P a g e

{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%3d",adj[i][j]);
printf("\n");
}

}/*End of display()*/

int findpath(int s,int d,int path[MAX],int *sdist)


{
struct node state[MAX];
int i,min,count=0,current,newdist,u,v;
*sdist=0;
/* Make all nodes temporary */
for(i=1;i<=n;i++)
{
state[i].predecessor=0;
state[i].dist = infinity;
state[i].status = TEMP;
}

/*Source node should be permanent*/


state[s].predecessor=0;
state[s].dist = 0;
state[s].status = PERM;

/*Starting from source node until destination is found*/


current=s;
while(current!=d)
{
for(i=1;i<=n;i++)
{
/*Checks for adjacent temporary nodes */
if ( adj[current][i] > 0 && state[i].status == TEMP )
{
newdist=state[current].dist + adj[current][i];
/*Checks for Relabeling*/
if( newdist < state[i].dist )

VIVEK GUPTA ROLL NO. 0905210064


53 | P a g e

{
state[i].predecessor = current;
state[i].dist = newdist;
}
}
}/*End of for*/

/*Search for temporary node with minimum distand make it current node*/
min=infinity;
current=0;
for(i=1;i<=n;i++)
{
if(state[i].status == TEMP && state[i].dist < min)
{
min = state[i].dist;
current=i;
}
}/*End of for*/

if(current==0) /*If Source or Sink node is isolated*/


return 0;
state[current].status=PERM;
}/*End of while*/

/* Getting full path in array from destination to source */


while( current!=0 )
{
count++;
path[count]=current;
current=state[current].predecessor;
}

/*Getting distance from source to destination*/


for(i=count;i>1;i--)
{
u=path[i];
v=path[i-1];
*sdist+= adj[u][v];
}
return (count);
}/*End of findpath()*/

VIVEK GUPTA ROLL NO. 0905210064

Вам также может понравиться