You are on page 1of 22

Data Structures

Assignment 1

2021UCS1650
1. Write a program to find the mean and the mean
of the numbers stored in an array

Code:

1. #include <stdio.h>
2.
3. int main() {
4.
5. float marks[] = {20,90,12,59,30,44,99,19,39,73,85};
6. int len = sizeof(marks)/sizeof(float);
7.
8. // mean
9. int total = 0;
10. for (int i = 0; i < len; ++i) {
11. total += marks[i];
12. }
13. printf("The means marks are: %.2f\n", total / (float) len);
14.
15. // median
16. // 1. sorting
17. float temp;
18. for (int i = 0; i<len - 1; i++) {
19. for (int j = 0; j < len - i - 1; j++) {
20. if (marks[j] > marks[j+1]) {
21. temp = marks[j];
22. marks[j] = marks[j+1];
23. marks[j+1] = temp;
24. }
25. }
26. }
27. // 2. finding median
28. if (len % 2 == 0) {
29. temp = (marks[len/2 - 1] + marks[len/2])/2;
30. } else {
31. temp = marks[(len - 1)/2];
32. }
33. printf("The median marks are: %.2f", temp);
34. return 0;
35. }

2021UCS1650
Output:

2021UCS1650
2. Write a program to insert one element in an
array and delete an element from an array

Code:

1. #include <stdio.h>
2. #include <stdlib.h>
3. #define SIZE 10
4.
5. int main() {
6.
7. int *ar;
8. ar = (int *) malloc(SIZE * 4);
9. for (int i = 0; i < SIZE; i++) {
10. ar[i] = i+1;
11. }
12.
13. puts("\nThe array before editing:");
14. for (int i = 0; i < SIZE; i++) {
15. printf("%d ", ar[i]);
16. }puts("\n");
17.
18. //deletion
19. int ind;
20. printf("Enter index to the number to be deleted: ");
21. scanf("%d", &ind);
22.
23. if (ind < SIZE) {
24. for (int i = ind; i < SIZE - 1; i++) {
25. ar[i] = ar[i+1];
26. }
27. }
28.
29. ar = (int *) realloc(ar, 4 * (SIZE - 1));
30.
31. puts("\nAfter deletion:");
32. for (int i = 0; i < SIZE - 1; i++) {
33. printf("%d ", ar[i]);
34. }puts("\n");
35.
36. int val;
37. printf("Enter value to be inserted: ");

2021UCS1650
38. scanf("%d", &val);
39. printf("Enter index to be inserted at: ");
40. scanf("%d", &ind);
41.
42. ar = (int *) realloc(ar, SIZE * 4);
43. if (ind < SIZE) {
44. for (int i = SIZE - 1; i > ind; i--) {
45. ar[i] = ar[i-1];
46. }
47. } ar[ind] = val;
48.
49. puts("\nAfter insertion:");
50. for (int i = 0; i < SIZE; i++) {
51. printf("%d ", ar[i]);
52. }
53.
54. }

Output:

2021UCS1650
3. Write a program to search for a number in an
array

Code:

1. #include <stdio.h>
2. int main() {
3. int arr[] = {0,23,24,9,55,334,26,29,90,73,320}, val;
4. int len = sizeof(arr)/sizeof(int);
5. int x = len;
6. printf("Enter value to be searched: ");
7. scanf("%d", &val);
8. printf("%d", val);
9. for (int i = 0; i < len; i++){
10. if (arr[i] == val) {
11. x = i;
12. break;
13. }
14. }
15. x == len ? printf(" is not in the array.") :
16. printf(" is at index number %d", x);
17. }

Output:

2021UCS1650
4. Write a program to store the marks obtained by
10 students in 5 courses in a two-dimensional
array

Code:

1. #include <stdio.h>
2.
3. int main() {
4. float score[5][10];
5. int max;
6. printf("Enter the max marks: ");
7. scanf("%d", &max);
8.
9. for (int i = 0; i < 5; i++) {
10. printf("\nEnter the marks for subject %d\n", i+1);
11. for (int j = 0; j < 10; j++) {
12. printf("Marks of student %d: ", j+1);
13. scanf("%f", &score[i][j]);
14. if (score[i][j] > max || score[i][j] < 0) {
15. printf("Invalid marks!\n");
16. if (j == 0) { j = 10; i--; } else {j--;}
17. }
18. }
19. }
20. for (int i = 0; i < 5; i++) {
21. printf("\n\nSubject %d:\n", i+1);
22. for (int j = 0; j < 10; j++) {
23. printf("%.2f ", score[i][j]);
24. }
25. }
26. }

2021UCS1650
Output:

2021UCS1650
5. Write a program to implement a stack using an
array

Code:

1. #include <stdio.h>
2. #define SIZE 3
3.
4. int arr[SIZE], top = -1;
5.
6. void peek() {
7. top == -1 ? printf("Stack is empty!\n") : printf("%d\n", arr[top]);
8. }
9.
10. void push(int val) {
11. if (top == SIZE - 1) {
12. printf("Overflow!\n");
13. } else {
14. arr[++top] = val;
15. printf("Successfully pushed %d!\n", val);
16. }
17. }
18.
19. int pop() {
20. if (top == -1) {
21. printf("Underflow!\n");
22. } else {
23. int temp = arr[top--];
24. return temp;
25. }
26. }
27.
28. int main() {
29. peek();
30.
31. push(3);
32. push(4);
33. push(5);
34. push(6);

2021UCS1650
35.
36. peek();
37.
38. printf("Popped %d", pop());
39. printf("\nPopped %d", pop());
40. printf("\nPopped %d\n", pop());
41. pop();
42. }

Output:

2021UCS1650
6. Write a program to implement a queue using an
array

Code:
1. #include <stdio.h>
2. #define MAX 10
3.
4. int queue[MAX];
5. int f = -1, r = -1, size = -1;
6.
7. void enqueue(int val) {
8. if(size < MAX) {
9. if (size < 0) {
10. queue[0] = val;
11. f++; r++;
12. size = 1;
13. } else if (r == MAX-1) {
14. queue[0] = val;
15. r = 0;
16. size++;
17. } else {
18. queue[++r] = val;
19. size++;
20. }
21. } else {
22. printf("Queue is full\n");
23. }
24. }
25.
26. int dequeue() {
27. if (size < 0) {
28. printf("Queue is empty\n");
29. } else {
30. size--;
31. f++;
32. }
33. }
34.
35. void display()
36. {
37. int i;
38. if( r >= f ) {
39. for (i = f; i <= r; i++) {

2021UCS1650
40. printf("%d ",queue[i]);
41. }
42. } else {
43. for (i = f; i < MAX; i++) {
44. printf("%d ",queue[i]);
45. }
46. for (i = 0; i <= r; i++) {
47. printf("%d ",queue[i]);
48. }
49. }
50. }
51.
52. int main()
53. {
54. enqueue(24);
55. enqueue(9);
56. enqueue(22);
57. enqueue(93);
58. display();
59. dequeue();
60. printf("\nAfter dequeue\n");
61. display();
62. enqueue(8);
63. enqueue(63);
64. enqueue(57);
65. enqueue(900);
66. dequeue();
67. enqueue(84);
68. enqueue(73);
69. printf("\nAfter enqueue\n");
70. display();
71. return 0;
72. }

Output:

2021UCS1650
7. Write a program to implement a circular queue
using an array

Code:

1. #include <stdio.h>
2. #define SIZE 5
3.
4. int cirqueue[SIZE], f = -1, r = -1;
5.
6. int isFull() {
7. if ((f == r + 1) || (f == 0 && r == SIZE - 1)) { return 1; }
8. return 0;
9. }
10.
11. int isEmpty() {
12. if (f == -1) { return 1; }
13. return 0;
14. }
15.
16. void enqueue(int val) {
17. if (isFull()) { printf("\nQueue is full!\n"); }
18. else {
19. if (f == -1) { f = 0; }
20. r = (r + 1) % SIZE;
21. cirqueue[r] = val;
22. printf("\nInserted: %d", val);
23. }
24. }
25.
26. int dequeue() {
27. int val;
28. if (isEmpty()) {
29. printf("\nQueue is empty!\n");
30. } else {
31. val = cirqueue[f];
32. if (f == r) { f = r = -1;}
33. else {
34. f = (f + 1) % SIZE;
35. } printf("\nDeleted element: %d \n", val);
36. return val;

2021UCS1650
37. }
38. }
39.
40. void display() {
41. int i;
42. if (isEmpty())
43. printf("\nEmpty Queue\n");
44. else {
45. printf("\nFront: %d ", f);
46. printf("\nCircular Queue: ");
47. for (i = f; i != r; i = (i + 1) % SIZE) {
48. printf("%d ", cirqueue[i]);
49. }
50. printf("%d ", cirqueue[i]);
51. printf("\nrear: %d \n", r);
52. }
53. }
54.
55. int main() {
56. dequeue();
57.
58. enqueue(1);
59. enqueue(2);
60. enqueue(3);
61. enqueue(4);
62. enqueue(5);
63.
64. enqueue(6);
65.
66. display();
67. dequeue();
68.
69. display();
70.
71. enqueue(7);
72. display();
73.
74. enqueue(8);
75.
76. return 0;
77. }

2021UCS1650
Output:

2021UCS1650
8. Write a program to implement a priority queue
using a linked list

Code:

1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. typedef struct node {
5. int val;
6. int priority;
7. struct node* next;
8. } Node;
9.
10. Node* initNode(int v, int p) {
11. Node* temp = (Node*) malloc( sizeof(Node) );
12. temp -> val = v;
13. temp -> priority = p;
14. temp -> next = NULL;
15. return temp;
16. }
17.
18. int peek(Node** head) {
19. return (*head) -> val;
20. }
21.
22. void pop(Node** head) {
23. Node* temp = *head;
24. (*head) = (*head) -> next;
25. free(temp);
26. }
27.
28. void push(Node** head, int v, int p) {
29. Node* first = (*head);
30. Node* temp = initNode(v, p);
31. if ((*head) -> priority > p) {
32. temp -> next = *head;
33. (*head) = temp;
34. } else {
35. while (first -> next != NULL && first -> next -> priority < p) {
36. first = first -> next;
37. }

2021UCS1650
38. temp -> next = first -> next;
39. first -> next = temp;
40. } printf("Successfuly pushed %d!\n", v);
41. }
42.
43. int isEmpty(Node** head) {
44. return (*head) == NULL;
45. }
46.
47. int main() {
48. Node* pq = initNode(7, 1);
49. printf("Created linked list with value 7.\n");
50. push(&pq, 1, 2);
51. push(&pq, 3, 3);
52. push(&pq, 2, 0);
53. while (!isEmpty(&pq)) {
54. printf("Popped %d\n", peek(&pq));
55. pop(&pq);
56. }
57. printf("Priority queue is now empty.");
58. return 0;
59. }

Output:

2021UCS1650
9. Write a program to implement a double-ended
queue using a linked list

Code:

1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. typedef struct node {
5. int data;
6. struct node *prev, *next;
7. } Node;
8.
9. Node *head = NULL, *end = NULL;
10.
11. Node* initNode(int data) {
12. Node *new = (Node *) malloc(sizeof (Node));
13. new -> data = data;
14. new -> next = new -> prev = NULL;
15. return new;
16. }
17.
18. void makeEnds() {
19. head = initNode(0);
20. end = initNode(0);
21. head -> next = end;
22. end -> prev = head;
23. }
24.
25. void enqueueFront(int data) {
26. Node *new, *temp;
27. new = initNode(data);
28. temp = head -> next;
29. head -> next = new;
30. new -> prev = head;
31. new -> next = temp;
32. temp -> prev = new;
33. }
34.
35. void enqueueRear(int data) {

2021UCS1650
36. Node *new, *temp;
37. new = initNode(data);
38. temp = end -> prev;
39. end -> prev = new;
40. new -> next = end;
41. new -> prev = temp;
42. temp -> next = new;
43. }
44.
45. void dequeueFront() {
46. Node *temp;
47. if (head -> next == end) {
48. printf("Queue is empty\n");
49. } else {
50. temp = head -> next;
51. head -> next = temp -> next;
52. temp -> next -> prev = head;
53. free(temp);
54. } return;
55. }
56.
57. void dequeueRear() {
58. Node *temp;
59. if (end -> prev == head) {
60. printf("Queue is empty\n");
61. } else {
62. temp = end -> prev;
63. end -> prev = temp -> prev;
64. temp -> prev -> next = end;
65. free(temp);
66. } return;
67. }
68.
69. void display() {
70. Node *temp;
71.
72. if (head -> next == end) {
73. printf("Queue is empty\n");
74. return;
75. }
76. temp = head -> next;
77. while (temp != end) {
78. printf("%-3d", temp -> data);
79. temp = temp -> next;
80. }

2021UCS1650
81. printf("\n");
82. }
83.
84. int main() {
85. makeEnds();
86.
87. enqueueFront(23);
88. enqueueRear(29);
89. enqueueRear(30);
90. enqueueFront(40);
91. display();
92.
93. dequeueFront();
94. dequeueRear();
95. display();
96.
97. dequeueFront();
98. dequeueRear();
99. display();
100. return 0;
101. }

Output:

2021UCS1650
10. Write a program to construct a binary tree
and display its pre-order, in-order and post-order
traversals.

Code:
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. typedef struct node {
5. int data;
6. struct node* left;
7. struct node* right;
8. } Node;
9.
10. Node* newNode(int data) {
11. Node* node = (Node*) malloc(sizeof(Node));
12. node -> data = data;
13. node -> left = NULL;
14. node -> right = NULL;
15. return node;
16. }
17.
18. void Postorder(Node* node) {
19. if (node == NULL) { return; }
20.
21. Postorder(node -> left);
22.
23. Postorder(node -> right);
24.
25. printf("%d ", node -> data);
26. }
27.
28. void Inorder(Node* node)
29. {
30. if (node == NULL) { return; }
31.
32. Inorder(node -> left);
33.
34. printf("%d ", node -> data);
35.
36. Inorder(node -> right);
37. }

2021UCS1650
38.
39. void Preorder(Node* node)
40. {
41. if (node == NULL) { return; }
42.
43. printf("%d ", node -> data);
44.
45. Preorder(node -> left);
46.
47. Preorder(node -> right);
48. }
49.
50. int main()
51. {
52. Node* base = newNode(1);
53. base -> left = newNode(2);
54. base -> right = newNode(3);
55. base -> left -> left = newNode(4);
56. base -> left -> right = newNode(5);
57. base -> right -> left = newNode(6);
58. base -> right -> right = newNode(7);
59.
60. printf("\nPreorder traversal of binary tree:\n");
61. Preorder(base);
62.
63. printf("\nInorder traversal of binary tree:\n");
64. Inorder(base);
65.
66. printf("\nPostorder traversal of binary tree:\n");
67. Postorder(base);
68. return 0;
69. }

Output:

2021UCS1650

You might also like