Anda di halaman 1dari 102

Data Structures Lab Solution Manual

University Experiment #1
AIM- WAP to perform linear search & binary search on array.

#include<stdio.h>
#include<conio.h>
int linear_search(int [], int, int);
int binary_search(int [],int,int,int);
void main()
{
int a[20],search,i,n,pos,x;
int item,f=0,n,l,b;
clrscr();
printf(“search\n”);
printf(“1.linear search.\n”);
printf(“2.binary search”);
printf(“enter type of search u want to perform”);
scanf(“%d”,&x);
switch(x)
{case 1:
printf("Enter the size of the array(max. 20):");
scanf("%d",&n);
printf("\nEnter the array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the number to search:");
scanf("%d",&search);
pos = linear_search(a,n,search);
if(pos==-1)
printf("\n The element %d is not present in array.", search);
else
printf("\n The element %d is present at location %d.", search, pos+1);
break;
case 2:
printf("\n ENTER THE ELEMENT FOR SEARCH:");
scanf("%d",&item);
l=n-1;
b=binary_search(a,f,l,item);
if(b==-1)
printf("\n ELEMENT IS NOT FOUND.");
else
printf("\n ELEMENT %d IS FOUND AT LOCATION =%d",item,b+1);

1 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

break;
default: printf(“u enter a wrong choice”);
}
getch();
}
int linear_search(int arr[], int n, int find)
{
int i;
for (i=0;i<n;i++)
{
if (arr[i]==find)
return i;
}
return -1;
}

int binary_search(int arr[],int f,int l,int item)


{
int j;
while(f<=l)
{
j=(f+l)/2;
if(arr[j]==item)
return j;
else if(item<arr[j])
l=j-1;
else if(item>arr[j])
f=j+1;
}
return(-1);
}

2 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #1

3 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

University Experiment #2
AIM- WAP to create a stack and Perform Push, Pop and Traverse operations on the
stack using Linear Linked List.

#include<stdio.h>
#include<conio.h>
#include<process.h>
typedef struct node
{ int data;
struct node *link;
}NODE;
void Push(int);
int pop();
void Display();
NODE *top=NULL;
void main()
{ int opn,elem;
clrscr();
printf("\n ### Linked List Implementation of STACK Operations ### \n\n");
printf("\n Press 1-Push, 2-Pop, 3-Display,4-Exit");
do { printf("\n Your option- ");
scanf("%d",&opn);
switch(opn)
{ case 1: printf("\n\nRead the Element to be pushed-");
scanf("%d",&elem);
Push(elem);
break;
case 2: elem=Pop();
if(elem != -1)
printf(" Deleted Node(From Top)with the Data: %d\n",elem);
break;
case 3: printf("Linked List Implementation of Stack: Status:\n");
Display();
break;
case 4: exit(0);
default: printf("\nInvalid Option !!! Try Again !! ");
break;
}
}while(opn != 4);
getch();
}

4 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

void Push(int info)


{ NODE *temp;
temp=(NODE *)malloc(sizeof(NODE));
if( temp == NULL)
printf(" Out of Memory !! Overflow !!!");
else
{ temp->data=info;
temp->link=top;
top=temp;
}
}
int Pop()
{ int info;
NODE *t;
if( top == NULL) { printf(" Underflow!!!"); return -1; }
else
{ t=top;
info=top->data;
top=top->link;
t->link=NULL;
free(t);
return(info);
}
}
void Display()
{ NODE *t;
if( top == NULL) printf("Empty Stack\n");
else
{ t=top;
printf("Top->");
while(t)
{ printf("[%d]->",t->data);
t=t->link;
}
printf("Null\n");
}
}

5 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #2

6 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

University Experiment #3
AIM- WAP to create a Linear Queue using Linked List and Implement different
operations such as Insert, Delete and Display the queue elements.

#include <stdlib.h>
#include<conio.h>
typedef struct node
{ int data;
struct node *link;
} NODE;
NODE *front, *rear;
void Insert(int);
int Delete();
void Display();
void main()
{ int opn, elem;
clrscr();
printf("\n ### Linked List Implementation of QUEUE Operations ### ");
printf("\n Press 1-Insert, 2-Delete, 3-Display,4-Exit");
front = rear = NULL;
do{ printf("\n Your option- ");
scanf("%d", &opn);
switch (opn)
{ case 1: printf("\nRead the Employee no. to be Inserted-");
scanf("%d", &elem);
Insert(elem);
break;
case 2: elem = Delete();
if (elem != -1)
printf(" Deleted Node(From Front)with the Data: %d\n", elem);
break;
case 3: printf("Linked List Implementation of Queue: Status:\n");
Display();
break;
case 4: exit(0);
break;
default: printf("\n\nInvalid Option !!! Try Again !! \n\n");
break;
}
} while (opn != 4);
getch(); }

7 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

void Insert(int info)


{ NODE *temp;
temp = (NODE *) malloc(sizeof(NODE));
if (temp == NULL)
printf(" Out of Memory !! Overflow !!!");
else
{ temp->data = info;
temp->link = NULL;
if (front == NULL) {
front = rear = temp; }
else
{ rear->link = temp;
rear = temp; }
printf(" Node has been inserted at End Successfully !!"); }}
int Delete() {
int info;
NODE *t;
if (front == NULL) {
printf(" Underflow!!!");
return -1;
}
Else
{ t = front;
info = front->data;
if (front == rear)
rear = NULL;
front = front->link;
t->link = NULL;
free(t);
return (info); }}
void Display() {
NODE *t;
if (front == NULL)
printf("Empty Queue\n");
else
{ t = front;
printf("Front->");
while (t) {
printf("[%d]->", t->data);
t = t->link;
}
printf("Rear\n");
}}

8 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #3

9 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

University Experiment #4
AIM- WAP to Create a Binary Tree and perform Tree traversals (Preorder, Postorder,
Inorder) using the concept of Recursion.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct bint
{
int data,flag;
struct bint *left,*right;
}node;

node * create(node *r,int d)


{
if(r == NULL)
{
r = (node *)malloc(sizeof(node));
r->data = d;
r->left = r->right = NULL;
}
else
{
if(r->data <= d)
r->right = create(r->right,d);
else
r->left = create(r->left,d);
}
return r;
}

void inorder(node *r)


{
if(r != NULL)
{
inorder(r->left);
printf("%d\t",r->data);
inorder(r->right);
}
}

10 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

void preorder(node *r)


{
if(r != NULL)
{
printf("%d\t",r->data);
preorder(r->left);
preorder(r->right);
}
}

void postorder(node *r)


{
if(r != NULL)
{
postorder(r->left);
postorder(r->right);
printf("%d\t",r->data);
}
}
void main()
{
int d;
char ch = 'Y';
node *head = NULL;
clrscr();
while(toupper(ch) == 'Y')
{
printf("\n Enter the item to insert");
scanf("%d",&d);
head = create(head,d);
printf("\n Do you want to continue(y/n)");
fflush(stdin);
ch = getchar();
}
printf("\ninorder recursive\n");
inorder(head);
printf("\npostorder recursive\n");
postorder(head);
printf("\npreorder recursive\n");
preorder(head);
getch(); }

11 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #4

12 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

University Experiment #5
AIM- WAP to Implement insertion, deletion and display (Preorder, Postorder, Inorder)
on binary search tree with the information in the tree about the details of an automobile
(type, company, year of make).

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
struct btnode
{
char value1[30];
char value2[30];
int value3;
struct btnode *l;
struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;
void delete1();
void insert();
void delete();
void inorder(struct btnode *t);
void create();
void search(struct btnode *t);
void preorder(struct btnode *t);
void postorder(struct btnode *t);
void search1(struct btnode *t,int data);
int smallest(struct btnode *t);
int largest(struct btnode *t);
int flag = 1;
void main()
{
int ch;
clrscr();
printf("\nOPERATIONS ---");
printf("\n1 - Insert an element into tree\n");
printf("2 - Delete an element from the tree\n");
printf("3 - Inorder Traversal\n");
printf("4 - Preorder Traversal\n");
printf("5 - Postorder Traversal\n");
printf("6 - Exit\n");
while(1)
{

13 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

printf("\nEnter your choice : ");


scanf("%d", &ch);
switch (ch)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
exit(0);
default :
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
void insert()
{
create();
if (root == NULL)
root = temp;
else
search(root);
}
void create()
{
char type[30];
char company[30];
int year;

printf("Enter data of automobile to be inserted : ");


printf("\ntype :");

14 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

scanf("%s",&type);
printf("company :");
scanf("%s",&company);
printf("year :");
scanf("%d", &year);
temp = (struct btnode *)malloc(1*sizeof(struct btnode));
strcpy(temp->value1,type);
strcpy(temp->value2,company);
temp->value3=year;
temp->l = temp->r = NULL;
}
void search(struct btnode *t)
{
if ((temp->value3 > t->value3) && (t->r != NULL)) /* value more than root node value
insert at right */
search(t->r);
else if ((temp->value3 > t->value3) && (t->r == NULL))
t->r = temp;
else if ((temp->value3 < t->value3) && (t->l != NULL)) /* value less than root node
value insert at left */
search(t->l);
else if ((temp->value3 < t->value3) && (t->l == NULL))
t->l = temp;
}
void inorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display");
return;
}
if (t->l != NULL)
inorder(t->l);
puts(t->value1);
puts(t->value2);
printf("%d -> ", t->value3);
if (t->r != NULL)
inorder(t->r);
}
void delete()
{
int year;

15 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

if (root == NULL)
{
printf("No elements in a tree to delete");
return;
}
printf("Enter the year of data to be deleted : ");
scanf("%d", &year);
t1 = root;
t2 = root;
search1(root, year);
}
void preorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display");
return;
}
puts(t->value1);
puts(t->value2);
printf("%d -> ", t->value3);
if (t->l != NULL)
preorder(t->l);
if (t->r != NULL)
preorder(t->r);
}
void postorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display ");
return;
}
if (t->l != NULL)
postorder(t->l);
if (t->r != NULL)
postorder(t->r);
puts(t->value1);
puts(t->value2);
printf("%d -> ", t->value3);
}
void search1(struct btnode *t, int year)
{

16 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

if ((year>t->value3))
{
t1 = t;
search1(t->r, year);
}
else if ((year < t->value3))
{
t1 = t;
search1(t->l, year);
}
else if ((year==t->value3))
{
delete1(t);
}
}
void delete1(struct btnode *t)
{
int k;
if ((t->l == NULL) && (t->r == NULL))
{
if (t1->l == t)
{
t1->l = NULL;
}
else
{
t1->r = NULL;
}
t = NULL;
free(t);
return;
}
else if ((t->r == NULL))
{
if (t1 == t)
{
root = t->l;
t1 = root;
}
else if (t1->l == t)
{
t1->l = t->l;

17 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

}
else
{
t1->r = t->l;
}
t = NULL;
free(t);
return;
}
else if (t->l == NULL)
{
if (t1 == t)
{
root = t->r;
t1 = root;
}
else if (t1->r == t)
t1->r = t->r;
else
t1->l = t->r;
t = NULL;
free(t);
return;
}
else if ((t->l != NULL) && (t->r != NULL))
{
t2 = root;
if (t->r != NULL)
{
k = smallest(t->r);
flag = 1;
}
else
{
k =largest(t->l);
flag = 2;
}
search1(root, k);
t->value3 = k;
}

}
int smallest(struct btnode *t)

18 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

{
t2 = t;
if (t->l != NULL)
{
t2 = t;
return(smallest(t->l));
}
else
return (t->value3);
}
int largest(struct btnode *t)
{
if (t->r != NULL)
{
t2 = t;
return(largest(t->r));
}
else
return(t->value3);
}

19 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #5

20 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

University Experiment #6
AIM: WAP to create a stack and Perform Push, Pop and Traverse operations on the
stack using Linear Linked List.

#include<stdio.h>
#include<conio.h>
#include<process.h>
typedef struct node
{ int data;
struct node *link;
}NODE;
void Push(int);
int pop();
void Display();
NODE *top=NULL;
void main()
{ int opn,elem;
clrscr();
printf("\n ### Linked List Implementation of STACK Operations ### \n\n");
printf("\n Press 1-Push, 2-Pop, 3-Display,4-Exit");
do { printf("\n Your option- ");
scanf("%d",&opn);
switch(opn)
{ case 1: printf("\n\nRead the Element to be pushed-");
scanf("%d",&elem);
Push(elem);
break;
case 2: elem=Pop();
if(elem != -1)
printf(" Deleted Node(From Top)with the Data: %d\n",elem);
break;
case 3: printf("Linked List Implementation of Stack: Status:\n");
Display();
break;
case 4: exit(0);
default: printf("\nInvalid Option !!! Try Again !! ");
break;
}
}while(opn != 4);
getch();
}

21 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

void Push(int info)


{ NODE *temp;
temp=(NODE *)malloc(sizeof(NODE));
if( temp == NULL)
printf(" Out of Memory !! Overflow !!!");
else
{ temp->data=info;
temp->link=top;
top=temp;
}
}
int Pop()
{ int info;
NODE *t;
if( top == NULL) { printf(" Underflow!!!"); return -1; }
else
{ t=top;
info=top->data;
top=top->link;
t->link=NULL;
free(t);
return(info);
}
}
void Display()
{ NODE *t;
if( top == NULL) printf("Empty Stack\n");
else
{ t=top;
printf("Top->");
while(t)
{ printf("[%d]->",t->data);
t=t->link;
}
printf("Null\n");
}
}

22 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #6

23 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

University Experiment #7
AIM- WAP to create a Linear Queue using Linked List and Implement different
operations such as Insert, Delete and Display the queue elements.

#include <stdlib.h>
#include<conio.h>
typedef struct node
{ int data;
struct node *link;
} NODE;
NODE *front, *rear;
void Insert(int);
int Delete();
void Display();
void main()
{ int opn, elem;
clrscr();
printf("\n ### Linked List Implementation of QUEUE Operations ### ");
printf("\n Press 1-Insert, 2-Delete, 3-Display,4-Exit");
front = rear = NULL;
do{ printf("\n Your option- ");
scanf("%d", &opn);
switch (opn)
{ case 1: printf("\nRead the Employee no. to be Inserted-");
scanf("%d", &elem);
Insert(elem);
break;
case 2: elem = Delete();
if (elem != -1)
printf(" Deleted Node(From Front)with the Data: %d\n", elem);
break;
case 3: printf("Linked List Implementation of Queue: Status:\n");
Display();
break;
case 4: exit(0);
break;
default: printf("\n\nInvalid Option !!! Try Again !! \n\n");
break;
}
} while (opn != 4);
getch();

24 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

}
void Insert(int info)
{ NODE *temp;
temp = (NODE *) malloc(sizeof(NODE));
if (temp == NULL)
printf(" Out of Memory !! Overflow !!!");
else
{ temp->data = info;
temp->link = NULL;
if (front == NULL) {
front = rear = temp; }
else
{ rear->link = temp;
rear = temp; }
printf(" Node has been inserted at End Successfully !!"); } }
int Delete() {
int info;
NODE *t;
if (front == NULL) {
printf(" Underflow!!!");
return -1; }
Else
{ t = front;
info = front->data;
if (front == rear)
rear = NULL;
front = front->link;
t->link = NULL;
free(t);
return (info); } }
void Display() {
NODE *t;
if (front == NULL)
printf("Empty Queue\n");
else
{ t = front;
printf("Front->");
while (t) {
printf("[%d]->", t->data);
t = t->link; }
printf("Rear\n");
}
}

25 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #7

26 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

University Experiment #8
AIM- WAP to Create a Binary Tree and perform Tree traversals (Preorder, Postorder,
Inorder) using the concept of Recursion.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct bint
{
int data,flag;
struct bint *left,*right;
}node;

node * create(node *r,int d)


{
if(r == NULL)
{
r = (node *)malloc(sizeof(node));
r->data = d;
r->left = r->right = NULL;
}
else
{
if(r->data <= d)
r->right = create(r->right,d);
else
r->left = create(r->left,d);
}
return r;
}

void inorder(node *r)


{
if(r != NULL)
{
inorder(r->left);
printf("%d\t",r->data);
inorder(r->right);
}
}

27 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

void preorder(node *r)


{
if(r != NULL)
{
printf("%d\t",r->data);
preorder(r->left);
preorder(r->right);
}
}

void postorder(node *r)


{
if(r != NULL)
{
postorder(r->left);
postorder(r->right);
printf("%d\t",r->data);
}
}
void main()
{
int d;
char ch = 'Y';
node *head = NULL;
clrscr();
while(toupper(ch) == 'Y')
{
printf("\n Enter the item to insert");
scanf("%d",&d);
head = create(head,d);
printf("\n Do you want to continue(y/n)");
fflush(stdin);
ch = getchar();
}
printf("\ninorder recursive\n");
inorder(head);
printf("\npostorder recursive\n");
postorder(head);
printf("\npreorder recursive\n");
preorder(head);
getch(); }

28 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #8

29 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

University Experiment #9
AIM- WAP to Implement insertion, deletion and display (Preorder, Postorder, Inorder)
on binary search tree with the information in the tree about the details of a automobile
(type, company, year of make).

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
struct btnode
{
char value1[30];
char value2[30];
int value3;
struct btnode *l;
struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;
void delete1();
void insert();
void delete();
void inorder(struct btnode *t);
void create();
void search(struct btnode *t);
void preorder(struct btnode *t);
void postorder(struct btnode *t);
void search1(struct btnode *t,int data);
int smallest(struct btnode *t);
int largest(struct btnode *t);
int flag = 1;
void main()
{
int ch;
clrscr();
printf("\nOPERATIONS ---");
printf("\n1 - Insert an element into tree\n");
printf("2 - Delete an element from the tree\n");
printf("3 - Inorder Traversal\n");
printf("4 - Preorder Traversal\n");
printf("5 - Postorder Traversal\n");
printf("6 - Exit\n");
while(1)
{

30 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

printf("\nEnter your choice : ");


scanf("%d", &ch);
switch (ch)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
exit(0);
default :
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
void insert()
{
create();
if (root == NULL)
root = temp;
else
search(root);
}
void create()
{
char type[30];
char company[30];
int year;

printf("Enter data of automobile to be inserted : ");


printf("\ntype :");

31 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

scanf("%s",&type);
printf("company :");
scanf("%s",&company);
printf("year :");
scanf("%d", &year);
temp = (struct btnode *)malloc(1*sizeof(struct btnode));
strcpy(temp->value1,type);
strcpy(temp->value2,company);
temp->value3=year;
temp->l = temp->r = NULL;
}
void search(struct btnode *t)
{
if ((temp->value3 > t->value3) && (t->r != NULL)) /* value more than root node value
insert at right */
search(t->r);
else if ((temp->value3 > t->value3) && (t->r == NULL))
t->r = temp;
else if ((temp->value3 < t->value3) && (t->l != NULL)) /* value less than root node
value insert at left */
search(t->l);
else if ((temp->value3 < t->value3) && (t->l == NULL))
t->l = temp;
}
void inorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display");
return;
}
if (t->l != NULL)
inorder(t->l);
puts(t->value1);
puts(t->value2);
printf("%d -> ", t->value3);
if (t->r != NULL)
inorder(t->r);
}
void delete()
{
int year;

32 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

if (root == NULL)
{
printf("No elements in a tree to delete");
return;
}
printf("Enter the year of data to be deleted : ");
scanf("%d", &year);
t1 = root;
t2 = root;
search1(root, year);
}
void preorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display");
return;
}
puts(t->value1);
puts(t->value2);
printf("%d -> ", t->value3);
if (t->l != NULL)
preorder(t->l);
if (t->r != NULL)
preorder(t->r);
}
void postorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display ");
return;
}
if (t->l != NULL)
postorder(t->l);
if (t->r != NULL)
postorder(t->r);
puts(t->value1);
puts(t->value2);
printf("%d -> ", t->value3);
}
void search1(struct btnode *t, int year)
{

33 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

if ((year>t->value3))
{
t1 = t;
search1(t->r, year);
}
else if ((year < t->value3))
{
t1 = t;
search1(t->l, year);
}
else if ((year==t->value3))
{
delete1(t);
}
}
void delete1(struct btnode *t)
{
int k;
if ((t->l == NULL) && (t->r == NULL))
{
if (t1->l == t)
{
t1->l = NULL;
}
else
{
t1->r = NULL;
}
t = NULL;
free(t);
return;
}
else if ((t->r == NULL))
{
if (t1 == t)
{
root = t->l;
t1 = root;
}
else if (t1->l == t)
{
t1->l = t->l;

34 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

}
else
{
t1->r = t->l;
}
t = NULL;
free(t);
return;
}
else if (t->l == NULL)
{
if (t1 == t)
{
root = t->r;
t1 = root;
}
else if (t1->r == t)
t1->r = t->r;
else
t1->l = t->r;
t = NULL;
free(t);
return;
}
else if ((t->l != NULL) && (t->r != NULL))
{
t2 = root;
if (t->r != NULL)
{
k = smallest(t->r);
flag = 1;
}
else
{
k =largest(t->l);
flag = 2;
}
search1(root, k);
t->value3 = k;
}

}
int smallest(struct btnode *t)

35 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

{
t2 = t;
if (t->l != NULL)
{
t2 = t;
return(smallest(t->l));
}
else
return (t->value3);
}
int largest(struct btnode *t)
{
if (t->r != NULL)
{
t2 = t;
return(largest(t->r));
}
else
return(t->value3);
}

36 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #9

37 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

University Experiment #10


Aim: To implement Insertion sort, Merge sort, Quick sort, Bubble sort, Bucket sort,
Radix sort, Shell sort, Selection sort, Heap sort and Exchange sort using array as a data
structure

Insertion sort

#include<stdio.h>
#include<conio.h>
void InsertionSort(int *array , int number_of_elements)
{ int iter,jter;
for(iter=1;iter<number_of_elements;iter++)
{ int current_element = array[iter];
jter = iter-1;
while(jter>=0 && array[jter] > current_element)
{ array[jter+1] = array[jter];
jter--;
}
array[jter+1] = current_element;
}
}
int main()
{ int number_of_elements;
int array[30];
int iter;
clrscr();
printf("Enter no. of elements you want to add") ;
scanf("%d",&number_of_elements);
for(iter = 0;iter < number_of_elements;iter++)
{ scanf("%d",&array[iter]);
}
InsertionSort(array,number_of_elements);
for(iter = 0;iter < number_of_elements;iter++)
printf("%d ",array[iter]);
printf("\n");
return 0;
getch();
}

38 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Merge Sort

#include<stdio.h>
#include<conio.h>
void Merge(int * , int , int , int );
void MergeSort(int *array, int left, int right)
{ int mid = (left+right)/2;
if(left<right)
{ MergeSort(array,left,mid);
MergeSort(array,mid+1,right);
Merge(array,left,mid,right);
}
}
void Merge(int *array, int left, int mid, int right)
{ int tempArray[60];
int pos=0,lpos = left,rpos = mid + 1;
int iter;
while(lpos <= mid && rpos <= right)
{ if(array[lpos] < array[rpos])
tempArray[pos++] = array[lpos++];
else
tempArray[pos++] = array[rpos++];
}
while(lpos <= mid) tempArray[pos++] = array[lpos++];
while(rpos <= right)tempArray[pos++] = array[rpos++];
for(iter = 0;iter < pos; iter++)
array[iter+left] = tempArray[iter];
return;
}
int main()
{ int number_of_elements;
int iter;
int array[30];
clrscr();
printf("Enter no. of elements you want to sort: ");
scanf("%d",&number_of_elements);
for(iter = 0;iter < number_of_elements;iter++)
scanf("%d",&array[iter]);
MergeSort(array,0,number_of_elements-1);
for(iter = 0;iter < number_of_elements;iter++)
printf("%d ",array[iter]);
printf("\n");
return 0; }

39 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Quick Sort

#include<stdio.h>
#include<conio.h>
void QuickSort(int *array, int from, int to)
{ int i = from, j, pivot,temp;
if(from>=to)return;
pivot = array[from];
for(j = from + 1;j <= to;j++)
{ if(array[j] < pivot)
{ i = i + 1;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
temp = array[i];
array[i] = array[from];
array[from] = temp;
QuickSort(array,from,i-1);
QuickSort(array,i+1,to);
}
int main()
{ int number_of_elements;
int array[30];
int iter;
clrscr();
printf("Enter no. of elements you want to sort");
scanf("%d",&number_of_elements);
for(iter = 0;iter < number_of_elements;iter++)
scanf("%d",&array[iter]);
QuickSort(array,0,number_of_elements-1);
for(iter = 0;iter < number_of_elements;iter++)
printf("%d ",array[iter]);
printf("\n");
return 0;
}

Bubble sort

#include<stdio.h>
#include<conio.h>
void BubbleSort(int *array,int number_of_elements)

40 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

{ int iter, temp, swapped;


do { swapped = 0;
for(iter = 1; iter < number_of_elements; iter++)
{ if(array[iter-1] > array[iter])
{ temp = array[iter-1];
array[iter-1] = array[iter];
array[iter] = temp;
swapped = 1;
}
}
}while(swapped);
}
int main()
{ int number_of_elements;
int iter;
int array[30];
clrscr();
printf(" Enter no. of element you want to sort : ");
scanf("%d",&number_of_elements);
for(iter = 0;iter < number_of_elements;iter++)
scanf("%d",&array[iter]);
BubbleSort(array,number_of_elements);
for(iter = 0;iter < number_of_elements;iter++)
printf("%d ",array[iter]);
printf("\n");
return 0;
}

Bucket sort

#include<stdio.h>
#include<conio.h>
void Bucket_Sort(int array[], int n)
{ int i, j;
int count[30];
for(i=0; i < n; i++)
count[i] = 0;
for(i=0; i < n; i++)
(count[array[i]])++;

for(i=0,j=0; i < n; i++)


for(; count[i]>0;(count[i])--)
array[j++] = i; }

41 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

}
int main()
{ int array[100];
int num;
int i;
clrscr();
printf("Enter How many Numbers : ");
scanf("%d",&num);
printf("Enter the %d elements to be sorted:\n",num);
for(i = 0; i < num; i++ )
scanf("%d",&array[i]);
printf("\nThe array of elements before sorting : \n");
for (i = 0;i < num;i++)
printf("%d ", array[i]);
printf("\nThe array of elements after sorting : \n");
Bucket_Sort(array, num);
for (i = 0;i < num;i++)
printf("%d ", array[i]);
printf("\n");
return 0;
getch();
}

Radix sort

#include <stdio.h>
#include<conio.h>
#define MAX 20
#define SHOWPASS
#define BASE 10
void print(int *a, int n)
{ int i;
for (i = 0; i < n; i++)
printf("%d\t", a[i]);
}
void radixsort(int *a, int n)
{ int i, b[MAX], m = a[0], exp = 1;
for (i = 1; i < n; i++)
{ if (a[i] > m)
m = a[i];
}
while (m / exp > 0)
{ int bucket[BASE] = { 0 };

42 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

for (i = 0; i < n; i++)


bucket[(a[i] / exp) % BASE]++;
for (i = 1; i < BASE; i++)
bucket[i] += bucket[i - 1]; //similar to count sort algorithm i.e. c[i]=c[i]+c[i-1];
for (i = n - 1; i >= 0; i--)
b[--bucket[(a[i] / exp) % BASE]] = a[i];
for (i = 0; i < n; i++)
a[i] = b[i];
exp *= BASE;
#ifdef SHOWPASS
printf("\nPASS : ");
print(a, n);
#endif
}
}
int main()
{ int arr[MAX];
int i, n;
clrscr();
printf("Enter total elements (n <= %d) : ", MAX);
scanf("%d", &n);
n = n < MAX ? n : MAX;
printf("Enter %d Elements : ", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("\nARRAY : ");
print(&arr[0], n);
radixsort(&arr[0], n);
printf("\nSORTED : ");
print(&arr[0], n);
printf("\n");
return 0;
getch();
}

Shell sort

#include<stdio.h>
#include<conio.h>
void ShellSort(int *array, int number_of_elements)
{ int iter,i,j,jter, increment, temp;
for(increment = number_of_elements/2;increment > 0; increment /= 2)
{ for(i = increment; i<number_of_elements; i++)

43 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

{ temp = array[i];
for(j = i; j >= increment ;j-=increment)
{ if(temp < array[j-increment])
{ array[j] = array[j-increment];
}
else
break;
}
array[j] = temp;
}
}
}
int main()
{ int number_of_elements;
int array[30];
int iter;
clrscr();
printf("Enter no. of elements you want to sort: ");
scanf("%d",&number_of_elements);
for(iter = 0;iter < number_of_elements;iter++)
scanf("%d",&array[iter]);
ShellSort(array,number_of_elements);
for(iter = 0;iter < number_of_elements;iter++)
printf("%d ",array[iter]);
printf("\n");
return 0;
getch();
}

Selection sort

#include<stdio.h>
#include<conio.h>
void SelectionSort(int *array,int number_of_elements)
{ int iter,jter,minIndex,temp;
for(iter = 0;iter<number_of_elements;iter++)
{ minIndex = iter;
for(jter = iter+1; jter<number_of_elements;jter++)
{ if(array[jter] < array[minIndex])
minIndex = jter;
}
temp = array[iter];
array[iter] = array[minIndex];

44 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

array[minIndex] = temp;
}
}
int main()
{ int number_of_elements;
int array[30];
int iter;
clrscr();
printf("Enter no. of elements you want to sort: ");
scanf("%d",&number_of_elements);
for(iter = 0;iter < number_of_elements;iter++)
scanf("%d",&array[iter]);
SelectionSort(array,number_of_elements);
for(iter = 0;iter < number_of_elements;iter++)
printf("%d ",array[iter]);
printf("\n");
return 0;
}

Heap sort
#include <stdio.h>
#include<conio.h>
void main()
{ int heap[10], no, i, j, c, root, temp;
clrscr();
printf("\n Enter no of elements :");
scanf("%d", &no);
printf("\n Enter the nos : ");
for (i = 0; i < no; i++)
scanf("%d", &heap[i]);
for (i = 1; i < no; i++)
{ c = i;
do
{ root = (c - 1) / 2;
if (heap[root] < heap[c])
{ temp = heap[root];
heap[root] = heap[c];
heap[c] = temp;
}
c = root;
} while (c != 0);
}
printf("Heap array : ");

45 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

for (i = 0; i < no; i++)


printf("%d\t ", heap[i]);
for (j = no - 1; j >= 0; j--)
{ temp = heap[0];
heap[0] = heap[j ];
heap[j] = temp;
root = 0;
do
{ c = 2 * root + 1;
if ((heap[c] < heap[c + 1]) && c < j-1)
c++;
if (heap[root]<heap[c] && c<j)
{ temp = heap[root];
heap[root] = heap[c];
heap[c] = temp;
}
root = c;
} while (c < j);
}
printf("\n The sorted array is : ");
for (i = 0; i < no; i++)
printf("\t %d", heap[i]);
}

Exchange sort

#include<stdio.h>
#include <conio.h>
int main(void)
{ clrscr();
int array[5];
int length = 5;
int i, j;
int temp;
printf("Enter a four numbers: ");
for (i = 0; i <5; i++)
scanf(“%d”, array[i]);

for(i = 0; i < (length -1); i++)


{
for (j=(i + 1); j < length; j++)
{
if (array[i] < array[j])
{

46 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
Printf("exchange sort\n");
for (i = 0; i < 5; i++)
{
Printf(“%d\n”,array[i] );
}
getch();
}

47 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #10


Insertion sort

Merge Sort

48 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Quick Sort

Bubble sort

49 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Bucket sort

Radix sort

50 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Shell sort

Selection sort

51 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Heap sort

Exchange sort

52 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Practice Experiment #1
AIM: WAP to insert an element at user defined position in an array of type float.
(Unsorted)

#include<stdio.h>
#include<conio.h>
void main()
{
float a[20];
int n,i,j,k,m;
clrscr();
printf("\n Enter the array length(max. 20):");
scanf("%d",&n);
printf("\n Enter the array elements: ");
for(i=0;i<n;i++)
scanf("%f",&a[i]);
printf("\n Enter the position: ");
scanf("%d", &k);
printf("\n Enter the element you want to insert..");
scanf("%d",&m);
n=n+1;
for(j=n;j>=k;j--)
{
a[j]=a[j-1];
}
a[j]=m;
printf(" \n The new list is :");
for(i=0;i<n;i++)
{
printf( "\n %.2f ",a[i]);
}
getch();
}

53 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #1

54 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Practice Experiment #2
AIM: WAP to insert an element in an array of type float. (Sorted)

#include<stdio.h>
#include<conio.h>
void main( )
{
float a[20];
int n,item,i;
clrscr();
printf("Enter the size of the array(max. 20):");
scanf("%d",&n);
printf("Enter elements of the array in the sorted order:");
for(i=0; i<n; i++)
{
scanf("%f", &a[i]);
}
printf("\nEnter ITEM to be inserted : ");
scanf("%d", &item);
i=n-1;
while(item<a[i] && i>=0)
{
a[i+1]=a[i];
i--;
}
a[i+1] = item;
n=n+1;
printf("\n\nAfter inserting element in array:\n");
for(i=0;i<n;i++)
{
printf("\n%.2f", a[i]);
}
getch();
}

55 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #2

56 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Practice Experiment #3
AIM: WAP to delete an element from user defined position in an array of type float.

#include<stdio.h>
#include<conio.h>
void main()
{
float a[20];
int n,i,k,m;
clrscr();
printf("\n Enter the array length(max. 20):");
scanf("%d",&n);
printf("\n Enter the array elements: ");
for(i=0;i<n;i++)
scanf("%f",&a[i]);
printf("\n Enter the element you want to delete..");
scanf("%d",&m);
for(i=0;i<n;i++)
if(a[i]==m)
{
k=i;
break;
}
for(i=k;i<n;i++)
{
a[i]=a[i+1];
}
a[n-1]=0;
printf(" \n The new list is :");
for(i=0;i<n;i++)
{
printf( "\n %.2f ",a[i]);
}
getch();
}

57 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #3

58 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Practice Experiment #4
AIM: WAP to add two polynomials using array.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10], b[10], c[10],m,n,k,k1,i,j,x;
clrscr();
printf("\n\tPolynomial Addition\n");
printf("\t= = = = = = = = = = = = = = = = = = =\n");
printf("\n\tEnter the no. of terms of 1st polynomial:");
scanf("%d", &m);
printf("\n\tEnter the degrees and coefficients:");
for (i=0;i<2*m;i++)
scanf("%d", &a[i]);
printf("\n\tFirst polynomial is:");
k1=0;
if(a[k1+1]==1)
printf("x^%d", a[k1]);
else
printf("%dx^%d", a[k1+1],a[k1]);
k1+=2;
while (k1<i)
{
printf("+%dx^%d", a[k1+1],a[k1]);
k1+=2;
}
printf("\n\n\n\tEnter the no. of terms of 2nd polynomial:");
scanf("%d", &n);
printf("\n\tEnter the degrees and co-efficients:");
for(j=0;j<2*n;j++)
scanf("%d", &b[j]);
printf("\n\tSecond polynomial is:");
k1=0;
if(b[k1+1]==1)
printf("x^%d", b[k1]);
else
printf("%dx^%d",b[k1+1],b[k1]);
k1+=2;
while (k1<2*n)

59 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

{
printf("+%dx^%d", b[k1+1],b[k1]);
k1+=2;
}
i=0;
j=0;
k=0;
while (m>0 && n>0)
{
if (a[i]==b[j])
{
c[k+1]=a[i+1]+b[j+1];
c[k]=a[i];
m--;
n--;
i+=2;
j+=2;
}
else if (a[i]>b[j])
{
c[k+1]=a[i+1];
c[k]=a[i];
m--;
i+=2;
}
else
{
c[k+1]=b[j+1];
c[k]=b[j];
n--;
j+=2;
}
k+=2;
}
while (m>0)
{
c[k+1]=a[i+1];
c[k]=a[i];
k+=2;
i+=2;
m--;
}
while (n>0)

60 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

{
c[k+1]=b[j+1];
c[k]=b[j];
k+=2;
j+=2;
n--;
}
printf("\n\n\n\n\tSum of the two polynomials is:");
k1=0;
if (c[k1+1]==1)
printf("x^%d", c[k1]);
else
printf("%dx^%d", c[k1+1],c[k1]);
k1+=2;
while (k1<k)
{
if (c[k1+1]==1)
printf("+x^%d", c[k1]);
else
printf("+%dx^%d", c[k1+1], c[k1]);
k1+=2;
}
getch();
}

61 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #4

62 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Practice Experiment #5
AIM: WAP to create a Matrix. Perform Addition, Transpose and Multiplication using
Switch-Case statement.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,i,k,j,c1,c2,r1,r2;
int m1[3][3],m2[3][3],m3[3][3];
clrscr();
while(1)
{
printf("\n 1. Transpose of Matrix:-\n");
printf("\n 2. Addition of Matrix:-\n");
printf("\n 3. Multiplication of Matrix:-\n");
printf("\n 4. Exit\n");
printf("\n Enter your choice:-");
scanf("%d",&a);
switch(a)
{
case 1 : printf("\n Enter the element of array :-");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
scanf("%d",&m1[i][j]);
m2[j][i]=m1[i][j];
}
printf("\n Transpose of Matrix is:-\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("\t%d",m2[i][j]);
printf("\n");
}
break;
case 2: printf("\n Input Matrix one:-");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&m1[i][j]);
printf("\n Input Matrix two:-");

63 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&m2[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
m3[i][j]=m1[i][j]+ m2[i][j];
printf("\n The sum is:-\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%5d",m3[i][j]);
printf("\n");
}
break;
case 3: printf("\n Input value of Matrix one:-");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&m1[i][j]);
printf("\n Input value of Matrix two:-");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&m2[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
m3[i][j]=0;
for(k=0;k<3;k++)
m3[i][j]=m3[i][j]+(m1[i][k]*m2[k][j]);
}
printf("\n Multiplication of Matrix:-\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("\t%d",m3[i][j]);
printf("\n");
}
break;
case 4: exit(0);
break;
}
}
getch();
}

64 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #5

65 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

66 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Practice Experiment #6
AIM: WAP to perform addition of two Polynomials using Circular Linked List.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<math.h>
typedef struct node
{ int expo,coef;
struct node *next;
}node;
node * insert(node *,int,int);
node * create();
node * add(node *p1,node *p2);
void display(node *head);
node *insert(node*head,int expo1,int coef1)
{ node *p,*q;
p=(node *)malloc(sizeof(node));
p->expo=expo1;
p->coef=coef1;
p->next=NULL;
if(head==NULL)
{ head=p;
head->next=head;
return(head);
}
if(expo1>head->expo)
{ p->next=head->next;
head->next=p;
head=p;
return(head);
}
if(expo1==head->expo)
{
head->coef=head->coef+coef1;
return(head);
}
q=head;
while(q->next!=head&&expo1>=q->next->expo)
q=q->next;
if(p->expo==q->expo)

67 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

q->coef=q->coef+coef1;
else
{ p->next=q->next;
q->next=p;
}
return(head);
}
node *create()
{ int n,i,expo1,coef1;
node *head=NULL;
printf("\nEnter no of terms of polynomial==>");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("\nEnter coef & expo==>");
scanf("%d%d",&coef1,&expo1);
head=insert(head,expo1,coef1);
}
return(head);
}
node *add(node *p1,node *p2)
{ node *p;
node *head=NULL;
printf("\nAddition of polynomial==>");
p=p1->next;
do { head=insert(head,p->expo,p->coef);
p=p->next;
}while(p!=p1->next);
p=p2->next;
do { head=insert(head,p->expo,p->coef);
p=p->next;
}while(p!=p2->next);
return(head);
}
void display(node *head)
{ node *p,*q;
int n=0;
q=head->next;
p=head->next;
do { n++;
q=q->next;
}while(q!=head->next);
printf("\n\tThe polynomial is==>");
do { if(n-1)

68 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

{ printf("%dx^(%d) + ",p->coef,p->expo);
p=p->next;
}
else
{ printf(" %dx^(%d)",p->coef,p->expo);
p=p->next; }
n--;
} while(p!=head->next);
}
void main()
{ node *p1,*p2,*p3;
p1=p2=p3=NULL;
clrscr();
p1=create();
display(p1);
p2=create();
display(p2);
p3=add(p1,p2);
display(p3);
getch();
}

69 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #6

70 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Practice Experiment #7
AIM: WAP to implement push and pop operations in a stack using an array. The array
should be storing the roll numbers of the students in the integer form. Separate
functions for display, push and pop should be designed with appropriate arguments.
The pop function should return the element which is poped out.

#include<stdio.h>
#include<conio.h>
#include<process.h>
#define size 5
int item;
int s[10];
int top;
void push()
{ if(top==size-1)
{ printf("Stack is full\n");
return;
}
printf("\nEnter item:");
scanf("%d",&item);
s[++top]=item;
}
void pop()
{ if(top==-1)
{ printf("\nstack is empty");
return;
}
printf("\nDeleted item is: %d",s[top]);
top--;
}
void display()
{ int i;
if(top==-1)
{ printf("\nstack is empty");
return;
}
printf("\nContent of stack is:");
for(i=0;i<=top;i++)
printf("%d\t",s[i]);
}
void main()

71 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

{ int ch;
top=-1;
clrscr();
for(;;)
{ printf("\n1.push\t\t2.pop\n3.display\t4.exit\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{case 1:push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong entry ! try again");
break;
}
}
getch();
}

72 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #7

73 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Practice Experiment #8
AIM: WAP to convert Infix Expression to Postfix Form using stack.

#define SIZE 50
#include<conio.h>
#include <ctype.h>
char s[SIZE];
int top=-1;
void push(char elem)
{ s[++top]=elem;
}
char pop()
{ return(s[top--]);
}
int pr(char elem)
{ switch(elem)
{ case '#': return 0;
case '(': return 1;
case '+':
case '-': return 2;
case '*':
case '/': return 3;
}
}
void main()
{ char infx[50],pofx[50],ch,elem;
int i=0,k=0;
clrscr();
printf("\n\nRead the Infix Expression - ");
scanf("%s",infx);
push('#');
while( (ch=infx[i++]) != '\0')
{ if( ch == '(')
push(ch);
else if(isalnum(ch))
pofx[k++]=ch;
else if( ch == ')')
{ while( s[top] != '(')
pofx[k++]=pop();
elem=pop();
}

74 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

else
{ while( pr(s[top]) >= pr(ch) )
pofx[k++]=pop();
push(ch);
}
}
while( s[top] != '#')
pofx[k++]=pop();
pofx[k]='\0';
printf("\n\nGiven Infix Expn: %s and its Postfix Expn: %s\n",infx,pofx);
getch();
}

75 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #8

76 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Practice Experiment #9
AIM: WAP to convert Infix Expression to Prefix Form using stack.

#define SIZE 50
#include<conio.h>
#include<string.h>
#include <ctype.h>
char s[SIZE];
int top=-1;
void push(char elem)
{ s[++top]=elem;
}
char pop()
{ return(s[top--]);
}
int pr(char elem)
{ switch(elem)
{ case '#': return 0;
case ')': return 1;
case '+':
case '-': return 2;
case '*':
case '/': return 3;
}
}
void main()
{ char infx[50],prfx[50],ch,elem;
int i=0,k=0;
clrscr();
printf("\n\nRead the Infix Expression - ");
scanf("%s",infx);
push('#');
strrev(infx);
while( (ch=infx[i++]) != '\0')
{ if( ch == ')') push(ch);
else if(isalnum(ch)) prfx[k++]=ch;
else if( ch == '(')
{ while( s[top] != ')')
prfx[k++]=pop();
elem=pop();
}

77 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

else
{ while( pr(s[top]) >= pr(ch) )
prfx[k++]=pop();
push(ch);
}
}
while( s[top] != '#')
prfx[k++]=pop();
prfx[k]='\0';
strrev(prfx);
strrev(infx);
printf("\n\nGiven Infix Expn: %s and Prefix Expn: %s\n",infx,prfx);
getch();
}

78 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #9

79 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Practice Experiment #10


AIM: WAP to implement insert and delete operations in a queue using an array. The
array should be storing the employee numbers of the employees in the integer form.
Separate functions for display, insert and delete should be designed with appropriate
arguments.

#include<stdio.h>
#include<conio.h>
#define MAX 10
int queue[MAX],front=-1,rear=-1;
void insert_element();
void delete_element();
void display_queue();
void main()
{ int option;
clrscr();
printf(">>> c program to implement queue operations <<<");
printf("\n\n 1.Insert an element");
printf("\n 2.Delete an element");
printf("\n 3.Display queue");
printf("\n 4.Exit");
do { printf("\n Enter your choice: ");
scanf("%d",&option);
switch(option)
{ case 1: insert_element();
break;
case 2: delete_element();
break;
case 3: display_queue();
break;
case 4: exit(0);
default:printf("Wrong choice");
break;
}
}while(option!=4);
getch();
}
void insert_element()
{ int num;
printf("\n Enter the number to be inserted: ");
scanf("%d",&num);

80 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

if(front==0 && rear==MAX-1)


printf("\n Queue OverFlow Occured");
else if(front==-1&&rear==-1)
{ front=rear=0;
queue[rear]=num;
}
else if(rear==MAX-1 && front!=0)
{ rear=0;
queue[rear]=num;
}
else
{ rear++;
queue[rear]=num;
}
}
void delete_element()
{ int element;
if(front==-1)
{ printf("\n Underflow"); }
element=queue[front];
if(front==rear)
front=rear=-1;
else
{ if(front==MAX-1)
front=0;
else
front++;
printf("\n The deleted element is: %d",element);
}
}
void display_queue()
{ int i;
if(front==-1)
printf("\n No elements to display");
else
{ printf("\n The queue elements are:\n ");
for(i=front;i<=rear;i++)
{ printf("\t %d",queue[i]);
}
}
}

81 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #10

82 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Practice Experiment #11


AIM: WAP to implement insertion and deletion operations on a circular queue using
linked list and each node of the linked list should store information about the lab with
name of the lab and number of computers in that lab. Separate functions should be
designed to insert and display information in the queue.

#include<stdio.h>
#include<malloc.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct que
{ int info;
char name[30];
struct que *next; };
void inqueue(struct que**,struct que**,int,char []);
void delqueue(struct que**,struct que**);
void display(struct que*,struct que*);
void main()
{
struct que *front, *rear;
int choice, item;
char labname[30];
clrscr();
front = NULL;
rear = NULL;
printf("\n\n\n\t MAIN MENU FOR CIRCULAR QUEUE OPERATIONS");
printf("\n\t 1. Insert in Queue ");
printf("\n\t 2. Delete from Queue ");
printf("\n\t 3. Display the Queue ");
printf("\n\t 4. Exit ");
do
{
printf("\n\t Enter choice : ");
scanf(" %d",&choice);
switch(choice)
{
case 1: printf("\n Enter lab name and no. of computers: ");
scanf("%s %d",&labname,&item);
inqueue(&front,&rear,item,labname);
display(front,rear);

83 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

break;
case 2: delqueue(&front,&rear);
display(front,rear);
break;
case 3: display(front,rear);
break;
default:printf("\n End of program ");
}
}while(choice !=4);
getch();
}

void inqueue(struct que **front,struct que **rear,int x,char n[])


{
struct que *nw;
nw=(struct que*)malloc(sizeof(struct que));
nw->info = x;
strcpy(nw->name,n);
if((*front)==NULL)
(*front)=nw;
else
(*rear)->next=nw;
(*rear)=nw;
(*rear)->next=*front;
}
void delqueue(struct que **front,struct que **rear)
{
struct que *save;
if((*front)==NULL)
printf("\n Queue is already empty, cannot delete");
else if ((*front)== (*rear))
{
save=(*front);
(*front)=NULL;
(*rear)=NULL;
printf("\n\t After deleting this node now queue has become empty\n");
}
else
{
save = *front;
(*front)=(*front)->next;
(*rear)->next=*front;
free(save);

84 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

}
}
void display(struct que *front,struct que *rear)
{
printf ("\n Now queue is : ");
if (front==NULL)
printf(" Queue is empty, nothing to display");
else
{
if(front==rear)
printf(" ->%s %d",front->name,front->info);
else
{
while(front != rear)
{
printf(" ->%s %d",front->name,front->info);
front=front->next;
}
printf(" ->%s %d",rear->name,rear->info);
}
}
}

85 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #11

86 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Practice Experiment #12


AIM: WAP to Create a Binary Tree without Recursion and perform Preorder,
Postorder and Inorder traversal on that tree.

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

typedef struct bint


{
int data,flag;
struct bint *left,*right;
}node;

node * create(node *r,int d)


{
if(r == NULL)
{
r = (node *)malloc(sizeof(node));
r->data = d;
r->left = r->right = NULL;
}
else
{
if(r->data <= d)
r->right = create(r->right,d);
else
r->left = create(r->left,d);
}
return r;
}

void non_in(node *r)


{
int top=0;
node *s[20],*pt=r;
s[0]=NULL;
while(pt != NULL)
{
s[++top] = pt;
pt = pt->left;

87 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

}
pt = s[top--];
while(pt != NULL)
{
printf("%d\t",pt->data);
if(pt->right != NULL)
{
pt = pt->right;
while(pt != NULL)
{
s[++top] = pt;
pt = pt->left;
}
}
pt = s[top--];
}
}

void non_pre(node *r)


{
int top=0;
node *s[20],*pt=r;
s[0]=NULL;
while(pt != NULL)
{
printf("%d\t",pt->data);
if(pt->right != NULL)
s[++top] = pt->right;
if(pt->left != NULL)
pt = pt->left;
else
pt = s[top--];
}
}

void postorder(node *r)


{
if(r != NULL)
{
postorder(r->left);
postorder(r->right);
printf("%d\t",r->data);
}

88 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

void non_post(node *root)


{
node *stack[100];
int top=-1;
node *temp=root;
while(temp!=NULL)
{
while(temp!= NULL)
{
top++;
stack[top] = temp;
temp = temp->left;
}
label:temp =stack[top];
top--;
if(temp->flag==1)
{
printf("%d\t",temp->data);
break;
}
else
{
temp->flag=1;
top++;
stack[top] = temp;
temp=temp->right;
}
}
if(top>=0)
goto label;
}
void main()
{
int d;
char ch = 'Y';
node *head = NULL;
clrscr();
while(toupper(ch) == 'Y')
{
printf("\n Enter the item to insert");
scanf("%d",&d);

89 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

head = create(head,d);
printf("\n Do you want to continue(y/n)");
fflush(stdin);
ch = getchar();
}
printf("\ninorder non recursive\n");
non_in(head);
printf("\npostorder non recursive\n");
non_post(head);
printf("\npreorder non recursive\n");
non_pre(head);
getch();
}

90 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #12

91 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Practice Experiment #13


AIM: WAP to Implement Insertion in a graph and then traversal in graph using
Breadth First Search.

#include <stdio.h>
#include <stdlib.h>
struct btnode
{
int value;
struct btnode *left, *right;
};
typedef struct btnode node;
void insert(node *, node *);
void bfs_traverse(node *);
node *root = NULL;
int val, front = 0, rear = -1, i;
int queue[20];

void main()
{
node *new = NULL ;
int num = 1;
printf("Enter the elements of the tree(enter 0 to exit)\n");
while (1)
{
scanf("%d", &num);
if (num == 0)
break;
new = malloc(sizeof(node));
new->left = new->right = NULL;
new->value = num;
if (root == NULL)
root = new;
else
{
insert(new, root);
}
}
printf("elements in a tree in inorder are\n");
queue[++rear] = root->value;
bfs_traverse(root);

92 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

for (i = 0;i <= rear;i++)


printf("%d -> ", queue[i]);
printf("%d\n", root->right->right->right->value);
}
void insert(node * new , node *root)
{
if (new->value>root->value)
{
if (root->right == NULL)
root->right = new;
else
insert (new, root->right);
}
if (new->value < root->value)
{
if (root->left == NULL)
root->left = new;
else
insert (new, root->left);
}
}
void bfs_traverse(node *root)
{
val = root->value;
if ((front <= rear)&&(root->value == queue[front]))
{
if (root->left != NULL)
queue[++rear] = root->left->value;
if (root->right != NULL || root->right == NULL)
queue[++rear] = root->right->value;
front++;
}
if (root->left != NULL)
{
bfs_traverse(root->left);
}
if (root->right != NULL)
{
bfs_traverse(root->right);
}
}

93 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #13

94 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Practice Experiment #14


AIM: WAP to Implement Insertion in a graph and then traversal in graph using Depth
First Search.

#include <stdio.h>
#include <stdlib.h>
struct node
{
int a;
struct node *left;
struct node *right;
};
void generate(struct node **, int);
void DFS(struct node *);
void delete(struct node **);
int main()
{
struct node *head = NULL;
int choice = 0, num, flag = 0, key;
do
{ printf("\nEnter your choice:\n1. Insert\n2. Perform DFS Traversal\n3. Exit\nChoice:
");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("Enter element to insert: ");
scanf("%d", &num);
generate(&head, num);
break;
case 2: DFS(head);
break;
case 3: delete(&head);
printf("Memory Cleared\nPROGRAM TERMINATED\n");
break;
default: printf("Not a valid input, try again\n");
}
} while (choice != 3);
return 0;
}
void generate(struct node **head, int num)
{

95 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

struct node *temp = *head, *prev = *head;


if (*head == NULL)
{
*head = (struct node *)malloc(sizeof(struct node));
(*head)->a = num;
(*head)->left = (*head)->right = NULL;
}
else
{
while (temp != NULL)
{
if (num > temp->a)
{ prev = temp;
temp = temp->right; }
else
{ prev = temp;
temp = temp->left; }
}
temp = (struct node *)malloc(sizeof(struct node));
temp->a = num;
if (num >= prev->a)
prev->right = temp;
else
prev->left = temp; }
void DFS(struct node *head)
{ if (head)
{ if (head->left)
DFS(head->left);
if (head->right)
DFS(head->right);
printf("%d ", head->a); } }
void delete(struct node **head)
{
if (*head != NULL)
{
if ((*head)->left)
delete(&(*head)->left);
if ((*head)->right)
delete(&(*head)->right);
free(*head);
}
}

96 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #14

97 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Practice Experiment #15


AIM: WAP to implement single source shortest path algorithm.

#include "stdio.h"
#include "conio.h"
#define infinity 999
void dij(int n,int v,int cost[10][10],int dist[])
{ int i,u,count,w,flag[10],min;
for(i=1;i<=n;i++)
flag[i]=0,dist[i]=cost[v][i];
count=2;
while(count<=n)
{ min=99;
for(w=1;w<=n;w++)
if(dist[w]<min && !flag[w])
min=dist[w],u=w;
flag[u]=1;
count++;
for(w=1;w<=n;w++)
if((dist[u]+cost[u][w]<dist[w]) && !flag[w])
dist[w]=dist[u]+cost[u][w]; } }

void main()
{ int n,v,i,j,cost[10][10],dist[10];
clrscr();
printf("\n Enter the number of nodes:");
scanf("%d",&n);
printf("\n Enter the cost matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{ scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=infinity; }
printf("\n Enter the source matrix:");
scanf("%d",&v);
dij(n,v,cost,dist);
printf("\n Shortest path:\n");
for(i=1;i<=n;i++)
if(i!=v)
printf("%d->%d,cost=%d\n",v,i,dist[i]);
getch(); }

98 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Output Experiment #15

99 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Practice Experiment #16


AIM: WAP to Implement all pair shortest path algorithm.

#include<stdio.h>
#include<conio.h>
#include<math.h>
int max(int,int);

void warshal(int p[10][10],int n)


{
int i,j,k;
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
p[i][j]=max(p[i][j],p[i][k]&&p[k][j]);
}
int max(int a,int b)
{
if(a>b)
return(a);
else
return(b);
}

void main()
{
int p[10][10]={0},n,e,u,v,i,j;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
printf("\n Enter the number of edges:");
scanf("%d",&e);
for(i=1;i<=e;i++)
{
printf("\n Enter the end vertices of edge %d:",i);
scanf("%d%d",&u,&v);
p[u][v]=1;
}
printf("\n Matrix of input data: \n");
for(i=1;i<=n;i++)
{

100 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

for(j=1;j<=n;j++)
printf("%d\t",p[i][j]);
printf("\n");
}
warshal(p,n);
printf("\n Transitive closure: \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",p[i][j]);
printf("\n");
}
getch();
}

101 Faculty Name: Namrata Sukhija


Data Structures Lab Solution Manual

Ouput Experiment #16

102 Faculty Name: Namrata Sukhija

Anda mungkin juga menyukai