Lab Cycle
Lab Cycle
Lab Cycle:
8. Bubble Sort
9. Insertion Sort
18. Deque
22.
1) How to insert element into the Array
#include <stdio.h>
#include<conio.h>
void main()
clrscr();
scanf(“%d”,&n);
scanf(“%d”,&arr[i]);
scanf(“%d”,&pos);
scanf(“%d”,&ele);
if(pos > n)
printf(“Invalid Input”);
else
arr[i+1] = arr[i];
arr[pos-1] = ele;
getch();
Output:
1 2 3 10 4 5
#include <stdio.h>
#include<conio.h>
void main()
clrscr();
scanf(“%d”, &n);
scanf(“%d”, &arr[i]);
}
scanf(“%d”, &pos);
else
arr[i] = arr[i+1];
printf(“%d\n”, arr[i]);
getch();
Output:
1 2 3 5
3) How to update the element into the Array
#include<stdio.h>
#include<conio.h>
void main()
int a[10],i,pos,n,s;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&pos);
scanf("%d",&s);
for(i=0;i<n;i++)
if(i==pos)
a[i]=s;
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
getch();
Output
Enter the size of the array
#include<stdio.h>
#include<conio.h>
int stack[10],choice,n,top,x,i;
void push();
void pop();
void display();
void main()
clrscr();
top = -1;
scanf("%d",&n);
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
while(choice!=4);
void push()
if(top >= n - 1)
printf("STACK OVERFLOW\n");
else
scanf("%d",&x);
top++;
stack[top] = x;
void pop()
printf("STACK UNDERFLOW\n");
else
top--;
}
void display()
if(top >= 0)
printf("%d\t",stack[i]);
else
printf("EMPTY STACK\n");
Output:
5) Stack Using Linked List
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
int data;
}*top = NULL;
void push(int);
void pop();
void display();
void main()
clrscr();
while(1)
scanf("%d",&choice);
switch(choice)
case 1:
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nInvalid Choice\n");
newNode->data = value;
if(top == NULL)
newNode->next = NULL;
else
newNode->next = top;
top = newNode;
printf("Node is Inserted\n\n");
void pop()
if(top == NULL)
printf("\nEMPTY STACK\n");
else
{
printf("\n");
top = temp->next;
free(temp);
void display()
if(top == NULL)
printf("\nEMPTY STACK\n");
else
while(temp->next != NULL)
printf("%d--->",temp->data);
printf("%d--->NULL\n\n",temp->data);
}
Output:
6) Queue Using Array
#include <stdio.h>
#include<conio.h>
#define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
void main()
int choice;
clrscr();
while(1)
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit \n");
scanf("%d", &choice);
switch (choice)
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
void insert()
int item;
if (rear == MAX - 1)
else
if (front == - 1)
front = 0;
scanf("%d", &item);
rear = rear + 1;
queue_array[rear] = item;
void delete()
return ;
else
front = front + 1;
void display()
int i;
if (front == - 1)
else
printf("Queue is : \n");
printf("\n");
}
Output:
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
struct node
int data;
} *rear,*front,*temp,*newNode;
void create()
rear = newNode;
else
int deletequeue()
int data;
temp = front;
free(temp);
if(front == NULL)
rear = NULL;
return data;
int empty()
if(front == NULL)
return 1;
else
return 0;
void display()
{
if(empty())
printf("\nEMPTY QUEUE\n");
else
temp = front;
while(temp != NULL)
void main()
int num,choice;
clrscr();
while(1)
printf("\n\nQUEUE OPERATIONS\n\n1.INSERT\n2.DELETE\n3.DISPLAY\n\n");
scanf("%d",&choice);
switch (choice)
case 1:
scanf("%d",&num);
enqueue(num);
break;
case 2:
if(!(empty()))
printf("\nDeleted queue element : %d",deletequeue());
break;
case 3:
display();
break;
default:
exit(0);
getch();
Output:
8) Bubble Sort
#include<stdio.h>
#include<conio.h>
void main()
int n, i, j, temp;
int arr[20];
clrscr();
scanf("%d", &n);
scanf("%d", &arr[i]);
temp = arr[j];
arr[j + 1] = temp;
Output:
10
66 33 11 88 99 77 55 22 00 44
00 11 22 33 44 55 66 77 88 99
9) Selection Sort
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
scanf("%d", &n);
scanf("%d", &arr[i]);
min = i;
min = j;
temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
Output:
10
66 33 11 88 99 77 55 22 00 44
00 11 22 33 44 55 66 77 88 99
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
scanf("%d", &n);
scanf("%d", &arr[i]);
key = arr[i];
j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
Output:
10
66 33 11 88 99 77 55 22 00 44
00 11 22 33 44 55 66 77 88 99
#include <stdio.h>
#include<conio.h>
int mid;
mid = (start + end) / 2;
int i, j, k;
i = 0;
j = 0;
k = start;
arr[k] = arr1[i];
i++;
else
arr[k] = arr2[j];
j++;
}
k++;
arr[k] = arr1[i];
i++;
k++;
arr[k] = arr2[j];
j++;
k++;
void main()
int n, i, arr[30];
clrscr();
scanf("%d", &n);
scanf("%d", &arr[i]);
divide(arr, 0, n - 1);
display(arr, n);
Output:
10
66 33 11 88 99 77 55 22 00 44
00 11 22 33 44 55 66 77 88 99
#include<stdio.h>
#include<conio.h>
int temp;
i++;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
arr[i + 1] = arr[high];
arr[high] = temp;
return (i + 1);
quicksort(arr, pi + 1, high);
void main()
int n, i, arr[30];
clrscr();
scanf("%d", &n);
scanf("%d", &arr[i]);
quicksort(arr, 0, n - 1);
display(arr, n);
Output:
10
66 33 11 88 99 77 55 22 00 44
00 11 22 33 44 55 66 77 88 99
#include<stdio.h>
#include<conio.h>
int temp;
int largest = i;
largest = left;
largest = right;
if (largest != i)
temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
swaplargest(arr, n, largest);
swaplargest(arr, n, i);
temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
swaplargest(arr, i, 0);
void main()
int n, i, arr[30];
clrscr();
scanf("%d", &n);
printf(“enter the Heap sort elements:\n”);
scanf("%d", &arr[i]);
heap(arr, n);
display(arr, n);
Output:
10
66 33 11 88 99 77 55 22 00 44
00 11 22 33 44 55 66 77 88 99
#include <stdio.h>
#include <conio.h>
void main()
{
int array[100], search, i, n;
clrscr();
{
scanf("%d", &array[i]);
{
printf("%d is present at location %d\n", search, (i+1));
break;
}
}
if (i == n)
{
getch();
}
Output:
4 9 3 8 2 0 6
3 is present at location 3
#include <stdio.h>
#include <conio.h>
void main()
scanf("%d",&n);
scanf("%d", &search);
low = 0;
high = n - 1;
mid = (low+high)/2;
if(array[mid] <search)
low = mid + 1;
else
if (array[mid] == key)
break;
else
high = mid - 1;
getch();
}
Output:
2 3 4 5 7 8 9
5 is found at location 4
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include <string.h>
int top;
int stack[20];
int pop();
int isEmpty();
void infix_to_postfix();
void main()
char temp;
clrscr();
top = -1;
scanf("%s", infix_string);
infix_to_postfix();
return 0;
void infix_to_postfix()
char next;
char symbol;
symbol = infix_string[count];
if(!check_space(symbol))
switch(symbol)
case '(':
push(symbol);
break;
case ')':
postfix_string[temp++] = next;
break;
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
postfix_string[temp++] = pop();
push(symbol);
break;
default:
postfix_string[temp++] = symbol;
while(!isEmpty())
postfix_string[temp++] = pop();
postfix_string[temp] = '\0';
switch(symbol)
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
case '%':
return 2;
case '^':
return 3;
default:
return 0;
return 1;
else
return 0;
printf("Stack Overflow\n");
exit(1);
top = top + 1;
stack[top] = symbol;
int isEmpty()
{
if(top == -1)
return 1;
else
return 0;
int pop()
if(isEmpty())
printf("Stack is Empty\n");
exit(1);
return(stack[top--]);
Output:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include <stdlib.h>
#define MAX 20
void push(int);
char pop();
void infix_to_prefix();
char stack[20],infix[20],prefix[20];
void main()
clrscr();
scanf("%s",infix);
infix_to_prefix();
return 0;
if(top == MAX-1)
printf("\nSTACK OVERFLOW\n");
else {
top++;
stack[top] = infix[pos];
}
char pop()
char ch;
if(top < 0)
printf("\nSTACK UNDERFLOW\n");
exit(0);
else
ch = stack[top];
stack[top] = '\0';
top--;
return(ch);
return 0;
void infix_to_prefix()
int i = 0,j = 0;
strrev(infix);
while(infix[i] != '\0')
prefix[j] = infix[i];
j++;
i++;
}
else
push(i);
i++;
else
if(infix[i] == '(')
prefix[j] = pop();
j++;
pop();
i++;
else
if(infix[i] == '[')
while(stack[top] != ']')
prefix[j] = pop();
j++;
pop();
i++;
}
else
if(infix[i] == '{')
prefix[j] = pop();
j++;
pop();
i++;
else
if(top == -1)
push(i);
i++;
else
prefix[j] = pop();
j++;
prefix[j] = pop();
j++;
if(top < 0)
{
break;
push(i);
i++;
else
push(i);
i++;
while(top != -1)
prefix[j] = pop();
j++;
strrev(prefix);
prefix[j] = '\0';
return(1);
}
if(alpha == '*' || alpha =='/')
return(2);
getch();
Output:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
int data;
};
void begin_delete();
void last_delete();
void display();
void main ()
clrscr();
while(choice != 6)
printf("\n\n*********Main Menu*********\n");
printf("\n===============================================\n");
scanf("\n%d",&choice);
switch(choice)
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
begin_delete();
break;
case 4:
last_delete();
break;
case 5:
display();
break;
case 6:
exit(0);
break;
default:
printf("Please enter valid choice..");
void beginsert()
int item;
if(ptr == NULL)
printf("\nOVERFLOW");
else
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted");
void lastinsert()
int item;
if(ptr == NULL)
{
printf("\nOVERFLOW");
else
printf("\nEnter value?\n");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
head = ptr;
printf("\nNode inserted");
else
temp = head;
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted");
void begin_delete()
if(head == NULL)
{
printf("\nList is empty\n");
else
ptr = head;
head = ptr->next;
free(ptr);
void last_delete()
if(head == NULL)
printf("\nlist is empty");
else
head = NULL;
free(head);
else
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr1->next = NULL;
free(ptr);
void display()
ptr = head;
if(ptr == NULL)
printf("Nothing to print");
else
while (ptr!=NULL)
printf("\n%d",ptr->data);
}
Output:
*********Main Menu*********
===============================================
1.Insert in beginning
2.Insert at last
5.Show
6.Exit
Enter value
Node inserted
*********Main Menu*********
===============================================
1.Insert in beginning
2.Insert at last
5.Show
6.Exit
Enter value
Node inserted
*********Main Menu*********
===============================================
1.Insert in beginning
2.Insert at last
5.Show
6.Exit
Node inserted
*********Main Menu*********
===============================================
1.Insert in beginning
2.Insert at last
5.Show
6.Exit
printing values . . . . .
2
1
*********Main Menu*********
===============================================
1.Insert in beginning
2.Insert at last
5.Show
6.Exit
Enter value
123
Node inserted
*********Main Menu*********
===============================================
1.Insert in beginning
2.Insert at last
5.Show
6.Exit
Enter value
1234
Node inserted
*********Main Menu*********
===============================================
1.Insert in beginning
2.Insert at last
5.Show
6.Exit
*********Main Menu*********
===============================================
1.Insert in beginning
2.Insert at last
5.Show
6.Exit
*********Main Menu*********
==============================================
1.Insert in beginning
2.Insert at last
5.Show
6.Exit
Enter the location of the node after which you want to perform deletion
Deleted node 2
*********Main Menu*********
===============================================
1.Insert in beginning
2.Insert at last
5.Show
6.Exit
Printing values . . . . .
*********Main Menu*********
===============================================
1.Insert in beginning
2.Insert at last
5.Show
6.Exit
*********Main Menu*********
===============================================
1.Insert in beginning
2.Insert at last
5.Show
6.Exit
9
19) Double Linked List
#include <stdio.h>
#include<conio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node
int data;
int key;
};
bool isEmpty()
int length()
int length = 0;
length++;
return length;
}
void displayForward()
printf("\n[ ");
while(ptr != NULL)
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
printf(" ]");
void displayBackward()
printf("\n[ ");
while(ptr != NULL)
printf("(%d,%d) ",ptr->key,ptr->data);
link->key = key;
link->data = data;
if(isEmpty())
last = link;
}
else
head->prev = link;
link->next = head;
head = link;
link->key = key;
link->data = data;
if(isEmpty())
last = link;
else
last->next = link;
link->prev = last;
last = link;
if(head->next == NULL)
last = NULL;
}
else
head->next->prev = NULL;
head = head->next;
return tempLink;
if(head->next == NULL)
head = NULL;
else
last->prev->next = NULL;
last = last->prev;
return tempLink;
if(head == NULL)
return NULL;
}
while(current->key != key)
if(current->next == NULL)
return NULL;
else
previous = current;
current = current->next;
if(current == head)
head = head->next;
else
current->prev->next = current->next;
if(current == last)
last = current->prev;
else
current->next->prev = current->prev;
return current;
}
bool insertAfter(int key, int newKey, int data)
if(head == NULL)
return false;
while(current->key != key)
if(current->next == NULL)
return false;
else
current = current->next;
newLink->key = newKey;
newLink->data = data;
if(current == last)
newLink->next = NULL;
last = newLink;
else
newLink->next = current->next;
current->next->prev = newLink;
}
newLink->prev = current;
current->next = newLink;
return true;
void main()
clrscr();
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,40);
insertFirst(5,50);
insertFirst(6,60);
displayForward();
printf("\n");
displayBackward();
deleteFirst();
displayForward();
deleteLast();
displayForward();
insertAfter(4,7, 13);
displayForward();
delete(4);
displayForward();
getch();
Output:
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
struct node
int data;
};
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
if (node == NULL)
return;
Postorder(node->left);
Postorder(node->right);
if (node == NULL)
return;
Inorder(node->left);
printInorder(node->right);
if (node == NULL)
return;
Preorder(node->left);
Preorder(node->right);
}
void main()
clrscr();
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
Preorder(root);
Inorder(root);
Postorder(root);
getch();
Output:
#include<stdio.h>
#include<conio.h>
int a[20][20],reach[20],n;
void dfs(int v)
int i;
reach[v]=1;
for (i=1;i<=n;i++)
printf("\n %d->%d",v,i);
dfs(i);
void main()
int i,j,count=0;
clrscr();
scanf("%d",&n);
for (i=1;i<=n;i++)
reach[i]=0;
for (j=1;j<=n;j++)
a[i][j]=0;
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
dfs(1);
printf("\n");
for (i=1;i<=n;i++)
if(reach[i])
count++;
if(count==n)
else
getch();
Output:
22) Breadth First Search
#include<stdio.h>
#include<conio.h>
int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;
void bfs(int v)
for (i=1;i<=n;i++)
q[++r]=i;
if(f<=r)
visited[q[f]]=1;
bfs(q[f++]);
void main()
int v;
clrscr();
scanf("%d",&n);
for (i=1;i<=n;i++)
q[i]=0;
visited[i]=0;
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
scanf("%d",&v);
bfs(v);
for (i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i);
else
getch();
Output:
DEQUE
#include <stdio.h>
#define size 5
int deque[size];
int front = -1, rear = -1;
void insert_front(int x)
{
if((front==0 && rear==size-1) || (front==rear+1))
{
printf("Overflow");
}
else if((front==-1) && (rear==-1))
{
front=r=0;
deque[front]=x;
}
else if(front==0)
{
front=size-1;
deque[front]=x;
}
else
{
front=front-1;
deque[front]=x;
}
}
void insert_rear(int x)
{
if((front==0 && rear==size-1) || (front==rear+1))
{
printf("Overflow");
}
else if((front==-1) && (rear==-1))
{
rear=0;
deque[rear]=x;
}
else if(rear==size-1)
{
rear=0;
deque[rear]=x;
}
else
{
rear++;
deque[rear]=x;
}
void display()
{
int i=front;
printf("\nElements in a deque are: ");
while(i!=rear)
{
printf("%d ",deque[i]);
i=(i+1)%size;
}
printf("%d",deque[rear]);
}
void getfront()
{
if((front==-1) && (rear==-1))
{
printf("Deque is empty");
}
else
{
printf("\nThe value of the element at front is: %d", deque[front]);
}
}
void getrear()
{
if((front==-1) && (rear==-1))
{
printf("Deque is empty");
}
else
{
printf("\nThe value of the element at rear is %d", deque[rear]);
}
void delete_front()
{
if((front==-1) && (rear==-1))
{
printf("Deque is empty");
}
else if(front==rear)
{
printf("\nThe deleted element is %d", deque[front]);
front=-1;
rear=-1;
}
else if(front==(size-1))
{
printf("\nThe deleted element is %d", deque[front]);
f=0;
}
else
{
printf("\nThe deleted element is %d", deque[front]);
front=front+1;
}
}
void delete_rear()
{
if((front==-1) && (rear==-1))
{
printf("Deque is empty");
}
else if(front==rear)
{
printf("\nThe deleted element is %d", deque[rear]);
front=-1;
rear=-1;
}
else if(rear==0)
{
printf("\nThe deleted element is %d", deque[rear]);
rear=size-1;
}
else
{
printf("\nThe deleted element is %d", deque[rear]);
rear=rear-1;
}
}
void main()
{
insert_front(20);
insert_front(10);
insert_rear(30);
insert_rear(50);
insert_rear(80);
display();
getfront();
getrear();
delete_front();
delete_rear();
display();
getch();
}
How to insert a node in BST
#include <stdio.h>
#include <stdlib.h>
struct node {
int key;
};
temp->key = item;
return temp;
if (root != NULL) {
inorder(root->left);
inorder(root->right);
if (node == NULL)
return newNode(key);
if (key < node->key)
return node;
void main()
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
inorder(root);
getch();
Output:
How to delete a Node in BST
#include <stdio.h>
#include <stdlib.h>
struct node {
int key;
};
temp->key = item;
return temp;
if (root != NULL) {
inorder(root->left);
inorder(root->right);
}
struct node* insert(struct node* node, int key)
if (node == NULL)
return newNode(key);
else
return node;
current = current->left;
return current;
if (root == NULL)
return root;
else {
if (root->left == NULL)
free(root);
return temp;
free(root);
return temp;
root->key = temp->key;
return root;
void main()
inorder(root);
printf("\nDelete 20\n");
inorder(root);
printf("\nDelete 30\n");
inorder(root);
printf("\nDelete 50\n");
inorder(root);
getch();
Output:
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
struct node
int data;
};
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
if (node == NULL)
return;
Postorder(node->left);
Postorder(node->right);
if (node == NULL)
return;
Inorder(node->left);
printInorder(node->right);
}
if (node == NULL)
return;
Preorder(node->left);
Preorder(node->right);
void main()
clrscr();
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
Preorder(root);
Inorder(root);
Postorder(root);
getch();
Output: