DFS Lab File
DFS Lab File
1.Traverse
2.Insert
3.Delete
4.Linear Search
5.Binary Search
7.Bubble Sort
8.Insertion Sort
9.Selection Sort
Shuffle
Exit
#include <stdio.h>
int main(){
int array[100];
int idx;
int temp;
int choice2;
int choice;
int choice3;
scanf("%d", &size);
scanf("%d", &array[i]);
printf("%d\t", array[i]);
do{
printf("\nMenuDriven\n");
scanf("%d", &choice);
switch (choice){
case 1:
traverse(array, size);
break;
case 2:
break;
case 3:
break;
case 4:
do{
printf("\n");
scanf("%d", &choice2);
switch (choice2){
case 1:
break;
case 2:
break;
case 3:
break;
default:
break;
case 5:
// Sorting of array
do{
printf("\n");
scanf("%d", &choice3);
switch (choice3){
case 1:
bubble_sort(array, size);
break;
case 2:
insertion_sort(array, size);
break;
case 3:
// Selection sort of the array
selection_sort(array, size);
break;
case 4:
break;
default:
break;
case 6:
second_largest(array, size);
break;
case 7:
break;
default:
return 0;
int i;
printf("%d\t", array[i]);
}
return 0;
int i;
scanf("%d", &idx);
scanf("%d", &key);
array[idx - 1] = key;
size++;
printf("%d\t", array[i]);
}
return 0;
int count;
int i, j;
scanf("%d", &key);
if (array[i] == key){
size--;
count++;
}
if (count == 0){
else{
printf("%d\t", array[i]);
return 0;
int i;
int count = 0;
int flag = 0;
scanf("%d", &key);
for (i = 0; i < size; i++){
if (array[i] == key){
if (flag == 0){
flag = 1;
printf("%d\n", i + 1);
count++;
printf("\n");
return 0;
int i, j;
int count = 0;
temp = array[j];
array[j + 1] = temp;
printf("%d\t", array[i]);
Printf(“\n”);
scanf("%d", &key);
start = 0;
end = size - 1;
if (array[mid] == key){
count++;
break;
start = mid + 1;
end = mid - 1;
if (count == 0){
printf("\n");
return 0;
// Bubble sorting
int i, j;
int temp;
temp = array[j];
array[j + 1] = temp;
Printf(“\n”);
return 0;
// Insertion sorting
int i, j;
int temp;
temp = array[i];
else
break;
array[j] = temp;
printf("\nSorted array is after phase %d is: \n", i);
printf("%d\t", array[j]);
return 0;
// Selection sorting
int i, j, k;
min = i;
min = j;
}
if (min != i){
temp = array[i];
array[i] = array[min];
array[min] = temp;
printf("%d\t", array[k]);
return 0;
int temp;
int i, j;
array[j + 1] = temp;
}
2. Write a C program to check whether a matrix is sparse or not.
#include <stdio.h>
int main(){
int i,j;
int count=0;
int array[10][10];
scanf("%d", &row);
scanf("%d", &col);
scanf("%d", &array[i][j]);
printf("%d\t", array[i][j]);
printf("\n");
printf("\n");
if(array[i][j] == 0){
count++;
}
}
else
return 0;
Output:-
3. Write a C program to convert sparse matrix into 3 - row triplet
representation.
#include <stdio.h>
int main(){
int i,j;
int count=0;
int array[10][10];
scanf("%d", &row);
scanf("%d", &col);
scanf("%d", &array[i][j]);
printf("%d\t", array[i][j]);
printf("\n");
printf("\n");
if(array[i][j] != 0){
count++;
int sm[3][count];
int ele=0;
scanf("%d", &val);
if(val== 1){
if(array[i][j] != 0){
sm[0][ele] = i;
sm[1][ele] = j;
sm[2][ele] = array[i][j];
ele++;
printf("%d\t", sm[i][j]);
printf("\n");
}
else if(val == 2){
if(array[i][j] != 0){
sm[ele][0] = i;
sm[ele][1] = j;
sm[ele][2]= array[i][j];
ele++;
printf("%d\t", sm[i][j]);
printf("\n");
}
}
else
return 0;
Output:-
4. Write A C Program to Perform Following Operation on Matrix.
Exit.
#include <stdio.h>
int sub(int array1[10][10], int array2[10][10], int arrayresult[10][10], int row, int
col);
int main(){
int i, j;
int array1[10][10];
int array2[10][10];
int arrayresult[10][10];
int choice;
scanf("%d", &row);
scanf("%d", &col);
scanf("%d", &array1[i][j]);
scanf("%d", &array2[i][j]);
printf("%d\t", array1[i][j]);
printf("\n");
printf("\n");
printf("%d\t", array2[i][j]);
printf("\n");
do{
printf("\nMenuDriven\n");
scanf("%d", &choice);
switch (choice){
case 1:
// Addition
break;
case 2:
// Subtraction
break;
case 3:
// Multiplication
break;
case 4:
// Transpose
break;
case 5:
// Diagonal Sum
break;
case 6:
// Identity or Not
break;
case 7:
break;
default:
return 0;
// sum
int i, j;
printf("%d\t", arrayresult[i][j]);
printf("\n");
return 0;
//subtraction
int sub(int array1[10][10], int array2[10][10], int arrayresult[10][10], int row, int
col){
int i, j;
printf("%d\t", arrayresult[i][j]);
printf("\n");
return 0;
//multiply
int i, j, k;
arrayresult[i][j] = 0;
}
arrayresult[i][j] += (array1[i][k]*array2[k][j]);
printf("%d\t", arrayresult[i][j]);
printf("\n");
return 0;
//Transpose
int transpose(int array1[10][10], int array2[10][10], int arrayresult[10][10], int
row, int col){
int i, j;
arrayresult[j][i] = 0;
arrayresult[j][i] = array1[i][j];
printf("%d\t", arrayresult[i][j]);
printf("\n");
}
}
printf("%d\t", arrayresult[i][j]);
printf("\n");
arrayresult[j][i] = array2[i][j];
printf("%d\t", arrayresult[i][j]);
}
printf("\n");
printf("%d\t", arrayresult[i][j]);
printf("\n");
return 0;
//Diagonal sum
int i, j;
int sum = 0;
int sum1 = 0;
if(i == j){
sum += array1[i][j];
if(i == j){
sum1 += array2[i][j];
return 0;
//Identity or Not
int count=0;
int count2 = 0;
int i, j;
count++;
count2++;
else{
count=0;
count2 = 0;
count++;
count2++;
else{
return 0;
Outputs:-
Addition:-
Subtraction:-
Multiplication:-
Transpose:-
Diagonal Sum:-
Identity or Not:-
Exit:-
5. Write A C Program to Perform Count Sort on An Unsorted Array.
#include <stdio.h>
int main(){
int i;
int arr[50];
int array[50];
int count[10];
scanf("%d", &size);
scanf("%d", &array[i]);
printf("%d\n", array[i]);
max= array[0];
count[i] = 0;
count[array[i]]++;
count[i]= count[i]+count[i-1];
arr[count[array[i]]-1] = array[i];
count[array[i]]--;
array[i] = arr[i];
return 0;}
Output:-
6. Write a C program to perform Radix sort on an unsorted array.
#include<stdio.h>
#include<stdlib.h>
max = A[i];
return max;
int b[n];
a[i] = b[i];
CountSort(a, n, pos);
int main() {
int a[] = {432, 8, 530, 90, 88, 231, 11, 45, 677, 199};
int n = sizeof(a) / sizeof(a[0]);
redixSort(a, n);
return 0;
}
7. Write a C program to dynamically allocate an array and calculate sum of its
elements.
Using malloc:-
#include <stdlib.h>
#include <stdio.h>
int main(){
int *p;
int n, i;
int sum = 0;
scanf("%d", &n);
scanf("%d", p+i);
printf("%d\n", *(p+i));
return 0;
Output:-
Using calloc:-
#include <stdlib.h>
#include <stdio.h>
int main(){
int *p;
int n, i;
int sum = 0;
int size;
scanf("%d", &n);
scanf("%d", p+i);
printf("%d\n", *(p+i));
sum +=*(p+i);
return 0;
Output:-
8. Write A C Program to Remove Duplicate Elements from An Array.
#include <stdio.h>
int main(){
int i, j;
int arr[20];
scanf("%d", &size);
scanf("%d", &arr[i]);
printf("%d\n", arr[i]);
if(arr[i]>arr[j]){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
printf("%d\n", arr[i]);
if(arr[i] == arr[i+1]){
arr[j]=arr[j+1];
size--;
printf("%d\n", arr[i]);
return 0;
}
Output:-
Deleting a node from the linked list by specifying the value of the deleting
node.
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node*next;
};
int main(){
int choice = 1;
while(choice){
scanf("%d", &newnode->data);
newnode->next = NULL;
if(start == NULL){
start = newnode;
temp = newnode;
else{
temp->next = newnode;
temp = newnode;
printf("created successsfully\n");
printf("Do you want to add more node if yes type any number if no type 0:
");
scanf("%d", &choice);
printf("\n");
temp = start;
while(temp != NULL){
do{
printf("\nMenuDriven\n");
printf("1. Addition \n");
scanf("%d", &choice2);
switch (choice2){
case 1:
printf("\n");
do{
scanf("%d", &choice4);
switch (choice4){
case 1:
add_node_end(start);
break;
case 2:
add_node_begin(start);
break;
case 3:
add_node_speposc(start);
break;
case 4:
break;
default:
break;
case 2:
// traverse of node
traverse(start);
break;
case 3:
// counting of node
printf("You selected Option 3.\n");
count_node(start);
break;
case 4:
// deletion of node
printf("\n");
do{
scanf("%d", &choice3);
switch (choice3){
case 1:
break;
case 2:
delete_node_end(start);
break;
case 3:
delete_node_spec(start);
break;
case 4:
break;
default:
break;
case 5:
// reverse of node
reverse(start);
break;
case 6:
// sorting of node
sorting(start);
break;
case 7:
// searching of node
search(start);
break;
case 8:
insert_sort(start);
break;
case 9:
break;
default:
scanf("%d", &newnode->data);
newnode->next = NULL;
temp = start;
printf("Added successsfully\n");
temp = start;
while(temp != NULL){
temp = temp->next;
}
//add new node at begin
temp = start;
scanf("%d", &newnode->data);
newnode->next = NULL;
start = newnode;
printf("Added successsfully\n");
temp = start;
while(temp != NULL){
temp = temp->next;
}
int choice = 1;
int pos, i;
scanf("%d", &pos);
temp = start;
i = 1;
while(i<pos-1){
i++;
newnode->next=NULL;
temp->next = newnode;
printf("Added successsfully.\n");
temp = start;
while(temp != NULL){
temp = temp->next;
//traverse of nodes
temp = start;
while(temp !=NULL){
temp = temp->next;
//counting of node
int count = 0;
temp = start;
while(temp !=NULL){
temp = temp->next;
count++;
//deletion of nodes
void delete_node_beg(struct node* start){
temp = start;
start= temp->next;
free(temp);
temp = start;
while(temp !=NULL){
temp = temp->next;
int i = 1;
int pos;
scanf("%d", &pos);
temp = start;
i++;
free(temp1);
temp = start;
while(temp != NULL){
}
// delete node at end
int choice;
temp = start;
while(temp->next != NULL){
prev = temp;
free(temp);
temp = start;
while(temp != NULL){
}
//reverse of linked list
int count = 0;
int temp2;
int i, j;
temp = start;
temp = temp->next;
count++;
temp = start;
tail=start;
printf("\n");
temp = start;
temp = temp->next;
int count = 0;
int temp2, i, j;
temp = start;
while(temp !=NULL){
temp = temp->next;
count++;
temp = start;
tail = temp->next;
temp2 = temp->data;
temp->data = tail->data;
tail->data = temp2;
temp = tail;
tail = tail->next;
temp = start;
printf("\n");
temp = temp->next;
// search of node
int flag = 0;
int ser;
int count = 0;
scanf("%d", &ser);
printf("\n");
temp = start;
count++;
if(temp->data == ser){
flag++;
temp = temp->next;
if(flag){
else{
int choice2 = 1;
int count = 0;
temp = start;
while(temp !=NULL){
temp = temp->next;
count++;
temp = start;
tail = temp->next;
temp2 = temp->data;
temp->data = tail->data;
tail->data = temp2;
temp = tail;
tail = tail->next;
temp = start;
printf("\n");
temp = temp->next; }
printf("\n");
newnode->next = NULL;
scanf("%d", &ins);
newnode->data = ins;
temp = start;
tail = temp->next;
if (ins < temp->data){
newnode->next = temp;
start = newnode;
else{
temp = tail;
tail = tail->next;
newnode->next = tail;
temp->next = newnode;
printf("\n");
temp = start;
temp = temp->next;
}
Output:-
Addition at end:-
Addition at beginning:-
Addition at specific position:-
Traversing:-
Counting of Nodes:-
Sorting:-
Searching:-
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node*next;
struct node*prev;
};
int main(){
int choice = 1;
while(choice){
scanf("%d", &newnode->data);
newnode->next = NULL;
if(start == NULL){
start = newnode;
temp = newnode;
else{
temp->next = newnode;
temp = newnode;
printf("created successsfully\n");
printf("Do you want to add more node if yes type any number if no type 0:
");
scanf("%d", &choice);
printf("\n");
temp = start;
while(temp != NULL){
do{
printf("\nMenuDriven\n");
scanf("%d", &choice2);
switch (choice2){
case 1:
printf("\n");
do{
scanf("%d", &choice4);
switch (choice4){
case 1:
add_node_end(start);
break;
case 2:
add_node_begin(start);
break;
case 3:
break;
case 4:
break;
default:
break;
case 2:
// traverse of node
traverse(start);
break;
case 3:
// counting of node
printf("You selected Option 3.\n");
count_node(start);
break;
case 4:
// deletion of node
printf("\n");
do{
scanf("%d", &choice3);
switch (choice3){
case 1:
break;
case 2:
delete_node_end(start);
break;
case 3:
delete_node_spec(start);
break;
case 4:
break;
default:
}
}
break;
case 5:
// reverse of node
reverse(start);
break;
case 6:
// sorting of node
sorting(start);
break;
case 7:
// searching of node
search(start);
break;
case 8:
break;
default:
temp = start;
temp = temp->next;
}
newnode = (struct node *)malloc(sizeof(struct node));
newnode->next = NULL;
newnode->prev = NULL;
scanf("%d", &newnode->data);
printf("\n");
temp = start;
temp = temp->next;
newnode->next = NULL;
newnode->prev = NULL;
scanf("%d", &newnode->data);
temp =start;
start = newnode;
printf("\n");
temp = start;
temp = temp->next;
}
//add new node at specific position
int pos;
scanf("%d", &pos);
temp = start;
int i =1;
temp = temp->next;
i++;
newnode->next = NULL;
newnode->prev = NULL;
scanf("%d", &newnode->data);
printf("\n");
temp = start;
temp = temp->next;
//traverse of nodes
temp = start;
temp = temp->next;
}
}
//counting of node
int count = 0;
temp = start;
temp = temp->next;
count++;
printf("\n");
//deletion of nodes
temp = start;
start = temp ->next;
free(temp);
printf("\n");
temp = start;
temp = temp->next;
int pos;
int i = 0;
printf("Enter the position which you want to delete:");
scanf("%d", &pos);
temp = start;
temp2 = temp;
temp = temp->next;
i++;
free(temp);
printf("\n");
temp = start;
temp = temp->next;
temp = start;
temp2 = temp;
temp = temp->next;
free(temp);
printf("\n");
temp = start;
temp = temp->next;
}
//reverse of Linked list(it does not work)
int count = 0;
int temp2;
temp = start;
tail = temp;
temp = temp->next;
count++;
temp = start;
temp = start;
temp = temp->next;
int i, j, temp2;
temp = start;
temp = temp->next;
count++;
}
temp = start;
printf("\n");
temp = start;
temp = temp->next;
}
// search of node
int ser;
scanf("%d", &ser);
temp = start;
while(temp != NULL){
count++;
flag = 0;
if(flag ==1){
Outputs:-
Addition at end:-
Addition at beginning:-
Addition at specific position:-
Traversing:-
1Counting of nodes:-
Searching:-
11.Write a program to following operations on Circular singly linked list
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node*next;
};
int main(){
int choice = 1;
while(choice){
scanf("%d", &newnode->data);
newnode->next = NULL;
if(start == NULL){
start = newnode;
temp = newnode;
else{
temp->next = newnode;
temp = newnode;
printf("created successsfully\n");
printf("Do you want to add more node if yes type any number if no type 0:
");
scanf("%d", &choice);
printf("\n");
temp = start;
do{
printf("\nMenuDriven\n");
scanf("%d", &choice2);
switch (choice2){
case 1:
printf("\n");
do{
scanf("%d", &choice4);
switch (choice4){
case 1:
add_node_end(start);
break;
case 2:
add_node_begin(start);
break;
case 3:
add_node_speposc(start);
break;
case 4:
break;
default:
break;
case 2:
// traverse of node
break;
case 3:
// counting of node
count_node(start);
break;
case 4:
// deletion of node
printf("\n");
do{
scanf("%d", &choice3);
switch (choice3){
case 1:
delete_node_beg(start);
break;
case 2:
delete_node_end(start);
break;
case 3:
delete_node_spec(start);
break;
case 4:
printf("Exiting the program. Back to main menu! \n");
break;
default:
break;
case 5:
// reverse of node
reverse(start);
break;
case 6:
// sorting of node
sorting(start);
break;
case 7:
// searching of node
search(start);
break;
case 8:
break;
default:
scanf("%d", &newnode->data);
newnode->next = NULL;
temp = start;
temp = temp->next;
printf("\n");
temp = start;
temp = temp->next;
temp = start;
temp = temp->next;
start = newnode;
printf("\n");
temp = start;
temp = temp->next;
int pos;
int i =1;
scanf("%d", &pos);
temp = start;
i++;
printf("\n");
temp = start;
temp = temp->next;
//traverse of nodes
temp = start;
temp = temp->next;
//counting of node
temp = start;
temp = temp->next;
count++;
}
printf("\n");
//deletion of nodes
temp = start;
temp = temp->next;
tail = temp;
temp = start;
free(temp);
temp = start;
temp = temp->next; }
int pos;
int i =0;
scanf("%d", &pos);
temp = start;
i++;
free(temp);
printf("\n");
temp = start;
temp = temp->next;
tail = tail->next;
temp = temp->next;
free(temp);
temp = start;
temp = temp->next;
// int count = 0;
// int temp2;
// temp = start;
// tail = temp;
// temp = temp->next;
// count++;
// }
// temp = start;
// }
// printf("\n");
// temp = temp->next;
// }
int temp2, I, j;
int count = 0;
temp = start;
count++;
temp = temp->next;
}
printf("\n");
temp = start;
temp = tail;
temp = start;
temp = temp->next;
}
// search of node
int count = 0;
int ser;
scanf("%d", &ser);
printf("\n");
temp = start;
count++;
flag--;
break; }
while(flag){
else{
} }
Output:-
Addition at end:-
Addition at beginning:-
Deletion at Beginning:-
Deletion at Ending:-
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
};
void add_node_end();
void add_node_begin();
void add_node_speposc();
void traverse();
int count_node();
void delete_node_beg();
void delete_node_spec();
void delete_node_end();
void reverse();
void sorting();
void search();
int choice = 1;
int main(){
while (choice){
scanf("%d", &newnode->data);
newnode->next = NULL;
newnode->prev = NULL;
if (start == NULL){
start = newnode;
tail = newnode;
newnode->next = start;
newnode->prev = temp;
else{
tail->next = newnode;
newnode->next = start;
newnode->prev = tail;
tail = newnode;
start->prev = tail;
printf("created successsfully\n");
printf("Do you want to add more node if yes type any number if no type 0:
");
scanf("%d", &choice);
printf("\n");
temp = start;
temp = temp->next;
do{
printf("\nMenuDriven\n");
switch (choice2){
case 1:
printf("\n");
do{
scanf("%d", &choice4);
switch (choice4){
case 1:
add_node_end();
break;
case 2:
add_node_begin();
break;
case 3:
add_node_speposc(start, tail);
break;
case 4:
break;
default:
}
}
break;
case 2:
// traverse of node
traverse(start, tail);
break;
case 3:
// counting of node
count_node(start, tail);
break;
case 4:
// deletion of node
printf("\n");
do{
scanf("%d", &choice3);
switch (choice3){
case 1:
delete_node_beg(start, tail);
break;
case 2:
delete_node_end(start, tail);
break;
case 3:
delete_node_spec(start, tail);
break;
case 4:
break;
default:
break;
case 5:
// reverse of node
reverse(start, tail);
break;
case 6:
// sorting of node
sorting(start, tail);
break;
case 7:
// searching of node
search(start, tail);
break;
case 8:
break;
default:
}
}
void add_node_end(){
scanf("%d", &newnode->data);
tail->next = newnode;
newnode->next = start;
newnode->prev = tail;
start->prev = newnode;
tail = newnode;
printf("\n");
temp = start;
while (temp->next != start){
temp = temp->next;
void add_node_begin(){
scanf("%d", &newnode->data);
newnode->next = start;
newnode->prev = tail;
start = newnode;
tail->next = newnode;
newnode -> next -> prev = newnode;
printf("\n");
temp = start;
temp = temp->next;
void add_node_speposc(){
int pos;
int i = 1;
scanf("%d", &pos);
temp = start;
temp = temp->next;
i++;
scanf("%d", &newnode->data);
newnode->next = temp->next;
newnode->prev = temp;
temp->next = newnode;
printf("\n");
temp = start;
temp = temp->next;
}
printf("Data in node present at address %d is %d: \n", temp, temp->data);
// traverse of nodes
void traverse(){
temp = start;
temp = temp->next;
// counting of node
int count_node(){
int count = 0;
temp = start;
temp = temp->next;
count++;
printf("\n");
// deletion of nodes
void delete_node_beg(){
temp = start;
start = temp->next;
tail->next = start;
free(temp);
temp = start;
temp = temp->next;
void delete_node_spec(){
int pos;
int i = 0;
scanf("%d", &pos);
temp = start;
while (i < pos - 1){
temp = temp->next;
i++;
free(temp);
printf("\n");
temp = start;
temp = temp->next;
void delete_node_end(){
temp = tail;
free(temp);
temp = start;
temp = temp->next;
void reverse(){
// int temp2;
// temp = start;
// tail = temp;
// temp = temp->next;
// count++;
// }
// temp = start;
// }
// printf("\n");
// temp = start;
// temp = temp->next;
// }
void sorting(){
int temp3;
int count = 0;
int i, j;
temp = start;
count++;
temp = temp->next;
printf("\n");
for (i = 0; i <= count; i++){
temp = start;
temp2 = temp->next;
temp3 = temp->data;
temp->data = temp2->data;
temp2->data = temp3;
temp = temp2;
temp2 = temp2->next;
temp = start;
temp = temp->next;
// search of node
void search(){
int count = 0;
int ser;
int flag = 1;
scanf("%d", &ser);
printf("\n");
temp = start;
count++;
if (temp->data == ser){
flag--;
break;
temp = temp->next;
while (flag){
if (temp->data == ser){
else{
Output:-
Addition at end:-
Addition at Beginning:-
Addition at Specific Position:-
Traverse:-
Node Count:-
Deletion at Beginning:-
Deletion at End:-
Deletion at Specific Position:-
Reverse:-
Sorting:-
Searching:-
14. Write a program to perform following operations on Stack using Arrays.
#include<stdio.h>
#include<stdlib.h>
#define N 5
int stack[N];
int top=-1;
void push(){
int x;
scanf("%d",&x);
if(top==N-1){
printf("stack is overflow\n");
else{
top++;
stack[top]=x;
void pop()
{
//int item;
if(top==-1)
printf("Underflow\n");
else{
int item=stack[top];
top--;
printf("%d",item);
void peek()
if(top==-1)
printf("stack is empty\n");
else{
printf("%d",stack[top]);
}
void traverse(){
if(top==-1){
printf("Stack is empty\n");
else{
printf("stack empty");
printf("%d",stack[i]);
printf("\n");
int main(){
int choice;
while(1){
printf("1. push\n");
printf("2 pop\n");
printf("3. peek\n");
printf("4. traverse\n");
printf("5. exit\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice){
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
traverse();
break;
case 5:
exit(0);
default:
}
}
return 0;
}
15. Write a program to implement stack using linked list and perform
following operations on it.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
};
if(new_node == NULL){
return;
top = new_node;
void pop(){
struct node *temp;
//temp=top;
if(top==NULL){
printf("underflow");
return;
else{
temp = top;
printf("%d\n",top->data);
top=top->link;
free(temp);
void peek(){
if(top==NULL)
printf("stack is empty\n");
else{
}
}
void display()
//temp = top;
if(top == NULL){
printf("Stack is empty\n");
else {
while(temp!=NULL){
printf("%d\n", temp->data);
temp = temp->link;
printf("\n");
int main(){
push(10);
push(20);
push(30);
push(40);
push(50);
//display();
pop();
pop();
peek();
display();
return 0;
}
16. Write a program to convert an expression from infix to postfix using stack
#include<stdi.h>
char stack[MAX_SIZE];
void push(char x) {
stack[++top] = x;
} else {
printf("Stack Overflow\n");
char pop() {
if (top == -1) {
} else {
return stack[top--];
int priority(char x) {
if (x == '(') {
return 0;
return 1;
return 2;
int main() {
char exp[MAX_SIZE];
char *e, x;
scanf("%s", exp);
e = exp;
if (isalnum(*e))
printf("%c", *e);
push(*e);
printf("%c", x);
}
else
printf("%c", pop());
push(*e);
e++;
printf("%c", pop());
printf("\n");
return 0;
}
17. Write a program to evaluate a postfix expression using stack.
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
void push(int);
int pop();
void display();
void main() {
char postfix[20] = {'\0'}, element;
scanf("%s", postfix);
i = 0;
element = postfix[i];
if (isdigit(element)) {
push(element - '0');
} else {
num1 = pop();
num2 = pop();
switch (element) {
case '^':
break;
case '/':
case '*':
break;
case '+':
break;
case '-':
break;
push(ans);
i++;
if (top == 19)
printf("\n Stack is full");
else {
top++;
s[top] = element;
int pop() {
if (top == -1) {
} else {
int ch = s[top];
top--;
return ch;
void display() {
int i;
18. Write a program to implement linear queue using Array and perform the
following operation.
#include<stdio.h>
#define N 5
int queue[N];
void enqueue(int x) {
if(rear == N-1) {
printf("Overflow\n");
front = rear = 0;
queue[rear] = x;
} else {
rear++;
queue[rear] = x;
void dequeue() {
printf("Queue is underflow\n");
printf("%d\n", queue[front]);
} else {
printf("%d\n", queue[front]);
front++;
}
}
void peek() {
printf("Underflow\n");
} else {
printf("%d\n", queue[front]);
void display() {
int i;
printf("Underflow\n");
} else {
printf("\n");
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
display();
dequeue();
display();
peek();
dequeue();
dequeue();
return 0;
}
19. Write a program to implement linear queue to using linked list and
perform the following operations.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
};
newnode->data=x;
newnode->next=NULL;
front=rear=newnode;
else{
rear->next=newnode;
rear=newnode;
void dequeue(){
struct node*temp;
temp=front;
printf("queue is empty\n");
else{
printf("%d\n", front->data);
front=front->next;
free(temp);
void display()
else{
temp=front;
while(temp!=NULL)
printf("%d",temp->data);
temp=temp->next;
printf("\n");
void peek(){
printf("queue is empty\n");
else{
printf("%d\n",front->data);
int main()
{
enqueue(10);
enqueue(20);
enqueue(30);
display();
dequeue();
peek();
return 0;
}
20. Write a program to implement circular queue using array and perform the
following operation.
#include<stdio.h>
#include<stdlib.h>
#define N 5
int queue[N];
int front=-1;
int rear=-1;
void enqueue(int x)
if((rear+1)%N == front)
front=rear=0;
queue[rear]=x;
else{
rear=(rear+1)%N;
queue[rear]=x;
}
void dequeue()
printf("Queue is empty");
else if(front==rear)
front=rear=-1;
else{
printf("%d\n",queue[front]);
front==(front+1)%N;
void display()
int i = 0;
printf("Queue is empty");
else{
printf("Queue is:");
while(i!=rear)
printf("%d\t", queue[i]);
i=(i+1)%N;
printf("%d\n", queue[rear]);
void peek()
printf("Queue is underflow");
else{
printf("\t%d",queue[front]);
}
}
int main(){
enqueue(15);
enqueue(25);
enqueue(29);
enqueue(10);
printf("Queue is :");
display();
printf("Dequeue is:");
dequeue();
peek();
return 0;
}
23. Write a program to implement concept of merge Sort.
#include <stdio.h>
int n1 = mid - lb + 1;
int n2 = ub - mid;
int i = 0, j = 0, k = lb;
A[k] = L[i];
i++;
} else {
A[k] = R[j];
j++;
}
k++;
A[k] = L[i];
i++;
k++;
A[k] = R[j];
j++;
k++;
printf("\n");
int main() {
MergeSort(A, 0, n - 1);
printArray(A, n);
return 0;
}
24. Write a program to implement Quick Sort.
#include<stdio.h>
int pivot=a[lb];
int start=lb;
int end=ub;
while(start<end)
while(a[start]<=pivot)
start++;
while(a[end]>pivot)
end--;
if(start<end)
a[start] = a[end];
a[end] = temp;
a[lb] = a[end];
a[end] = temp;
return end;
if(lb<ub)
QuickSort(A,lb,loc-1);
QuickSort(A,loc+1,ub);
int main() {
printf("\n");
QuickSort(arr, 0, n - 1);
printf("\n");
return 0;
}
25. Write a program to implement Shell Sort.
#include<stdio.h>
i=j;
a[i]=a[i-gap];
i-=gap;
a[i]=temp;
int main(){
int a[]={15,5,24,8,1,3,16,10,20};
int n=sizeof(a)/sizeof(a[0]);
printf("Array before sorting\n");
printf("%d\t",a[i]);
printf("\n");
shellSort(a,n);
printf("%d\t",a[i]);
printf("\n");
return 0;