BCA data structure lab file
BCA data structure lab file
2
Write a C program to find the largest element in an array
7) Write a C program to find the sum of each row and each column of a
matrix
8) Write a C program to check whether the given matrix is sparse matrix or
not
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
scanf("%d",&n);
printf("\n\t--------------------------------");
do
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;-
case 2:
pop();
break;
}
case 3:
display();
break;
case 4:
break;
default:
while(choice!=4);
return 0;
void push()
{
if(top>=n-1)
else
scanf("%d",&x);
top++;
stack[top]=x;
void pop()
if(top<=-1)
else
void display()
if(top>=0)
printf("\n%d",stack[i]);
else
Solution:
#include <stdio.h>
#include <stdlib.h>
// List is empty
if (start == NULL)
printf("\nList is empty\n");
// Changes links
temp->link = 0;
temp->info = data;
head = start;
while (head->link != NULL) {
head = head->link;
}
head->link = temp;
}
// Change Links
temp = start;
newnode->info = data;
newnode->link = 0;
while (i < pos - 1) {
temp = temp->link;
i++;
}
newnode->link = temp->link;
temp->link = newnode;
}
// If LL is empty
if (start == NULL)
printf("\nList is empty\n");
// Otherwise
else {
printf("\nEnter index : ");
// Position to be deleted
scanf("%d", &pos);
position = malloc(sizeof(struct node));
temp = start;
// Change Links
position = temp->link;
temp->link = position->link;
// Free memory
free(position);
}
}
// If LL is empty
if (start == NULL)
printf("\nList is empty\n");
// Otherwise
else {
temp = start;
int max = temp->info;
// If LL is empty
if (start == NULL)
printf("\nList is empty\n");
// Otherwise
else {
temp = start;
// Traverse the LL
while (temp != NULL) {
// If LL is empty
if (start == NULL) {
return;
}
// Else
else {
// Traverse the LL
while (current != NULL) {
index = current->link;
// If LL is empty
if (start == NULL)
printf("List is empty\n");
// Else
else {
// Traverse the LL
while (start != NULL) {
// reversing of points
t2 = start->link;
start->link = t1;
t1 = start;
start = t2;
}
start = t1;
// Print the LL
while (temp != NULL) {
printf("%d ", temp->info);
temp = temp->link;
}
}
}
// Function to search an element in linked list
void search()
{
int found = -1;
// creating node to traverse
struct node* tr = start;
// checking by traversing
while (tr != NULL) {
// checking for key
if (tr->info == key) {
found = 1;
break;
}
// moving forward if not at this position
else {
tr = tr->link;
}
}
// Driver Code
int main()
{
createList();
int choice;
while (1) {
switch (choice) {
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
insertAtPosition();
break;
case 5:
deleteFirst();
break;
case 6:
deleteEnd();
break;
case 7:
deletePosition();
break;
case 8:
maximum();
break;
case 9:
mean();
break;
case 10:
sort();
break;
case 11:
reverseLL();
break;
case 12:
search();
break;
case 13:
exit(1);
break;
default:
printf("Incorrect Choice\n");
}
}
return 0;
}
// function prototyping
struct node* create(int);
void insert_begin(int);
void insert_end(int);
void insert_mid(int, int);
void delete_begin();
void delete_end();
void delete_mid();
int search(int);
void update(int, int);
void sort();
int list_size();
void display();
void display_reverse(struct node*);
int get_data();
int get_position();
int main()
{
char user_active = 'Y';
int user_choice;
int data, position;
case 2:
printf("\nInserting a node at end");
data = get_data();
insert_end(data);
break;
case 3:
printf("\nInserting a node at the given position");
data = get_data();
position = get_position();
insert_mid(position, data);
break;
case 4:
printf("\nDeleting a node from beginning\n");
delete_begin();
break;
case 5:
printf("\nDeleting a node from end\n");
delete_end();
break;
case 6:
printf("\nDelete a node from given position\n");
position = get_position();
delete_mid(position);
break;
case 7:
printf("\nPrinting the list from beginning\n\n");
display();
break;
case 8:
printf("\nPrinting the list from end\n\n");
if (head == NULL)
{
printf("\n\tList is Empty!\n");
} else {
display_reverse(head);
}
break;
case 9:
printf("\nSearching the node data");
data = get_data();
if (search(data) == 1) {
printf("\n\tNode Found\n");
} else {
printf("\n\tNode Not Found\n");
}
break;
case 10:
printf("\nUpdating the node data");
data = get_data();
position = get_position();
update(position, data);
break;
case 11:
sort();
printf("\nList was sorted\n");
break;
case 12:
printf("\nProgram was terminated\n\n");
return 0;
default:
printf("\n\tInvalid Choice\n");
}
printf("\n...............................\n");
printf("\nDo you want to continue? (Y/N) : ");
fflush(stdin);
scanf(" %c", &user_active);
}
return 0;
}
if (new_node == NULL)
{
printf("\nMemory can't be allocated\n");
return NULL;
}
new_node->data = data;
new_node->next = NULL;
new_node->prev = NULL;
return new_node;
}
if (new_node)
{
if (head == NULL)
{
new_node->next = new_node;
new_node->prev = new_node;
head = new_node;
return;
}
head->prev->next = new_node;
new_node->prev = head->prev;
new_node->next = head;
head->prev = new_node;
}
}
// inserts node at the given position
void insert_mid(int position, int data)
{
// checking if the position is valid or not
if (position <= 0)
{
printf("\nInvalid Position\n");
} else if (head == NULL && position > 1) {
printf("\nInvalid Position\n");
} else if (head != NULL && position > list_size()) {
printf("\nInvalid Position\n");
} else if (position == 1) {
insert_begin(data);
} else {
struct node *new_node = create(data);
if (new_node != NULL) {
struct node *temp = head, *prev = NULL;
int i = 1;
void delete_begin()
{
if (head == NULL) {
printf("\nList is Empty\n");
return;
} else if (head->next == head) {
free(head);
head = NULL;
return;
}
free(temp);
temp = NULL;
}
last_node->prev->next = head;
head->prev = last_node->prev;
free(last_node);
last_node = NULL;
}
// deletes the node from the given position
void delete_mid(int position)
{
if (position <= 0) {
printf("\n Invalid Position \n");
}
else if (position > list_size()) {
printf("\n Invalid position \n");
}
else if (position == 1) {
delete_begin();
}
else if (position == list_size()) {
delete_end();
}
else {
struct node *temp = head;
struct node *prev = NULL;
int i = 1;
return 0;
}
do {
temp2 = temp1->next;
while(temp2 != head)
{
if (temp1->data > temp2->data)
{
value = temp1->data;
temp1->data = temp2->data;
temp2->data = value;
}
temp2 = temp2->next;
}
temp1 = temp1->next;
}while (temp1->next != head);
display_reverse(temp->next);
printf("%d ", temp->data);
}
do {
count += 1;
temp = temp->next;
} while (temp != head);
return count;
}
int get_data()
{
int data;
printf("\n\nEnter Data: ");
scanf("%d", &data);
return data;
}
int get_position()
{
int position;
printf("\n\nEnter Position: ");
scanf("%d", &position);
return position;
}
// Driver code
int main()
{
/* Start with the empty list */
struct Node* head = NULL;
push(&head, 7);
push(&head, 1);
push(&head, 3);
push(&head, 2);
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int coeff;
int pow;
struct Node* next;
};
if(first)
{
temp->coeff = first->coeff;
temp->pow = first->pow;
first = first->next;
}
else if(second)
{
temp->coeff = second->coeff;
temp->pow = second->pow;
second = second->next;
}
}
}
int main()
{
1. Solution: /*
2. * C Program to Implement a Queue using an Array
3. */
4. #include <stdio.h>
5.
6. #define MAX 50
7.
8. void insert();
9. void delete();
10. void display();
11. int queue_array[MAX];
12. int rear = - 1;
13. int front = - 1;
14. main()
15. {
16. int choice;
17. while (1)
18. {
19. printf("1.Insert element to queue \n");
20. printf("2.Delete element from queue \n");
21. printf("3.Display all elements of queue \n");
22. printf("4.Quit \n");
23. printf("Enter your choice : ");
24. scanf("%d", &choice);
25. switch (choice)
26. {
27. case 1:
28. insert();
29. break;
30. case 2:
31. delete();
32. break;
33. case 3:
34. display();
35. break;
36. case 4:
37. exit(1);
38. default:
39. printf("Wrong choice \n");
40. } /* End of switch */
41. } /* End of while */
42. } /* End of main() */
43.
44. void insert()
45. {
46. int add_item;
47. if (rear == MAX - 1)
48. printf("Queue Overflow \n");
49. else
50. {
51. if (front == - 1)
52. /*If queue is initially empty */
53. front = 0;
54. printf("Inset the element in queue : ");
55. scanf("%d", &add_item);
56. rear = rear + 1;
57. queue_array[rear] = add_item;
58. }
59. } /* End of insert() */
60.
61. void delete()
62. {
63. if (front == - 1 || front > rear)
64. {
65. printf("Queue Underflow \n");
66. return ;
67. }
68. else
69. {
70. printf("Element deleted from queue is : %d\n",
queue_array[front]);
71. front = front + 1;
72. }
73. } /* End of delete() */
74.
75. void display()
76. {
77. int i;
78. if (front == - 1)
79. printf("Queue is empty \n");
80. else
81. {
82. printf("Queue is : \n");
83. for (i = front; i <= rear; i++)
84. printf("%d ", queue_array[i]);
85. printf("\n");
86. }
87. } /* End of display() */
// Node
typedef struct node {
int data;
} Node;
return temp;
}
// Driver code
int main()
{
// Create a Priority Queue
// 7->4->5->6
Node* pq = newNode(4, 1);
push(&pq, 5, 2);
push(&pq, 6, 3);
push(&pq, 7, 0);
while (!isEmpty(&pq)) {
printf("%d ", peek(&pq));
pop(&pq);
}
return 0;
}
23) C program to implement preorder,postorder,inorder
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node* left;
node* right;
};
struct node* root;
struct node* insert(struct node* r,int datatonode)
{
if(r = NULL)
{
r = (struct node*)malloc (sizeof(struct
node));
r->data = datatonode;
r->left = NULL;
r->right = NULL;
}
else if(datatonode < r->data)
r->left= insert(r->left,datatovalue);
else
r->rigth = insert(r->right,datatovalue);
return r;
}
void inOrder(struct node* r)
{
if(r!= NULL)
{
inOrder(r->left);
printf("%d",r->data);
inOrder(r->right);
}
}
void preOrder(struct node* r)
{
if(r!= NULL)
{
printf("%d",r->data);
preOrder(r->left);
preOrder(r->right);
}
}
void postOrder(struct node* r)
{
if(r!= NULL)
{
postOrder(r->left);
postOrder(r->right);
printf("%d",r->data);
}
}
int main()
{
root = NULL;
int number,value;
printf("entre the number of element to be inserted ?\n");
scanf("%d",&number);
for(int i=0;i<n;i++)
{
printf("Data no %d is",i+1);
scanf("%d",&value);
root = insert(root,value);
}
printf("========INRODER
TARVERSAL===========");
inOrder(root);
printf("\n");
printf("=======PREORDER
TRAVERSAL===========");
preOrder(root);
printf("\n");
printf("=======POSTORDRER
TRAVERSAL=========");
postOrder(root);
printf("\n");
getch();
return 0;
}
#include <stdio.h>
// N vertices and M Edges
int N, M;
// Update value to 1
Adj[x][y] = 1;
Adj[y][x] = 1;
}
}
// Driver Code
int main()
{
// Number of vertices
N = 5;
// Given Edges
int arr[][2]
= { { 1, 2 }, { 2, 3 },
{ 4, 5 }, { 1, 5 } };
// Number of Edges
M = sizeof(arr) / sizeof(arr[0]);
return 0;
}
26) C program to implement bubble sort
1. Solution: #include <stdio.h>
2.
3. void bubble_sort(int arr[], int n) {
4. int i, j;
5. for (i = 0; i < n - 1; i++) {
6. for (j = 0; j < n - i - 1; j++) {
7. if (arr[j] > arr[j + 1]) {
8. int temp = arr[j];
9. arr[j] = arr[j + 1];
10. arr[j + 1] = temp;
11. }
12. }
13. }
14. }
15. int main() {
16. int arr[] = {64, 34, 25, 12, 22, 11, 90};
17. int n = sizeof(arr) / sizeof(arr[0]);
18. bubble_sort(arr, n);
19. printf("Sorted array: ");
20. for (int i = 0; i < n; i++) {
21. printf("%d ", arr[i]);
22. }
23. return 0;
24. }
// Driver code
int main()
{
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
// Driver code
int main()
{
int arr[] = { 10, 7, 8, 9, 1, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
quickSort(arr, 0, N - 1);
printf("Sorted array: \n");
for (int i = 0; i < N; i++)
printf("%d ", arr[i]);
return 0;
}
merge(arr, l, m, r);
}
}
// UTILITY FUNCTIONS
// Function to print an array
void printArray(int A[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
// Driver code
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int arr_size = sizeof(arr) / sizeof(arr[0]);
Solution: #include<stdio.h>
int get_max (int a[], int n){
int max = a[0];
for (int i = 1; i < n; i++)
if (a[i] > max)
max = a[i];
return max;
}
void radix_sort (int a[], int n){
int bucket[10][10], bucket_cnt[10];
int i, j, k, r, NOP = 0, divisor = 1, lar, pass;
lar = get_max (a, n);
while (lar > 0){
NOP++;
lar /= 10;
}
for (pass = 0; pass < NOP; pass++){
for (i = 0; i < 10; i++){
bucket_cnt[i] = 0;
}
for (i = 0; i < n; i++){
r = (a[i] / divisor) % 10;
bucket[r][bucket_cnt[r]] = a[i];
bucket_cnt[r] += 1;
}
i = 0;
for (k = 0; k < 10; k++){
for (j = 0; j < bucket_cnt[k]; j++){
a[i] = bucket[k][j];
i++;
}
}
divisor *= 10;
printf ("After pass %d : ", pass + 1);
for (i = 0; i < n; i++)
printf ("%d ", a[i]);
printf ("
");
}
}
int main (){
int i, n, a[10];
printf ("Enter the number of items to be sorted: ");
scanf ("%d", &n);
printf ("Enter items: ");
for (i = 0; i < n; i++){
scanf ("%d", &a[i]);
}
radix_sort (a, n);
printf ("Sorted items : ");
for (i = 0; i < n; i++)
printf ("%d ", a[i]);
printf ("
");
return 0;
}
first = 0;
last = n - 1;
middle = (first+last)/2;
return 0;
}
33) C program to implement modulo division
Solution:
int main(void)
{
int x, y;
int result;
x = 3;
y = 4;
// using modulo operator
result = x % y;
printf("%d", result);
result = y % x;
printf("\n%d", result);
return 0;
}
1. Solution: #include<stdio.h>
2. #include<stdlib.h>
3.
4. /* to store a data (consisting of key and value) in hash table array */
5. struct item
6. {
7. int key;
8. int value;
9. };
10.
11. /* each hash table item has a flag (status) and data (consisting of key
and value) */
12. struct hashtable_item
13. {
14.
15. int flag;
16. /*
17. * flag = 0 : data does not exist
18. * flag = 1 : data exists
19. * flag = 2 : data existed at least once
20. */
21.
22. struct item *data;
23.
24. };
25.
26. struct hashtable_item *array;
27. int size = 0;
28. int max = 10;
29.
30. /* initializing hash table array */
31. void init_array()
32. {
33. int i;
34. for (i = 0; i < max; i++)
35. {
36. array[i].flag = 0;
37. array[i].data = NULL;
38. }
39. }
40.
41. /* to every key, it will generate a corresponding index */
42. int hashcode(int key)
43. {
44. return (key % max);
45. }
46.
47. /* to insert an element in the hash table */
48. void insert(int key, int value)
49. {
50. int index = hashcode(key);
51. int i = index;
52.
53. /* creating new item to insert in the hash table array */
54. struct item *new_item = (struct item*) malloc(sizeof(struct item));
55. new_item->key = key;
56. new_item->value = value;
57.
58. /* probing through the array until we reach an empty space */
59. while (array[i].flag == 1)
60. {
61.
62. if (array[i].data->key == key)
63. {
64.
65. /* case where already existing key matches the given
key */
66. printf("\n Key already exists, hence updating its value
\n");
67. array[i].data->value = value;
68. return;
69.
70. }
71.
72. i = (i + 1) % max;
73. if (i == index)
74. {
75. printf("\n Hash table is full, cannot insert any more
item \n");
76. return;
77. }
78.
79. }
80.
81. array[i].flag = 1;
82. array[i].data = new_item;
83. size++;
84. printf("\n Key (%d) has been inserted \n", key);
85.
86. }
87.
88.
89. /* to remove an element from the hash table */
90. void remove_element(int key)
91. {
92. int index = hashcode(key);
93. int i = index;
94.
95. /* probing through array until we reach an empty space where not
even once an element had been present */
96. while (array[i].flag != 0)
97. {
98.
99. if (array[i].flag == 1 && array[i].data->key == key )
100. {
101.
102. // case when data key matches the given key
103. array[i].flag = 2;
104. array[i].data = NULL;
105. size--;
106. printf("\n Key (%d) has been removed \n",
key);
107. return;
108.
109. }
110. i = (i + 1) % max;
111. if (i == index)
112. {
113. break;
114. }
115.
116. }
117.
118. printf("\n This key does not exist \n");
119.
120. }
121.
122. /* to display all the elements of hash table */
123. void display()
124. {
125. int i;
126. for (i = 0; i < max; i++)
127. {
128. struct item *current = (struct item*) array[i].data;
129.
130. if (current == NULL)
131. {
132. printf("\n Array[%d] has no elements \n", i);
133. }
134. else
135. {
136. printf("\n Array[%d] has elements -: \n %d (key)
and %d(value) ", i, current->key, current->value);
137. }
138. }
139.
140. }
141.
142. int size_of_hashtable()
143. {
144. return size;
145. }
146.
147. void main()
148. {
149. int choice, key, value, n, c;
150. clrscr();
151.
152. array = (struct hashtable_item*) malloc(max *
sizeof(struct hashtable_item*));
153. init_array();
154.
155. do {
156. printf("Implementation of Hash Table in C
with Linear Probing \n\n");
157. printf("MENU-: \n1.Inserting item in the
Hashtable"
158. "\n2.Removing item from the Hashtable"
159. "\n3.Check the size of Hashtable"
160. "\n4.Display Hashtable"
161. "\n\n Please enter your choice-:");
162.
163. scanf("%d", &choice);
164.
165. switch(choice)
166. {
167.
168. case 1:
169.
170. printf("Inserting element in Hashtable\n");
171. printf("Enter key and value-:\t");
172. scanf("%d %d", &key, &value);
173. insert(key, value);
174.
175. break;
176.
177. case 2:
178.
179. printf("Deleting in Hashtable \n Enter the
key to delete-:");
180. scanf("%d", &key);
181. remove_element(key);
182.
183. break;
184.
185. case 3:
186.
187. n = size_of_hashtable();
188. printf("Size of Hashtable is-:%d\n", n);
189.
190. break;
191.
192. case 4:
193.
194. display();
195.
196. break;
197.
198. default:
199.
200. printf("Wrong Input\n");
201.
202. }
203.
204. printf("\n Do you want to continue-:(press 1 for
yes)\t");
205. scanf("%d", &c);
206.
207. }while(c == 1);
208.
209. getch();
210.
211. }