0% found this document useful (0 votes)
80 views

Lab Manual FOR Data Structures (C-09) : Diploma in Computer Engineering

This document contains instructions and exercises for a data structures lab manual. It includes instructions for students on proper lab conduct as well as a list of exercises involving different data structure implementations like linked lists, stacks, queues, binary trees, and sorting algorithms. The exercises provided allow students to practice creating, inserting, deleting and displaying elements in singly linked lists, implementing stacks and queues, and performing sorting algorithms like selection, insertion and bubble sort.

Uploaded by

Anjali Naidu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Lab Manual FOR Data Structures (C-09) : Diploma in Computer Engineering

This document contains instructions and exercises for a data structures lab manual. It includes instructions for students on proper lab conduct as well as a list of exercises involving different data structure implementations like linked lists, stacks, queues, binary trees, and sorting algorithms. The exercises provided allow students to practice creating, inserting, deleting and displaying elements in singly linked lists, implementing stacks and queues, and performing sorting algorithms like selection, insertion and bubble sort.

Uploaded by

Anjali Naidu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

1

DIPLOMA IN COMPUTER ENGINEERING


LAB MANUAL
FOR
DATA STRUCTURES
(C- 09)






SANKETIKA POLYTECHNIC COLLEGE
AFFILIATED TO STATE BOARD OF EDUCATION AND TRAINING , HYDERABAD


2


INSTRUCTIONS TO STUDENTS

Do?s and Don? ts:

Do?s:
Be punctual
Follow dress code
Remove the foot wears
Keep personal belonging in allotted place
Submit the observation record of previous experiments
Use the allotted system and login as per instruction only
Take the printouts regularly
While leaving the lab, arrange the chairs you are seated in a proper way.
Help to maintain the cleanliness of the lab
Maintain strict discipline

Don?ts:

Don?t use the system for typing letters, reports etc., during lab hours
Don?t use others login
Don?t change the configuration and system settings
Don?t bring floppies, cds inside lab without permission
Don?t load unauthorized software





















3

Lab Title : Data Structures Lab Periods per Week : 04
Lab Code : CM ? 306 Periods per Semester: 60



LIST OF EXCERCISES

1. Exercises on creation, insertion, deletions & display of elements in a singly
linked List.
2. Write a program to implement a singly circular linked list
3. Exercises on creation, insertion, deletions & display of elements in a doubly
linked List.
4. Write a program to Implement a stack
5. Write a program to implement a queue
6. Write a program to create a sparse matrix
7. Write a program to create a binary tree & its traversal operations
8. Exercise on Selection sort
9. Exercise on insertion sort
10. Exercise on bubble sort
11. Implement a program for merge sort on two sorted lists of elements
12. Exercises on linear search
13. Exercise on binary search




















4

1.Write programs to create,insert,delete and display elements in a singly linked list

1a) Aim: Write a Program to Insert Node at the beginning of the singly Linked List

Source code:

/**** Program to Insert Node at the beginning of the singly Linked List ****/
#include <stdio.h>
void insert_first();
void display();
struct node
{
int info;
struct node *link;
}*start=NULL;
int item;
main()
{
int ch;
do
{
printf("\n\n\n1. Insert First\n2. Display\n3. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:insert_first();
break;
case 2:display();
break;
case 3:exit(0);
default:printf("\n\nInvalid choice. Please try again.\n");
}
}while (1);
}
void insert_first()
{
struct node *ptr;
printf("\n\nEnter item: ");
scanf("%d", &item);
if(start ==NULL)
{
start =(struct node *)malloc(sizeof(struct node));

5

start->info =item;
start->link =NULL;
}
else
{
ptr =start;
start =(struct node *)malloc(sizeof(struct node));
start->info =item;
start->link =ptr;
}
printf("\nItem inserted: %d\n", item);
}
void display()
{
struct node *ptr =start;
int i=1;
if (ptr ==NULL)
printf("\nLinked list is empty.\n");
else
{
printf("\nSr. No.\t\tAddress\t\tInfo\t\tLink\n");
while(ptr !=NULL)
{
printf("\n%d.\t\t%d\t\t%d\t\t%d\n", i, ptr, ptr->info,ptr->link);
ptr =ptr->link;
i++;
}
}














6


Output:

















7

1b) Aim: Write a Program to Insert Node at last in a singly Linked List

Source code:

/**** Program to insert Node at last in a singly Linked List ****/

#include <stdio.h>
void insert_last();
void display();
struct node
{
int info;
struct node *link;
}*start=NULL;
int item;
main()
{
int ch;
do
{
printf("\n\n\n1. Insert Last\n2. Display\n3. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: insert_last();
break;
case 2: display();
break;
case 3:exit(0);

default: printf("\n\nInvalid choice. Please try again.\n");
}
}while (1);
}
void insert_last()
{
struct node *ptr;
printf("\n\nEnter item: ");
scanf("%d", &item);
if(start ==NULL)

8

{
start =(struct node *)malloc(sizeof(struct node));
start->info =item;
start->link =NULL;
}
else
{
ptr =start;
while (ptr->link !=NULL)
ptr =ptr->link;
ptr->link =(struct node *)malloc(sizeof(struct node));
ptr =ptr->link;
ptr->info =item;
ptr->link =NULL;
}
}
void display()
{
struct node *ptr =start;
int i=1;
if (ptr ==NULL)
printf("\nLinklist is empty.\n");
else
{
printf("\nSr. No.\t\tAddress\t\tInfo\t\tLink\n");
while(ptr !=NULL)
{
printf("\n%d.\t\t%d\t\t%d\t\t%d\n", i, ptr, ptr->info, ptr->link);
ptr =ptr->link;
i++;
}

}
}












9

Output:






















10

1c) Aim: Write a Program to Insert Node at Specific position in a singly Linked List

Source code:

/**** Program to Insert a node at Specific position in a singly Linked List ****/
#include <stdio.h>
void insert_last();
void insert_specific();
void display();
struct node
{
int info;
struct node *link;
}*start=NULL;
int item;

main()
{
int ch;
do
{
printf("\n\n\n1. Insert Last\n2. Insert Specific\n3. Display\n4. exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{

case 1:insert_last();
break;
case 2:insert_specific();
break;
case 3:display();
break;
case 4:exit(0);
default: printf("\n\nInvalid choice. Please try again.\n");
}
}while (1);
}
void insert_last()
{
struct node *ptr;
printf("\n\nEnter item: ");
scanf("%d", &item);
if(start ==NULL)
{
start =(struct node *)malloc(sizeof(struct node));
start->info =item;

11

start->link =NULL;
}
else
{
ptr =start;
while (ptr->link !=NULL)
ptr =ptr->link;
ptr->link =(struct node *)malloc(sizeof(struct node));
ptr =ptr->link;
ptr->info =item;
ptr->link =NULL;
}
printf("\nItem inserted: %d\n", item);
}
void insert_specific()
{
int n;
struct node *nw, *ptr;
if (start ==NULL)
printf("\n\nLinked list is empty. It must have at least one node.\n");
else
{
printf("\n\nEnter INFO after which new node is to be inserted: ");
scanf("%d", &n);
printf("\n\nEnter ITEM: ");
scanf("%d", &item);
ptr =start;
nw =start;
while (ptr !=NULL)
{
if (ptr->info ==n)
{
nw =(struct node *)malloc(sizeof(struct node));
nw->info =item;
nw->link =ptr->link;
ptr->link =nw;
printf("\n\nItem inserted: %d", item);
return;
}
else
ptr =ptr->link;
}
}
}

void display()
{
struct node *ptr =start;

12

int i=1;
if (ptr ==NULL)
printf("\nLinklist is empty.\n");
else
{
printf("\nSr. No.\t\tAddress\t\tInfo\t\tLink\n");
while(ptr !=NULL)
{
printf("\n%d.\t\t%d\t\t%d\t\t%d\n", i, ptr, ptr->info, ptr->link);
ptr =ptr->link;
i++;
}
}
}

Output:



13

1d) Aim: Write a Program to Delete First Node in a singly Linked List

Source code:

/**** Program to Delete First Node in a singly Linked List ****/

#include <stdio.h>
void insert_last();
void delete_first();
void display();

struct node
{
int info;
struct node *link;
}*start=NULL;
int item;

main()
{
int ch;
do
{
printf("\n\n\n1. Insert Last\n2. Delete First\n3. Display\n4.Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{

case 1:insert_last();
break;

case 2:delete_first();
break;

case 3:display();
break;

case 4:exit(0);
default:printf("\n\nInvalid choice: Please try again.\n");
}
}while (1);
}

void insert_last()
{
struct node *ptr;
printf("\n\nEnter item: ");

14

scanf("%d", &item);
if(start ==NULL)
{
start =(struct node *)malloc(sizeof(struct node));
start->info =item;
start->link =NULL;
}
else

{
ptr =start;
while (ptr->link !=NULL)
ptr =ptr->link;
ptr->link =(struct node *)malloc(sizeof(struct node));
ptr =ptr->link;
ptr->info =item;
ptr->link =NULL;

}
printf("\nItem inserted: %d\n", item);
}

void delete_first()
{
struct node *ptr;
if (start ==NULL)
printf("\n\nLinked list is empty.\n");
else
{
ptr =start;
item =start->info;
start =start->link;
free(ptr);
printf("\n\nItem deleted: %d", item);
}
}

void display()
{
struct node *ptr =start;
int i=1;
if (ptr ==NULL)
printf("\nLinklist is empty.\n");
else

{

printf("\nSr. No.\t\tAddress\t\tInfo\t\tLink\n");

15

while(ptr !=NULL)
{
printf("\n%d.\t\t%d\t\t%d\t\t%d\n", i, ptr, ptr->info,ptr->link);
ptr =ptr->link;
i++;

}

}
}


Output:




16

1e)Aim:Write a Program to Delete Last Node in a Linked List

Source code:
/** Program to Delete Last Node in a singly Linked List ****/
#include <stdio.h>
void insert_last();
void delete_last();
void display();
struct node
{
int info;
struct node *link;
}*start=NULL;
int item;
main()
{
int ch;
do
{
printf("\n\n\n1. Insert Last\n2. Delete Last\n3. Display\n4.Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:insert_last();
break;
case 2:delete_last();
break;
case 3:display();
break;
case 4:exit(0);

default:printf("\n\nInvalid choice: Please try again.\n");
}
}while (1);
}
void insert_last()
{
struct node *ptr;
printf("\n\nEnter item: ");
scanf("%d", &item);

if(start ==NULL)
{
start =(struct node *)malloc(sizeof(struct node));
start->info =item;
start->link =NULL;
}

17

else
{
ptr =start;
while (ptr->link !=NULL)
ptr =ptr->link;
ptr->link =(struct node *)malloc(sizeof(struct node));
ptr =ptr->link;
ptr->info =item;
ptr->link =NULL;
}
printf("\nItem inserted: %d\n", item);
}
void delete_last()
{
struct node *ptr, *prev;
if (start ==NULL)
printf("\n\nLinked list is empty.\n");
else
{

ptr =start;
prev =start;

while (ptr->link !=NULL)
{
prev =ptr;
ptr =ptr->link;
}

item =ptr->info;

if (start->link ==NULL)
start =NULL;
else
prev->link =NULL;
prev->link =NULL;
free(ptr);
printf("\n\nItem deleted: %d", item);
}
}
void display()
{
struct node *ptr =start;
int i=1;
if (ptr ==NULL)
printf("\nLinklist is empty.\n");
else


18

{
printf("\nSr. No.\t\tAddress\t\tInfo\t\tLink\n");
while(ptr !=NULL)
{
printf("\n%d.\t\t%d\t\t%d\t\t%d\n", i, ptr, ptr->info,ptr->link);
ptr =ptr->link;
i++;
}
}
}

Output:






19

1f)Aim:Write a Program to Delete any Specific Node in a singly Linked List

Source code:

/**** Program to Delete any Specific Node in a singly Linked List ****/
#include <stdio.h>
void insert_last();
void delete_specific();
void display();
struct node
{
int info;
struct node *link;
}*start=NULL;
int item;
main()
{
int ch;
do
{
printf("\n\n\n1. Insert Last\n2. Delete Specific\n3. Display\n4.Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{

case 1:insert_last();
break;
case 2:delete_last();
break;
case 3:display();
break;
case 4:exit(0);
default:printf("\n\nInvalid choice: Please try again.\n");
}
}while (1);
}
void insert_last()
{
struct node *ptr;
printf("\n\nEnter item: ");
scanf("%d", &item);
if(start ==NULL)
{
start =(struct node *)malloc(sizeof(struct node));
start->info =item;
start->link =NULL;
}

20

else
{
ptr =start;
while (ptr->link !=NULL)
ptr =ptr->link;
ptr->link =(struct node *)malloc(sizeof(struct node));
ptr =ptr->link;
ptr->info =item;
ptr->link =NULL;
}
printf("\nItem inserted: %d\n", item);
}
void delete_specific()
{
struct node *ptr, *prev;

printf("\n\nEnter ITEM which is to be deleted: ");
scanf("%d", &item);
if (start ==NULL)
printf("\n\nLinked list is empty.\n");
else if (start->info ==item)
{
ptr =start;
start =start->link;
free(ptr);
}
else
{
ptr =start;
prev =start;
while (ptr !=NULL)
{
if (ptr->info ==item)
{
prev->link =ptr->link;
free(ptr);
}
else
{
prev =ptr;
ptr =ptr->link;
}
}
printf("\n\nItem deleted: %d", item);
}
}
void display()
{

21

struct node *ptr =start;
int i=1;
if (ptr ==NULL)
printf("\nLinklist is empty.\n");
else
{
printf("\nSr. No.\t\tAddress\t\tInfo\t\tLink\n");
while(ptr !=NULL)
{
printf("\n%d.\t\t%d\t\t%d\t\t%d\n", i, ptr, ptr->info,ptr->link);
ptr =ptr->link;
i++;
}
} }
Output:


22


2.Write a program to implement a singly circular linked list

Aim: Write a program to implement a singly circular linked list

Source code:
#include<alloc.h>
#include<conio.h>
#include<stdio.h>
struct node
{
int data;
struct node *next;
};
struct node *head=NULL;
struct node *tail=NULL;
void main()
{
void addrecord();
void deleterecord();
void disrecord();
int ch;
clrscr();
do
{
printf("\n 1. To add records\n");
printf("\n 2. To delete a records\n");
printf("\n 3. To view the records\n");
printf("\n 4. To exit\n");
printf("\n Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:addrecord();
break;
case 2:deleterecord();
break;
case 3: disrecord();
break;
case 4:exit(0);
}
}while (ch!=4);
}
void addrecord()
{
int new_data;
char ans='y';
struct node *ptr,*prev,*temp;

23

while (ans=='y')
{
temp=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the new element:\n");
scanf("%d",&new_data);
temp->data=new_data;
temp->next=NULL;
if (head==NULL)
{
head=tail=temp;
temp->next=head;
}
else
{
tail->next=temp;
tail=temp;
}
printf("\n Would you like to enter another data(y\\n): \n");
ans =getchar();
}
}
void deleterecord()
{
struct node *ptr,*prev,*delnode;
int elt;
printf("\n Enter the enrollment number to be deleted \n");
scanf("%d",&elt);
if (head==NULL)
{
printf("\n No elements in the list \n");
return;
}
else
{
if (head->data==elt)
{
delnode=head;
if (head==tail)
head=tail=NULL;
else
{
head=head->next;
tail->next=head;
}
}
else if (tail->data==elt)
{for(ptr=head;(ptr!=tail);prev=ptr,ptr=ptr->next);
delnode=tail;

24

tail=prev;
tail->next=head;
}
else
{
for(prev=ptr=head;(ptr->data!=elt)&&(ptr!=tail);
prev=ptr,ptr=ptr->next);
if(ptr->data==elt)
{
delnode=ptr;
prev->next=ptr->next;
printf("yes...");
}
else
{
printf("Given element not found in the list");
getch();
return;
}
}
}
free(delnode);
}
void disrecord()
{
struct node *ptr,*prev=NULL;
if (head==NULL)
{
printf("\n No records to view\n");
return;
}
printf("\n The elements in the circular list are\n");
for (ptr=head;prev!=tail;prev=ptr,ptr=ptr->next)
printf("\n\n %d",ptr->data);
printf(" NULL\n\n ");
getch();
}












25

Output:

1. To add records

2. To delete a records

3. To view the records

4. To exit

Enter your choice
1
Enter the new element:
10

Would you like to enter another data(y\n):

1. To add records

2. To delete a records

3. To view the records

4. To exit

Enter your choice
1
Enter the new element:
20

Would you like to enter another data(y\n):

1. To add records

2. To delete a records

3. To view the records

4. To exit

Enter your choice
1

Enter the new element:
30

Would you like to enter another data(y\n):


26

1. To add records

2. To delete a records

3. To view the records

4. To exit

Enter your choice
3
The elements in the circular list are

10

20

30 NULL

1. To add records

2. To delete a records

3. To view the records

4. To exit

Enter your choice
2

Enter the enrollment number to be deleted
20

yes...
1. To add records

2. To delete a records

3. To view the records

4. To exit

Enter your choice
3

The elements in the circular list are
10

30 NULL

27



3.Write programs to create,insert,delete and display elements in a doubly linked list

3a) Aim: Write a Program to insert element in the front of doubly linked list

Source code:
/*Program to insert element in the front of doubly linked list */
#include <stdio.h>
void insert_front();
void display();
struct node
{
int info;
struct node *link;
struct node *prev;
}*start=NULL;
int item;
main()
{
int ch;
do
{
printf("\n\n\n1. Insert front\n2.display\n3.exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: insert_front();
break;
case 2:display();
break;
case 3: exit(0);
default: printf("\n\nInvalid choice: Please try again.\n");
}
}while (1);
}
void insert_front()
{
struct node *ptr,*nw;
printf("\n\nEnter item: ");
scanf("%d", &item);

28

if (start ==NULL)
{
start =(struct node *)malloc(sizeof(struct node));
start->info =item;
start->link =NULL;
start->prev=NULL;
}
else
{
ptr =start;
nw=(struct node *)malloc(sizeof(struct node));
nw->prev =NULL;
nw->info =item;
nw->link =ptr ;
start=nw;
ptr=start;
ptr=ptr->link;
ptr->prev=start;
}
printf("\nItem inserted: %d\n", item);
}
void display()
{
struct node *ptr =start;
int i=1;
if (ptr ==NULL)
printf("\nLinklist is empty.\n");
else
{
printf("\nSr. No.\t\tPrev\t\tAddress\t\tInfo\t\tLink\n");
while(ptr !=NULL)
{
printf("\n%d.\t\t%d\t\t%d\t\t%d\t\t%d\n", i, ptr->prev,ptr, ptr->info,ptr->link);
ptr =ptr->link;
i++;
}
}
}





29


Output:
1. Insert front
2.display
3.exit

Enter your choice: 1

Enter item: 10
Item inserted: 10

1. Insert front
2.display
3.exit

Enter your choice: 1

Enter item: 20
Item inserted: 20


1. Insert front
2.display
3.exit

Enter your choice: 2

Sr. No. Prev Address Info Link

1. 0 3394 20 3378

2. 3394 3378 10 0


1. Insert front
2.display
3.exit

Enter your choice:1

Enter item: 30
Item inserted: 30

30


1. Insert front
2.display
3.exit

Enter your choice: 2

Sr. No. Prev Address Info Link

1. 0 3436 30 3420

2. 3436 3420 20 3404

3. 3420 3404 10 0

1. Insert front
2.display
3.exit

Enter your choice:























31


3b) Aim: Write a Program to insert element at last in a doubly linked list

Source code:
/*Program to insert element at last in a doubly linked list */
#include <stdio.h>
void insert_last ();
void display();
struct node
{
int info;
struct node *link;
struct node *prev;
}*start=NULL;
int item;
main()
{
int ch;
do
{
printf("\n\n\n1. Insert last\n2.display\n3.exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: insert_last ();
break;
case 2:display();
break;
case 3: exit(0);
default: printf("\n\nInvalid choice: Please try again.\n");
}
}while (1);
}
void insert_last()
{
struct node *ptr,*nw;
printf("\n\nEnter item: ");
scanf("%d", &item);
if (start ==NULL)
{
start =(struct node *)malloc(sizeof(struct node));
start->info =item;
start->link =NULL;
start->prev=NULL;
}

32

else
{
ptr =start;
while(ptr->link!=NULL)
ptr=ptr->link;
nw=(struct node *)malloc(sizeof(struct node));
nw->info =item;
nw->prev =ptr;
nw->link=NULL ;
ptr->link=nw;
}
printf("\nItem inserted: %d\n", item);
}
void display()
{
struct node *ptr =start;
int i=1;
if (ptr ==NULL)
printf("\nLinklist is empty.\n");
else
{
printf("\nSr. No.\t\tPrev\t\tAddress\t\tInfo\t\tLink\n");
while(ptr !=NULL)
{
printf("\n%d.\t\t%d\t\t%d\t\t%d\t\t%d\n", i, ptr->prev,ptr, ptr->info,ptr->link);
ptr =ptr->link;
i++;
}
}
}














33


Output:

1. Insert last
2.display
3.exit

Enter your choice: 1

Enter item: 10
Item inserted: 10

1. Insert last
2.display
3.exit

Enter your choice: 1

Enter item: 20
Item inserted: 20

1. Insert last
2.display
3.exit

Enter your choice: 2

Sr. No. Prev Address Info Link

1. 0 3378 10 3394

2. 3378 3394 20 0


1. Insert last
2.display
3.exit

Enter your choice:1

Enter item: 30

34

Item inserted: 30

1. Insert last
2.display
3.exit

Enter your choice: 2

Sr. No. Prev Address Info Link

1. 0 3378 10 3394

2. 3378 3394 20 3410

3. 3394 3410 30 0

1. Insert last
2.display
3.exit
Enter your choice:























35


3c) Aim: Write a Program to delete first element in a doubly linked list

Source code:
/* Program to delete first element in a doubly linked list*/
#include <stdio.h>
void insert_last ();
void delete_front()
void display();
struct node
{
int info;
struct node *link;
struct node *prev;
}*start=NULL;
int item;
main()
{
int ch;
do
{
printf("\n\n\n1. Insert last\n2. Delete front\n3.display\n4.exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: insert_last ();
break;
case 2: delete_front ();
break;
case 3:display();
break;
case 4: exit(0);
default: printf("\n\nInvalid choice: Please try again.\n");
}
}while (1);
}
void insert_last()
{
struct node *ptr,*nw;
printf("\n\nEnter item: ");
scanf("%d", &item);

36

if (start ==NULL)
{
start =(struct node *)malloc(sizeof(struct node));
start->info =item;
start->link =NULL;
start->prev=NULL;
}
else
{
ptr =start;
while(ptr->link!=NULL)
ptr=ptr->link;
nw=(struct node *)malloc(sizeof(struct node));
nw->info =item;
nw->prev =ptr;
nw->link=NULL ;
ptr->link=nw;
}
printf("\nItem inserted: %d\n", item);
}
void delete_front()
{
struct node *ptr;
if(start==NULL)
printf("linked list is empty");
else
{
ptr=start;
item=start->info;
start=start->link;
start->prev=NULL;
free(ptr);
printf("deleted item is %d",item);
}
}
void display()
{
struct node *ptr =start;
int i=1;
if (ptr ==NULL)
printf("\nLinklist is empty.\n");
else
{
printf("\nSr. No.\t\tPrev\t\tAddress\t\tInfo\t\tLink\n");
while(ptr !=NULL)
{

37

printf("\n%d.\t\t%d\t\t%d\t\t%d\t\t%d\n", i, ptr->prev,ptr, ptr->info,ptr->link);
ptr =ptr->link;
i++;
}
} }

Output:
1. Insert last
2.delete front
3. display
4.exit

Enter your choice: 1

Enter item: 10
Item inserted: 10


1. Insert last
2.delete front
3. display
4.exit

Enter your choice: 1

Enter item: 20
Item inserted: 20

1. Insert last
2.delete front
3. display
4.exit

Enter your choice: 1


Enter item: 30
Item inserted: 30

1. Insert last
2.delete front

38

3. display
4.exit

Enter your choice: 3

Sr. No. Prev Address Info Link

1. 0 3436 10 3452

2. 3436 3452 20 3468

3. 3452 3468 30 0


1. Insert last
2.delete front
3. display
4.exit

Enter your choice: 2
deleted item is 10

1. Insert last
2.delete front
3. display
4.exit

Enter your choice: 3

Sr. No. Prev Address Info Link

1. 0 3452 20 3468

2. 3452 3468 30 0

1. Insert last
2.delete front
3. display
4.exit

Enter your choice:

39

4.Write a program to implement a stack

4 a) Stack using arrays

Aim: Write a program to implement Stack using arrays

Source code:
/****** Program to Implement Stack using Array ******/
#include <stdio.h>
#define MAX 50
void push();
void pop();
void display();
int stack[MAX], top=-1, item;
main()
{
int ch;
do
{
printf("\n\n\n\n1.\tPush\n2.\tPop\n3.\tDisplay\n4.\tExit\n");
printf("\nEnter 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("\n\nInvalid entry. Please try again...\n");
}
}while(ch!=4);
getch();
}

void push()
{
if(top ==MAX-1)
printf("\n\nStack is full.");
else
{
printf("\n\nEnter ITEM: ");
scanf("%d", &item);
top++;
stack[top] =item;
printf("\n\nITEM inserted =%d", item);

40

}
}

void pop()
{
if(top ==-1)
printf("\n\nStack is empty.");
else
{
item =stack[top];
top--;
printf("\n\nITEM deleted =%d", item);
}
}

void display()
{
int i;
if(top ==-1)
printf("\n\nStack is empty.");
else
{
for(i=top; i>=0; i--)
printf("\n%d", stack[i]);
}
}

Output:
1. Push
2. Pop
3. Display
4. Exit

Enter your choice: 1
Enter ITEM: 10
ITEM inserted =10

1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 20
ITEM inserted =20

1. Push
2. Pop
3. Display

41

4. Exit
Enter your choice: 1
Enter ITEM: 30
ITEM inserted =30


1. Push
2. Pop
3. Display
4. Exit

Enter your choice: 3

30
20
10


1. Push
2. Pop
3. Display
4. Exit

Enter your choice: 2
ITEM deleted =30

1. Push
2. Pop
3. Display
4. Exit

Enter your choice: 3

20
10

1. Push
2. Pop
3. Display
4. Exit

Enter your choice:







42

4 b)Stack using linked list

Aim: Write a program to implement Stack using Linked List

Source code:
/****** Program to Implement Stack using Linked List ******/
#include <stdio.h>
void push();
void pop();
void display();
struct node
{
int info;
struct node *link;
}*top =NULL;

int item;

main()
{
int ch;
do
{
printf("\n\n1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("\nEnter 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("Invalid choice. Please try again.\n");

}
}while(1);
getch();
}

void push()
{
struct node *ptr;
printf("\n\nEnter ITEM: ");
scanf("%d", &item);
if (top ==NULL)

43

{
top =(struct node *)malloc(sizeof(struct node));
top->info =item;
top->link =NULL;
}
else
{
ptr =top;
top =(struct node *)malloc(sizeof(struct node));
top->info =item;
top->link =ptr;
}

printf("\nItem inserted: %d\n", item);
}

void pop()
{
struct node *ptr;
if (top ==NULL)
printf("\n\nStack is empty\n");
else
{
ptr =top;
item =top->info;
top =top->link;
free(ptr);
printf("\n\nItem deleted: %d", item);
}
}

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



44


Output:
1. Push
2. Pop
3. Display
4. Exit

Enter your choice: 1
Enter ITEM: 10
ITEM inserted =10

1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 20
ITEM inserted =20

1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 30
ITEM inserted =30


1. Push
2. Pop
3. Display
4. Exit

Enter your choice: 3

30
20
10


1. Push
2. Pop
3. Display
4. Exit

Enter your choice: 2
ITEM deleted =30

45


1. Push
2. Pop
3. Display
4. Exit

Enter your choice: 3

20
10



1. Push
2. Pop
3. Display
4. Exit

Enter your choice:

















46

5.Write a program to implement a queue.
5a )Queue using arrays
Aim:Write a Program to Implement Queue using Array
Source code:
/****** Program to Implement Queue using Array ******/
#include <stdio.h>
#define MAX 50
void insert();
void delete();
void display();
int queue[MAX], rear=-1, front=-1, item;

main()
{
int ch;
do
{
printf("\n\n1. Insert\n2. Delete\n3. Display\n4. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: insert();
break;
case 2: delete();
break;
case 3:display();
break;
case 4: exit(0);
default: printf("\n\nInvalid entry. Please try again...\n");
}

}while(1);

getch();

}

void insert()

{
if(rear ==MAX-1)
printf("\n\nQueue is full.");
else

47

{

printf("\n\nEnter ITEM: ");

scanf("%d", &item);
if (rear ==-1 && front ==-1)
{
rear =0;
front =0;
}
else
rear++;
queue[rear] =item;
printf("\n\nItem inserted: %d", item);

}

}

void delete()

{
if(front ==-1)
printf("\n\nQueue is empty.");
else
{
item =queue[front];
if (front ==rear)
{
front =-1;
rear =-1;
}
else
front++;
printf("\n\nItem deleted: %d", item);
}
}


void display()

{
int i;
if(front ==-1)
printf("\n\nQueue is empty.");
else
{
printf("\n\n");

48

for(i=front; i<=rear; i++)
printf(" %d", queue[i]);

}

}

Output:

1. Insert
2. Delete
3. Display
4. Exit

Enter your choice: 1
Enter ITEM: 10
Item inserted: 10

1. Insert
2. Delete
3. Display
4. Exit

Enter your choice: 1
Enter ITEM: 20
Item inserted: 20

1. Insert
2. Delete
3. Display
4. Exit

Enter your choice: 1
Enter ITEM: 30
Item inserted: 30

1. Insert
2. Delete
3. Display
4. Exit

Enter your choice: 3


10 20 30

1. Insert
2. Delete

49

3. Display
4. Exit

Enter your choice: 2


Item deleted: 10

1. Insert
2. Delete
3. Display
4. Exit

Enter your choice: 3

20 30

1. Insert
2. Delete
3. Display
4. Exit

Enter your choice:















50

5a )Queue using Linked List
Aim: Write a Program to Implement Queue using Linked List

Source code:

/**** Program to Implement Queue using Linked List ****/
#include<stdio.h>
struct node
{
int info;
struct node *link;
}*front =NULL, *rear =NULL;

void insert();
void delete();
void display();
int item;
main()
{
int ch;
do
{
printf("\n\n1.\tInsert\n2.\tDelete\n3.\tDisplay\n4.\tExit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: insert();
break;
case 2: delete();
break;
case 3:display();
break;
case 4: exit(0);
default:printf("\n\nInvalid choice. Please try again...\n");
}
}while(1);
getch();
}
void insert()

{
printf("\n\nEnter ITEM: ");
scanf("%d", &item);
if(rear ==NULL)
{
rear =(struct node *)malloc(sizeof(struct node));

51

rear->info =item;
rear->link =NULL;
front =rear;
}
else
{
rear->link =(struct node *)malloc(sizeof(struct node));
rear =rear->link;
rear->info =item;
rear->link =NULL;
}
}
void delete()
{
struct node *ptr;
if(front ==NULL)
printf("\n\nQueue is empty.\n");
else
{
ptr =front;
item =front->info;
front =front->link;
free(ptr);
printf("\nItem deleted: %d\n", item);
if(front ==NULL)
rear =NULL;
}
}
void display()
{
struct node *ptr =front;
if(rear ==NULL)
printf("\n\nQueue is empty.\n");
else

{
printf("\n\n");
while(ptr !=NULL)
{
printf("%d\t",ptr->info);
ptr =ptr->link;

}
}
}




52

Output:

1. Insert
2. Delete
3. Display
4. Exit

Enter your choice: 1
Enter ITEM: 10
Item inserted: 10

1. Insert
2. Delete
3. Display
4. Exit

Enter your choice: 1
Enter ITEM: 20
Item inserted: 20

1. Insert
2. Delete
3. Display
4. Exit

Enter your choice: 1
Enter ITEM: 30
Item inserted: 30

1. Insert
2. Delete
3. Display
4. Exit

Enter your choice: 3


10 20 30

1. Insert
2. Delete
3. Display
4. Exit

Enter your choice: 2
Item deleted: 10

1. Insert

53

2. Delete
3. Display
4. Exit

Enter your choice: 3

20 30

1. Insert
2. Delete
3. Display
4. Exit

Enter your choice:

































54

6. Write a program to create a Sparse matrix

Aim: Write a program to create a Sparse matrix

Source code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][3],r,c,s=0,i,j;
clrscr();
printf("\nenter the order of the sparse matrix");
scanf("%d%d",&r,&c);
printf("\nenter the elements in the sparse matrix(mostly zeroes)");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\n%d row and %d column ",i,j);
scanf("%d",&a[i][j]);
if(a[i][j]!=0)
{
b[s][0]=a[i][j];
b[s][1]=i;
b[s][2]=j;
s++;
}
}
}
printf("\nthe sparse matrix is given by");
printf("\n");
for(i=0;i<s;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",b[i][j]);
}
printf("\n");
}
getch();
}

55

Output:

enter the order of the sparse matrix
3
3

enter the elements in the sparse matrix(mostly zeroes)
0 row and 0 column 1

0 row and 1 column 0

0 row and 2 column 0

1 row and 0 column 2

1 row and 1 column 0

1 row and 2 column 0

2 row and 0 column 0

2 row and 1 column 5

2 row and 2 column 6

the sparse matrix is given by
1 0 0
2 1 0
5 2 1
6 2 2














56

7. Write a program to create a binary tree & its traversal operations

Aim: Write a program to create a binary tree & its traversal operations

Source code:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct tree*node;
node insert(int,node T);
void inorder(node T);
void preorder(node T);
void postorder(node T);
struct tree
{
int data;
struct tree*right,*left;
}*root;

void main()
{
node T= NULL;
int data,ch,i=0,n;
clrscr();
printf("\nEnter the number of elements in the Tree:");
scanf("%d",&n);
printf("\n The elements are :\n");
while(i<n)
{
scanf("%d",&data);
T=insert(data,T);
i++;
}
printf("1. INORDER\t2.PREORDER\t3.POSTOTRDER\t4.EXIT");
do
{
printf("\nEnter your choide:");
scanf("%d",&ch);
switch (ch)
{
case 1:printf ("Inorder traversal of the given Tree\n");
inorder(T);
break;
case 2:printf("Preoroder traversal of the given Tree\n");
preorder(T);
break;
case 3:printf("Postorder traversal of the given Tree\n");
postorder(T);
break;
default:printf("Exit");

57

exit(0);
}

}
while(ch<4);
getch();
}
node insert(int X, node T)
{
struct tree*newnode;
newnode=malloc(sizeof(struct tree));
if(newnode==NULL)
printf("Out of space\n");
else
{
if(T==NULL)
{
newnode->data=X;
newnode->left=NULL;
newnode->right=NULL;
T=newnode;
}
else
{
if(X<T->data)
T->left=insert(X,T->left);
else
T->right=insert(X,T->right);
}
}
return T;
}
void inorder(node T)
{
if(T!=NULL)
{
inorder(T->left);
printf("%d\t",T->data);
inorder(T->right);
}
}
void preorder(node T)
{
if(T!=NULL)
{
printf("%d\t",T->data);
preorder(T->left);
preorder(T->right);
}
}
void postorder(node T)

58

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

Output:

Enter the number of elements in the Tree:4

The elements are :
30
20
40
25
1. INORDER 2.PREORDER 3.POSTOTRDER 4.EXIT

Enter your choide:1

Inorder traversal of the given Tree
20 25 30 40
Enter your choide:2

Preoroder traversal of the given Tree
30 20 25 40
Enter your choide:3

Postorder traversal of the given Tree
25 20 40 30
Enter your choide:















59

8. Exercise on Selection sort
Aim: Write a program on selection sort

Source code:
/* Selection sort. */
#include <stdio.h>
#include <conio.h>
void main( )
{
int arr[5] ={25, 17, 31, 13, 2 };
int i, j, temp ;
clrscr( ) ;
printf ( "Selection sort.\n" ) ;
printf ( "\nArray before sorting:\n") ;
for ( i =0 ; i <=4 ; i++)
printf ( "%d\t", arr[i] ) ;
for ( i =0 ; i <=3 ; i++)
{
for ( j =i +1 ; j <=4 ; j++)
{
if ( arr[i] >arr[j] )
{
temp =arr[i] ;
arr[i] =arr[j] ;
arr[j] =temp ;
}
}
}
printf ( "\n\nArray after sorting:\n") ;
for ( i =0 ; i <=4 ; i++)
printf ( "%d\t", arr[i] ) ;
getch( ) ;
}
Output:



60


9. Exercise on insertion sort

Aim: Write a program on insertion sort

Source code:
/* Insertion sort. */
#include <stdio.h>
#include <conio.h>
void main( )
{
int arr[5] ={25, 17, 31, 13, 2 };
int i, j, k, temp ;
clrscr( ) ;
printf ( "Insertion sort.\n" ) ;
printf ( "\nArray before sorting:\n") ;
for ( i =0 ; i <=4 ; i++)
printf ( "%d\t", arr[i] ) ;
for ( i =1 ; i <=4 ; i++)
{
for ( j =0 ; j <i ; j++)
{
if ( arr[j] >arr[i] )
{
temp =arr[j] ;
arr[j] =arr[i] ;
for ( k =i ; k >j ; k-- )
arr[k] =arr[k - 1] ;
arr[k +1] =temp ;
}
}
}
printf ( "\n\nArray after sorting:\n") ;
for ( i =0 ; i <=4 ; i++)
printf ( "%d\t", arr[i] ) ;
getch( ) ;
}
Output:


61

10.Excercise on Bubble sort

Aim:Write a program on Bubble sort.

Source code:
/* Bubble sort. */
#include <stdio.h>
#include <conio.h>
void main( )
{
int arr[5] ={25, 17, 31, 13, 2 };
int i, j, temp ;
clrscr( ) ;
printf ( "Bubble sort.\n" ) ;
printf ( "\nArray before sorting:\n") ;
for ( i =0 ; i <=4 ; i++)
printf ( "%d\t", arr[i] ) ;

for ( i =0 ; i <=3 ; i++)
{
for ( j =0 ; j <=3 - i ; j++)
{
if ( arr[j] >arr[j +1] )
{
temp =arr[j] ;
arr[j] =arr[j +1] ;
arr[j +1] =temp ;
}
}
}
printf ( "\n\nArray after sorting:\n") ;
for ( i =0 ; i <=4 ; i++)
printf ( "%d\t", arr[i] ) ;
getch( ) ;
}

Output:




62

11. Implement a program for merge sort on two sorted lists of elements
Aim:Write a program for merge sort on two sorted lists of elements
Sourcecode:
/* Merge Sort. */

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

void main( )
{
int a[5] ={2,6,9,12,35 };
int b[5] ={7,10 ,18,48,33};
int c[10] ;
int i, j, k, temp ;

clrscr( ) ;

printf ( "Merge sort.\n" ) ;

printf ( "\nFirst array:\n" ) ;
for ( i =0 ; i <=4 ; i++)
printf ( "%d\t", a[i] ) ;

printf ( "\n\nSecond array:\n" ) ;
for ( i =0 ; i <=4 ; i++)
printf ( "%d\t", b[i] ) ;

for ( i =0 ; i <=3 ; i++)
{
for ( j =i +1 ; j <=4 ; j++)
{
if ( a[i] >a[j] )
{
temp =a[i] ;
a[i] =a[j] ;
a[j] =temp ;
}
if ( b[i] >b[j] )
{
temp =b[i] ;
b[i] =b[j] ;
b[j] =temp ;
}
}
}

63


for ( i =j =k =0 ; i <=9 ; )
{
if ( a[j] <= b[k] )
c[i++] =a[j++] ;
else
c[i++] =b[k++] ;

if ( j ==5 | | k ==5 )
break ;
}

for ( ; j <=4 ; )
c[i++] =a[j++] ;

for ( ; k <=4 ; )
c[i++] =b[k++] ;

printf ( "\n\nArray after sorting:\n") ;
for ( i =0 ; i <=9 ; i++)
printf ( "%d\t", c[i] ) ;

getch( ) ;
}


Output:







64


12.Exercises on linear search

12 a)Aim: Write a program on linear search of sorted array

Source code:
/* Linear search in a sorted array. */
#include <stdio.h>
#include <conio.h>
void main( )
{
int arr[10] ={1, 2, 3, 9, 11, 13, 17, 25, 57, 90 };
int i, num ;
clrscr( ) ;
printf ( "Enter number to search: " ) ;
scanf ( "%d", &num ) ;
for ( i =0 ; i <=9 ; i++)
{
if ( arr[9] <num | | arr[i] >=num )
{
if ( arr[i] ==num )
printf ( "The number is at position %d in the array.", i ) ;
else
printf ( "Number is not present in the array." ) ;
break ;
}
}

getch( ) ;
}
Output:




65

12 b)Aim: Write a program on linear search of unsorted array

Source code:
/* Linear search in an unsorted array. */
#include <stdio.h>
#include <conio.h>
void main( )
{
int arr[10] ={11, 2, 9, 13, 57, 25, 17, 1, 90, 3 };
int i, num ;
clrscr( ) ;
printf ( "Enter number to search: " ) ;
scanf ( "%d", &num ) ;
for ( i =0 ; i <=9 ; i++)
{
if ( arr[i] ==num )
break ;
}
if ( i ==10 )
printf ( "Number is not present in the array." ) ;
else
printf ( "The number is at position %d in the array.", i ) ;
getch( ) ;
}
Output:




66

13 )Excercises on binary search

Aim: Write a program on binary search
Source code:
/* Binary search in a sorted array. */
#include <stdio.h>
#include <conio.h>
void main( )
{
int arr[10] ={1, 2, 3, 9, 11, 13, 17, 25, 57, 90 };
int mid, lower =0 , upper =9, num, flag =1 ;
clrscr( ) ;
printf ( "Enter number to search: " ) ;
scanf ( "%d", &num ) ;
for ( mid =( lower +upper ) / 2 ; lower <=upper ;
mid =( lower +upper ) / 2 )
{
if ( arr[mid] ==num )
{
printf ( "The number is at position %d in the array.", mid ) ;
flag =0 ;
break ;
}
if ( arr[mid] >num )
upper =mid - 1 ;
else
lower =mid +1 ;
}

if ( flag )
printf ( "Element is not present in the array." ) ;
getch( ) ;
}
Output:



67

You might also like