SlideShare ist ein Scribd-Unternehmen logo
1 von 76
Downloaden Sie, um offline zu lesen
S.C.D. Govt. College
Practical File
Data Structures
Submitted to:
Prof. Vijay Sehgal
Signature
Submitted by:
Jasbir Singh
BCA-3rd
sem
7114
Subject Code: BCA-16-307
SESSION 2018-19
I have taken efforts in this practical file. I am highly indebted to
the Data Structures Lab teacher Prof. Vijay Sehgal for his
guidance and constant supervision as well as for providing
necessary information regarding the programs and also for his
support in completing the practical file.
I would like to express my gratitude towards my parents for
their kind co-operation and encouragement which helped me in
the completion of this practical file.
My thanks and appreciations also go to my classmates in
developing the practical file and to the people who have
willingly helped me out with their abilities.
Place: Ludhiana Name: Jasbir Singh
Date: 10/11/2018 Signature
Acknowledgment
Table of Contents
S. No. Title Page No. Remarks
1. Program to implement various
operations on arrays (insert, delete,
traverse, copy and merge).
1
2. Program to implement various
operations on a singly linked list
(insertion all, deletion all, counting both,
creation, traverse and copy).
7
3. Program to implement various
operations on a double linked list
(insertion all, deletion all, counting both,
creation, traverse both and reverse).
16
4. Program to implement various
operations on sorted single linked list
(insertion, deletion, display/ traversal).
25
5. Program to implement various
operations on sorted doubly linked list
(insertion, deletion, display/ traversal).
28
6. Program to perform Linear Search. 31
7. Program to perform Binary Search. 32
8. Program to perform Bubble Sort. 33
9. Program to perform Insertion Sort. 34
10. Program to perform Selection Sort. 35
11. Program to perform Merge Sort. 36
12. Program to perform Quick Sort. 38
13. Program to perform various operations
on stack using array representation
(create, push, pop, peek, empty, full).
40
14. Program to perform various operations
on stack using linked list representation
(create, push, pop, peek, empty, full).
44
15. Program to perform various operations
on queue using array representation
(create, insertion, deletion, front, rear,
display).
48
16. Program to perform various operations
on queue using linked list representation
(create, insertion, deletion, front, rear,
display).
50
17. Program to implement sparse matrix. 53
18. Program to perform various operations
on circular queue.
56
19. Program to perform various operations
on circular linked list.
58
20. Program to perform various operations
on graphs (traverse (2)).
• Breadth first search (BFS)
• Depth first search (DFS)
62
65
21. Program to perform various operations
on trees (traverse (3), find, delete,
insertion).
67
1
// Program to implement various operations on arrays (insert, delete, traverse,
copy and merge).
#include<stdio.h>
#include<conio.h>
#define N 10
void insert();
void delet();
void traverse();
void copy();
void merge();
void merge_sort(int *, int, int *, int, int *);
void main(){
int ch;
while(1){
clrscr();
printf("n******** MAIN MENU ********");
printf("n1. INSERTION ");
printf("n2. DELETION ");
printf("n3. TRAVERSING ");
printf("n4. COPYING ");
printf("n5. MERGING ");
printf("n6. EXIT ");
printf("nnENTER YOUR CHOICE: ");
scanf("%d", &ch);
switch(ch){
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
traverse();
break;
case 4:
copy();
break;
case 5:
merge();
2
break;
case 6:
exit(1);
break;
default:
printf("nWrong choice");
getch();
}
}
}
void insert(){
/* This function gets the N-1 elements of Array from the user and gets one
another element and location for its Insertion. */
int i, j, la[N], loc, item;
clrscr();
printf("nEnter %d elements of an Arrayn", N-1);
for(i = 0; i < N-1; i++)
scanf("%d", &la[i]);
printf("nEnter item to Insert in Array: ");
scanf("%d", &item);
getagain:
printf("nEnter location of array <= %d: ", N);
scanf("%d", &loc);
if(loc > N)
goto getagain;
j = N-2;
while(j >= loc-1){
la[j+1] = la[j];
j--;
}
la[loc-1] = item;
printf("nElements of Array are: n");
for(i = 0; i < N; i++)
printf("%d ", la[i]);
getch();
/*
Insertion
Enter 9 elements of an Array
23 46 69 92 115 161 184 207 230
3
Enter item to Insert in Array: 138
Enter location of array <= 10: 6
Elements of Array are:
23 46 69 92 115 138 161 184 207 230
*/
}
void delet(){
/* This function gets N elements of Array from the user and gets the location
for deletion of an element from array.*/
int i, j, la[N], loc, item;
clrscr();
printf("nEnter %d elements of an Array n", N);
for(i = 0; i < N; i++)
scanf("%d", &la[i]);
getagain:
printf("nEnter location of element to be deleted <= %dn", N);
scanf("%d", &loc);
if(loc > N)
goto getagain;
item = la[loc-1];
for(j = loc-1; j < N-1; j++)
la[j] = la[j+1];
printf("nDeleted item is = %d", item);
printf("nElements of Array are:n");
for(i = 0; i < N-1; i++)
printf("%d ", la[i]);
getch();
/*
Deletion
Enter 10 elements of an Array
18 36 45 54 72 90 108 126 144 162
Enter location of element to be deleted <= 10
3
Deleted item is = 45
4
Elements of Array are:
18 36 54 72 90 108 126 144 162
*/
}
void traverse(){
// Traversal of Array
/* This function gets the elements of array from the user and display these
elements to the user. */
int i, la[N];
clrscr();
printf("nEnter %d element of an Array n", N);
for(i = 0; i < N; i++)
scanf("%d", &la[i]);
printf("nElements of Array are: n");
for(i = 0; i < N; i++)
printf("%d ", la[i]);
getch();
/*
Traversal of Array:
Enter 10 element of an Array
12 24 36 48 60 72 84 96 108 120
Elements of Array are:
12 24 36 48 60 72 84 96 108 120
*/
}
void copy(){
/* This function gets N elements of Array from the user and copies all the
elements to another array. */
int i, j, la[N], lb[N];
clrscr();
printf("nEnter %d elements of an Arrayn", N);
for(i = 0; i < N; i++)
scanf("%d", &la[i]);
// Copying process
for(i = 0; i < N; i++)
lb[i] = la[i];
printf("nCopied Elements of Array are:n");
5
for(i = 0; i < N; i++)
printf("%d ", lb[i]);
getch();
/*
Copying
Enter 10 elements of an Array
15 30 45 60 75 90 105 120 135 150
Copied Elements of Array are:
15 30 45 60 75 90 105 120 135 150
*/
}
void merge(){
/* Function to Sort the elements by Merge Sort Technique */
int la[100], lb[100], lc[200];
int asize, bsize, i, j, k;
clrscr();
printf("nEnter the size of First Listn");
scanf("%d", &asize);
printf("nEnter %d elements for sorted Array - In", asize);
for(i = 0; i < asize; i++)
scanf("%d", &la[i]);
printf("nEnter the size of Second Listn");
scanf("%d", &bsize);
printf("nEnter %d elements for sorted Array - IIn", bsize);
for(j = 0; j < bsize; j++)
scanf("%d", &lb[j]);
merge_sort(la, asize, lb, bsize, lc);
printf("nSorted List after applying merge sort is n");
for(k = 0; k < (asize + bsize); k++)
printf("%d ", lc[k]);
getch();
/*
Merging
Enter the size of First List
7
Enter 7 elements for sorted Array - I
6
5 14 15 18 51 55 88
Enter the size of Second List
6
Enter 6 elements for sorted Array - II
3 7 9 25 48 76
Sorted List after applying merge sort is
3 5 7 9 14 15 18 25 48 51 55 76 88
*/
}
void merge_sort(int la[], int m, int lb[], int n, int lc[]){
int i, j, k;
i = j = k = 0;
while(i < m && j < n){
if(la[i] < lb[j]){
lc[k] = la[i];
i++;
}
else{
lc[k] = lb[j];
j++;
}
k++;
}
while(i < m){
lc[k] = la[i];
i++;
k++;
}
while(j < n){
lc[k] = lc[j];
j++;
k++;
}
}
7
// Program to implement various operations on a singly linked list (insertion
all, deletion all, counting both, creation, traverse and copy).
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
int ele, item;
struct node{
int info;
struct node *link;
};
struct node *start = NULL, *ptr, *loc = NULL, *locp = NULL, *new1, *save,
*temp, *l2;
struct node * searchp(int ele){
if(ele == start->info){
loc = start;
locp = NULL;
return locp;
}
save = start;
ptr = start->link;
while(ptr != NULL){
if(ele == ptr->info){
locp = save;
loc = ptr;
return locp;
}
save = ptr;
ptr = ptr->link;
}
locp = save;
return locp;
}
struct node * search(int ele){
ptr = start;
while(ptr != NULL){
if(ptr->info == ele){
loc = ptr;
printf("nThe element %d present at %u", ptr->info, ptr);
getch();
return loc;
8
}
ptr = ptr->link;
}
return NULL;
}
void insrbeg(int item){
new1 = (struct node *) malloc(sizeof(struct node));
if(new1 == NULL){
printf("nOverflow");
getch();
return;
}
new1->info = item;
new1->link = NULL;
if(start == NULL){
start = new1;
}
else{
new1->link = start;
start = new1;
}
}
void insrend(int item){
new1 = (struct node *) malloc(sizeof(struct node));
if(new1 == NULL){
printf("nOverflow");
getch();
return;
}
new1->info = item;
new1->link = NULL;
if(start == NULL){
start = new1;
}
else{
save = start;
ptr = start->link;
while(ptr != NULL){
save = ptr;
ptr = ptr->link;
9
}
save->link = new1;
}
}
void insrbef(int item){
printf("nEnter the element Before which you want to insert: ");
scanf("%d", &ele);
locp = searchp(ele);
loc = search(ele);
if(loc == NULL){
printf("nItem Not Found !");
getch();
return;
}
new1 = (struct node *) malloc(sizeof(struct node));
if(new1 == NULL){
printf("nOverflow");
getch();
return;
}
new1->info = item;
new1->link = NULL;
if(locp == NULL){
new1->link = start;
start = new1;
}
else{
new1->link = locp->link;
locp->link = new1;
}
}
void insraft(int item){
printf("nEnter the element After which you want to insert: ");
scanf("%d", &ele);
loc = search(ele);
if(loc == NULL){
printf("nSorry, location not found!");
getch();
return;
}
10
new1 = (struct node *) malloc(sizeof(struct node));
if(new1 == NULL){
printf("nOverflow");
getch();
return;
}
new1->info = item;
new1->link = loc->link;
loc->link = new1;
}
void deletbeg(){
if(start == NULL){
printf("nUnderflow");
getch();
return;
}
else{
temp = start;
start = start->link;
free(temp);
}
}
void deletend(){
if(start == NULL){
printf("nUnderflow");
getch();
return;
}
save = start;
ptr = start->link;
while(ptr->link != NULL){
save = ptr;
ptr = ptr->link;
}
if(save->link == NULL){
temp = start;
start = start->link;
free (temp);
}
temp = ptr;
11
save->link = NULL;
free(temp);
}
void delet(int item){
locp = searchp(item);
if(loc == NULL){
printf("nItem not found");
getch();
return;
}
if(locp == NULL){
temp = start;
start = start->link;
free(temp);
}
temp = loc;
locp->link = loc->link;
free(temp);
}
void count1(){
int num = 0;
for(ptr = start; ptr != NULL; ptr = ptr->link)
num++;
printf("nTotal number of elements = %d", num);
}
void count2(int ele){
int num = 0;
for(ptr = start; ptr != NULL; ptr = ptr->link)
if(ptr->info == ele)
num++;
printf("nElement %d exists %d times in the list.", ele, num);
}
void create(){
int i, n;
start = NULL;
printf("nEnter the no. of elements: ");
scanf("%d", &n);
printf("nEnter elements: ");
for(i = 1; i <= n; i++){
12
new1 = (struct node *) malloc(sizeof(struct node));
if(new1 == NULL){
printf("nOverflow");
getch();
return;
}
scanf("%d", &new1->info);
new1->link = NULL;
if(start == NULL){
start = new1;
ptr = start;
}
else
ptr = ptr->link = new1;
}
}
void traverse(){
if(start == NULL){
printf("nEmpty List");
return;
}
for(ptr = start; ptr != NULL; ptr = ptr->link)
printf("nThe element %d present at location %u", ptr->info, ptr);
}
void copy(){
l2 = NULL;
if(start == NULL){
printf("nEmpty List");
return;
}
new1 = (struct node *) malloc(sizeof(struct node));
if(new1 == NULL){
printf("nOverflow");
getch();
return;
}
new1->info = start->info;
new1->link = NULL;
loc = l2 = new1;
13
for(ptr = start->link; ptr != NULL; ptr = ptr->link){
new1 = (struct node *) malloc(sizeof(struct node));
if(new1 == NULL){
printf("nOverflow");
getch();
return;
}
new1->info = ptr->info;
new1->link = NULL;
loc = loc->link = new1;
}
}
void main(){
int ch;
while(1){
clrscr();
printf("n******** MAIN MENU ********n");
printf("n 1. INSERTION AT BEGINNING");
printf("n 2. INSERTION AT END");
printf("n 3. INSERTION BEFORE AN ELEMENT");
printf("n 4. INSERTION AFTER AN ELEMENT");
printf("n 5. DELETION FROM BEGINNING");
printf("n 6. DELETION FROM ENDING");
printf("n 7. DELETION AN ITEM");
printf("n 8. COUNT TOTAL NO. OF ELEMENTS ");
printf("n 9. COUNTING A PARTICULAR ELEMENT");
printf("n10. CREATION");
printf("n11. TRAVERSING");
printf("n12. COPYING");
printf("n13. EXIT");
printf("nnENTER YOUR CHOICE: ");
scanf("%d", &ch);
switch(ch){
case 1:
printf("nEnter an element ");
scanf("%d", &item);
insrbeg(item);
break;
case 2:
printf("nEnter an element ");
14
scanf("%d", &item);
insrend(item);
break;
case 3:
printf("nEnter the element which you want to insert: ");
scanf("%d", &item);
insrbef(item);
break;
case 4:
printf("nEnter the element you want to insert: ");
scanf("%d", &item);
insraft(item);
break;
case 5:
deletbeg();
break;
case 6:
deletend();
break;
case 7:
printf("nEnter the element which you want to delete: ");
scanf("%d", &ele);
delet(ele);
break;
case 8:
count1();
getch();
break;
case 9:
printf("nEnter the element which you want to count: ");
scanf("%d", &ele);
count2(ele);
getch();
break;
case 10:
create();
break;
case 11:
traverse();
getch();
15
break;
case 12:
copy();
break;
case 13:
exit();
break;
default:
printf("nWrong choice");
getch();
}
}
}
16
// Program to implement various operations on a double linked list (insertion
all, deletion all, counting both, creation, traverse both and reverse).
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
int item, ele;
struct node{
int info;
struct node *prev, *link;
};
struct node *temp, *new1, *start = NULL, *end = NULL, *loc = NULL, *ptr;
struct node * search(int item){
for(ptr = start; ptr != NULL; ptr = ptr->link)
if(item == ptr->info){
loc = ptr;
printf("nItem found at loc %u", loc);
return loc;
}
return NULL;
}
void insrbeg(int item){
new1 = (struct node *) malloc(sizeof(struct node));
if(new1 == NULL){
printf("nOverflow");
getch();
return;
}
new1->prev = new1->link = NULL;
new1->info = item;
if(start == NULL){
start = end = new1;
}
else{
new1->link = start;
start = start->prev = new1;
}
}
void insrend(int item){
17
new1 = (struct node *) malloc(sizeof(struct node));
if(new1 == NULL){
printf("nOverflow");
getch();
return;
}
new1->prev = new1->link = NULL;
new1->info = item;
if(start == NULL){
start = end = new1;
}
else{
new1->prev = end;
end = end->link = new1;
}
}
void insrbef(){
printf("nEnter the element before which you want to insert: ");
scanf("%d", &ele);
loc = search
(ele);
if(loc == NULL){
printf("nLocation not found");
getch();
return;
}
new1 = (struct node *) malloc(sizeof(struct node));
if(new1 == NULL){
printf("nOverflow");
getch();
return;
}
new1->prev = new1->link = NULL;
new1->info = item;
if(loc == start){
new1->link = start;
start = start->prev = new1;
}
else{
new1->prev = loc->prev;
18
new1->link = loc;
loc->prev = loc->prev->link = new1;
}
}
void insraft(){
printf("nEnter the element after which you want to insert: ");
scanf("%d", &ele);
loc = search(ele);
if(loc == NULL){
printf("nLocation not found");
getch();
return;
}
new1 = (struct node *) malloc(sizeof(struct node));
if(new1 == NULL){
printf("nOverflow");
getch();
return;
}
new1->prev = new1->link = NULL;
new1->info = item;
if(loc == end){
new1->prev = end;
end = end->link = new1;
}
else{
new1->prev = loc;
new1->link = loc->link;
loc->link = loc->link->prev = new1;
}
}
void deletbeg(){
if(start == NULL){
printf("nUnderflow");
getch();
return;
}
if(start == end){
temp = start;
start = end = NULL;
19
free(temp);
return;
}
else{
temp = start;
start->link->prev = NULL;
start = start->link;
free(temp);
}
}
void deletend(){
if(start == NULL){
printf("nUnderflow");
getch();
return;
}
if(start == end){
temp = start;
start = end = NULL;
free(temp);
return;
}
else{
temp = end;
end->prev->link = NULL;
end = end->prev;
free(temp);
}
}
void delet(int ele){
loc = search(ele);
if(loc == NULL){
printf("nItem %d not found", ele);
getch();
return;
}
if(loc->prev == NULL){
temp = start;
if(start == end){
start = end = NULL;
20
}
else{
start->link->prev = NULL;
start = start->link;
}
free(temp);
getch();
return;
}
if(loc->link == NULL){
temp = end;
end->prev->link = NULL;
end = end->prev;
free(temp);
getch();
return;
}
temp = loc;
loc->prev->link = loc->link;
loc->link->prev = loc->prev;
free(temp);
printf("n%d has been deleted", ele);
getch();
}
void count1(){
int num = 0;
for(ptr = start; ptr != NULL; ptr = ptr->link)
num++;
printf("nTotal elements = %d", num);
}
void count2(int ele){
int num = 0;
for(ptr = start; ptr != NULL; ptr = ptr->link)
if(ele == ptr->info)
num++;
printf("nElement %d exists %d times in the list.", ele, num);
}
void create(){
int i, n;
start = NULL;
21
printf("nEnter the number of elements: ");
scanf("%d", &n);
printf("nEnter elements: ");
for(i = 1 ; i <= n; i++){
new1 = (struct node *) malloc(sizeof(struct node));
if(new1 == NULL){
printf("nOverflow");
getch();
return;
}
new1->prev = new1->link = NULL;
scanf("%d", &new1->info);
if(start == NULL){
start = end = new1;
}
else{
new1->prev = end;
end = end->link = new1;
}
}
}
void travbeg(){
printf("nItem in the lists are ");
printf("nItem tLoc");
for(ptr = start; ptr != NULL; ptr = ptr->link)
printf("n%d t%u", ptr->info, ptr);
}
void travend(){
printf("nItem in the lists are ");
printf("nItem tLoc");
for(ptr = end; ptr != NULL; ptr = ptr->prev)
printf("n%d t%u", ptr->info, ptr);
}
void reverse(){
temp = NULL;
for(ptr = start; ptr != NULL; ptr = ptr->prev){
temp = ptr->link;
ptr->link = ptr->prev;
ptr->prev = temp;
if(ptr->prev == NULL)
22
start = ptr;
if(ptr->link == NULL)
end = ptr;
}
}
void main(){
int ch;
while(1){
clrscr();
printf("n******** MAIN MENU ********n");
printf("n 1. Insert at Beginning");
printf("n 2. Insert at End");
printf("n 3. Insert Before");
printf("n 4. Insert After");
printf("n 5. Delete Beg");
printf("n 6. Delete End");
printf("n 7. Delete Item");
printf("n 8. Count total no. of elements");
printf("n 9. Counting a particular element");
printf("n10. Creation");
printf("n11. Traversing beginning");
printf("n12. Traversing end");
printf("n13. Reversing");
printf("n14. Exit");
printf("nnEnter your choice: ");
scanf("%d", &ch);
switch(ch){
case 1:
printf("nEnter item to Insert: ");
scanf("%d", &item);
insrbeg(item);
break;
case 2:
printf("nEnter item to Insert: ");
scanf("%d", &item);
insrend(item);
break;
case 3:
printf("nEnter item to Insert: ");
scanf("%d", &item);
23
insrbef();
break;
case 4:
printf("nEnter item to Insert: ");
scanf("%d", &item);
insraft();
break;
case 5:
deletbeg();
travbeg();
getch();
break;
case 6:
deletend();
travend();
getch();
break;
case 7:
printf("nEnter the element which you want to delete: ");
scanf("%d", &ele);
delet(ele);
break;
case 8:
count1();
getch();
break;
case 9:
printf("nEnter the element you want to count: ");
scanf("%d", &ele);
count2(ele);
getch();
break;
case 10:
create();
break;
case 11:
travbeg();
getch();
break;
case 12:
24
travend();
getch();
break;
case 13:
reverse();
break;
case 14:
exit(1);
default:
printf("nWrong choice");
getch();
}
}
}
25
// Program to implement various operations on sorted single linked list
(insertion, deletion, display/ traversal).
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
int item;
struct node{
int info;
struct node *link;
};
struct node *start = NULL, *new1, *save, *ptr;
void insert_sorted(int item){
new1 = (struct node *) malloc(sizeof(struct node));
if(new1 == NULL){
printf("nOverflow");
getch();
return;
}
new1->info = item;
new1->link = NULL;
if(start == NULL){
start = new1;
return;
}
if(item < start->info){
new1->link = start;
start = new1;
return;
}
for(save = start, ptr = start->link; ptr != NULL; save = ptr, ptr = ptr->link)
if(item < ptr->info){
new1->link = ptr;
save->link = new1;
return;
}
save->link = new1;
}
void delet_sorted(int item){
if(start == NULL){
printf("Underflow");
26
getch();
return;
}
ptr = start;
if(item == start->info){
start = start->link;
free(ptr);
return;
}
for(save = ptr, ptr = ptr->link; (ptr->info <= item) && (ptr != NULL); save
= ptr, ptr = ptr->link)
if(item == ptr->info){
save->link = ptr->link;
free(ptr);
return;
}
printf("nItem %d not found", item);
getch();
}
void traversal(){
printf("nElements are: n");
for(ptr = start; ptr != NULL; ptr = ptr->link)
printf("%d %c ", ptr->info, 16);
}
void main(){
int ch;
while(1){
clrscr();
printf("n******** MAIN MENU ********n");
printf("n1. Insertion in a Sorted Linked List");
printf("n2. Deletion in a Sorted Linked List");
printf("n3. Display/ Traversal");
printf("n4. Exit");
printf("nnEnter your choice: ");
scanf("%d", &ch);
switch(ch){
case 1:
printf("nEnter item to Insert: ");
scanf("%d", &item);
insert_sorted(item);
27
break;
case 2:
printf("nEnter item to Delete: ");
scanf("%d", &item);
delet_sorted(item);
break;
case 3:
traversal();
getch();
break;
case 4:
exit(1);
default:
printf("nWrong choice");
getch();
}
}
}
28
// Program to implement various operations on sorted doubly linked list
(insertion, deletion, display/ traversal).
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
int item;
struct node{
int info;
struct node *link, *prev;
};
struct node *start = NULL, *end = NULL, *ptr, *save, *new1;
void insert_sorted(int item){
new1 = (struct node *) malloc(sizeof(struct node));
if(new1 == NULL){
printf("nOverflow");
getch();
return;
}
new1->prev = new1->link = NULL;
new1->info = item;
if(start == NULL){
start = end = new1;
return;
}
if(item < start->info){
new1->link = start;
start = start->prev = new1;
return;
}
for(ptr = start->link; ptr != NULL; ptr = ptr->link)
if(item < ptr->info){
new1->prev = ptr->prev;
new1->link = ptr;
ptr->prev = ptr->prev->link = new1;
return;
}
new1->prev = end;
end = end->link = new1;
}
void delet_sorted(int item){
29
if(start == NULL){
printf("Underflow");
getch();
return;
}
ptr = start;
if(item == start->info){
if(start == end){
start = end = NULL;
}
else{
start->link->prev = NULL;
start = start->link;
}
free(ptr);
return;
}
for(ptr = ptr->link; (ptr->info <= item) && (ptr != NULL); ptr = ptr->link)
if(item == ptr->info){
if(ptr->link == NULL){
end = end->prev;
}
ptr->link->prev = ptr->prev;
ptr->prev->link = ptr->link;
free(ptr);
return;
}
printf("nItem %d not found", item);
getch();
}
void traversal(){
printf("nElements are: n");
for(ptr = start; ptr != NULL; ptr = ptr->link)
printf("%d %c ", ptr->info, 16);
}
void main(){
int ch;
while(1){
clrscr();
printf("n******** MAIN MENU ********n");
30
printf("n1. Insertion in a Sorted Linked List");
printf("n2. Deletion in a Sorted Linked List");
printf("n3. Display/ Traversal");
printf("n4. Exit");
printf("nnEnter your choice: ");
scanf("%d", &ch);
switch(ch){
case 1:
printf("nEnter item to Insert: ");
scanf("%d", &item);
insert_sorted(item);
break;
case 2:
printf("nEnter item to Delete: ");
scanf("%d", &item);
delet_sorted(item);
break;
case 3:
traversal();
getch();
break;
case 4:
exit(1);
default:
printf("nWrong choice");
getch();
}
}
}
31
// Program to perform Linear Search.
#include<stdio.h>
#include<conio.h>
#define N 7
void main(){
int a[N], loc, item;
int i;
clrscr();
printf("nEnter %d elements of an Arrayn", N);
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
printf("nEnter item to search: ");
scanf("%d", &item);
loc = NULL;
for(i = 0; i < N; i++)
if(item == a[i]){
loc = i+1;
break;
}
if(loc == NULL)
printf("nItem %d Not found in List", item);
else
printf("nItem %d found at %d location.", item, loc);
getch();
}
/* Output
Enter 7 elements of an Array
9 7 11 6 3 -10 15
Enter item to search: 3
Item 3 found at 5 location.
*/
32
// Program to perform Binary Search.
#include<stdio.h>
#include<conio.h>
#define N 6
void main(){
int a[N], item, beg, end, mid;
int i;
clrscr();
printf("nEnter %d elements of an Arrayn", N);
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
printf("nEnter item to Search: ");
scanf("%d", &item);
beg = 0;
end = N-1;
mid = (beg+end)/2;
while((beg <= end) && a[mid] != item){
if(a[mid] > item)
end = mid-1;
else
beg = mid+1;
mid = (beg+end)/2;
}
if(a[mid] != item)
printf("nItem %d Not found in List", item);
else
printf("nItem %d found at %d location.", item, mid+1);
getch();
}
/* Output
Enter 6 elements of an Array
5 10 15 20 25 30
Enter item to Search: 25
Item 25 found at 5 location.
*/
33
// Program to perform Bubble Sort.
#include<stdio.h>
#include<conio.h>
#define N 10
void main(){
int a[N], i, j;
clrscr();
printf("nEnter %d elements of an Arrayn", N);
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
for(i = 0; i < N-1; i++)
for(j = 0; j < N-i-1; j++)
if(a[j] > a[j+1]){
a[j] = a[j] + a[j+1];
a[j+1] = a[j] - a[j+1];
a[j] = a[j] - a[j+1];
}
printf("nSorted Array: ");
for(i = 0; i < N; i++)
printf("%d ", a[i]);
getch();
}
/* Output
Enter 10 elements of an Array
6 3 -5 37 6 37 57 27 478 4
Sorted Array: -5 3 4 6 6 27 37 37 57 478
*/
34
// Program to perform Insertion Sort.
#include<stdio.h>
#include<conio.h>
#define N 10
void main(){
int a[N], i, k, temp, ptr;
clrscr();
printf("nEnter the elements: n");
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
for(k = 1; k < N; k++){
temp = a[k];
ptr = k-1;
while((temp < a[ptr]) && (ptr >= 0)){
a[ptr+1] = a[ptr];
ptr--;
}
a[ptr+1] = temp;
}
printf("nSorted Arrayn");
for(i = 0; i < N; i++)
printf("%d ", a[i]);
getch();
}
/* Output
Enter the elements:
10 2 34 5 36 35 67 75 67 46
Sorted Array
2 5 10 34 35 36 46 67 67 75
*/
35
// Program to perform Selection Sort.
#include<stdio.h>
#include<conio.h>
#define N 10
void main(){
int a[N], i, j, min, loc;
clrscr();
printf("nEnter the elements: n");
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
for(i = 0; i < N-1; i++){
min = a[i];
loc = i;
for(j = i+1; j < N; j++)
if(min > a[j]){
min = a[j];
loc = j;
}
if(a[i] != a[loc]){
a[i] = a[i] + a[loc];
a[loc] = a[i] - a[loc];
a[i] = a[i] - a[loc];
}
}
printf("nSorted elements of arrayn");
for(i = 0; i < N; i++)
printf("%d ", a[i]);
getch();
}
/* Output
Enter the elements:
12 3 35 89 2 57 75 57 89 45
Sorted elements of array
2 3 12 35 45 57 57 75 89 89
*/
36
// Program to perform Merge Sort.
#include<stdio.h>
#include<conio.h>
#define N 100
void merge_sort(int *, int, int);
void merge(int *, int, int, int);
void merge_sort(int a[], int low, int high){
int mid;
if(low < high){
mid = (low+high)/ 2;
merge_sort(a, low, mid);
merge_sort(a, mid+1, high);
merge(a, low, mid, high);
}
}
void merge(int a[], int low, int mid, int high){
int h, i, j, k, temp[N];
h = i = low;
j = mid+1;
while((h <= mid) && (j <= high)){
if(a[h] <= a[j]){
temp[i] = a[h];
h++;
}
else{
temp[i] = a[j];
j++;
}
i++;
}
while(h <= mid){
temp[i] = a[h];
i++;
h++;
}
while(j <= high){
temp[i] = a[j];
i++;
j++;
}
37
for(k = low; k <= high; k++)
a[k] = temp[k];
}
void main(){
int i, a[N], size;
clrscr();
printf("nEnter the size of Listn");
scanf("%d", &size);
printf("nEnter %d elements of Listn", size);
for(i = 0; i < size; i++)
scanf("%d", &a[i]);
merge_sort(a, 0, size-1);
printf("nSorted elements are:n");
for(i = 0; i < size; i++)
printf("%d ", a[i]);
getch();
}
/* Output
Enter the size of List
6
Enter 6 elements of List
90 28 39 10 23 45
Sorted elements are:
10 23 28 39 45 90
*/
38
// Program to perform Quick Sort.
#include<stdio.h>
#include<conio.h>
#define N 100
void q_sort(int *, int, int);
void q_sort(int array[], int left, int right){
int pivot, l_hold, r_hold;
l_hold = left;
r_hold = right;
pivot = array[left];
while(left < right){
while((array[right] >= pivot) && (left < right))
right--;
if(left != right){
array[left] = array[right];
left++;
}
while((array[left] <= pivot) && (left < right))
left++;
if(left != right){
array[right] = array[left];
right--;
}
}
array[left] = pivot;
pivot = left;
left = l_hold;
right = r_hold;
if(left < pivot)
q_sort(array, left, pivot-1);
if(right > pivot)
q_sort(array, pivot+1, right);
}
void main(){
int a[N], i, size;
clrscr();
printf("nEnter the size of listn");
scanf("%d", &size);
printf("nEnter %d elements of the listn", size);
for(i = 0; i < size; i++)
39
scanf("%d", &a[i]);
q_sort(a, 0, size-1);
printf("nElements after sorting are:n");
for(i = 0; i < size; i++)
printf("%d ", a[i]);
getch();
}
/* Output
Enter the size of list
8
Enter 8 elements of the list
18 2 98 25 58 21 20 6
Elements after sorting are:
2 6 18 20 21 25 58 98
*/
40
// Program to perform various operations on stack using array representation
(create, push, pop, peek, empty, full).
#include<stdio.h>
#include<conio.h>
#define Max 10
int stack[Max], top = -1;
// Stack Creation and initializing the top
void push(int item){
// Check for stack full
if(top == Max-1){
printf("nStack Full");
getch();
return;
}
top++;
// Assigning the item at the top of stack
stack[top] = item;
}
int pop(){
int item;
// Check for empty stack
if(top == -1){
printf("nStack is Empty");
return NULL;
}
// Removing item from the top of stack
item = stack[top];
// Updating the top
top--;
return item;
}
int peek(){
int item;
// Check for empty stack
if(top == -1){
printf("nUnderflow");
return NULL;
}
// Assigning the topmost element
item = stack[top];
41
return item;
}
void main(){
int ch, item;
while(1){
clrscr();
printf("n1. Push");
printf("n2. Pop");
printf("n3. Peek");
printf("n4. Exit");
printf("nnEnter your choice: ");
/* Clearing the input buffer */
fflush(stdin);
scanf("%d", &ch);
switch(ch){
case 1:
printf("nEnter item to push: ");
scanf("%d", &item);
push(item);
break;
case 2:
item = pop();
if(item != NULL)
printf("nItem from the top of stack = %d", item);
getch();
break;
case 3:
item = peek();
if(item != NULL)
printf("nItem at the top of stack = %d", item);
getch();
break;
case 4:
exit(1);
default:
printf("nInvalid input");
getch();
break;
}
}
42
}
/* Output
1. Push
2. Pop
3. Peek
4. Exit
Enter your choice: 1
Enter item to push: 10
1. Push
2. Pop
3. Peek
4. Exit
Enter your choice: 3
Item at the top of stack = 10
1. Push
2. Pop
3. Peek
4. Exit
Enter your choice: 2
Item from the top of stack = 10
1. Push
2. Pop
3. Peek
4. Exit
Enter your choice: 2
Stack is Empty
1. Push
2. Pop
3. Peek
4. Exit
Enter your choice: 5
43
Invalid input
1. Push
2. Pop
3. Peek
4. Exit
Enter your choice: 4
*/
44
// Program to perform various operations on stack using linked list
representation (create, push, pop, peek, empty, full).
#include<stdio.h>
#include<conio.h>
// Defining node structure for link list
struct node{
int info;
struct node *link;
};
struct node *top = NULL;
void push(int item){
struct node *new1;
// Allocate new memory for stack
new1 = (struct node *) malloc(sizeof(struct node));
// Check for memory availability
if(new1 == NULL){
printf("nOverflow");
getch();
return;
}
// Assigning item to new allocated node for stack
new1->info = item;
new1->link = NULL;
// Updating the stack top pointer
new1->link = top;
top = new1;
}
int pop(){
int item;
struct node *temp;
// Check for empty stack
if(top == NULL){
printf("nUnderflow");
return NULL;
}
// Removing item from top of stack
item = top->info;
// Remembering the address of stack top
temp = top;
// Updating the top pointer
45
top = top->link;
// Releasing the memory
free(temp);
return item;
}
int peek(){
int item;
// Check for empty stack
if(top == NULL){
printf("nUnderflow");
return NULL;
}
// Assigning the topmost element
item = top->info;
return item;
}
void main(){
int ch, item;
while(1){
clrscr();
printf("n1. Push");
printf("n2. Pop");
printf("n3. Peek");
printf("n4. Exit");
printf("nnEnter your choice: ");
// Clearing the input buffer
fflush(stdin);
scanf("%d", &ch);
switch(ch){
case 1:
printf("nEnter item to push: ");
scanf("%d", &item);
push(item);
break;
case 2:
item = pop();
if(item != NULL)
printf("nItem from the top of stack = %d", item);
getch();
break;
46
case 3:
item = peek();
if(item != NULL)
printf("nItem at the top of stack = %d", item);
getch();
break;
case 4:
exit(1);
default:
printf("nInvalid input");
getch();
}
}
}
/* Output
1. Push
2. Pop
3. Peek
4. Exit
Enter your choice: 1
Enter item to push: 89
1. Push
2. Pop
3. Peek
4. Exit
Enter your choice: 3
Item at the top of stack = 89
1. Push
2. Pop
3. Peek
4. Exit
Enter your choice: 2
Item from the top of stack = 89
1. Push
47
2. Pop
3. Peek
4. Exit
Enter your choice: 2
Stack is Empty
1. Push
2. Pop
3. Peek
4. Exit
Enter your choice: 5
Invalid input
1. Push
2. Pop
3. Peek
4. Exit
Enter your choice: 4
*/
48
// Program to perform various operations on queue using array
representation (create, insertion, deletion, front, rear, display).
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define N 5
int Q[N], front, rear, item;
void qinsert(int item){
if(rear == N-1){
printf("nOverflow");
getch();
return;
}
if((rear == -1) && (front == -1))
rear = front = 0;
else
rear++;
Q[rear] = item;
}
int qdelete(){
if(front == -1){
printf("nUnderflow");
return NULL;
}
item = Q[front];
if(front == rear)
front = rear = -1;
else
front++;
return item;
}
void display(){
int i;
if(front == -1)
printf("nQueue is empty");
else{
printf("nQueue is: n");
for(i = front; i <= rear; i++)
printf("%d ", Q[i]);
}
49
}
void main(){
int ch;
front = rear = -1;
while(1){
clrscr();
printf("n******** MAIN MENU ********n");
printf("n1. Insert");
printf("n2. Delete");
printf("n3. Display");
printf("n4. Exit");
printf("nnEnter your choice: ");
scanf("%d", &ch);
switch(ch){
case 1:
printf("nEnter the element: ");
scanf("%d", &item);
qinsert(item);
break;
case 2:
item = qdelete();
if(item != NULL)
printf("nItem = %d", item);
getch();
break;
case 3:
display();
getch();
break;
case 4:
exit(1);
default:
printf("nInvalid choice");
getch();
}
}
}
50
// Program to perform various operations on queue using linked list
representation (create, insertion, deletion, front, rear, display).
#include<stdio.h>
#include<conio.h>
#include<process.h>
struct node{
int info;
struct node *link;
} *front = NULL, *rear = NULL;
void qinsert(int item){
struct node *new1;
new1 = (struct node *) malloc(sizeof(struct node));
if(new1 == NULL){
printf("nOverflow");
getch();
return;
}
new1->info = item;
new1->link = NULL;
if(front == NULL)
front = rear = new1;
else
rear = rear->link = new1;
}
int qdelete(){
int item;
struct node *temp;
if(front == NULL){
printf("nUnderflow");
return NULL;
}
item = front->info;
temp = front;
if(front == rear)
front = rear = NULL;
else
front = front->link;
free(temp);
return item;
}
51
void display(){
struct node *ptr;
if(front == NULL){
printf("nQueue is Empty");
return;
}
printf("nElements of Queue are: n");
for(ptr = front; ptr != NULL; ptr = ptr->link)
printf("%d ", ptr->info);
}
void main(){
int ch, item;
while(1){
clrscr();
printf("n******** Menu ********");
printf("n1. Insert");
printf("n2. Delete");
printf("n3. Display");
printf("n4. Exit");
printf("n**********************");
printf("nEnter your choice: ");
fflush(stdin);
scanf("%d", &ch);
switch(ch){
case 1:
printf("nEnter item to Insert: ");
scanf("%d", &item);
qinsert(item);
break;
case 2:
item = qdelete();
if(item != NULL)
printf("nGiven item is %d", item);
getch();
break;
case 3:
display();
getch();
break;
case 4:
52
exit(0);
default:
printf("nInvalid input");
}
}
}
53
// Program to implement sparse matrix.
#include<stdio.h>
#include<conio.h>
int a[100][100], b[100][100];
void main(){
int i, m, n, p, q, col, t;
clrscr();
printf("nEnter the no. of rows: ");
scanf("%d", &a[0][0]);
printf("nEnter the no. of cols: ");
scanf("%d", &a[0][1]);
printf("nEnter the no. of non zero terms: ");
scanf("%d", &a[0][2]);
for(i = 1; i <= a[0][2]; i++){
printf("nEnter the value (that is non zero): ");
scanf("%d", &a[i][2]);
printf("Enter the row for %d: ", a[i][2]);
scanf("%d", &a[i][0]);
printf("Enter the col for %d: ", a[i][2]);
scanf("%d", &a[i][1]);
}
/* Printing for testing the sparse input */
printf("nThe matrix you entered is");
printf("n*************************");
printf("nRow tCol tValue ");
printf("n*************************");
for(i = 0; i <= a[0][2]; i++)
printf("n%d t%d t%d", a[i][0], a[i][1], a[i][2]);
exit(0);
/* Calling function for evaluation of transpose */
m = a[0][0];
n = a[0][1];
t = a[0][2];
b[0][0] = n;
b[0][1] = m;
b[0][2] = t;
q = 1;
for(col = 1; col <= n; col++)
for(p = 1; p <= t; p++)
if(a[p][1] == col){
54
b[q][0] = a[p][1];
b[q][1] = a[p][0];
b[q][2] = a[p][2];
q++;
}
getch();
/* Printing the transposed matrix */
printf("nnThe Transpose of the above matrix is: ");
for(i = 0; i <= a[0][2]; i++)
printf("n%d t%d t%d", b[i][0], b[i][1], b[i][2]);
getch();
}
/* Output
Enter the no. of rows: 5
Enter the no. of cols: 4
Enter the no. of non zero terms: 3
Enter the value (that is non zero): 1
Enter the row for 1: 3
Enter the col for 1: 1
Enter the value (that is non zero): 2
Enter the row for 2: 4
Enter the col for 2: 3
Enter the value (that is non zero): 1
Enter the row for 1: 5
Enter the col for 1: 3
The matrix you entered is
*************************
Row Col Value
*************************
5 4 3
3 1 1
4 3 2
5 3 1
55
The Transpose of the above matrix is:
4 5 3
1 3 1
3 4 2
3 5 1
*/
56
// Program to perform various operations on circular queue.
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define N 5
int q[N], front, rear, item;
void qinsert(int item){
if((front == 0 && rear == N-1) || (front == rear+1)){
printf("nQueue is Full");
getch();
return;
}
if(front == -1)
front = rear = 0;
else if(front == N-1)
rear = 0;
else
rear++;
q[rear] = item;
}
int qdelete(){
if(front == -1){
printf("nQueue is Empty");
getch();
return NULL;
}
item = q[front];
if(front == rear)
front = rear = -1;
else if(front == N-1)
front = 0;
else
front++;
return item;
}
void main(){
int ch;
front = rear = -1;
while(1){
clrscr();
57
printf("n******** MAIN MENU ********n");
printf("n1. Insert");
printf("n2. Delete");
printf("n3. Exit");
printf("nEnter your choice: ");
scanf("%d", &ch);
switch(ch){
case 1:
printf("nEnter the element: ");
scanf("%d", &item);
qinsert(item);
break;
case 2:
item = qdelete();
if(item != NULL){
printf("nItem = %d", item);
getch();
}
break;
case 3:
exit(1);
break;
default:
printf("nInvalid choice");
getch();
}
}
}
58
// Program to perform various operations on circular linked list.
#include<stdio.h>
#include<conio.h>
int item;
struct node{
int info;
struct node *link;
};
struct node *start = NULL, *ptr, *save, *new1;
void insert(int item){
new1 = (struct node *) malloc(sizeof(struct node));
if(new1 == NULL){
printf("nOverflow");
getch();
return;
}
new1->info = item;
if(start == NULL){
start = new1;
}
else{
ptr = start;
do{
save = ptr;
ptr = ptr->link;
}while(ptr != start);
save->link = new1;
}
new1->link = start;
}
void delet(){
if(start == NULL){
printf("nUnderflow");
getch();
return;
}
printf("nEnter item to Delete: ");
scanf("%d", &item);
// Find the required node
for(ptr = start; ptr->info != item; save = ptr, ptr = ptr->link)
59
if(ptr->link == start){
printf("nItem not found");
getch();
return;
}
// Check if node is the first and last node
if((ptr == start) && (ptr->link == start)){
start = NULL;
free(ptr);
return;
}
// If more than one node, check if it is the first node
if(ptr == start){
for(save = start; save->link != start; save = save->link);
save->link = start = start->link;
}
// Check if node is the last node
else if(ptr->link == start){
save->link = start;
free(ptr);
}
else{
save->link = ptr->link;
free(ptr);
}
}
void find(int item){
int loc = NULL;
if(start == NULL){
printf("nList is empty.");
}
ptr = start;
do{
if(ptr->info == item){
loc = ptr;
printf("nItem %d found at %d location", item, loc);
break;
}
ptr = ptr->link;
}while(ptr != start);
60
if(loc == NULL)
printf("nItem %d Not found", item);
getch();
}
void traverse(){
if(start == NULL){
printf("nList is Empty.");
return;
}
printf("nElements are: n");
ptr = start;
do{
printf("%d %c ", ptr->info, 16);
ptr = ptr->link;
}while(ptr != start);
}
void main(){
int ch;
while(1){
clrscr();
printf("n******** MAIN MENU ********n");
printf("n1. Insert");
printf("n2. Delete");
printf("n3. Find");
printf("n4. Print");
printf("n5. Exit");
printf("nnEnter your choice: ");
scanf("%d", &ch);
switch(ch){
case 1:
printf("nEnter item to Insert: ");
scanf("%d", &item);
insert(item);
break;
case 2:
delet();
break;
case 3:
printf("nEnter item to Find: ");
scanf("%d", &item);
61
find(item);
break;
case 4:
traverse();
getch();
break;
case 5:
exit(0);
default:
printf("nInvalid choice");
getch();
}
}
}
62
// Program to perform various operations on graphs (traverse (2)).
// Breadth first search (BFS)
#include<stdio.h>
#include<conio.h>
#define MAX 20
typedef struct queue{
int r, f;
int item[MAX];
}que;
int empty(que *);
int full(que *);
void insert(que *, int);
int delet(que *);
void bfs(int);
int g[MAX][MAX], n;
void main(){
int i, j, v;
clrscr();
printf("nEnter number of vertices: ");
scanf("%d", &n);
printf("nEnter adjacency matrix of graph:n");
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
scanf("%d", &g[i][j]);
printf("nEnter the starting node for BFS: ");
scanf("%d", &v);
bfs(v);
getch();
}
void bfs(int v){
int visit[20], i;
que q;
q.r = q.f = -1;
for(i = 0; i < n; i++)
visit[i] = 0;
insert(&q, v);
printf("Visit %d", v);
visit[v] = 1;
while(! empty(&q)){
v = delet(&q);
63
// Visit adjacency matrix of graph
for(i = 0; i < n; i++)
if(visit[i] == 0 && g[v][i] != 0){
insert(&q, i);
visit[i] = 1;
printf("nVisit %d", i);
}
}
}
int empty(que *p){
return ((p->r == -1)? 1: 0);
}
int full(que *p){
return ((p->r == MAX-1)? 1: 0);
}
void insert(que *p, int x){
if(p->r == -1)
p->r = p->f = 0;
else
p->r = p->r+1;
p->item[p->r] = x;
}
int delet(que *p){
int x;
x = p->item[p->f];
if(p->r == p->f)
p->r = p->f = -1;
else
p->f = p->f+1;
return x;
}
/* Output
Enter number of vertices: 6
Enter adjacency matrix of graph:
0 1 1 1 1 0
1 0 0 1 0 0
1 0 0 0 1 1
1 1 0 0 1 0
64
1 0 1 1 0 1
0 0 1 0 1 0
Enter the starting node for BFS: 0
Visit 0
Visit 1
Visit 2
Visit 3
Visit 4
Visit 5
*/
65
// Program to perform various operations on graphs (traverse (2)).
// Depth first search (DFS)
#include<stdio.h>
#include<conio.h>
void dfs(int);
int g[20][20], visit[20], n;
void main(){
int i, j;
clrscr();
printf("nEnter number of vertices: ");
scanf("%d", &n);
printf("nEnter adjacency matrix of graph:n");
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
scanf("%d", &g[i][j]);
for(i = 0; i < n; i++)
visit[i] = 0;
dfs(0);
getch();
}
void dfs(int i){
int j;
printf("nVisit %d", i);
visit[i] = 1;
for(j = 0; j < n; j++)
if(! visit[j] && g[i][j] == 1)
dfs(j);
}
/* Output
Enter number of vertices: 6
Enter adjacency matrix of graph:
0 1 1 1 1 0
1 0 0 1 0 0
1 0 0 0 1 1
1 1 0 0 1 0
1 0 1 1 0 1
0 0 1 0 1 0
66
Visit 0
Visit 1
Visit 3
Visit 4
Visit 2
Visit 5
*/
67
// Program to perform various operations on trees (traverse (3), find, delete,
insertion).
// Traversal with Recursion
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node{
int info;
struct node *left, *right;
} *loc, *par, *root, *save, *ptr;
void find(int item){
// Check if Tree is empty
if(root == NULL){
loc = par = NULL;
return;
}
if(item == root->info){
par = NULL;
loc = root;
return;
}
save = root;
if(item < root->info)
ptr = root->left;
else
ptr = root->right;
while(ptr != NULL){
if(item == ptr->info){
par = save;
loc = ptr;
return;
}
save = ptr;
if(item < ptr->info)
ptr = ptr->left;
else
ptr = ptr->right;
}
loc = NULL;
par = save;
68
}
void insert(int item){
struct node *new1;
find(item);
if(loc != NULL){
printf("nDulpicate Item can not be inserted.");
getch();
return;
}
new1 = (struct node *) malloc(sizeof(struct node));
if(new1 == NULL){
printf("nOverflow");
getch();
return;
}
new1->info = item;
new1->left = new1->right = NULL;
if(par == NULL)
root = new1;
else if(item < par->info)
par->left = new1;
else
par->right = new1;
}
void inorder(struct node *r){
if(r != NULL){
inorder(r->left);
printf("n%d %u", r->info, r);
inorder(r->right);
}
}
void preorder(struct node *r){
if(r != NULL){
printf("n%d %u", r->info, r);
preorder(r->left);
preorder(r->right);
}
}
void postorder(struct node *r){
if(r != NULL){
69
postorder(r->left);
postorder(r->right);
printf("n%d %u", r->info, r);
}
}
void del(){
struct node *ptr, *prev, *next;
int item;
if(root == NULL){
printf("nTree is empty");
getch();
return;
}
printf("nEnter the no. to be deleted: ");
scanf("%d", &item);
ptr = root;
while(ptr != NULL){
if(item == ptr->info)
break;
prev = ptr;
if(item < ptr->info)
ptr = ptr->left;
else
ptr = ptr->right;
}
if(ptr == NULL){
printf("nItem not found");
getch();
return;
}
if((ptr->left != NULL) && (ptr->right != NULL)){
prev = ptr;
next = ptr->right;
while(next->left != NULL){
prev = next;
next = next->right;
}
ptr->info = next->info;
ptr = next;
}
70
else if((ptr->left == NULL) && (ptr->right == NULL)){
if(ptr == root)
root = NULL;
else if(prev->left == ptr)
prev->left = NULL;
else
prev->right = NULL;
}
else if((ptr->left != NULL) && (ptr->right == NULL)){
if(ptr == root)
root = ptr->left;
else if(prev->left == ptr)
prev->left = ptr->left;
else
prev->right = ptr->left;
free(ptr);
}
else if((ptr->left == NULL) && (ptr->right != NULL)){
if(ptr == root)
root = ptr->right;
else if(prev->left == ptr)
prev->left = ptr->right;
else
prev->right = ptr->right;
}
}
void search(){
struct node *ptr, *prev, *next;
int item;
if(root == NULL){
printf("nTree is empty");
return;
}
printf("nEnter the no. to be searched: ");
scanf("%d", &item);
ptr = root;
while(ptr != NULL){
if(item == ptr->info)
break;
prev = ptr;
71
if(item < ptr->info)
ptr = ptr->left;
else
ptr = ptr->right;
}
if(ptr == NULL)
printf("nItem not found");
else
printf("nItem found");
}
void main(){
int item, ch;
root = par = loc = NULL;
while(1){
clrscr();
printf("n1. Insert");
printf("n2. Inorder");
printf("n3. Pre Order");
printf("n4. Post Order");
printf("n5. Delete");
printf("n6. Search");
printf("n7. Exit");
printf("nnEnter your choice: ");
scanf("%d", &ch);
switch(ch){
case 1:
printf("nEnter item to Insert: ");
scanf("%d", &item);
insert(item);
break;
case 2:
inorder(root);
getch();
break;
case 3:
preorder(root);
getch();
break;
case 4:
postorder(root);
72
getch();
break;
case 5:
del();
break;
case 6:
search();
getch();
break;
case 7:
exit(0);
default:
printf("nInvalid choice");
getch();
}
}
}

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Python tuple
Python   tuplePython   tuple
Python tuple
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 
stack & queue
stack & queuestack & queue
stack & queue
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Data Structures Notes 2021
Data Structures Notes 2021Data Structures Notes 2021
Data Structures Notes 2021
 
File handling in c
File handling in cFile handling in c
File handling in c
 
single linked list
single linked listsingle linked list
single linked list
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Strings
StringsStrings
Strings
 
Data structures using c
Data structures using cData structures using c
Data structures using c
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
List in Python
List in PythonList in Python
List in Python
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
 
Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Tree
 
Stack application
Stack applicationStack application
Stack application
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 

Ähnlich wie Data Structures Practical File (20)

Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab files
 
CBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical fileCBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical file
 
Pnno
PnnoPnno
Pnno
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTURE
 
Sorting programs
Sorting programsSorting programs
Sorting programs
 
Singly linked list.pptx
Singly linked list.pptxSingly linked list.pptx
Singly linked list.pptx
 
Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project File
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
Final ds record
Final ds recordFinal ds record
Final ds record
 
Data struture lab
Data struture labData struture lab
Data struture lab
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
Leniar datastructure
Leniar datastructureLeniar datastructure
Leniar datastructure
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
Most Important C language program
Most Important C language programMost Important C language program
Most Important C language program
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 

Kürzlich hochgeladen

PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 

Kürzlich hochgeladen (20)

PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 

Data Structures Practical File

  • 1. S.C.D. Govt. College Practical File Data Structures Submitted to: Prof. Vijay Sehgal Signature Submitted by: Jasbir Singh BCA-3rd sem 7114 Subject Code: BCA-16-307 SESSION 2018-19
  • 2. I have taken efforts in this practical file. I am highly indebted to the Data Structures Lab teacher Prof. Vijay Sehgal for his guidance and constant supervision as well as for providing necessary information regarding the programs and also for his support in completing the practical file. I would like to express my gratitude towards my parents for their kind co-operation and encouragement which helped me in the completion of this practical file. My thanks and appreciations also go to my classmates in developing the practical file and to the people who have willingly helped me out with their abilities. Place: Ludhiana Name: Jasbir Singh Date: 10/11/2018 Signature Acknowledgment
  • 3. Table of Contents S. No. Title Page No. Remarks 1. Program to implement various operations on arrays (insert, delete, traverse, copy and merge). 1 2. Program to implement various operations on a singly linked list (insertion all, deletion all, counting both, creation, traverse and copy). 7 3. Program to implement various operations on a double linked list (insertion all, deletion all, counting both, creation, traverse both and reverse). 16 4. Program to implement various operations on sorted single linked list (insertion, deletion, display/ traversal). 25 5. Program to implement various operations on sorted doubly linked list (insertion, deletion, display/ traversal). 28 6. Program to perform Linear Search. 31 7. Program to perform Binary Search. 32 8. Program to perform Bubble Sort. 33 9. Program to perform Insertion Sort. 34 10. Program to perform Selection Sort. 35 11. Program to perform Merge Sort. 36 12. Program to perform Quick Sort. 38 13. Program to perform various operations on stack using array representation (create, push, pop, peek, empty, full). 40 14. Program to perform various operations on stack using linked list representation (create, push, pop, peek, empty, full). 44 15. Program to perform various operations on queue using array representation (create, insertion, deletion, front, rear, display). 48
  • 4. 16. Program to perform various operations on queue using linked list representation (create, insertion, deletion, front, rear, display). 50 17. Program to implement sparse matrix. 53 18. Program to perform various operations on circular queue. 56 19. Program to perform various operations on circular linked list. 58 20. Program to perform various operations on graphs (traverse (2)). • Breadth first search (BFS) • Depth first search (DFS) 62 65 21. Program to perform various operations on trees (traverse (3), find, delete, insertion). 67
  • 5. 1 // Program to implement various operations on arrays (insert, delete, traverse, copy and merge). #include<stdio.h> #include<conio.h> #define N 10 void insert(); void delet(); void traverse(); void copy(); void merge(); void merge_sort(int *, int, int *, int, int *); void main(){ int ch; while(1){ clrscr(); printf("n******** MAIN MENU ********"); printf("n1. INSERTION "); printf("n2. DELETION "); printf("n3. TRAVERSING "); printf("n4. COPYING "); printf("n5. MERGING "); printf("n6. EXIT "); printf("nnENTER YOUR CHOICE: "); scanf("%d", &ch); switch(ch){ case 1: insert(); break; case 2: delet(); break; case 3: traverse(); break; case 4: copy(); break; case 5: merge();
  • 6. 2 break; case 6: exit(1); break; default: printf("nWrong choice"); getch(); } } } void insert(){ /* This function gets the N-1 elements of Array from the user and gets one another element and location for its Insertion. */ int i, j, la[N], loc, item; clrscr(); printf("nEnter %d elements of an Arrayn", N-1); for(i = 0; i < N-1; i++) scanf("%d", &la[i]); printf("nEnter item to Insert in Array: "); scanf("%d", &item); getagain: printf("nEnter location of array <= %d: ", N); scanf("%d", &loc); if(loc > N) goto getagain; j = N-2; while(j >= loc-1){ la[j+1] = la[j]; j--; } la[loc-1] = item; printf("nElements of Array are: n"); for(i = 0; i < N; i++) printf("%d ", la[i]); getch(); /* Insertion Enter 9 elements of an Array 23 46 69 92 115 161 184 207 230
  • 7. 3 Enter item to Insert in Array: 138 Enter location of array <= 10: 6 Elements of Array are: 23 46 69 92 115 138 161 184 207 230 */ } void delet(){ /* This function gets N elements of Array from the user and gets the location for deletion of an element from array.*/ int i, j, la[N], loc, item; clrscr(); printf("nEnter %d elements of an Array n", N); for(i = 0; i < N; i++) scanf("%d", &la[i]); getagain: printf("nEnter location of element to be deleted <= %dn", N); scanf("%d", &loc); if(loc > N) goto getagain; item = la[loc-1]; for(j = loc-1; j < N-1; j++) la[j] = la[j+1]; printf("nDeleted item is = %d", item); printf("nElements of Array are:n"); for(i = 0; i < N-1; i++) printf("%d ", la[i]); getch(); /* Deletion Enter 10 elements of an Array 18 36 45 54 72 90 108 126 144 162 Enter location of element to be deleted <= 10 3 Deleted item is = 45
  • 8. 4 Elements of Array are: 18 36 54 72 90 108 126 144 162 */ } void traverse(){ // Traversal of Array /* This function gets the elements of array from the user and display these elements to the user. */ int i, la[N]; clrscr(); printf("nEnter %d element of an Array n", N); for(i = 0; i < N; i++) scanf("%d", &la[i]); printf("nElements of Array are: n"); for(i = 0; i < N; i++) printf("%d ", la[i]); getch(); /* Traversal of Array: Enter 10 element of an Array 12 24 36 48 60 72 84 96 108 120 Elements of Array are: 12 24 36 48 60 72 84 96 108 120 */ } void copy(){ /* This function gets N elements of Array from the user and copies all the elements to another array. */ int i, j, la[N], lb[N]; clrscr(); printf("nEnter %d elements of an Arrayn", N); for(i = 0; i < N; i++) scanf("%d", &la[i]); // Copying process for(i = 0; i < N; i++) lb[i] = la[i]; printf("nCopied Elements of Array are:n");
  • 9. 5 for(i = 0; i < N; i++) printf("%d ", lb[i]); getch(); /* Copying Enter 10 elements of an Array 15 30 45 60 75 90 105 120 135 150 Copied Elements of Array are: 15 30 45 60 75 90 105 120 135 150 */ } void merge(){ /* Function to Sort the elements by Merge Sort Technique */ int la[100], lb[100], lc[200]; int asize, bsize, i, j, k; clrscr(); printf("nEnter the size of First Listn"); scanf("%d", &asize); printf("nEnter %d elements for sorted Array - In", asize); for(i = 0; i < asize; i++) scanf("%d", &la[i]); printf("nEnter the size of Second Listn"); scanf("%d", &bsize); printf("nEnter %d elements for sorted Array - IIn", bsize); for(j = 0; j < bsize; j++) scanf("%d", &lb[j]); merge_sort(la, asize, lb, bsize, lc); printf("nSorted List after applying merge sort is n"); for(k = 0; k < (asize + bsize); k++) printf("%d ", lc[k]); getch(); /* Merging Enter the size of First List 7 Enter 7 elements for sorted Array - I
  • 10. 6 5 14 15 18 51 55 88 Enter the size of Second List 6 Enter 6 elements for sorted Array - II 3 7 9 25 48 76 Sorted List after applying merge sort is 3 5 7 9 14 15 18 25 48 51 55 76 88 */ } void merge_sort(int la[], int m, int lb[], int n, int lc[]){ int i, j, k; i = j = k = 0; while(i < m && j < n){ if(la[i] < lb[j]){ lc[k] = la[i]; i++; } else{ lc[k] = lb[j]; j++; } k++; } while(i < m){ lc[k] = la[i]; i++; k++; } while(j < n){ lc[k] = lc[j]; j++; k++; } }
  • 11. 7 // Program to implement various operations on a singly linked list (insertion all, deletion all, counting both, creation, traverse and copy). #include<stdio.h> #include<conio.h> #include<malloc.h> int ele, item; struct node{ int info; struct node *link; }; struct node *start = NULL, *ptr, *loc = NULL, *locp = NULL, *new1, *save, *temp, *l2; struct node * searchp(int ele){ if(ele == start->info){ loc = start; locp = NULL; return locp; } save = start; ptr = start->link; while(ptr != NULL){ if(ele == ptr->info){ locp = save; loc = ptr; return locp; } save = ptr; ptr = ptr->link; } locp = save; return locp; } struct node * search(int ele){ ptr = start; while(ptr != NULL){ if(ptr->info == ele){ loc = ptr; printf("nThe element %d present at %u", ptr->info, ptr); getch(); return loc;
  • 12. 8 } ptr = ptr->link; } return NULL; } void insrbeg(int item){ new1 = (struct node *) malloc(sizeof(struct node)); if(new1 == NULL){ printf("nOverflow"); getch(); return; } new1->info = item; new1->link = NULL; if(start == NULL){ start = new1; } else{ new1->link = start; start = new1; } } void insrend(int item){ new1 = (struct node *) malloc(sizeof(struct node)); if(new1 == NULL){ printf("nOverflow"); getch(); return; } new1->info = item; new1->link = NULL; if(start == NULL){ start = new1; } else{ save = start; ptr = start->link; while(ptr != NULL){ save = ptr; ptr = ptr->link;
  • 13. 9 } save->link = new1; } } void insrbef(int item){ printf("nEnter the element Before which you want to insert: "); scanf("%d", &ele); locp = searchp(ele); loc = search(ele); if(loc == NULL){ printf("nItem Not Found !"); getch(); return; } new1 = (struct node *) malloc(sizeof(struct node)); if(new1 == NULL){ printf("nOverflow"); getch(); return; } new1->info = item; new1->link = NULL; if(locp == NULL){ new1->link = start; start = new1; } else{ new1->link = locp->link; locp->link = new1; } } void insraft(int item){ printf("nEnter the element After which you want to insert: "); scanf("%d", &ele); loc = search(ele); if(loc == NULL){ printf("nSorry, location not found!"); getch(); return; }
  • 14. 10 new1 = (struct node *) malloc(sizeof(struct node)); if(new1 == NULL){ printf("nOverflow"); getch(); return; } new1->info = item; new1->link = loc->link; loc->link = new1; } void deletbeg(){ if(start == NULL){ printf("nUnderflow"); getch(); return; } else{ temp = start; start = start->link; free(temp); } } void deletend(){ if(start == NULL){ printf("nUnderflow"); getch(); return; } save = start; ptr = start->link; while(ptr->link != NULL){ save = ptr; ptr = ptr->link; } if(save->link == NULL){ temp = start; start = start->link; free (temp); } temp = ptr;
  • 15. 11 save->link = NULL; free(temp); } void delet(int item){ locp = searchp(item); if(loc == NULL){ printf("nItem not found"); getch(); return; } if(locp == NULL){ temp = start; start = start->link; free(temp); } temp = loc; locp->link = loc->link; free(temp); } void count1(){ int num = 0; for(ptr = start; ptr != NULL; ptr = ptr->link) num++; printf("nTotal number of elements = %d", num); } void count2(int ele){ int num = 0; for(ptr = start; ptr != NULL; ptr = ptr->link) if(ptr->info == ele) num++; printf("nElement %d exists %d times in the list.", ele, num); } void create(){ int i, n; start = NULL; printf("nEnter the no. of elements: "); scanf("%d", &n); printf("nEnter elements: "); for(i = 1; i <= n; i++){
  • 16. 12 new1 = (struct node *) malloc(sizeof(struct node)); if(new1 == NULL){ printf("nOverflow"); getch(); return; } scanf("%d", &new1->info); new1->link = NULL; if(start == NULL){ start = new1; ptr = start; } else ptr = ptr->link = new1; } } void traverse(){ if(start == NULL){ printf("nEmpty List"); return; } for(ptr = start; ptr != NULL; ptr = ptr->link) printf("nThe element %d present at location %u", ptr->info, ptr); } void copy(){ l2 = NULL; if(start == NULL){ printf("nEmpty List"); return; } new1 = (struct node *) malloc(sizeof(struct node)); if(new1 == NULL){ printf("nOverflow"); getch(); return; } new1->info = start->info; new1->link = NULL; loc = l2 = new1;
  • 17. 13 for(ptr = start->link; ptr != NULL; ptr = ptr->link){ new1 = (struct node *) malloc(sizeof(struct node)); if(new1 == NULL){ printf("nOverflow"); getch(); return; } new1->info = ptr->info; new1->link = NULL; loc = loc->link = new1; } } void main(){ int ch; while(1){ clrscr(); printf("n******** MAIN MENU ********n"); printf("n 1. INSERTION AT BEGINNING"); printf("n 2. INSERTION AT END"); printf("n 3. INSERTION BEFORE AN ELEMENT"); printf("n 4. INSERTION AFTER AN ELEMENT"); printf("n 5. DELETION FROM BEGINNING"); printf("n 6. DELETION FROM ENDING"); printf("n 7. DELETION AN ITEM"); printf("n 8. COUNT TOTAL NO. OF ELEMENTS "); printf("n 9. COUNTING A PARTICULAR ELEMENT"); printf("n10. CREATION"); printf("n11. TRAVERSING"); printf("n12. COPYING"); printf("n13. EXIT"); printf("nnENTER YOUR CHOICE: "); scanf("%d", &ch); switch(ch){ case 1: printf("nEnter an element "); scanf("%d", &item); insrbeg(item); break; case 2: printf("nEnter an element ");
  • 18. 14 scanf("%d", &item); insrend(item); break; case 3: printf("nEnter the element which you want to insert: "); scanf("%d", &item); insrbef(item); break; case 4: printf("nEnter the element you want to insert: "); scanf("%d", &item); insraft(item); break; case 5: deletbeg(); break; case 6: deletend(); break; case 7: printf("nEnter the element which you want to delete: "); scanf("%d", &ele); delet(ele); break; case 8: count1(); getch(); break; case 9: printf("nEnter the element which you want to count: "); scanf("%d", &ele); count2(ele); getch(); break; case 10: create(); break; case 11: traverse(); getch();
  • 20. 16 // Program to implement various operations on a double linked list (insertion all, deletion all, counting both, creation, traverse both and reverse). #include<stdio.h> #include<conio.h> #include<malloc.h> int item, ele; struct node{ int info; struct node *prev, *link; }; struct node *temp, *new1, *start = NULL, *end = NULL, *loc = NULL, *ptr; struct node * search(int item){ for(ptr = start; ptr != NULL; ptr = ptr->link) if(item == ptr->info){ loc = ptr; printf("nItem found at loc %u", loc); return loc; } return NULL; } void insrbeg(int item){ new1 = (struct node *) malloc(sizeof(struct node)); if(new1 == NULL){ printf("nOverflow"); getch(); return; } new1->prev = new1->link = NULL; new1->info = item; if(start == NULL){ start = end = new1; } else{ new1->link = start; start = start->prev = new1; } } void insrend(int item){
  • 21. 17 new1 = (struct node *) malloc(sizeof(struct node)); if(new1 == NULL){ printf("nOverflow"); getch(); return; } new1->prev = new1->link = NULL; new1->info = item; if(start == NULL){ start = end = new1; } else{ new1->prev = end; end = end->link = new1; } } void insrbef(){ printf("nEnter the element before which you want to insert: "); scanf("%d", &ele); loc = search (ele); if(loc == NULL){ printf("nLocation not found"); getch(); return; } new1 = (struct node *) malloc(sizeof(struct node)); if(new1 == NULL){ printf("nOverflow"); getch(); return; } new1->prev = new1->link = NULL; new1->info = item; if(loc == start){ new1->link = start; start = start->prev = new1; } else{ new1->prev = loc->prev;
  • 22. 18 new1->link = loc; loc->prev = loc->prev->link = new1; } } void insraft(){ printf("nEnter the element after which you want to insert: "); scanf("%d", &ele); loc = search(ele); if(loc == NULL){ printf("nLocation not found"); getch(); return; } new1 = (struct node *) malloc(sizeof(struct node)); if(new1 == NULL){ printf("nOverflow"); getch(); return; } new1->prev = new1->link = NULL; new1->info = item; if(loc == end){ new1->prev = end; end = end->link = new1; } else{ new1->prev = loc; new1->link = loc->link; loc->link = loc->link->prev = new1; } } void deletbeg(){ if(start == NULL){ printf("nUnderflow"); getch(); return; } if(start == end){ temp = start; start = end = NULL;
  • 23. 19 free(temp); return; } else{ temp = start; start->link->prev = NULL; start = start->link; free(temp); } } void deletend(){ if(start == NULL){ printf("nUnderflow"); getch(); return; } if(start == end){ temp = start; start = end = NULL; free(temp); return; } else{ temp = end; end->prev->link = NULL; end = end->prev; free(temp); } } void delet(int ele){ loc = search(ele); if(loc == NULL){ printf("nItem %d not found", ele); getch(); return; } if(loc->prev == NULL){ temp = start; if(start == end){ start = end = NULL;
  • 24. 20 } else{ start->link->prev = NULL; start = start->link; } free(temp); getch(); return; } if(loc->link == NULL){ temp = end; end->prev->link = NULL; end = end->prev; free(temp); getch(); return; } temp = loc; loc->prev->link = loc->link; loc->link->prev = loc->prev; free(temp); printf("n%d has been deleted", ele); getch(); } void count1(){ int num = 0; for(ptr = start; ptr != NULL; ptr = ptr->link) num++; printf("nTotal elements = %d", num); } void count2(int ele){ int num = 0; for(ptr = start; ptr != NULL; ptr = ptr->link) if(ele == ptr->info) num++; printf("nElement %d exists %d times in the list.", ele, num); } void create(){ int i, n; start = NULL;
  • 25. 21 printf("nEnter the number of elements: "); scanf("%d", &n); printf("nEnter elements: "); for(i = 1 ; i <= n; i++){ new1 = (struct node *) malloc(sizeof(struct node)); if(new1 == NULL){ printf("nOverflow"); getch(); return; } new1->prev = new1->link = NULL; scanf("%d", &new1->info); if(start == NULL){ start = end = new1; } else{ new1->prev = end; end = end->link = new1; } } } void travbeg(){ printf("nItem in the lists are "); printf("nItem tLoc"); for(ptr = start; ptr != NULL; ptr = ptr->link) printf("n%d t%u", ptr->info, ptr); } void travend(){ printf("nItem in the lists are "); printf("nItem tLoc"); for(ptr = end; ptr != NULL; ptr = ptr->prev) printf("n%d t%u", ptr->info, ptr); } void reverse(){ temp = NULL; for(ptr = start; ptr != NULL; ptr = ptr->prev){ temp = ptr->link; ptr->link = ptr->prev; ptr->prev = temp; if(ptr->prev == NULL)
  • 26. 22 start = ptr; if(ptr->link == NULL) end = ptr; } } void main(){ int ch; while(1){ clrscr(); printf("n******** MAIN MENU ********n"); printf("n 1. Insert at Beginning"); printf("n 2. Insert at End"); printf("n 3. Insert Before"); printf("n 4. Insert After"); printf("n 5. Delete Beg"); printf("n 6. Delete End"); printf("n 7. Delete Item"); printf("n 8. Count total no. of elements"); printf("n 9. Counting a particular element"); printf("n10. Creation"); printf("n11. Traversing beginning"); printf("n12. Traversing end"); printf("n13. Reversing"); printf("n14. Exit"); printf("nnEnter your choice: "); scanf("%d", &ch); switch(ch){ case 1: printf("nEnter item to Insert: "); scanf("%d", &item); insrbeg(item); break; case 2: printf("nEnter item to Insert: "); scanf("%d", &item); insrend(item); break; case 3: printf("nEnter item to Insert: "); scanf("%d", &item);
  • 27. 23 insrbef(); break; case 4: printf("nEnter item to Insert: "); scanf("%d", &item); insraft(); break; case 5: deletbeg(); travbeg(); getch(); break; case 6: deletend(); travend(); getch(); break; case 7: printf("nEnter the element which you want to delete: "); scanf("%d", &ele); delet(ele); break; case 8: count1(); getch(); break; case 9: printf("nEnter the element you want to count: "); scanf("%d", &ele); count2(ele); getch(); break; case 10: create(); break; case 11: travbeg(); getch(); break; case 12:
  • 29. 25 // Program to implement various operations on sorted single linked list (insertion, deletion, display/ traversal). #include<stdio.h> #include<conio.h> #include<malloc.h> int item; struct node{ int info; struct node *link; }; struct node *start = NULL, *new1, *save, *ptr; void insert_sorted(int item){ new1 = (struct node *) malloc(sizeof(struct node)); if(new1 == NULL){ printf("nOverflow"); getch(); return; } new1->info = item; new1->link = NULL; if(start == NULL){ start = new1; return; } if(item < start->info){ new1->link = start; start = new1; return; } for(save = start, ptr = start->link; ptr != NULL; save = ptr, ptr = ptr->link) if(item < ptr->info){ new1->link = ptr; save->link = new1; return; } save->link = new1; } void delet_sorted(int item){ if(start == NULL){ printf("Underflow");
  • 30. 26 getch(); return; } ptr = start; if(item == start->info){ start = start->link; free(ptr); return; } for(save = ptr, ptr = ptr->link; (ptr->info <= item) && (ptr != NULL); save = ptr, ptr = ptr->link) if(item == ptr->info){ save->link = ptr->link; free(ptr); return; } printf("nItem %d not found", item); getch(); } void traversal(){ printf("nElements are: n"); for(ptr = start; ptr != NULL; ptr = ptr->link) printf("%d %c ", ptr->info, 16); } void main(){ int ch; while(1){ clrscr(); printf("n******** MAIN MENU ********n"); printf("n1. Insertion in a Sorted Linked List"); printf("n2. Deletion in a Sorted Linked List"); printf("n3. Display/ Traversal"); printf("n4. Exit"); printf("nnEnter your choice: "); scanf("%d", &ch); switch(ch){ case 1: printf("nEnter item to Insert: "); scanf("%d", &item); insert_sorted(item);
  • 31. 27 break; case 2: printf("nEnter item to Delete: "); scanf("%d", &item); delet_sorted(item); break; case 3: traversal(); getch(); break; case 4: exit(1); default: printf("nWrong choice"); getch(); } } }
  • 32. 28 // Program to implement various operations on sorted doubly linked list (insertion, deletion, display/ traversal). #include<stdio.h> #include<conio.h> #include<malloc.h> int item; struct node{ int info; struct node *link, *prev; }; struct node *start = NULL, *end = NULL, *ptr, *save, *new1; void insert_sorted(int item){ new1 = (struct node *) malloc(sizeof(struct node)); if(new1 == NULL){ printf("nOverflow"); getch(); return; } new1->prev = new1->link = NULL; new1->info = item; if(start == NULL){ start = end = new1; return; } if(item < start->info){ new1->link = start; start = start->prev = new1; return; } for(ptr = start->link; ptr != NULL; ptr = ptr->link) if(item < ptr->info){ new1->prev = ptr->prev; new1->link = ptr; ptr->prev = ptr->prev->link = new1; return; } new1->prev = end; end = end->link = new1; } void delet_sorted(int item){
  • 33. 29 if(start == NULL){ printf("Underflow"); getch(); return; } ptr = start; if(item == start->info){ if(start == end){ start = end = NULL; } else{ start->link->prev = NULL; start = start->link; } free(ptr); return; } for(ptr = ptr->link; (ptr->info <= item) && (ptr != NULL); ptr = ptr->link) if(item == ptr->info){ if(ptr->link == NULL){ end = end->prev; } ptr->link->prev = ptr->prev; ptr->prev->link = ptr->link; free(ptr); return; } printf("nItem %d not found", item); getch(); } void traversal(){ printf("nElements are: n"); for(ptr = start; ptr != NULL; ptr = ptr->link) printf("%d %c ", ptr->info, 16); } void main(){ int ch; while(1){ clrscr(); printf("n******** MAIN MENU ********n");
  • 34. 30 printf("n1. Insertion in a Sorted Linked List"); printf("n2. Deletion in a Sorted Linked List"); printf("n3. Display/ Traversal"); printf("n4. Exit"); printf("nnEnter your choice: "); scanf("%d", &ch); switch(ch){ case 1: printf("nEnter item to Insert: "); scanf("%d", &item); insert_sorted(item); break; case 2: printf("nEnter item to Delete: "); scanf("%d", &item); delet_sorted(item); break; case 3: traversal(); getch(); break; case 4: exit(1); default: printf("nWrong choice"); getch(); } } }
  • 35. 31 // Program to perform Linear Search. #include<stdio.h> #include<conio.h> #define N 7 void main(){ int a[N], loc, item; int i; clrscr(); printf("nEnter %d elements of an Arrayn", N); for(i = 0; i < N; i++) scanf("%d", &a[i]); printf("nEnter item to search: "); scanf("%d", &item); loc = NULL; for(i = 0; i < N; i++) if(item == a[i]){ loc = i+1; break; } if(loc == NULL) printf("nItem %d Not found in List", item); else printf("nItem %d found at %d location.", item, loc); getch(); } /* Output Enter 7 elements of an Array 9 7 11 6 3 -10 15 Enter item to search: 3 Item 3 found at 5 location. */
  • 36. 32 // Program to perform Binary Search. #include<stdio.h> #include<conio.h> #define N 6 void main(){ int a[N], item, beg, end, mid; int i; clrscr(); printf("nEnter %d elements of an Arrayn", N); for(i = 0; i < N; i++) scanf("%d", &a[i]); printf("nEnter item to Search: "); scanf("%d", &item); beg = 0; end = N-1; mid = (beg+end)/2; while((beg <= end) && a[mid] != item){ if(a[mid] > item) end = mid-1; else beg = mid+1; mid = (beg+end)/2; } if(a[mid] != item) printf("nItem %d Not found in List", item); else printf("nItem %d found at %d location.", item, mid+1); getch(); } /* Output Enter 6 elements of an Array 5 10 15 20 25 30 Enter item to Search: 25 Item 25 found at 5 location. */
  • 37. 33 // Program to perform Bubble Sort. #include<stdio.h> #include<conio.h> #define N 10 void main(){ int a[N], i, j; clrscr(); printf("nEnter %d elements of an Arrayn", N); for(i = 0; i < N; i++) scanf("%d", &a[i]); for(i = 0; i < N-1; i++) for(j = 0; j < N-i-1; j++) if(a[j] > a[j+1]){ a[j] = a[j] + a[j+1]; a[j+1] = a[j] - a[j+1]; a[j] = a[j] - a[j+1]; } printf("nSorted Array: "); for(i = 0; i < N; i++) printf("%d ", a[i]); getch(); } /* Output Enter 10 elements of an Array 6 3 -5 37 6 37 57 27 478 4 Sorted Array: -5 3 4 6 6 27 37 37 57 478 */
  • 38. 34 // Program to perform Insertion Sort. #include<stdio.h> #include<conio.h> #define N 10 void main(){ int a[N], i, k, temp, ptr; clrscr(); printf("nEnter the elements: n"); for(i = 0; i < N; i++) scanf("%d", &a[i]); for(k = 1; k < N; k++){ temp = a[k]; ptr = k-1; while((temp < a[ptr]) && (ptr >= 0)){ a[ptr+1] = a[ptr]; ptr--; } a[ptr+1] = temp; } printf("nSorted Arrayn"); for(i = 0; i < N; i++) printf("%d ", a[i]); getch(); } /* Output Enter the elements: 10 2 34 5 36 35 67 75 67 46 Sorted Array 2 5 10 34 35 36 46 67 67 75 */
  • 39. 35 // Program to perform Selection Sort. #include<stdio.h> #include<conio.h> #define N 10 void main(){ int a[N], i, j, min, loc; clrscr(); printf("nEnter the elements: n"); for(i = 0; i < N; i++) scanf("%d", &a[i]); for(i = 0; i < N-1; i++){ min = a[i]; loc = i; for(j = i+1; j < N; j++) if(min > a[j]){ min = a[j]; loc = j; } if(a[i] != a[loc]){ a[i] = a[i] + a[loc]; a[loc] = a[i] - a[loc]; a[i] = a[i] - a[loc]; } } printf("nSorted elements of arrayn"); for(i = 0; i < N; i++) printf("%d ", a[i]); getch(); } /* Output Enter the elements: 12 3 35 89 2 57 75 57 89 45 Sorted elements of array 2 3 12 35 45 57 57 75 89 89 */
  • 40. 36 // Program to perform Merge Sort. #include<stdio.h> #include<conio.h> #define N 100 void merge_sort(int *, int, int); void merge(int *, int, int, int); void merge_sort(int a[], int low, int high){ int mid; if(low < high){ mid = (low+high)/ 2; merge_sort(a, low, mid); merge_sort(a, mid+1, high); merge(a, low, mid, high); } } void merge(int a[], int low, int mid, int high){ int h, i, j, k, temp[N]; h = i = low; j = mid+1; while((h <= mid) && (j <= high)){ if(a[h] <= a[j]){ temp[i] = a[h]; h++; } else{ temp[i] = a[j]; j++; } i++; } while(h <= mid){ temp[i] = a[h]; i++; h++; } while(j <= high){ temp[i] = a[j]; i++; j++; }
  • 41. 37 for(k = low; k <= high; k++) a[k] = temp[k]; } void main(){ int i, a[N], size; clrscr(); printf("nEnter the size of Listn"); scanf("%d", &size); printf("nEnter %d elements of Listn", size); for(i = 0; i < size; i++) scanf("%d", &a[i]); merge_sort(a, 0, size-1); printf("nSorted elements are:n"); for(i = 0; i < size; i++) printf("%d ", a[i]); getch(); } /* Output Enter the size of List 6 Enter 6 elements of List 90 28 39 10 23 45 Sorted elements are: 10 23 28 39 45 90 */
  • 42. 38 // Program to perform Quick Sort. #include<stdio.h> #include<conio.h> #define N 100 void q_sort(int *, int, int); void q_sort(int array[], int left, int right){ int pivot, l_hold, r_hold; l_hold = left; r_hold = right; pivot = array[left]; while(left < right){ while((array[right] >= pivot) && (left < right)) right--; if(left != right){ array[left] = array[right]; left++; } while((array[left] <= pivot) && (left < right)) left++; if(left != right){ array[right] = array[left]; right--; } } array[left] = pivot; pivot = left; left = l_hold; right = r_hold; if(left < pivot) q_sort(array, left, pivot-1); if(right > pivot) q_sort(array, pivot+1, right); } void main(){ int a[N], i, size; clrscr(); printf("nEnter the size of listn"); scanf("%d", &size); printf("nEnter %d elements of the listn", size); for(i = 0; i < size; i++)
  • 43. 39 scanf("%d", &a[i]); q_sort(a, 0, size-1); printf("nElements after sorting are:n"); for(i = 0; i < size; i++) printf("%d ", a[i]); getch(); } /* Output Enter the size of list 8 Enter 8 elements of the list 18 2 98 25 58 21 20 6 Elements after sorting are: 2 6 18 20 21 25 58 98 */
  • 44. 40 // Program to perform various operations on stack using array representation (create, push, pop, peek, empty, full). #include<stdio.h> #include<conio.h> #define Max 10 int stack[Max], top = -1; // Stack Creation and initializing the top void push(int item){ // Check for stack full if(top == Max-1){ printf("nStack Full"); getch(); return; } top++; // Assigning the item at the top of stack stack[top] = item; } int pop(){ int item; // Check for empty stack if(top == -1){ printf("nStack is Empty"); return NULL; } // Removing item from the top of stack item = stack[top]; // Updating the top top--; return item; } int peek(){ int item; // Check for empty stack if(top == -1){ printf("nUnderflow"); return NULL; } // Assigning the topmost element item = stack[top];
  • 45. 41 return item; } void main(){ int ch, item; while(1){ clrscr(); printf("n1. Push"); printf("n2. Pop"); printf("n3. Peek"); printf("n4. Exit"); printf("nnEnter your choice: "); /* Clearing the input buffer */ fflush(stdin); scanf("%d", &ch); switch(ch){ case 1: printf("nEnter item to push: "); scanf("%d", &item); push(item); break; case 2: item = pop(); if(item != NULL) printf("nItem from the top of stack = %d", item); getch(); break; case 3: item = peek(); if(item != NULL) printf("nItem at the top of stack = %d", item); getch(); break; case 4: exit(1); default: printf("nInvalid input"); getch(); break; } }
  • 46. 42 } /* Output 1. Push 2. Pop 3. Peek 4. Exit Enter your choice: 1 Enter item to push: 10 1. Push 2. Pop 3. Peek 4. Exit Enter your choice: 3 Item at the top of stack = 10 1. Push 2. Pop 3. Peek 4. Exit Enter your choice: 2 Item from the top of stack = 10 1. Push 2. Pop 3. Peek 4. Exit Enter your choice: 2 Stack is Empty 1. Push 2. Pop 3. Peek 4. Exit Enter your choice: 5
  • 47. 43 Invalid input 1. Push 2. Pop 3. Peek 4. Exit Enter your choice: 4 */
  • 48. 44 // Program to perform various operations on stack using linked list representation (create, push, pop, peek, empty, full). #include<stdio.h> #include<conio.h> // Defining node structure for link list struct node{ int info; struct node *link; }; struct node *top = NULL; void push(int item){ struct node *new1; // Allocate new memory for stack new1 = (struct node *) malloc(sizeof(struct node)); // Check for memory availability if(new1 == NULL){ printf("nOverflow"); getch(); return; } // Assigning item to new allocated node for stack new1->info = item; new1->link = NULL; // Updating the stack top pointer new1->link = top; top = new1; } int pop(){ int item; struct node *temp; // Check for empty stack if(top == NULL){ printf("nUnderflow"); return NULL; } // Removing item from top of stack item = top->info; // Remembering the address of stack top temp = top; // Updating the top pointer
  • 49. 45 top = top->link; // Releasing the memory free(temp); return item; } int peek(){ int item; // Check for empty stack if(top == NULL){ printf("nUnderflow"); return NULL; } // Assigning the topmost element item = top->info; return item; } void main(){ int ch, item; while(1){ clrscr(); printf("n1. Push"); printf("n2. Pop"); printf("n3. Peek"); printf("n4. Exit"); printf("nnEnter your choice: "); // Clearing the input buffer fflush(stdin); scanf("%d", &ch); switch(ch){ case 1: printf("nEnter item to push: "); scanf("%d", &item); push(item); break; case 2: item = pop(); if(item != NULL) printf("nItem from the top of stack = %d", item); getch(); break;
  • 50. 46 case 3: item = peek(); if(item != NULL) printf("nItem at the top of stack = %d", item); getch(); break; case 4: exit(1); default: printf("nInvalid input"); getch(); } } } /* Output 1. Push 2. Pop 3. Peek 4. Exit Enter your choice: 1 Enter item to push: 89 1. Push 2. Pop 3. Peek 4. Exit Enter your choice: 3 Item at the top of stack = 89 1. Push 2. Pop 3. Peek 4. Exit Enter your choice: 2 Item from the top of stack = 89 1. Push
  • 51. 47 2. Pop 3. Peek 4. Exit Enter your choice: 2 Stack is Empty 1. Push 2. Pop 3. Peek 4. Exit Enter your choice: 5 Invalid input 1. Push 2. Pop 3. Peek 4. Exit Enter your choice: 4 */
  • 52. 48 // Program to perform various operations on queue using array representation (create, insertion, deletion, front, rear, display). #include<stdio.h> #include<conio.h> #include<process.h> #define N 5 int Q[N], front, rear, item; void qinsert(int item){ if(rear == N-1){ printf("nOverflow"); getch(); return; } if((rear == -1) && (front == -1)) rear = front = 0; else rear++; Q[rear] = item; } int qdelete(){ if(front == -1){ printf("nUnderflow"); return NULL; } item = Q[front]; if(front == rear) front = rear = -1; else front++; return item; } void display(){ int i; if(front == -1) printf("nQueue is empty"); else{ printf("nQueue is: n"); for(i = front; i <= rear; i++) printf("%d ", Q[i]); }
  • 53. 49 } void main(){ int ch; front = rear = -1; while(1){ clrscr(); printf("n******** MAIN MENU ********n"); printf("n1. Insert"); printf("n2. Delete"); printf("n3. Display"); printf("n4. Exit"); printf("nnEnter your choice: "); scanf("%d", &ch); switch(ch){ case 1: printf("nEnter the element: "); scanf("%d", &item); qinsert(item); break; case 2: item = qdelete(); if(item != NULL) printf("nItem = %d", item); getch(); break; case 3: display(); getch(); break; case 4: exit(1); default: printf("nInvalid choice"); getch(); } } }
  • 54. 50 // Program to perform various operations on queue using linked list representation (create, insertion, deletion, front, rear, display). #include<stdio.h> #include<conio.h> #include<process.h> struct node{ int info; struct node *link; } *front = NULL, *rear = NULL; void qinsert(int item){ struct node *new1; new1 = (struct node *) malloc(sizeof(struct node)); if(new1 == NULL){ printf("nOverflow"); getch(); return; } new1->info = item; new1->link = NULL; if(front == NULL) front = rear = new1; else rear = rear->link = new1; } int qdelete(){ int item; struct node *temp; if(front == NULL){ printf("nUnderflow"); return NULL; } item = front->info; temp = front; if(front == rear) front = rear = NULL; else front = front->link; free(temp); return item; }
  • 55. 51 void display(){ struct node *ptr; if(front == NULL){ printf("nQueue is Empty"); return; } printf("nElements of Queue are: n"); for(ptr = front; ptr != NULL; ptr = ptr->link) printf("%d ", ptr->info); } void main(){ int ch, item; while(1){ clrscr(); printf("n******** Menu ********"); printf("n1. Insert"); printf("n2. Delete"); printf("n3. Display"); printf("n4. Exit"); printf("n**********************"); printf("nEnter your choice: "); fflush(stdin); scanf("%d", &ch); switch(ch){ case 1: printf("nEnter item to Insert: "); scanf("%d", &item); qinsert(item); break; case 2: item = qdelete(); if(item != NULL) printf("nGiven item is %d", item); getch(); break; case 3: display(); getch(); break; case 4:
  • 57. 53 // Program to implement sparse matrix. #include<stdio.h> #include<conio.h> int a[100][100], b[100][100]; void main(){ int i, m, n, p, q, col, t; clrscr(); printf("nEnter the no. of rows: "); scanf("%d", &a[0][0]); printf("nEnter the no. of cols: "); scanf("%d", &a[0][1]); printf("nEnter the no. of non zero terms: "); scanf("%d", &a[0][2]); for(i = 1; i <= a[0][2]; i++){ printf("nEnter the value (that is non zero): "); scanf("%d", &a[i][2]); printf("Enter the row for %d: ", a[i][2]); scanf("%d", &a[i][0]); printf("Enter the col for %d: ", a[i][2]); scanf("%d", &a[i][1]); } /* Printing for testing the sparse input */ printf("nThe matrix you entered is"); printf("n*************************"); printf("nRow tCol tValue "); printf("n*************************"); for(i = 0; i <= a[0][2]; i++) printf("n%d t%d t%d", a[i][0], a[i][1], a[i][2]); exit(0); /* Calling function for evaluation of transpose */ m = a[0][0]; n = a[0][1]; t = a[0][2]; b[0][0] = n; b[0][1] = m; b[0][2] = t; q = 1; for(col = 1; col <= n; col++) for(p = 1; p <= t; p++) if(a[p][1] == col){
  • 58. 54 b[q][0] = a[p][1]; b[q][1] = a[p][0]; b[q][2] = a[p][2]; q++; } getch(); /* Printing the transposed matrix */ printf("nnThe Transpose of the above matrix is: "); for(i = 0; i <= a[0][2]; i++) printf("n%d t%d t%d", b[i][0], b[i][1], b[i][2]); getch(); } /* Output Enter the no. of rows: 5 Enter the no. of cols: 4 Enter the no. of non zero terms: 3 Enter the value (that is non zero): 1 Enter the row for 1: 3 Enter the col for 1: 1 Enter the value (that is non zero): 2 Enter the row for 2: 4 Enter the col for 2: 3 Enter the value (that is non zero): 1 Enter the row for 1: 5 Enter the col for 1: 3 The matrix you entered is ************************* Row Col Value ************************* 5 4 3 3 1 1 4 3 2 5 3 1
  • 59. 55 The Transpose of the above matrix is: 4 5 3 1 3 1 3 4 2 3 5 1 */
  • 60. 56 // Program to perform various operations on circular queue. #include<stdio.h> #include<conio.h> #include<process.h> #define N 5 int q[N], front, rear, item; void qinsert(int item){ if((front == 0 && rear == N-1) || (front == rear+1)){ printf("nQueue is Full"); getch(); return; } if(front == -1) front = rear = 0; else if(front == N-1) rear = 0; else rear++; q[rear] = item; } int qdelete(){ if(front == -1){ printf("nQueue is Empty"); getch(); return NULL; } item = q[front]; if(front == rear) front = rear = -1; else if(front == N-1) front = 0; else front++; return item; } void main(){ int ch; front = rear = -1; while(1){ clrscr();
  • 61. 57 printf("n******** MAIN MENU ********n"); printf("n1. Insert"); printf("n2. Delete"); printf("n3. Exit"); printf("nEnter your choice: "); scanf("%d", &ch); switch(ch){ case 1: printf("nEnter the element: "); scanf("%d", &item); qinsert(item); break; case 2: item = qdelete(); if(item != NULL){ printf("nItem = %d", item); getch(); } break; case 3: exit(1); break; default: printf("nInvalid choice"); getch(); } } }
  • 62. 58 // Program to perform various operations on circular linked list. #include<stdio.h> #include<conio.h> int item; struct node{ int info; struct node *link; }; struct node *start = NULL, *ptr, *save, *new1; void insert(int item){ new1 = (struct node *) malloc(sizeof(struct node)); if(new1 == NULL){ printf("nOverflow"); getch(); return; } new1->info = item; if(start == NULL){ start = new1; } else{ ptr = start; do{ save = ptr; ptr = ptr->link; }while(ptr != start); save->link = new1; } new1->link = start; } void delet(){ if(start == NULL){ printf("nUnderflow"); getch(); return; } printf("nEnter item to Delete: "); scanf("%d", &item); // Find the required node for(ptr = start; ptr->info != item; save = ptr, ptr = ptr->link)
  • 63. 59 if(ptr->link == start){ printf("nItem not found"); getch(); return; } // Check if node is the first and last node if((ptr == start) && (ptr->link == start)){ start = NULL; free(ptr); return; } // If more than one node, check if it is the first node if(ptr == start){ for(save = start; save->link != start; save = save->link); save->link = start = start->link; } // Check if node is the last node else if(ptr->link == start){ save->link = start; free(ptr); } else{ save->link = ptr->link; free(ptr); } } void find(int item){ int loc = NULL; if(start == NULL){ printf("nList is empty."); } ptr = start; do{ if(ptr->info == item){ loc = ptr; printf("nItem %d found at %d location", item, loc); break; } ptr = ptr->link; }while(ptr != start);
  • 64. 60 if(loc == NULL) printf("nItem %d Not found", item); getch(); } void traverse(){ if(start == NULL){ printf("nList is Empty."); return; } printf("nElements are: n"); ptr = start; do{ printf("%d %c ", ptr->info, 16); ptr = ptr->link; }while(ptr != start); } void main(){ int ch; while(1){ clrscr(); printf("n******** MAIN MENU ********n"); printf("n1. Insert"); printf("n2. Delete"); printf("n3. Find"); printf("n4. Print"); printf("n5. Exit"); printf("nnEnter your choice: "); scanf("%d", &ch); switch(ch){ case 1: printf("nEnter item to Insert: "); scanf("%d", &item); insert(item); break; case 2: delet(); break; case 3: printf("nEnter item to Find: "); scanf("%d", &item);
  • 66. 62 // Program to perform various operations on graphs (traverse (2)). // Breadth first search (BFS) #include<stdio.h> #include<conio.h> #define MAX 20 typedef struct queue{ int r, f; int item[MAX]; }que; int empty(que *); int full(que *); void insert(que *, int); int delet(que *); void bfs(int); int g[MAX][MAX], n; void main(){ int i, j, v; clrscr(); printf("nEnter number of vertices: "); scanf("%d", &n); printf("nEnter adjacency matrix of graph:n"); for(i = 0; i < n; i++) for(j = 0; j < n; j++) scanf("%d", &g[i][j]); printf("nEnter the starting node for BFS: "); scanf("%d", &v); bfs(v); getch(); } void bfs(int v){ int visit[20], i; que q; q.r = q.f = -1; for(i = 0; i < n; i++) visit[i] = 0; insert(&q, v); printf("Visit %d", v); visit[v] = 1; while(! empty(&q)){ v = delet(&q);
  • 67. 63 // Visit adjacency matrix of graph for(i = 0; i < n; i++) if(visit[i] == 0 && g[v][i] != 0){ insert(&q, i); visit[i] = 1; printf("nVisit %d", i); } } } int empty(que *p){ return ((p->r == -1)? 1: 0); } int full(que *p){ return ((p->r == MAX-1)? 1: 0); } void insert(que *p, int x){ if(p->r == -1) p->r = p->f = 0; else p->r = p->r+1; p->item[p->r] = x; } int delet(que *p){ int x; x = p->item[p->f]; if(p->r == p->f) p->r = p->f = -1; else p->f = p->f+1; return x; } /* Output Enter number of vertices: 6 Enter adjacency matrix of graph: 0 1 1 1 1 0 1 0 0 1 0 0 1 0 0 0 1 1 1 1 0 0 1 0
  • 68. 64 1 0 1 1 0 1 0 0 1 0 1 0 Enter the starting node for BFS: 0 Visit 0 Visit 1 Visit 2 Visit 3 Visit 4 Visit 5 */
  • 69. 65 // Program to perform various operations on graphs (traverse (2)). // Depth first search (DFS) #include<stdio.h> #include<conio.h> void dfs(int); int g[20][20], visit[20], n; void main(){ int i, j; clrscr(); printf("nEnter number of vertices: "); scanf("%d", &n); printf("nEnter adjacency matrix of graph:n"); for(i = 0; i < n; i++) for(j = 0; j < n; j++) scanf("%d", &g[i][j]); for(i = 0; i < n; i++) visit[i] = 0; dfs(0); getch(); } void dfs(int i){ int j; printf("nVisit %d", i); visit[i] = 1; for(j = 0; j < n; j++) if(! visit[j] && g[i][j] == 1) dfs(j); } /* Output Enter number of vertices: 6 Enter adjacency matrix of graph: 0 1 1 1 1 0 1 0 0 1 0 0 1 0 0 0 1 1 1 1 0 0 1 0 1 0 1 1 0 1 0 0 1 0 1 0
  • 70. 66 Visit 0 Visit 1 Visit 3 Visit 4 Visit 2 Visit 5 */
  • 71. 67 // Program to perform various operations on trees (traverse (3), find, delete, insertion). // Traversal with Recursion #include<stdio.h> #include<conio.h> #include<malloc.h> struct node{ int info; struct node *left, *right; } *loc, *par, *root, *save, *ptr; void find(int item){ // Check if Tree is empty if(root == NULL){ loc = par = NULL; return; } if(item == root->info){ par = NULL; loc = root; return; } save = root; if(item < root->info) ptr = root->left; else ptr = root->right; while(ptr != NULL){ if(item == ptr->info){ par = save; loc = ptr; return; } save = ptr; if(item < ptr->info) ptr = ptr->left; else ptr = ptr->right; } loc = NULL; par = save;
  • 72. 68 } void insert(int item){ struct node *new1; find(item); if(loc != NULL){ printf("nDulpicate Item can not be inserted."); getch(); return; } new1 = (struct node *) malloc(sizeof(struct node)); if(new1 == NULL){ printf("nOverflow"); getch(); return; } new1->info = item; new1->left = new1->right = NULL; if(par == NULL) root = new1; else if(item < par->info) par->left = new1; else par->right = new1; } void inorder(struct node *r){ if(r != NULL){ inorder(r->left); printf("n%d %u", r->info, r); inorder(r->right); } } void preorder(struct node *r){ if(r != NULL){ printf("n%d %u", r->info, r); preorder(r->left); preorder(r->right); } } void postorder(struct node *r){ if(r != NULL){
  • 73. 69 postorder(r->left); postorder(r->right); printf("n%d %u", r->info, r); } } void del(){ struct node *ptr, *prev, *next; int item; if(root == NULL){ printf("nTree is empty"); getch(); return; } printf("nEnter the no. to be deleted: "); scanf("%d", &item); ptr = root; while(ptr != NULL){ if(item == ptr->info) break; prev = ptr; if(item < ptr->info) ptr = ptr->left; else ptr = ptr->right; } if(ptr == NULL){ printf("nItem not found"); getch(); return; } if((ptr->left != NULL) && (ptr->right != NULL)){ prev = ptr; next = ptr->right; while(next->left != NULL){ prev = next; next = next->right; } ptr->info = next->info; ptr = next; }
  • 74. 70 else if((ptr->left == NULL) && (ptr->right == NULL)){ if(ptr == root) root = NULL; else if(prev->left == ptr) prev->left = NULL; else prev->right = NULL; } else if((ptr->left != NULL) && (ptr->right == NULL)){ if(ptr == root) root = ptr->left; else if(prev->left == ptr) prev->left = ptr->left; else prev->right = ptr->left; free(ptr); } else if((ptr->left == NULL) && (ptr->right != NULL)){ if(ptr == root) root = ptr->right; else if(prev->left == ptr) prev->left = ptr->right; else prev->right = ptr->right; } } void search(){ struct node *ptr, *prev, *next; int item; if(root == NULL){ printf("nTree is empty"); return; } printf("nEnter the no. to be searched: "); scanf("%d", &item); ptr = root; while(ptr != NULL){ if(item == ptr->info) break; prev = ptr;
  • 75. 71 if(item < ptr->info) ptr = ptr->left; else ptr = ptr->right; } if(ptr == NULL) printf("nItem not found"); else printf("nItem found"); } void main(){ int item, ch; root = par = loc = NULL; while(1){ clrscr(); printf("n1. Insert"); printf("n2. Inorder"); printf("n3. Pre Order"); printf("n4. Post Order"); printf("n5. Delete"); printf("n6. Search"); printf("n7. Exit"); printf("nnEnter your choice: "); scanf("%d", &ch); switch(ch){ case 1: printf("nEnter item to Insert: "); scanf("%d", &item); insert(item); break; case 2: inorder(root); getch(); break; case 3: preorder(root); getch(); break; case 4: postorder(root);
  • 76. 72 getch(); break; case 5: del(); break; case 6: search(); getch(); break; case 7: exit(0); default: printf("nInvalid choice"); getch(); } } }