Data Structures Lab File
Data Structures Lab File
ICT-255
Submitted in partial fulfilment of the requirements for the award of
the degree of
B.Tech.
in
COMPUTER SCIENCE
JANUARY - 2023
INDEX
S.No Assignment Name Date Remarks
.
1. Implement operations (traverse, insert,
delete, linear search, selection sort) on an
array.
2. Implement insertion and deletion (at the
beginning, at specified location, at the
end) on single linked list.
3. Implement insertion and deletion (at the
beginning, at specified location, at the
end) on circular single linked list.
4. Implement insertion and deletion (at the
beginning, at specified location, at the
end) on double linked list.
5. Implement insertion and deletion (at the
beginning, at specified location, at the
end) on circular double linked list
6. Write a program to count the number of
nodes & reverse the single linked list.
7. Write a program to merge two sorted
linked list and display the final sorted
linked list.
8. Implement addition of two polynomial
expressions using singly linked list.
9. Implement operations (push, pop) on a
stack using arrays. Check the status of the
stack whether there is underflow or
overflow.
10. .Implement the conversion of infix
notation to postfix notation.
11. .Implement the conversion of infix
notation to postfix notation.
12. .Implement binary search using recursion.
13. .Implement operations (enqueue,
dequeue) on a queue using arrays. Check
the status of the queue whether it is
empty or full.
14. Implement circular queue using arrays
and linked list.
15. .Implement stacks and queues using
linked list.
16. .Implement Sparse Array.
17. .Implement operations on Binary Search
Tree (Insertion, Deletion, Search,
Traversals (using recursion)- Inorder,
Preorder, Postorder).
18. .Implement traversals on Binary Search
Tree (using stacks) - Inorder, Preorder,
Postorder).
19. .Implement graph traversal (DFS & BFS)
20. Make a menu driven program to perform
various sorting techniques (insertion,
shell, merge, heap, bubble, quick).
1.Implement operations (traverse, insert, delete, linear search,
selection sort) on an array
#include <stdio.h>
#include <stdbool.h>
bool insertAtBegin(int A[], int size, int maxSize, int item) // Returns true
if inserted successfully
{
if (size == maxSize)
{
printf("Array is full\n");
return false;
}
bool insertAtPos(int A[], int size, int maxSize, int pos, int item) // Returns
true if inserted successfully
{
if (size == maxSize)
{
printf("Array is full\n");
return false;
}
if (size < pos || pos < 0)
{
printf("Invalid Position");
return false;
}
bool insertAtLast(int A[], int size, int maxSize, int item) // Returns true if
inserted successfully
{
if (size == maxSize)
{
printf("Array is full\n");
return false;
}
bool deleteAtBegin(int A[], int size, int item) // Returns true if deleted
successfully
{
if (size == 0)
{
printf("Array is empty\n");
return false;
}
bool deleteAtPos(int A[], int size, int pos) // Returns true if deleted
successfully
{
if (size == 0)
{
printf("Array is empty\n");
return false;
}
if (size < pos || pos < 0)
{
printf("Invalid Position");
return false;
}
printf("Deleted Successfully\n");
return true;
}
bool deleteAtLast(int A[], int size, int item) // Returns true if deleted
successfully
{
if (size == 0)
{
printf("Array is empty\n");
return false;
}
printf("Deleted Successfully\n");
return true;
}
void selectionSort(int A[], int size) // Modifies the array directly since
call by address
{
int i, j, k;
for (i = 0; i < size - 1; i++)
{
for (j = k = i; j < size; j++)
{
if (A[j] < A[k])
k = j;
}
int temp = A[k];
A[k] = A[i];
A[i] = temp;
}
printf("Done!\n");
}
int main()
{
int maxSize, curSize = 0;
printf("Enter max size of array: ");
scanf("%d", &maxSize);
if (maxSize <= 0)
{
printf("An array cant have negative or zero length");
return 0;
}
OUTPUT
struct node {
int val;
struct node *next;
};
if (head == NULL) {
printf("Inserted Successfully\n");
display(p);
return p;
}
printf("Inserted Successfully\n");
display(head);
return head;
}
struct node *insertAtPos(struct node *head, int val, int loc) { // To insert
after a certain node with given value
if (head == NULL) {
printf("No such node with given loc\n");
return NULL;
}
struct node *temp = head;
while (temp != NULL && temp->val != loc) {
temp = temp->next;
}
if (temp == NULL) {
printf("No such node with given loc\n");
display(head);
return head;
}
printf("Inserted Successfully\n");
display(head);
return head;
}
if (p == head) {
free(p);
printf("Deleted Successfully\n");
display(NULL);
return NULL;
}
free(p);
q->next = NULL;
printf("Deleted Successfully\n");
display(head);
return head;
}
struct node *deleteFromPos(struct node *head, int loc) { // Delete 1st node
with value equal to given loc
if (head == NULL) {
printf("Linked List is already empty");
return head;
}
if (p == NULL) {
printf("No such node with value as given loc\n");
display(head);
return head;
}
q->next = p->next;
free(p);
printf("Deleted Successfully\n");
display(head);
return head;
}
int main() {
struct node *head = NULL;
int ch;
bool exit = false;
while (exit == false) {
printf("1. Display\n2. Insert\n3. Delete\n4. Exit\nEnter your choice:
");
scanf("%d", &ch);
printf("\n");
switch (ch) {
case 1:
display(head);
break;
case 2: {
int val;
printf("Enter value: ");
scanf("%d", &val);
if (head == NULL) {
head = insertAtBegin(head, val);
break;
}
int ch;
printf("1. Begin\n2. End\n3. Locn\nEnter your choice: ");
scanf("%d", &ch);
if (ch == 1) head = insertAtBegin(head, val);
else if (ch == 2) head = insertAtEnd(head, val);
else if (ch == 3) {
int loc;
printf("Enter locn(value of node after which to insert):
");
scanf("%d", &loc);
head = insertAtPos(head, val, loc);
} else printf("Invalid choice\n");
break;
}
case 3: {
if (head == NULL) {
printf("Linked List is already empty");
break;
}
int ch;
printf("1. Begin\n2. End\n3. Locn\nEnter your choice: ");
scanf("%d", &ch);
if (ch == 1) head = deleteFromBegin(head);
else if (ch == 2) head = deleteFromEnd(head);
else if (ch == 3) {
int loc;
printf("Enter locn(value of node to delete): ");
scanf("%d", &loc);
head = deleteFromPos(head, loc);
} else printf("Invalid choice\n");
break;
}
case 4:
exit = true;
printf("Thank You! Created by Harsh Goyal");
break;
default:
printf("Invalid Choice");
break;
}
printf("\n");
}
return 0;
}
OUTPUT
struct node {
int val;
struct node *next;
};
if (head == NULL) {
p->next = p;
printf("Inserted Successfully\n");
display(p);
return p;
}
p->next = head;
struct node *temp = head;
while (temp->next != head)
temp = temp->next;
temp->next = p;
printf("Inserted Successfully\n");
display(p);
return p;
}
if (head == NULL) {
p->next = p;
printf("Inserted Successfully\n");
display(p);
return p;
}
p->next = head;
struct node *temp = head;
while (temp->next != head)
temp = temp->next;
temp->next = p;
printf("Inserted Successfully\n");
display(head);
return head;
}
struct node *insertAtPos(struct node *head, int val, int loc) { // To insert
after a certain node with given value
if (head == NULL) {
printf("No such node with given loc\n");
return NULL;
}
printf("Inserted Successfully\n");
display(head);
return head;
}
if (head->next == head) {
free(head);
printf("Deleted Successfully\n");
display(NULL);
return NULL;
}
free(p);
q->next = head;
printf("Deleted Successfully\n");
display(head);
return head;
}
struct node *deleteFromPos(struct node *head, int loc) { // Delete 1st node
with value equal to given loc
if (head == NULL) {
printf("Linked List is already empty");
return head;
}
int main() {
struct node *head = NULL;
int n;
int ch;
bool exit = false;
while (exit == false) {
printf("1. Display\n2. Insert\n3. Delete\n4. Exit\nEnter your choice:
");
scanf("%d", &ch);
printf("\n");
switch (ch) {
case 1:
display(head);
break;
case 2: {
int val;
printf("Enter value: ");
scanf("%d", &val);
if (head == NULL) {
head = insertAtBegin(head, val);
break;
}
int ch;
printf("1. Begin\n2. End\n3. Locn\nEnter your choice: ");
scanf("%d", &ch);
if (ch == 1) head = insertAtBegin(head, val);
else if (ch == 2) head = insertAtEnd(head, val);
else if (ch == 3) {
int loc;
printf("Enter locn(value of node after which to insert):
");
scanf("%d", &loc);
head = insertAtPos(head, val, loc);
} else printf("Invalid choice\n");
break;
}
case 3: {
if (head == NULL) {
printf("Linked List is already empty");
break;
}
int ch;
printf("1. Begin\n2. End\n3. Locn\nEnter your choice: ");
scanf("%d", &ch);
if (ch == 1) head = deleteFromBegin(head);
else if (ch == 2) head = deleteFromEnd(head);
else if (ch == 3) {
int loc;
printf("Enter locn(value of node to delete): ");
scanf("%d", &loc);
head = deleteFromPos(head, loc);
} else printf("Invalid choice\n");
break;
}
case 4:
exit = true;
printf("Thank You! Created by Harsh Goyal");
break;
default:
printf("Invalid Choice\n");
break;
}
printf("\n");
}
return 0;
}
OUTPUT
struct node {
int val;
struct node *prev;
struct node *next;
};
if (head == NULL) {
printf("Inserted Successfully\n");
display(p);
return p;
}
printf("Inserted Successfully\n");
display(head);
return head;
}
struct node *insertAtPos(struct node *head, int val, int loc) { // To insert
after a certain node with given value
if (head == NULL) {
printf("No such node with given loc\n");
return NULL;
}
if (temp == NULL) {
printf("No such node with given loc\n");
display(head);
return head;
}
printf("Inserted Successfully\n");
display(head);
return head;
}
if (p == head) {
free(p);
printf("Deleted Successfully\n");
display(NULL);
return NULL;
}
free(p);
q->next = NULL;
printf("Deleted Successfully\n");
display(head);
return head;
}
struct node *deleteFromPos(struct node *head, int loc) { // Delete 1st node
with value equal to given loc
if (head == NULL) {
printf("Linked List is already empty");
return head;
}
if (p == NULL) {
printf("No such node with value as given loc\n");
display(head);
return head;
}
q->next = p->next;
if (p->next != NULL) p->next->prev = q;
free(p);
printf("Deleted Successfully\n");
display(head);
return head;
}
int main() {
struct node *head = NULL;
int ch;
bool exit = false;
while (exit == false) {
printf("1. Display\n2. Insert\n3. Delete\n4. Exit\nEnter your choice:
");
scanf("%d", &ch);
printf("\n");
switch (ch) {
case 1:
display(head);
break;
case 2: {
int val;
printf("Enter value: ");
scanf("%d", &val);
if (head == NULL) {
head = insertAtBegin(head, val);
break;
}
int ch;
printf("1. Begin\n2. End\n3. Locn\nEnter your choice: ");
scanf("%d", &ch);
if (ch == 1) head = insertAtBegin(head, val);
else if (ch == 2) head = insertAtEnd(head, val);
else if (ch == 3) {
int loc;
printf("Enter locn(value of node after which to insert):
");
scanf("%d", &loc);
head = insertAtPos(head, val, loc);
} else printf("Invalid choice\n");
break;
}
case 3: {
if (head == NULL) {
printf("Linked List is already empty");
break;
}
int ch;
printf("1. Begin\n2. End\n3. Locn\nEnter your choice: ");
scanf("%d", &ch);
if (ch == 1) head = deleteFromBegin(head);
else if (ch == 2) head = deleteFromEnd(head);
else if (ch == 3) {
int loc;
printf("Enter locn(value of node to delete): ");
scanf("%d", &loc);
head = deleteFromPos(head, loc);
} else printf("Invalid choice\n");
break;
}
case 4:
exit = true;
printf("Thank You! Created by Harsh Goyal");
break;
default:
printf("Invalid Choice");
break;
}
printf("\n");
}
return 0;
}
struct node {
int val;
struct node *prev;
struct node *next;
};
if (head == NULL) {
p->next = p;
p->prev = p;
printf("Inserted Successfully\n");
display(p);
return p;
}
head->prev->next = p;
p->prev = head->prev;
head->prev = p;
p->next = head;
printf("Inserted Successfully\n");
display(p);
return p;
}
if (head == NULL) {
p->next = p;
printf("Inserted Successfully\n");
display(p);
return p;
}
head->prev->next = p;
p->prev = head->prev;
head->prev = p;
p->next = head;
printf("Inserted Successfully\n");
display(head);
return head;
}
struct node *insertAtPos(struct node *head, int val, int loc) { // To insert
after a certain node with given value
if (head == NULL) {
printf("No such node with given loc\n");
return NULL;
}
printf("Inserted Successfully\n");
display(head);
return head;
}
if (head->next == head) {
free(head);
printf("Deleted Successfully\n");
display(NULL);
return NULL;
}
struct node *p = head;
head->prev->next = head->next;
head->next->prev = head->prev;
head = head->next;
free(p);
printf("Deleted Successfully\n");
display(head);
return head;
}
struct node *deleteFromPos(struct node *head, int loc) { // Delete 1st node
with value equal to given loc
if (head == NULL) {
printf("Linked List is already empty");
return head;
}
q->next = p->next;
p->next->prev = q;
free(p);
printf("Deleted Successfully\n");
display(head);
return head;
}
int main() {
struct node *head = NULL;
int n;
int ch;
bool exit = false;
while (exit == false) {
printf("1. Display\n2. Insert\n3. Delete\n4. Exit\nEnter your choice:
");
scanf("%d", &ch);
printf("\n");
switch (ch) {
case 1:
display(head);
break;
case 2: {
int val;
printf("Enter value: ");
scanf("%d", &val);
if (head == NULL) {
head = insertAtBegin(head, val);
break;
}
int ch;
printf("1. Begin\n2. End\n3. Locn\nEnter your choice: ");
scanf("%d", &ch);
if (ch == 1) head = insertAtBegin(head, val);
else if (ch == 2) head = insertAtEnd(head, val);
else if (ch == 3) {
int loc;
printf("Enter locn(value of node after which to insert):
");
scanf("%d", &loc);
head = insertAtPos(head, val, loc);
} else printf("Invalid choice\n");
break;
}
case 3: {
if (head == NULL) {
printf("Linked List is already empty");
break;
}
int ch;
printf("1. Begin\n2. End\n3. Locn\nEnter your choice: ");
scanf("%d", &ch);
if (ch == 1) head = deleteFromBegin(head);
else if (ch == 2) head = deleteFromEnd(head);
else if (ch == 3) {
int loc;
printf("Enter locn(value of node to delete): ");
scanf("%d", &loc);
head = deleteFromPos(head, loc);
} else printf("Invalid choice\n");
break;
}
case 4:
exit = true;
printf("Thank You! Created by Harsh Goyal");
break;
default:
printf("Invalid Choice\n");
break;
}
printf("\n");
}
return 0;
}
OUTPUT
6. Write a program to count the number of nodes & reverse the single linked
list.
#include <stdio.h>
#include <malloc.h>
#include <stdbool.h>
// Experiment 6: Write a program to count the number of nodes & reverse the
single linked list.
struct node {
int val;
struct node *next;
};
if (head == NULL) {
printf("Inserted Successfully\n");
display(p);
return p;
}
printf("Inserted Successfully\n");
display(head);
return head;
}
struct node *insertAtPos(struct node *head, int val, int loc) { // To insert
after a certain node with given value
if (head == NULL) {
printf("No such node with given loc\n");
return NULL;
}
if (temp == NULL) {
printf("No such node with given loc\n");
display(head);
return head;
}
printf("Inserted Successfully\n");
display(head);
return head;
}
if (p == head) {
free(p);
printf("Deleted Successfully\n");
display(NULL);
return NULL;
}
free(p);
q->next = NULL;
printf("Deleted Successfully\n");
display(head);
return head;
}
struct node *deleteFromPos(struct node *head, int loc) { // Delete 1st node
with value equal to given loc
if (head == NULL) {
printf("Linked List is already empty");
return head;
}
if (p == NULL) {
printf("No such node with value as given loc\n");
display(head);
return head;
}
q->next = p->next;
free(p);
printf("Deleted Successfully\n");
display(head);
return head;
}
return p;
}
int main() {
struct node *head = NULL;
int ch;
bool exit = false;
while (exit == false) {
printf("1. Display\n2. Insert\n3. Delete\n4. Reverse\n5. Count Nodes\
n6. End\nEnter your choice: ");
scanf("%d", &ch);
printf("\n");
switch (ch) {
case 1:
display(head);
break;
case 2: {
int val;
printf("Enter value: ");
scanf("%d", &val);
if (head == NULL) {
head = insertAtBegin(head, val);
break;
}
int ch;
printf("1. Begin\n2. End\n3. Locn\nEnter your choice: ");
scanf("%d", &ch);
if (ch == 1) head = insertAtBegin(head, val);
else if (ch == 2) head = insertAtEnd(head, val);
else if (ch == 3) {
int loc;
printf("Enter locn(value of node after which to insert):
");
scanf("%d", &loc);
head = insertAtPos(head, val, loc);
} else printf("Invalid choice\n");
break;
}
case 3: {
if (head == NULL) {
printf("Linked List is already empty");
break;
}
int ch;
printf("1. Begin\n2. End\n3. Locn\nEnter your choice: ");
scanf("%d", &ch);
if (ch == 1) head = deleteFromBegin(head);
else if (ch == 2) head = deleteFromEnd(head);
else if (ch == 3) {
int loc;
printf("Enter locn(value of node to delete): ");
scanf("%d", &loc);
head = deleteFromPos(head, loc);
} else printf("Invalid choice\n");
break;
}
case 4:
head = reverse(head);
display(head);
break;
case 5:
printf("There are %d node(s) in the linked list\
n",countNodes(head));
break;
case 6:
exit = true;
printf("Thank You! Created by Harsh Goyal");
break;
default:
printf("Invalid Choice");
break;
}
printf("\n");
}
return 0;
}
OUTPUT
7. Write a program to merge two sorted linked list and display the final sorted
linked list.
#include <stdio.h>
#include <malloc.h>
// Experiment 7: Write a program to merge two sorted linked list and display
the final sorted linked list.
struct node {
int val;
struct node *next;
};
while (p!=NULL) {
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->val = p->val;
temp->next = NULL;
p = p->next;
last->next = temp;
last = last->next;
}
return head;
}
if (p != NULL) {
last->next = copy(p);
}
if (q != NULL) {
last->next = copy(q);
}
return head;
}
int ch=1;
while (ch == 1) {
int val;
printf("Enter value: ");
scanf("%d",&val);
if (last == NULL) {
last = head = q;
} else {
last->next = q;
last = q;
}
display(head);
int main() {
printf("For 1st linked list..\n");
struct node* head1= create();
return 0;
}
OUTPUT
struct node {
int coeff,exp;
struct node *next;
};
printf("(%d)x^(%d) ",head->coeff,head->exp);
head = head->next;
while (head != NULL) {
if (head->exp == 0) printf("+ (%d) ",head->coeff);
else printf("+ (%d)x^(%d) ",head->coeff,head->exp);
head = head->next;
}
printf("\n");
}
last->next = temp;
last = last->next;
}
if (x1 != NULL) {
while (x1 != NULL) {
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->next = NULL;
temp->exp = x1->exp;
temp->coeff = x1->coeff;
x1 = x1->next;
last->next = temp;
last = last->next;
}
} else if (x2 != NULL) {
while (x2 != NULL) {
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->next = NULL;
temp->exp = x2->exp;
temp->coeff = x2->coeff;
x2 = x2->next;
last->next = temp;
last = last->next;
}
}
return head;
}
int ch=1;
while (ch == 1) {
int exp;
printf("Enter exponent: ");
scanf("%d",&exp);
int coeff;
printf("Enter coefficient: ");
scanf("%d",&coeff);
if (last == NULL) {
last = head = q;
} else {
last->next = q;
last = q;
}
display(head);
int main() {
printf("For 1st polynomial..\n");
struct node* head1= create();
return 0;
}
OUTPUT
9. . Implement operations (push, pop) on a stack using arrays. Check the status
of the stack whether there is underflow or overflow.
#include <stdio.h>
#include <stdbool.h>
#include <malloc.h>
struct stack {
int *stk;
int size;
int top;
};
s->top++;
s->stk[s->top] = x;
}
s->top--;
return s->stk[s->top+1];
}
return s->stk[s->top];
}
int main() {
int n;
printf("Enter max size of stack: ");
scanf("%d",&n);
struct stack s;
s.stk = (int *)malloc(sizeof(int)*n);
s.top=-1;
s.size=n;
return 0;
}
OUTPUT
struct stack {
char *stk;
int size;
int top;
};
s->top++;
s->stk[s->top] = x;
}
s->top--;
return s->stk[s->top+1];
}
return s->stk[s->top];
}
int isOperand(char x) {
if (x == '+' || x == '-' || x == '*' || x == '/' || x == '^' || x =='(' ||
x == ')') return 0;
return 1;
}
int main() {
char infix[100],postfix[100],temp;
printf("Enter your infix expression(enter '?' whenever to stop): ");
int i=0;
for (;i<99;i++) {
// printf("Input %d: ",i+1);
scanf(" %c",&temp);
if (temp == '?') break;
infix[i] = temp;
}
if (i == 99) printf("You have reached max size of array");
infix[i] = '\0';
int ch = checkInfixExpression(infix);
if (ch == 0) {
printf("Entered infix expression is not valid\n");
return 0;
}
convert(infix,postfix);
return 0;
}
OUTPUT
struct stack {
int *stk;
int size;
int top;
};
s->top++;
s->stk[s->top] = x;
}
s->top--;
return s->stk[s->top+1];
}
return s->stk[s->top];
}
int isOperator(char x) {
if (x == '+' || x == '-' || x == '*' || x == '/' || x == '^') return 1;
return 0;
}
int isNumber(char x) {
if (x >= 48 && x <= 57) return 1;
return 0;
}
if (s.top != 0) {
printf("Unexpected error occurred. More than one values in stack after
executing postfix expression");
return 0;
}
return pop(&s);
}
int main() {
char postfix[100],temp;
printf("Enter your postfix expression(enter '?' whenever to stop): ");
int i=0;
for (;i<99;i++) {
// printf("Input %d: ",i+1);
scanf(" %c",&temp);
if (temp == '?') break;
postfix[i] = temp;
}
if (i == 99) printf("You have reached max size of array");
postfix[i] = '\0';
int ch = checkPostfixExpression(postfix);
if (ch == 0) {
printf("Entered postfix expression is not valid\n");
return 0;
}
return 0;
}
OUTPUT
int main() {
int n;
printf("Enter size of array: ");
scanf("%d",&n);
if (n<=0) return 0;
int A[n];
display(A,n);
int exit = 0;
while (exit == 0) {
int ch;
printf("\nMenu:\n1. Search an element\n2. Display\n3. Exit\nEnter a
choice:");
scanf("%d",&ch);
switch (ch) {
case 1:{
int x;
printf("Enter element to search: ");
scanf("%d",&x);
int index = binSearch(A,0,n-1,x);
display(A,n);
if (index == -1) printf("Element not found\n");
else printf("Element at index %d\n",index);
break;
}
case 2: {
display(A,n);
break;
}
case 3: {
exit = 1;
break;
}
default:{
printf("Invalid Choice\n");
break;
}
}
}
return 0;
}
OUTPUT
struct queue {
int *Q;
int front;
int rear;
int size;
};
x->Q[++x->rear]=elem;
return 1;
}
return x->Q[++x->front];
}
int main() {
int temp;
printf("Enter max size of queue: ");
scanf("%d",&temp);
if (temp <=0) return 0;
struct queue q;
q.size = temp;
q.Q = (int *)malloc(sizeof(int)*temp);
q.front = q.rear = -1;
int exit=0;
while (exit == 0) {
int ch;
printf("\nMenu:\n1. Display\n2. Enqueue\n3. Dequeue\n4. Check Full\n5.
Check Empty\n6. Exit\nEnter your choice: ");
scanf("%d",&ch);
switch (ch) {
case 1: {
display(&q);
break;
}
case 2: {
int elem;
printf("Enter element to insert: ");
scanf("%d",&elem);
int status = enqueue(&q,elem);
display(&q);
break;
}
case 3: {
int elem = dequeue(&q);
if (elem != -1) {
printf("Element dequeued is %d\n",elem);
display(&q);
}
break;
}
case 4: {
printf(isFull(&q) ? "Queue is full\n": "Queue is not full\n");
break;
}
case 5: {
printf(isEmpty(&q) ? "Queue is empty\n": "Queue is not empty\
n");
break;
}
case 6: {
exit = 1;
break;
}
default:{
printf("Invalid choice\n");
break;
}
}
}
return 0;
}
OUTPUT
// Experiment 14: Implement circular queue using arrays and linked list. (Part
1-Using Array)
struct queue {
int *Q;
int front;
int rear;
int size;
};
x->rear = (x->rear+1)%x->size;
x->Q[x->rear]=elem;
return 1;
}
x->front = (x->front+1)%x->size;
return x->Q[x->front];
}
int main() {
int temp;
printf("Enter max size of queue: ");
scanf("%d",&temp);
if (temp <=0) return 0;
struct queue q;
q.size = temp+1;
q.Q = (int *)malloc(sizeof(int)*temp+1);
q.front = q.rear = 0;
int exit=0;
while (exit == 0) {
int ch;
printf("\nMenu:\n1. Display\n2. Enqueue\n3. Dequeue\n4. Check Full\n5.
Check Empty\n6. Exit\nEnter your choice: ");
scanf("%d",&ch);
switch (ch) {
case 1: {
display(&q);
break;
}
case 2: {
int elem;
printf("Enter element to insert: ");
scanf("%d",&elem);
int status = enqueue(&q,elem);
display(&q);
break;
}
case 3: {
int elem = dequeue(&q);
if (elem != -1) {
printf("Element dequeued is %d\n",elem);
display(&q);
}
break;
}
case 4: {
printf(isFull(&q) ? "Queue is full\n": "Queue is not full\n");
break;
}
case 5: {
printf(isEmpty(&q) ? "Queue is empty\n": "Queue is not empty\
n");
break;
}
case 6: {
exit = 1;
break;
}
default:{
printf("Invalid choice\n");
break;
}
}
}
return 0;
}
OUTPUT
PART-2
#include <stdio.h>
#include <malloc.h>
// Experiment 14: Implement circular queue using arrays and linked list. (Part
2-Using Linked List)
struct node {
int val;
struct node* next;
};
struct queue {
struct node* front;
struct node* rear;
};
if (x->rear == NULL) {
// Queue was empty before
x->front = x->rear = q;
}
x->rear->next = q;
x->rear = q;
}
int main() {
struct queue q;
q.front = q.rear = NULL;
int exit=0;
while (exit == 0) {
int ch;
printf("\nMenu:\n1. Display\n2. Enqueue\n3. Dequeue\n4. Check Empty\
n5. Exit\nEnter your choice: ");
scanf("%d",&ch);
switch (ch) {
case 1: {
display(&q);
break;
}
case 2: {
int elem;
printf("Enter element to insert: ");
scanf("%d",&elem);
enqueue(&q,elem);
display(&q);
break;
}
case 3: {
int elem = dequeue(&q);
if (elem != -1) {
printf("Element dequeued is %d\n",elem);
display(&q);
}
break;
}
case 4: {
printf(isEmpty(&q) ? "Queue is empty\n": "Queue is not empty\
n");
break;
}
case 5: {
exit = 1;
break;
}
default:{
printf("Invalid choice\n");
break;
}
}
}
return 0;
}
OUTPUT
struct node {
int val;
struct node* next;
};
struct stack {
struct node* top;
};
return stk->top->val;
}
struct queue {
struct node* front;
struct node* rear;
};
if (x->rear == NULL) {
// Queue was empty before
x->front = x->rear = q;
}
x->rear->next = q;
x->rear = q;
}
switch (ch) {
case 1: {
displayQueue(q);
break;
}
case 2: {
int elem;
printf("Enter element to insert: ");
scanf("%d",&elem);
enqueue(q,elem);
displayQueue(q);
break;
}
case 3: {
int elem = dequeue(q);
if (elem != -1) {
printf("Element dequeued is %d\n",elem);
displayQueue(q);
}
break;
}
case 4: {
printf(isEmptyQueue(q) ? "Queue is empty\n": "Queue is not
empty\n");
break;
}
case 5: {
printf("Exiting...\n");
break;
}
default:{
printf("Invalid choice\n");
break;
}
}
}
}
int main() {
struct stack stk;
stk.top = NULL;
struct queue q;
q.front = q.rear = NULL;
int ch=1;
while (ch!=3) {
printf("\nMenu:\n1. Stack\n2. Queue\n3. Exit\nEnter your choice: ");
scanf("%d",&ch);
switch (ch) {
case 1:
stackMenu(&stk);
break;
case 2:
queueMenu(&q);
break;
case 3:
printf("Exiting...");
break;
default:
printf("Invalid Choice");
break;
}
}
}
OUTPUT
16.Implement Sparse Array.
#include <stdio.h>
#include <malloc.h>
#include <stdbool.h>
struct SparseMatrix {
int *row_ind;
int *col_ind;
int *values;
int count;
};
display(*mat);
return mat;
}
if (val == 0) return;
if (mat->values[0] == 99) {
printf("Default max size reached\n");
return;
}
mat->values[0]++;
mat->row_ind[mat->values[0]] = i;
mat->col_ind[mat->values[0]] = j;
mat->values[mat->values[0]] = val;
}
if (mat->values[0] == 0) {
printf("Sparse matrix has no elements\n");
return;
}
temp=mat->col_ind[k];
mat->col_ind[k] = mat->col_ind[mat->values[0]];
mat->col_ind[mat->values[0]] = temp;
temp=0;
mat->values[k] = mat->values[mat->values[0]];
mat->values[mat->values[0]] = temp;
mat->values[0]--;
return;
}
}
int main(){
struct SparseMatrix *mat = create();
int choice = 1;
while (choice != 4) {
printf("\nMenu:\n1. Insert an element\n2. Delete an element\n3.
Display the matrix\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1: {
int i, j, val;
printf("Enter the row index: ");
scanf("%d", &i);
printf("Enter the column index: ");
scanf("%d", &j);
printf("Enter the value: ");
scanf("%d", &val);
insert(mat, i, j, val);
display(*mat);
break;
}
case 2:{
int i, j;
printf("Enter the row index: ");
scanf("%d", &i);
printf("Enter the column index: ");
scanf("%d", &j);
deleteElement(mat, i, j);
display(*mat);
break;
}
case 3:
display(*mat);
break;
case 4:
break;
default:
printf("Invalid choice\n");
break; }
}
return 0;
}
OUTPUT
struct node {
int val;
struct node* left;
struct node* right;
};
if (cur->val == val) {
if (cur->left == NULL && cur->right == NULL) {
free(cur);
return NULL;
}
if (cur->left != NULL) {
struct node* leftLargest=cur->left;
while (leftLargest->right != NULL)
leftLargest = leftLargest->right;
cur->val = leftLargest->val;
cur->left = deleteBST(cur->left,leftLargest->val);
return cur;
}
printf("%d ",cur->val);
preorder(cur->left);
preorder(cur->right);
}
inorder(cur->left);
printf("%d ",cur->val);
inorder(cur->right);
}
postorder(cur->left);
postorder(cur->right);
printf("%d ",cur->val);
}
int main() {
struct node* root = NULL;
int ch=1;
while (ch != 7) {
printf("\nMenu:\n1. Insert\n2. Delete\n3. Preorder\n4. Inorder\n5.
Postorder\n6. Search\n7. Exit\nEnter your choice: ");
scanf("%d",&ch);
switch (ch) {
case 1: {
int elem;
printf("Enter element to insert: ");
scanf("%d",&elem);
root = insertBST(root,elem);
break;
}
case 2: {
int elem;
printf("Enter element to delete: ");
scanf("%d",&elem);
root = deleteBST(root,elem);
break;
}
case 3:
printf("Preorder: ");
preorder(root);
printf("\n");
break;
case 4:
printf("Inorder: ");
inorder(root);
printf("\n");
break;
case 5:
printf("Postorder: ");
postorder(root);
printf("\n");
break;
case 6:{
int elem;
printf("Enter element to search: ");
scanf("%d",&elem);
int ch = searchBST(root,elem);
if (ch == 1) printf("Element found\n");
else printf("Element not found\n");
}
case 7:
break;
default:
printf("Invalid Choice");
break;
}
}
return 0;
}
OUTPUT
struct node
{
int val;
struct node *left;
struct node *right;
};
struct nodeForSingleStack
{
struct node *val;
struct nodeForSingleStack *next;
};
struct singleStack
{
struct nodeForSingleStack *top;
};
struct nodeForDualStack
{
struct node *val;
int status;
struct nodeForDualStack *next;
};
struct dualStack
{
struct nodeForDualStack *top;
};
int isEmptyDualStack(struct dualStack *stk)
{
if (stk->top == NULL)
return 1;
return 0;
}
if (cur->val == val)
{
if (cur->left == NULL && cur->right == NULL)
{
free(cur);
return NULL;
}
if (cur->left != NULL)
{
struct node *leftLargest = cur->left;
while (leftLargest->right != NULL)
leftLargest = leftLargest->right;
cur->val = leftLargest->val;
cur->left = deleteBST(cur->left, leftLargest->val);
return cur;
}
cur = popSingleStack(&s);
cur = cur->right;
}
}
cur = popSingleStack(&s);
printf("%d ", cur->val);
cur = cur->right;
}
}
int main()
{
struct node *root = NULL;
int ch = 1;
while (ch != 7)
{
printf("\nMenu:\n1. Insert\n2. Delete\n3. Preorder\n4. Inorder\n5.
Postorder\n6. Search\n7. Exit\nEnter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
{
int elem;
printf("Enter element to insert: ");
scanf("%d", &elem);
root = insertBST(root, elem);
break;
}
case 2:
{
int elem;
printf("Enter element to delete: ");
scanf("%d", &elem);
root = deleteBST(root, elem);
break;
}
case 3:
printf("Preorder: ");
preorder(root);
printf("\n");
break;
case 4:
printf("Inorder: ");
inorder(root);
printf("\n");
break;
case 5:
printf("Postorder: ");
postorder(root);
printf("\n");
break;
case 6:
{
int elem;
printf("Enter element to search: ");
scanf("%d", &elem);
int ch = searchBST(root, elem);
if (ch == 1)
printf("Element found\n");
else
printf("Element not found\n");
}
case 7:
break;
default:
printf("Invalid Choice");
break;
}
}
return 0;
}
OUTPUT
// Experiment 19: Implement graph traversal (DFS & BFS) - (Undirected Graph)
struct vertex
{
int adjacency[MAX_VERTICES];
int num_neighbors;
};
struct queue
{
int *Q;
int front;
int rear;
int size;
};
int visited[MAX_VERTICES];
void BFS(int start, struct vertex *graph, int num_vertices, int num_edges)
{
for (int i = 0; i < num_vertices; i++)
{
visited[i] = 0;
}
struct queue Q;
Q.front = Q.rear = -1;
Q.size = QUEUE_SIZE;
Q.Q = (int *)malloc(sizeof(int) * QUEUE_SIZE);
enqueue(&Q, start);
visited[start] = 1;
while (!isEmpty(&Q))
{
int u = dequeue(&Q);
printf("%d ", u);
int toVisit[MAX_VERTICES], n = 0;
visited[u] = 2;
}
DFS_helper(start, graph);
}
int main()
{
int num_vertices, num_edges;
struct vertex *graph = (struct vertex *)malloc(sizeof(struct vertex) *
MAX_VERTICES);
printf("Enter the number of vertices: ");
scanf("%d", &num_vertices);
printf("Enter the number of edges: ");
scanf("%d", &num_edges);
//invalid case
if (num_vertices <= 0 || num_edges < 0)
return 0;
int already_present = 0;
for (int k = 0; k < graph[u].num_neighbors; k++)
{
if (graph[u].adjacency[k] == v)
{
already_present = 1;
break;
}
}
if (already_present)
{
printf("This vertices is already present!\n");
i--;
continue;
}
graph[u].adjacency[graph[u].num_neighbors++] = v;
graph[v].adjacency[graph[v].num_neighbors++] = u;
}
showAdjancencyLists(graph, num_vertices);
int choice = 1;
while (choice != 4)
{
printf("\nMenu:\n1. Breadth-first search\n2. Depth-first search\n3.
Show adjacency Lists\n4. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
int start;
printf("Enter the starting vertex: ");
scanf("%d", &start);
BFS(start, graph, num_vertices, num_edges);
break;
}
case 2:
{
int start;
printf("Enter the starting vertex: ");
scanf("%d", &start);
DFS(start, graph, num_vertices);
break;
}
case 3:
showAdjancencyLists(graph, num_vertices);
break;
case 4:
break;
default:
printf("Invalid choice\n");
break;
}
}
return 0;
}
OUTPUT
20. Make a menu driven program to perform various sorting techniques
(insertion, shell, merge, heap, bubble, quick).
#include <stdio.h>
#include <malloc.h>
if (size == 0)
{
printf("Array is empty\n");
return;
}
// Insertion Sort
// Shell Sort
j -= gap;
}
A[j + gap] = temp;
}
}
}
// Merge Sort
int B[100];
int i = l, j = mid + 1, k = 0;
else
B[k++] = A[j++];
}
// Heap Sort
int heapSize = 0;
void insertHeap(int heap[], int key)
{
heap[heapSize] = key;
heapSize++;
if (heapSize > 1)
{
int i = heapSize - 1;
while (i > 0 && key > heap[(i - 1) / 2])
{
heap[i] = heap[(i - 1) / 2];
i = (i - 1) / 2;
}
heap[i] = key;
}
}
void deleteHeap(int heap[])
{
int x = heap[0];
heap[0] = heap[heapSize - 1];
heapSize--;
int i = 0, j = 2 * i + 1;
while (j < heapSize)
{
if (heap[j + 1] > heap[j])
j++;
if (heap[i] < heap[j])
{
swapAdd(&heap[i], &heap[j]);
i = j;
j = 2 * i + 1;
}
else
break;
}
heap[heapSize] = x;
}
void heapSort(int A[], int size)
{
for (int i = 0; i < size; i++)
insertHeap(A, A[i]);
for (int i = 1; i < size; i++)
deleteHeap(A);
}
// Bubble Sort
void bubbleSort(int A[], int size)
{
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - 1 - i; j++)
{
if (A[j] > A[j + 1])
{
int temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
}
// Quick Sort
int Partition(int A[], int l, int h)
{
int Pivot = A[l];
int i = l, j = h;
do
{
do
{
i++;
} while (A[i] <= Pivot);
do
{
j--;
} while (A[j] > Pivot);
if (i < j)
swapAdd(&A[i], &A[j]);
} while (i < j);
swapAdd(&A[l], &A[j]);
return j;
}
void rQuickSort(int A[], int l, int h)
{
int j;
if (l < h)
{
j = Partition(A, l, h);
rQuickSort(A, l, j);
rQuickSort(A, j + 1, h);
}
}
void quickSort(int A[], int size)
{
A[size] = 32456;
rQuickSort(A, 0, size);
}
int main()
{
int size;
printf("Enter size of array: ");
scanf("%d", &size);
int A[size + 1];
for (int i = 0; i < size; i++)
{
printf("Enter %dth element: ", i + 1);
scanf("%d", &A[i]);
}
int ch;
printf("1. Insertion\n2. shell\n3. merge\n4. heap\n5. bubble\n6. quick\n7.
Exit\nEnter your choice");
scanf("%d", &ch);
switch (ch) {
case 1:
insertionSort(A, size);
break;
case 2:
shellSort(A, size);
break;
case 3:
mergeSort(A, size);
break;
case 4:
heapSort(A, size);
break;
case 5:
bubbleSort(A, size);
break;
case 6:
quickSort(A, size);
break;
case 7:
break;
default:
break;
}
display(A, size);
return 0;
}
OUTPUT