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;
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();
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:
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
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
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);