1.
Write a c program to implement the following operations for Stack ADT using Array
implementation
i) Insert ii) Delete iii) Display
program:
#include <stdio.h>
int stack[100],i,j,choice=0,n,top=-1;
void push();
void pop();
void display();
void main ()
printf("Enter the number of elements in the stack ");
scanf("%d",&n);
printf("Stack operations using array");
while(choice != 4)
printf("Chose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
case 2:
pop();
break;
}
case 3:
display();
break;
case 4:
printf("Exiting....");
break;
default:
printf("Please Enter valid choice ");
};
void push ()
int val;
if (top == n )
printf("\n Overflow");
else
printf("Enter the value?");
scanf("%d",&val);
top = top +1;
stack[top] = val;
}
}
void pop ()
if(top == -1)
printf("Underflow");
else
top = top -1;
void display()
for (i=top;i>=0;i--)
printf("%d\n",stack[i]);
if(top == -1)
printf("Stack is empty");
Output:
Enter the number of elements in the stack 4
Stack operations using arrayChose one from the below options...
1.Push
2.Pop
3.display
4.Exit
Enter your choice
Enter the value 45
45
Chose one from the below options...
1.Push
2.Pop
3.display
4.Exit
Enter your choice
Enter the value 65
Chose one from the below options...
1.Push
2.Pop
3.display
4.Exit
Enter your choice
Element is popped
Chose one from the below options...
1.Push
2.Pop
3.display
4.Exit
Enter your choice
45
Chose one from the below options...
1.Push
2.Pop
3.display
4.Exit
Enter your choice 4
Existing……
2.Write a C program to search an existing element in a singly linked list.
Program:
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node *next;
};
void display(struct Node* node){
while(node!=NULL)
printf("%d ",node->data);
node = node->next;
printf("\n");
int searchElement(struct Node* head, int item, int index)
if (head == NULL)
return -1;
if (head->data == item)
return index;
index++;
return searchElement(head->next, item, index);
int main()
int item;
struct Node* head = NULL;
struct Node* node2 = NULL;
struct Node* node3 = NULL;
struct Node* node4 = NULL;
head = (struct Node*)malloc(sizeof(struct Node));
node2 = (struct Node*)malloc(sizeof(struct Node));
node3 = (struct Node*)malloc(sizeof(struct Node));
node4 = (struct Node*)malloc(sizeof(struct Node));
head->data = 10; // data set for head node
head->next = node2;
node2->data = 15;
node2->next = node3;
node3->data = 20;
node3->next = node4;
node4->data = 25;
node4->next = NULL;
printf("Linked List: ");
display(head);
printf("Enter element to search: ");
scanf("%d", &item);
int index = searchElement(head, item, 0);
if(index == -1)
printf("Item not found");
else
printf("Item found at position: %d", index+1);
return 0;
Output:
Linked List: 10 15 20 25
Enter element to search: 15
Item found at position: 2
3.Write a C program to implement linear queue using linked list method.
Program:
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node * next;
};
struct node * front = NULL;
struct node * rear = NULL;
void enqueue(int value) {
struct node * ptr;
ptr = (struct node * ) malloc(sizeof(struct node));
ptr -> data = value;
ptr -> next = NULL;
if ((front == NULL) && (rear == NULL)) {
front = rear = ptr;
} else {
rear -> next = ptr;
rear = ptr;
printf("Node is Inserted\n\n");
int dequeue() {
if (front == NULL) {
printf("\nUnderflow\n");
return -1;
} else {
struct node * temp = front;
int temp_data = front -> data;
front = front -> next;
free(temp);
return temp_data;
void display() {
struct node * temp;
if ((front == NULL) && (rear == NULL)) {
printf("\nQueue is Empty\n");
} else {
printf("The queue is \n");
temp = front;
while (temp) {
printf("%d--->", temp -> data);
temp = temp -> next;
printf("NULL\n\n");
int main() {
int choice, value;
printf("\nImplementation of Queue using Linked List\n");
while (choice != 4) {
printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
printf("\nEnter your choice : ");
scanf("%d", & choice);
switch (choice) {
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", & value);
enqueue(value);
break;
case 2:
printf("Popped element is :%d\n", dequeue());
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
return 0;
Output:
Implementation of Queue using Linked List
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 1
Enter the value to insert: 45
Node is Inserted
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 1
Enter the value to insert: 34
Node is Inserted
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 3
The queue is
45--->34--->NULL
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 2
Popped element is :45
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 3
The queue is
34--->NULL
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 4
9.Find the element 41 from the given list, by implementing sequential search algorithm in C
language,
Program:
#include<stdio.h>
void main() {
int arr[20], n, key, i;
printf("Number of elements in the list: ");
scanf("%d", &n);
printf("Enter elements of the list: ");
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Enter the element to search : ");
scanf("%d", &key);
for (i = 0; i < n; i++)
if (arr[i] == key) {
printf("Key element found at index %d", i) ;
break;
if(i==n)
printf("Key element not found");
Output:
Number of elements in the list: 9
Enter elements of the list:
70
40
30
11
57
41
25
14
52
Enter the element to search : 41
Key element found at index 5
11. Write a C programs for implementing the following sorting methods to arrange a
list of integers in ascending order:
Insertion sort b) Merge sort
Program:
Insertion sort
#include <stdio.h>
int main() {
int arr[] = {12, 31, 25, 8, 32, 17};
int n = sizeof(arr)/sizeof(arr[0]);
int i, j, key;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
return 0;
Output:
Sorted array: 8 12 17 25 31 32
Merge sort
Program:
#include <stdio.h>
#include <stdlib.h>
void merge(int arr[], int left[], int leftSize, int right[], int rightSize) {
int i = 0, j = 0, k = 0;
while (i < leftSize && j < rightSize) {
if (left[i] < right[j]) {
arr[k++] = left[i++];
} else {
arr[k++] = right[j++];
while (i < leftSize) {
arr[k++] = left[i++];
while (j < rightSize) {
arr[k++] = right[j++];
void mergeSort(int arr[], int size) {
if (size < 2) {
return;
int mid = size / 2;
int left[mid];
int right[size - mid];
for (int i = 0; i < mid; i++) {
left[i] = arr[i];
for (int i = mid; i < size; i++) {
right[i - mid] = arr[i];
mergeSort(left, mid);
mergeSort(right, size - mid);
merge(arr, left, mid, right, size - mid);
int main() {
int arr[] = {38, 27, 43, 3, 9, 82, 10};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Original Array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
mergeSort(arr, size);
printf("\nSorted Array (Ascending): ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
return 0;
Output:
Original Array: 38 27 43 3 9 82 10
Sorted Array (Ascending): 3 9 10 27 38 43 82
12. i)Write a C program to implement selection sort for the given array
arr[] = {64, 25, 12, 22, 11}.
Program:
#include <stdio.h>
void selection(int arr[], int n)
int i, j, min;
for (i = 0; i < n-1; i++)
min = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min])
min = j;
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
void printArr(int a[], int n) /* function to print the array */
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
int main()
int a[] = {64,25,12,22,11};
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
selection(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
Output:
Before sorting array elements are -
64 25 12 22 11
After sorting array elements are -
11 12 22 25 64
ii)Write a C program to implement merge sort for the {38, 27, 43, 3, 9, 82, 10}
Merge sort
Program:
#include <stdio.h>
#include <stdlib.h>
void merge(int arr[], int left[], int leftSize, int right[], int rightSize) {
int i = 0, j = 0, k = 0;
while (i < leftSize && j < rightSize) {
if (left[i] < right[j]) {
arr[k++] = left[i++];
} else {
arr[k++] = right[j++];
while (i < leftSize) {
arr[k++] = left[i++];
while (j < rightSize) {
arr[k++] = right[j++];
void mergeSort(int arr[], int size) {
if (size < 2) {
return;
int mid = size / 2;
int left[mid];
int right[size - mid];
for (int i = 0; i < mid; i++) {
left[i] = arr[i];
}
for (int i = mid; i < size; i++) {
right[i - mid] = arr[i];
mergeSort(left, mid);
mergeSort(right, size - mid);
merge(arr, left, mid, right, size - mid);
int main() {
int arr[] = {38, 27, 43, 3, 9, 82, 10};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Original Array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
mergeSort(arr, size);
printf("\nSorted Array (Ascending): ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
return 0;
Output:
Original Array: 38 27 43 3 9 82 10
Sorted Array (Ascending): 3 9 10 27 38 43 82
13i)Write a C program to implement LIFO structure, using array implementation.
Program:
#include <stdio.h>
int stack[100],i,j,choice=0,n,top=-1;
void push();
void pop();
void display();
void main ()
printf("Enter the number of elements in the stack ");
scanf("%d",&n);
printf("Stack operations using array");
while(choice != 4)
printf("Chose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
{
printf("Exiting....");
break;
default:
printf("Please Enter valid choice ");
};
void push ()
int val;
if (top == n )
printf("\n Overflow");
else
printf("Enter the value?");
scanf("%d",&val);
top = top +1;
stack[top] = val;
void pop ()
if(top == -1)
printf("Underflow");
else
top = top -1;
}
void display()
for (i=top;i>=0;i--)
printf("%d\n",stack[i]);
if(top == -1)
printf("Stack is empty");
Output:
Enter the number of elements in the stack 4
Stack operations using arrayChose one from the below options...
1.Push
2.Pop
3.display
4.Exit
Enter your choice
Enter the value 45
45
Chose one from the below options...
1.Push
2.Pop
3.display
4.Exit
Enter your choice
Enter the value 65
Chose one from the below options...
1.Push
2.Pop
3.display
4.Exit
Enter your choice
Element is popped
Chose one from the below options...
1.Push
2.Pop
3.display
4.Exit
Enter your choice
45
Chose one from the below options...
1.Push
2.Pop
3.display
4.Exit
Enter your choice 4
Existing….
ii)Write a C program to find an element in a list by using sequential order.
Program:
#include<stdio.h>
void main() {
int arr[20], n, key, i;
printf("Number of elements in the list: ");
scanf("%d", &n);
printf("Enter elements of the list: ");
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Enter the element to search : ");
scanf("%d", &key);
for (i = 0; i < n; i++)
if (arr[i] == key) {
printf("Key element found at index %d", i) ;
break;
if(i==n)
printf("Key element not found");
Output:
Number of elements in the list: 9
Enter elements of the list:
70
40
30
11
57
41
25
14
52
Enter the element to search : 41
Key element found at index 5
16.Write a C program to implement any one of the hashing technique.
Program:
#include<stdio.h>
#include<conio.h>
void display(int a[],int n)
int i;
for(i=0;i<n;i++)
printf("\n %d %d",i,a[i]);
void linear_prob(int table[],int tsize,int num)
int key;
key=num%tsize;
while(table[key]!=-1)
key=(key+1)%tsize;
table[key]=num;
display(table,tsize);
int main()
int SIZE=10;
int num,i;
int hash_table[SIZE];
char ans;
for(i=0;i<SIZE;i++)
hash_table[i]=-1;
do
{
printf("\nEnter the Number:");
scanf("%d",&num);
linear_prob(hash_table,SIZE,num);
printf("\n Do you want to continue(y/n)");
ans=getch();
}while(ans=='y');
return 0;
Output:
Enter the Number:131
0 -1
1 131
2 -1
3 -1
4 -1
5 -1
6 -1
7 -1
8 -1
9 -1
Do you want to continue(y/n) y
Enter the Number:21
0 -1
1 131
2 21
3 -1
4 -1
5 -1
6 -1
7 -1
8 -1
9 -1
Do you want to continue(y/n) y
Enter the Number:3
0 -1
1 131
2 21
33
4 -1
5 -1
6 -1
7 -1
8 -1
9 -1
Do you want to continue(y/n) y
Enter the Number:4
0 -1
1 131
2 21
33
44
5 -1
6 -1
7 -1
8 -1
9 -1
Do you want to continue(y/n) y
Enter the Number:21
0 -1
1 131
2 21
33
44
55
6 -1
7 -1
8 -1
9 -1
Do you want to continue(y/n) n
17.Write a C program to implement circular queue data structures using array implementation.
Program:
#include <stdio.h>
#define SIZE 5
int items[SIZE];
int front = -1, rear = -1;
int isFull() {
if ((front == rear + 1) || (front == 0 && rear == SIZE - 1)) return 1;
return 0;
int isEmpty() {
if (front == -1) return 1;
return 0;
void enQueue(int element) {
if (isFull())
printf("\n Queue is full!! \n");
else {
if (front == -1) front = 0;
rear = (rear + 1) % SIZE;
items[rear] = element;
printf("\n Inserted -> %d", element);
}
int deQueue() {
int element;
if (isEmpty()) {
printf("\n Queue is empty !! \n");
return (-1);
} else {
element = items[front];
if (front == rear) {
front = -1;
rear = -1;
else {
front = (front + 1) % SIZE;
printf("\n Deleted element -> %d \n", element);
return (element);
void display() {
int i;
if (isEmpty())
printf(" \n Empty Queue\n");
else {
printf("\n Front -> %d ", front);
printf("\n Items -> ");
for (i = front; i != rear; i = (i + 1) % SIZE) {
printf("%d ", items[i]);
printf("%d ", items[i]);
printf("\n Rear -> %d \n", rear);
int main() {
deQueue();
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);
enQueue(6);
display();
deQueue();
display();
enQueue(7);
display();
enQueue(8);
return 0;
Output:
Queue is empty !!
Inserted -> 1
Inserted -> 2
Inserted -> 3
Inserted -> 4
Inserted -> 5
Queue is full!!
Front -> 0
Items -> 1 2 3 4 5
Rear -> 4
Deleted element -> 1
Front -> 1
Items -> 2 3 4 5
Rear -> 4
Inserted -> 7
Front -> 1
Items -> 2 3 4 5 7
Rear -> 0
Queue is full!!
20.Write a C program to sort the given unsorted array of elements using insertion sort
{12,31,25,8,32,17}
Program:
#include <stdio.h>
int main() {
int arr[] = {12, 31, 25, 8, 32, 17};
int n = sizeof(arr)/sizeof(arr[0]);
int i, j, key;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
}
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
return 0;
Output:
Sorted array: 8 12 17 25 31 32