Data Structures
Data Structures
[Link]. PROGRAMS
ARRAYS
1. Write a program to traverse an array.
2. Write a program to insert an element in an array.
3. Write a program to delete an element from an array.
4. Write a program to search an element in an array.
SINGLE LINKED LIST
5. Write a program to traverse a singly Linked List.
6. Write a program to insert a node at the beginning.
7. Write a program to insert a node at the end.
8. Write a program to insert a node in between the list.
9. Write a program to delete a node at the beginning.
10. Write a program to delete a node at the end.
11. Write a program to delete a node after the specified node.
DOUBLY LINKED LIST
12. Write a program to insert an element at the beginning.
13. Write a program to insert an element at the end.
14. Write a program to insert a node after the specified node.
15. Write a program to delete a node at the beginning.
16. Write a program to delete a node at the end.
17. Write a program to delete a node after the specified node.
CIRCULAR LINKED LIST
18. Write a program to insert an element at the beginning.
19. Write a program to insert an element at the end.
20. Write a program to insert a node after the specified node.
21. Write a program to delete a node at the beginning.
22. Write a program to delete a node at the end.
23. Write a program to delete a node after the specified node.
CIRCULAR DOUBLY LINKED LIST
24. Write a program to insert an element at the beginning.
25. Write a program to insert an element at the end.
26. Write a program to insert a node after the specified node.
27. Write a program to delete a node at the beginning.
28. Write a program to delete a node at the end.
29. Write a program to delete a node after the specified node.
STACKS
30. Write a program to perform Push and Pop operation in Stack.
QUEUES
31. Write a program to Insert and Delete an element in a Queue.
SEARCHING
32. Write a program to implement Linear Search.
33. Write a program to implement Binary Search.
BINARY SEARCH TREE(BST)
34. Write a program to search a node in Binary Search Tree.
35. Write a program of Inserting a node in Binary Search Tree.
36. Write a program for Deleting a leaf node, node having one or two
children in Binary Search Tree.
SORTING
37. Write a program to implement Bubble Sort.
38. Write a program to implement Heap Sort.
39. Write a program to implement Insertion sort.
40. Write a program to implement Merge Sort.
41. Write a program to implement Quick Sort.
42. Write a program to implement Selection Sort.
ARRAYS
✓ Write a program to traverse an array.
CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
int A[25],K=0,UB;
printf(“Enter number of elements in array : “);
scanf(“%d”,&UB);
printf(“Enter the elements in array: \n”);
for(K=0;K<UB;K++)
{
scanf(“%d”,&A[K]);
}
printf(“The Traverse of array is:\n”);
for(K=0;K<UB;K++)
{
printf(“%d\n”,A[K]);
}
getch();
return 0;
}
OUTPUT:
✓ Write a program to insert an element in an array.
CODE:
#include <stdio.h>
int main()
{
int array[25], position, c, n, value;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the location where you wish to insert an element\n");
scanf("%d", &position);
printf("Enter the value to insert\n");
scanf("%d", &value);
for (c = n - 1; c >= position - 1; c--)
array[c+1] = array[c];
array[position-1] = value;
printf("Resultant array is\n");
for (c = 0; c <= n; c++)
printf("%d\n", array[c]);
return 0;
}
OUTPUT:
✓ Write a program to delete an element from an array.
CODE:
#include <stdio.h>
int main()
{
int array[100], position, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the location where you wish to delete element\n");
scanf("%d", &position);
if (position >= n+1)
printf("Deletion not possible.\n");
else
{
for (c = position - 1; c < n - 1; c++)
array[c] = array[c+1];
printf("Resultant array:\n");
for (c = 0; c < n - 1; c++)
printf("%d\n", array[c]);
}
return 0;
}
OUTPUT:
✓ Write a program to search an element in an array.
CODE:
#include <stdio.h>
int main()
{
int a[25],i,n,key;
printf("Enter size of the array : ");
scanf("%d", &n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
printf("Enter the key : ");
scanf("%d", &key);
for(i=0; i<n; i++)
{
if(a[i]==key)
{
printf("element found ");
return 0;
}
}
printf("element not found");
}
OUTPUT:
SINGLE LINKED LIST
✓ Write a program to traverse a singly Linked List.
CODE:
#include<stdio.h>
#include<stdlib.h>
void createNode(int);
void traverse();
struct node
{
int data;
struct node *next;
};
struct node *head;
void main ()
{
int choice,item;
do
{
printf("\[Link] Node\[Link]\[Link]\[Link] your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the element to insert:\n");
scanf("%d",&item);
createNode(item);
break;
case 2:
traverse();
break;
case 3:
exit(0);
break;
default:
printf("\nPlease enter a valid choice.\n");
}
}
while(choice != 3);
}
void createNode(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted successfully!!\n");
}
}
void traverse()
{
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("List is Empty.");
}
else
{
printf("printing values . . . . .\n");
while (ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
}
}
OUTPUT:
✓ Write a program to insert a node at the beginning.
CODE:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *nextptr;
}
*stnode;
void createNodeList(int n);
void NodeInsertatBegin(int num);
void displayList();
int main()
{
int n,num;
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n);
printf("\n Data entered in the list are : \n");
displayList();
printf("\n Input data to insert at the beginning of the list : ");
scanf("%d", &num);
NodeInsertatBegin(num);
printf("\n Data after inserted in the list are : \n");
displayList();
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int num, i;
stnode = (struct node *)malloc(sizeof(struct node));
if(stnode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
printf(" Input data for node 1 : ");
scanf("%d", &num);
stnode-> num = num;
stnode-> nextptr = NULL;
tmp = stnode;
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &num);
fnNode->num = num;
fnNode->nextptr = NULL;
tmp->nextptr = fnNode; fnNode
tmp = tmp->nextptr;
}
}
}
}
void NodeInsertatBegin(int num)
{
struct node *fnNode;
fnNode = (struct node*)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
fnNode->num = num;
fnNode->nextptr = stnode;
stnode = fnNode;
}
}
void displayList()
{
struct node *tmp;
if(stnode == NULL)
{
printf(" No data found in the list.");
}
else
{
tmp = stnode;
while(tmp != NULL)
{
printf(" Data = %d\n", tmp->num);
tmp = tmp->nextptr;
}
}
}
OUTPUT:
✓ Write a program to insert a node at the end.
CODE:
#include <stdio.h>
struct node
{
int data;
struct node *next;
};
struct node *head, *tail = NULL;
void addAtEnd(int data) {
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
if(head == NULL) {
head = newNode;
tail = newNode;
}
else
{
tail->next = newNode;
tail = newNode;
}
}
void display() {
struct node *current = head;
if(head == NULL) {
printf("List is empty\n");
return;
}
printf("Adding nodes to the end of the list: \n");
while(current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main()
{
addAtEnd(1);
display();
addAtEnd(2);
display();
addAtEnd(3);
display();
addAtEnd(4);
display();
return 0;
}
OUTPUT:
✓ Write a program to insert a node in between the list.
CODE:
#include <stdio.h>
struct node
{
int data;
struct node *next;
};
int size;
struct node *head, *tail = NULL;
void addNode(int data) {
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
if(head == NULL) {
head = newNode;
tail = newNode;
}
else
{
tail->next = newNode;
tail = newNode;
}
size++;
}
void addInMid(int data){
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
if(head == NULL) {
head = newNode;
tail = newNode;
}
else
{
struct node *temp, *current;
int count = (size % 2 == 0) ? (size/2) : ((size+1)/2);
temp = head;
current = NULL;
for(int i = 0; i < count; i++) {
current = temp;
temp = temp->next;
}
current->next = newNode;
newNode->next = temp;
}
size++;
}
void display() {
struct node *current = head;
if(head == NULL) {
printf("List is empty\n");
return;
}
while(current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main()
{
addNode(1);
addNode(2);
printf("Original list: \n");
display();
addInMid(3);
printf( "Updated List: \n");
display();
addInMid(4);
printf("Updated List: \n");
display();
return 0;
}
OUTPUT:
✓ Write a program to delete a node at the beginning.
CODE:
#include <stdio.h>
struct node{
int data;
struct node *next;
};
struct node *head, *tail = NULL;
void addNode(int data)
{
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
if(head == NULL)
{
head = newNode;
tail = newNode;
}
else
{
tail->next = newNode;
tail = newNode;
}
}
void deleteFromStart() {
if(head == NULL) {
printf("List is empty \n");
return;
}
else
{
if(head != tail)
{
head = head->next;
}
else
{
head = tail = NULL;
}
}
}
void display()
{
struct node *current = head;
if(head == NULL)
{
printf("List is empty\n");
return;
}
while(current != NULL)
{
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main()
{
addNode(1);
addNode(2);
addNode(3);
addNode(4);
printf("Original List: \n");
display();
while(head != NULL) {
deleteFromStart();
printf("Updated List: \n");
display();
}
return 0;
}
OUTPUT:
✓ Write a program to delete a node at the end.
CODE:
#include <stdio.h>
struct node
{
int data;
struct node *next;
};
struct node *head, *tail = NULL;
void addNode(int data)
{
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
if(head == NULL)
{
head = newNode;
tail = newNode;
}
else
{
tail->next = newNode;
tail = newNode;
}
}
void deleteFromEnd() {
if(head == NULL) {
printf("List is empty \n");
return;
}
else
{
if(head != tail )
{
struct node *current = head;
while(current->next != tail) {
current = current->next;
}
tail = current;
tail->next = NULL;
}
else
{
head = tail = NULL;
}
}
}
void display() {
struct node *current = head;
if(head == NULL) {
printf("List is empty\n");
return;
}
while(current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main()
{
addNode(1);
addNode(2);
addNode(3);
addNode(4);
printf("Original List: \n");
display();
while(head != NULL) {
deleteFromEnd();
printf("Updated List: \n");
display();
}
return 0;
}
OUTPUT:
✓ Write a program to delete a node after the specified node.
CODE:
#include<stdio.h>
#include<stdlib.h>
void create(int);
void delete_specified();
struct node
{
int data;
struct node *next;
};
struct node *head;
void main ()
{
int choice,item;
do
{
printf("\[Link] List\[Link] node\[Link]\[Link] your choice?");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the item\n");
scanf("%d",&item);
create(item);
break;
case 2:
delete_specified();
break;
case 3:
exit(0);
break;
default:
printf("\nPlease enter valid choice\n");
}
}
while(choice != 3);
}
void create(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted\n");
}
}
void delete_specified()
{
struct node *ptr, *ptr1;
int loc,i;
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1 = ptr;
ptr = ptr->next;
if(ptr == NULL)
{
printf("\nThere are less than %d elements in the list..\n",loc);
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted %d node ",loc);
}
OUTPUT:
DOUBLY LINKED
LIST
✓ Write a program to insert an element at the beginning.
CODE:
#include <stdio.h>
struct node
{
int data;
struct node *previous;
struct node *next;
};
struct node *head, *tail = NULL;
void addAtStart(int data)
{
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
if(head == NULL) {
head = tail = newNode;
head->previous = NULL;
tail->next = NULL;
}
else
{
head->previous = newNode;
newNode->next = head;
newNode->previous = NULL;
head = newNode;
}
}
void display()
{
struct node *current = head;
if(head == NULL)
{
printf("List is empty\n");
return;
}
printf("Adding a node to the start of the list: \n");
while(current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main()
{
addAtStart(1);
display();
addAtStart(2);
display();
addAtStart(3);
display();
addAtStart(4);
display();
addAtStart(5);
display();
return 0;
}
OUTPUT:
✓ Write a program to insert an element at the end.
CODE:
#include <stdio.h>
struct node
{
int data;
struct node *previous;
struct node *next;
};
struct node *head, *tail = NULL;
void addAtEnd(int data)
{
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
if(head == NULL)
{
head = tail = newNode;
head->previous = NULL;
tail->next = NULL;
}
else
{
tail->next = newNode;
newNode->previous = tail;
tail = newNode;
tail->next = NULL;
}
}
void display()
{
struct node *current = head;
if(head == NULL)
{
printf("List is empty\n");
return;
}
printf("Adding a node to the end of the list: \n");
while(current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main()
{
addAtEnd(1);
display();
addAtEnd(2);
display();
addAtEnd(3);
display();
addAtEnd(4);
display();
addAtEnd(5);
display();
return 0;
}
OUTPUT:
✓ Write a program to insert a node after the specified node.
CODE:
#include<stdio.h>
#include<stdlib.h>
void insertAtSpecified(int);
void create(int);
struct node
{
int data;
struct node *next;
struct node *prev;
};
struct node *head;
void main ()
{
int choice,item,loc;
do
{
printf("\nEnter the element to insert:\n");
scanf("%d",&item);
if(head == NULL)
{
create(item);
}
else
{
insertAtSpecified(item);
}
printf("\nPress 1 to insert more elements.\n");
scanf("%d",&choice);
}
while(choice == 1);
}
void create(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
if(head==NULL)
{
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
}
else
{
ptr->data=item;printf("\nPress 1 to insert more elements.\n");
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
printf("\nNode Inserted Successfully!!\n");
}
}
void insertAtSpecified(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
struct node *temp;
int i, loc;
if(ptr == NULL)
{
printf("\n OVERFLOW");
}
else
{
printf("\nEnter the location:\n");
scanf("%d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\nNode can't be inserted.\n");
return;
}
}
ptr->data = item;
ptr->next = temp->next;
ptr -> prev = temp;
temp->next = ptr;
temp->next->prev=ptr;
printf("Node Inserted Successfully!!\n");
}
}
OUTPUT:
✓ Write a program to delete a node at the beginning.
CODE:
#include <stdio.h>
struct node
{
int data;
struct node *previous;
struct node *next;
};
struct node *head, *tail = NULL;
void addNode(int data) {
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
if(head == NULL)
{
head = tail = newNode;
head->previous = NULL;
tail->next = NULL;
}
else
{
tail->next = newNode;
newNode->previous = tail;
tail = newNode;
tail->next = NULL;
}
}
void deleteFromStart()
{
if(head == NULL)
{
return;
}
else
{
if(head != tail)
{
head = head->next;
head->previous = NULL;
}
else
{
head = tail = NULL;
}
}
}
void display()
{
struct node *current = head;
if(head == NULL)
{
printf("List is empty\n");
return;
}
while(current != NULL)
{
printf("%d ",current->data);
current = current->next;
}
printf("\n");
}
int main()
{
addNode(1);
addNode(2);
addNode(3);
addNode(4);
addNode(5);
printf("Original List: \n");
display();
while(head != NULL)
{
deleteFromStart();
printf("Updated List: \n");
display();
}
return 0;
}
OUPUT:
✓ Write a program to delete a node at the end.
CODE:
#include <stdio.h>
struct node
{
int data;
struct node *previous;
struct node *next;
};
struct node *head, *tail = NULL;
void addNode(int data)
{
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
if(head == NULL)
{
head = tail = newNode;
head->previous = NULL;
tail->next = NULL;
}
else
{
tail->next = newNode;
newNode->previous = tail;
tail = newNode;
tail->next = NULL;
}
}
void deleteFromEnd()
{
if(head == NULL)
{
return;
}
else
{
if(head != tail)
{
tail = tail->previous;
tail->next = NULL;
}
else
{
head = tail = NULL;
}
}
}
void display()
{
struct node *current = head;
if(head == NULL)
{
printf("List is empty\n");
return;
}
while(current != NULL)
{
printf("%d ",current->data);
current = current->next;
}
printf("\n");
}
int main()
{
addNode(1);
addNode(2);
addNode(3);
addNode(4);
addNode(5);
printf("Original List: \n");
display();
while(head != NULL)
{
deleteFromEnd();
printf("Updated List: \n");
display();
}
return 0;
}
OUTPUT:
✓ Write a program to delete a node after the specified node.
CODE:
#include<stdio.h>
#include<stdlib.h>
void create(int);
void delete_specified();
struct node
{
int data;
struct node *next;
struct node *prev;
};
struct node *head;
void main ()
{
int choice,item;
do
{
printf("[Link] List\[Link] node\[Link]\[Link] your choice?");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the item\n");
scanf("%d",&item);
create(item);
break;
case 2:
delete_specified();
break;
case 3:
exit(0);
break;
default:
printf("\nPlease enter valid choice\n");
}
}
while(choice != 3);
}
void create(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
if(head==NULL)
{
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
}
else
{
ptr->data=item;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
printf("\nNode Inserted\n");
}
}
void delete_specified( )
{
struct node *ptr, *temp;
int val;
printf("Enter the value");
scanf("%d",&val);
temp = head;
while(temp -> data != val)
temp = temp -> next;
if(temp -> next == NULL)
{
printf("\nCan't delete\n");
}
else if(temp -> next -> next == NULL)
{
temp ->next = NULL;
printf("\nNode Deleted\n");
}
else
{
ptr = temp -> next;
temp -> next = ptr -> next;
ptr -> next -> prev = temp;
free(ptr);
printf("\nNode Deleted\n");
}
}
OUTPUT:
CIRCULAR LINKED
LIST
✓ Write a program to insert an element at the beginning.
CODE:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head = NULL;
struct node *tail = NULL;
void addAtStart(int data){
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
if(head == NULL)
{
head = newNode;
tail = newNode;
newNode->next = head;
}
else
{
struct node *temp = head;
newNode->next = temp;
head = newNode;
tail->next = head;
}
}
void display()
{
struct node *current = head;
if(head == NULL)
{
printf("List is empty");
}
else
{
printf("Adding nodes to the start of the list: \n");
do
{
printf("%d ", current->data);
current = current->next;
}
while(current != head);
printf("\n");
}
}
int main()
{
addAtStart(1);
display();
addAtStart(2);
display();
addAtStart(3);
display();
addAtStart(4);
display();
return 0;
}
OUTPUT:
✓ Write a program to insert an element at the end.
CODE:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head = NULL;
struct node *tail = NULL;
void addAtEnd(int data)
{
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
if(head == NULL)
{
head = newNode;
tail = newNode;
newNode->next = head;
}
else
{
tail->next = newNode;
tail = newNode;
tail->next = head;
}
}
void display()
{
struct node *current = head;
if(head == NULL)
{
printf("List is empty");
}
else
{
printf("Adding nodes to the end of the list: \n");
do
{
printf("%d ", current->data);
current = current->next;
}
while(current != head);
printf("\n");
}
}
int main()
{
addAtEnd(1);
display();
addAtEnd(2);
display();
addAtEnd(3);
display();
addAtEnd(4);
display();
return 0;
}
OUTPUT:
✓ Write a program to insert a node after the specified node.
CODE:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head = NULL;
struct node *tail = NULL;
int size = 0;
void add(int data)
{
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
if(head == NULL)
{
head = newNode;
tail = newNode;
newNode->next = head;
}
else
{
tail->next = newNode;
tail = newNode;
tail->next = head;
}
size++;
}
void addInMid(int data)
{
int i;
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
if(head == NULL)
{
head = newNode;
tail = newNode;
newNode->next = head;
}
else
{
struct node *temp, *current = NULL;
int count = (size % 2 == 0) ? (size/2) : ((size+1)/2);
temp = head;
for(i = 0; i < count; i++)
{
current = temp;
temp = temp->next;
}
current->next = newNode;
newNode->next = temp;
}
size++;
}
void display()
{
struct node *current = head;
if(head == NULL)
{
printf("List is empty");
}
else
{
do
{
printf("%d ", current->data);
current = current->next;
}
while(current != head);
printf("\n");
}
}
int main()
{
add(1);
add(2);
add(3);
add(4);
printf("Original list:\n ");
display();
addInMid(5);
printf("Updated List:\n ");
display();
addInMid(6);
printf("Updated List:\n ");
display();
return 0;
}
OUTPUT:
✓ Write a program to delete a node at the beginning.
CODE:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node * nextptr;
}
*stnode;
struct node *tail,*p,*q,*store;
void ClListcreation(int n);
void ClListDeleteFirstNode();
void displayClList(int a);
int main()
{
int n,num1,a,insPlc;
stnode = NULL;
printf(" Input the number of nodes : ");
scanf("%d", &n);
ClListcreation(n);
a=1;
displayClList(a);
ClListDeleteFirstNode();
a=2;
displayClList(a);
return 0;
}
void ClListcreation(int n)
{
int i, num;
struct node *preptr, *newnode;
if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));
printf(" Input data for node 1 : ");
scanf("%d", &num);
stnode->num = num;
stnode->nextptr = NULL;
preptr = stnode;
for(i=2; i<=n; i++)
{
newnode = (struct node *)malloc(sizeof(struct node));
printf(" Input data for node %d : ", i);
scanf("%d", &num);
newnode->num = num;
newnode->nextptr = NULL;
preptr->nextptr = newnode;
preptr = newnode;
}
preptr->nextptr = stnode;
}
}
void ClListDeleteFirstNode()
{
p=stnode;
while(p->nextptr!=stnode)
{
p=p->nextptr;
}
store=stnode;
stnode=stnode->nextptr;
printf("\n The deleted node is -> %d",store->num);
p->nextptr=stnode;
free (store);
}
void displayClList(int m)
{
struct node *tmp;
int n = 1;
if(stnode == NULL)
{
printf(" No data found in the List yet.");
}
else
{
tmp = stnode;
if (m==1)
{
printf("\n Data entered in the list are :\n");
}
else
{
printf("\n After deletion the new list are :\n");
}
Do
{
printf(" Data %d = %d\n", n, tmp->num);
tmp = tmp->nextptr;
n++;
}
while(tmp != stnode);
}
}
OUTPUT:
✓ Write a program to delete a node at the end.
CODE:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head = NULL;
struct node *tail = NULL;
void add(int data)
{
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
if(head == NULL)
{
head = newNode;
tail = newNode;
newNode->next = head;
}
else
{
tail->next = newNode;
tail = newNode;
tail->next = head;
}
}
void deleteEnd()
{
if(head == NULL)
{
return;
}
else
{
if(head != tail )
{
struct node *current = head;
while(current->next != tail)
{
current = current->next;
}
tail = current;
tail->next = head;
}
else
{
head = tail = NULL;
}
}
}
void display()
{
struct node *current = head;
if(head == NULL)
{
printf("List is empty");
}
else
{
do
{
printf("%d ", current->data);
current = current->next;
}
while(current != head);
printf("\n");
}
}
int main()
{
add(1);
add(2);
add(3);
add(4);
printf("Original List:\n ");
display();
while(head != NULL)
{
deleteEnd();
printf("Updated List:\n ");
display();
}
return 0;
}
OUTPUT:
✓ Write a program to delete a node after the specified node.
CODE:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *head = NULL, *x, *y, *z;
void display();
void del_at_pos();
void main()
{
int c;
x = (struct node*)malloc(sizeof(struct node));
printf("\n Enter the data to create linked list:");
scanf("%d", &x->data);
x->link = x;
head = x;
printf("\n If you wish to continue press 1 otherwise 0:");
scanf("%d", &c);
while (c != 0)
{
y = (struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d", &y->data);
x->link = y;
y->link = head;
x = y;
printf("\n If you wish to continue press 1 otherwise 0:");
scanf("%d", &c);
}
del_at_pos();
display();
}
void del_at_pos()
{
if (head == NULL)
printf("\n List is empty");
else
{
int c = 1, pos;
printf("\n Enter the position to be deleted:");
scanf("%d", &pos);
x = head;
while (c < pos)
{
y = x;
x = x->link;
c++;
}
y->link = x->link;
free(x);
}
}
void display()
{
if (head == NULL)
printf("\n List is empty");
else
{
x = head;
while (x->link != head)
{
printf("%d->", x->data);
x = x->link;
}
printf("%d", x->data);
}
}
OUTPUT:
CIRCULAR DOUBLY
LINKED LIST
✓ Write a program to insert an element at the beginning.
CODE:
#include<stdio.h>
#include<stdlib.h>
void beg_insert(int);
struct node
{
int data;
struct node *next;
struct node *prev;
};
struct node *head;
void main ()
{
int choice,item;
do
{
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
beg_insert(item);
printf("\nPress 0 to insert more ?\n");
scanf("%d",&choice);
}
while(choice == 0);
}
void beg_insert(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
struct node *temp;
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
ptr->data=item;
if(head==NULL)
{
head = ptr;
ptr -> next = head;
ptr -> prev = head;
}
else
{
temp = head;
while(temp -> next != head)
{
temp = temp -> next;
}
temp -> next = ptr;
ptr -> prev = temp;
head -> prev = ptr;
ptr -> next = head;
head = ptr;
}
printf("Node Inserted");
}
}
OUTPUT:
✓ Write a program to insert an element at the end.
CODE:
#include<stdio.h>
#include<stdlib.h>
void insertion_last(int);
struct node
{
int data;
struct node *next;
struct node *prev;
};
struct node *head;
void main ()
{
int choice,item;
do
{
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
insertion_last(item);
printf("\nPress 0 to insert more ?\n");
scanf("%d",&choice);
}
while(choice == 0);
}
void insertion_last(int item)
{
struct node *ptr = (struct node *) malloc(sizeof(struct node));
struct node *temp;
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
ptr->data=item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
ptr -> prev = head;
}
else
{
temp = head;
while(temp->next !=head)
{
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
head -> prev = ptr;
ptr -> next = head;
}
}
printf("\nNode Inserted\n");
}
OUTPUT:
✓ Write a program to insert a node after the specified node.
CODE:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node * preptr;
struct node * nextptr;
}
*stnode, *ennode;
void DlListcreation(int n);
void DlLinsertNodeAtBeginning(int num);
void DlLinsertNodeAtEnd(int num);
void DlLinsertNodeAtAny(int num, int pos);
void displayDlList(int a);
int main()
{
int n,num1,a,insPlc;
stnode = NULL;
ennode = NULL;
printf(" Input the number of nodes : ");
scanf("%d", &n);
DlListcreation(n);
a=1;
displayDlList(a);
printf(" Input the position ( 1 to %d ) to insert a new node : ",n+1);
scanf("%d", &insPlc);
printf(" Input data for the position %d : ", insPlc);
scanf("%d", &num1);
DlLinsertNodeAtAny(num1,insPlc);
a=2;
displayDlList(a);
return 0;
}
void DlListcreation(int n)
{
int i, num;
struct node *fnNode;
if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));
if(stnode != NULL)
{
printf(" Input data for node 1 : ");
scanf("%d", &num);
stnode->num = num;
stnode->preptr = NULL;
stnode->nextptr = NULL;
ennode = stnode;
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode != NULL)
{
printf(" Input data for node %d : ", i);
scanf("%d", &num);
fnNode->num = num;
fnNode->preptr = ennode;
fnNode->nextptr = NULL;
ennode->nextptr = fnNode;
ennode = fnNode;
}
else
{
printf(" Memory can not be allocated.");
break;
}
}
}
else
{
printf(" Memory can not be allocated.");
}
}
}
void DlLinsertNodeAtAny(int num, int pos)
{
int i;
struct node * newnode, *tmp;
if(ennode == NULL)
{
printf(" No data found in the list!\n");
}
else
{
tmp = stnode;
i=1;
while(i<pos-1 && tmp!=NULL)
{
tmp = tmp->nextptr;
i++;
}
if(pos == 1)
{
DlLinsertNodeAtBeginning(num);
}
else if(tmp == ennode)
{
DlLinsertNodeAtEnd(num);
}
else if(tmp!=NULL)
{
newnode = (struct node *)malloc(sizeof(struct node));
newnode->num = num;
newnode->nextptr = tmp->nextptr;
newnode->preptr = tmp;
if(tmp->nextptr != NULL)
{
tmp->nextptr->preptr = newnode;
}
tmp->nextptr = newnode;
}
else
{
printf(" The position you entered, is invalid.\n");
}
}
}
void DlLinsertNodeAtBeginning(int num)
{
struct node * newnode;
if(stnode == NULL)
{
printf(" No data found in the list!\n");
}
else
{
newnode = (struct node *)malloc(sizeof(struct node));
newnode->num = num;
newnode->nextptr = stnode;
newnode->preptr = NULL;
stnode->preptr = newnode;
stnode = newnode
}
}
void DlLinsertNodeAtEnd(int num)
{
struct node * newnode;
if(ennode == NULL)
{
printf(" No data found in the list!\n");
}
else
{
newnode = (struct node *)malloc(sizeof(struct node));
newnode->num = num;
newnode->nextptr = NULL;
newnode->preptr = ennode;
ennode->nextptr = newnode;
ennode = newnode;
}
}
void displayDlList(int m)
{
struct node * tmp;
int n = 1;
if(stnode == NULL)
{
printf(" No data found in the List yet.");
}
else
{
tmp = stnode;
if (m==1)
{
printf("\n Data entered in the list are :\n");
}
else
{
printf("\n After insertion the new list are :\n");
}
while(tmp != NULL)
{
printf(" node %d : %d\n", n, tmp->num);
n++;
tmp = tmp->nextptr;
}
}
}
OUTPUT:
✓ Write a program to delete a node at the beginning.
CODE:
#include<stdio.h>
#include<stdlib.h>
void create(int);
void deletion_last();
struct node
{
int data;
struct node *next;
struct node *prev;
};
struct node *head;
void main ()
{
int choice,item;
do
{
printf("[Link] List\[Link] Node from last\[Link]\[Link] your choice?");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the item\n");
scanf("%d",&item);
create(item);
break;
case 2:
deletion_last();
break;
case 3:
exit(0);
break;
default:
printf("\nPlease Enter valid choice\n");
}
}
while(choice != 3);
}
void create(int item)
{
struct node *ptr = (struct node *) malloc(sizeof(struct node));
struct node *temp;
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
ptr->data=item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
ptr -> prev = head;
}
else
{
temp = head;
while(temp->next !=head)
{
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
head -> prev = ptr;
ptr -> next = head;
}
}
printf("\nNode Inserted\n");
}
void deletion_last()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW\n");
}
else if(head->next == head)
{
head = NULL;
free(head);
printf("\nNode Deleted\n");
}
else
{
ptr = head;
if(ptr->next != head)
{
ptr = ptr -> next;
}
ptr -> prev -> next = head;
head -> prev = ptr -> prev;
free(ptr);
printf("\nNode Deleted\n");
}
}
OUTPUT:
✓ Write a program to delete a node at the end.
CODE:
#include<stdio.h>
#include<stdlib.h>
void create(int);
void deletion_last();
struct node
{
int data;
struct node *next;
struct node *prev;
};
struct node *head;
void main ()
{
int choice,item;
do
{
printf("[Link] List\[Link] Node from last\[Link]\[Link] your choice?");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the item\n");
scanf("%d",&item);
create(item);
break;
case 2:
deletion_last();
break;
case 3:
exit(0);
break;
default:
printf("\nPlease Enter valid choice\n");
}
}
while(choice != 3);
}
void create(int item)
{
struct node *ptr = (struct node *) malloc(sizeof(struct node));
struct node *temp;
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
ptr->data=item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
ptr -> prev = head;
}
else
{
temp = head;
while(temp->next !=head)
{
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
head -> prev = ptr;
ptr -> next = head;
}
}
printf("\nNode Inserted\n");
}
void deletion_last()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW\n");
}
else if(head->next == head)
{
head = NULL;
free(head);
printf("\nNode Deleted\n");
}
else
{
ptr = head;
if(ptr->next != head)
{
ptr = ptr -> next;
}
ptr -> prev -> next = head;
head -> prev = ptr -> prev;
free(ptr);
printf("\nNode Deleted\n");
}
}
OUTPUT:
✓ Write a program to delete a node after the specified node.
CODE:
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
struct Node* prev;
};
void push_back(struct Node** head_ref, int newElement)
{
struct Node *newNode, *temp;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = newElement;
newNode->next = NULL;
newNode->prev = NULL;
if(*head_ref == NULL)
{
*head_ref = newNode;
newNode->next = *head_ref;
newNode->prev = *head_ref;
}
Else
{
temp = *head_ref;
while(temp->next != *head_ref)
{
temp = temp->next;
}
temp->next = newNode;
newNode->next = *head_ref;
newNode->prev = temp;
(*head_ref)->prev = newNode;
}
}
void pop_at(struct Node** head_ref, int position)
{
struct Node *nodeToDelete = *head_ref;
struct Node *temp = *head_ref;
int NoOfElements = 0;
if(temp != NULL)
{
NoOfElements++;
temp = temp->next;
}
while(temp != *head_ref)
{
NoOfElements++;
temp = temp->next;
}
if(position < 1 || position > NoOfElements)
{
printf("\nInavalid position.");
}
else if (position == 1)
{
if((*head_ref)->next == *head_ref)
{
*head_ref = NULL;
}
else
{
while(temp->next != *head_ref)
temp = temp->next;
*head_ref = (*head_ref)->next;
temp->next = *head_ref;
(*head_ref)->prev = temp;
free(nodeToDelete);
}
}
else
{
temp = *head_ref;
for(int i = 1; i < position-1; i++)
temp = temp->next;
nodeToDelete = temp->next;
temp->next = temp->next->next;
temp->next->prev = temp;
free(nodeToDelete);
}
}
void PrintList(struct Node* head_ref)
{
struct Node* temp = head_ref;
if(head_ref != NULL)
{
printf("The list contains: ");
while (1)
{
printf("%i ",temp->data);
temp = temp->next;
if(temp == head_ref)
break;
}
printf("\n");
}
else
{
printf("The list is empty.\n");
}
}
int main()
{
struct Node* MyList = NULL;
push_back(&MyList, 10);
push_back(&MyList, 20);
push_back(&MyList, 30);
PrintList(MyList);
pop_at(&MyList, 2);
PrintList(MyList);
pop_at(&MyList, 1);
PrintList(MyList);
return 0;
}
OUTPUT:
STACKS
✓ Write a program to perform Push and Pop operation in
Stack.
CODE:
#include<stdio.h>
#include<process.h>
#include<stdlib.h>
#define MAX 5
int top=-1,stack[MAX];
void push();
void pop();
void display();
void main()
{
int ch;
while(1)
{
printf("\n\[Link]\[Link]\[Link]\[Link]");
printf("\n\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong Choice!!");
}
}
}
void push()
{
int val;
if(top==MAX-1)
{
printf("\nStack is full!!");
}
else
{
printf("\nEnter element to push:");
scanf("%d",&val);
top=top+1;
stack[top]=val;
}
}
void pop()
{
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nDeleted element is %d",stack[top]);
top=top-1;
}
}
void display()
{
int i;
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nStack is...\n");
for(i=top;i>=0;--i)
printf("%d\n",stack[i]);
}
}
OUTPUT:
QUEUE
✓ Write a program to Insert and Delete an element in a
Queue.
CODE:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int q[25], n ,front=-1 , rear=-1 , item;
void insertion()
{
if((rear==n) && (front==(rear+1))||(front==rear+1))
{
printf(“\nQueue Overflow\n”);
}
else if (rear==0)
front = rear = 1;
else if(rear==n)
rear=1;
else
rear=rear+1;
printf(“Enter the item : “);
scanf(“%d”,&item);
q[rear] = item;
printf(“%d is inserted\n\n”,item);
}
void deletion()
{
if(front==0)
{
printf(“\nQueue Underflow\n\n”);
}
item=q[front];
if(front==rear)
{
front=0;
rear=0;
}
else if (front=n)
front=1;
else
front=front+1;
printf(“\n%d is deleted \n”,item);
}
void show()
{
for(int i=0;i<=rear;i++)
printf(“%d\t”,q[i]);
}
int main()
{
int op;
printf(“Enter the size of the queue : “);
scanf(“%d”,&n);
do
{
printf(“\n1 : Insert”);
printf(“\n2 : Delete”);
printf(“\n3 : Print”);
printf(“\n4 : Exit”);
printf(“\nEnter your choice : “);
scanf(“%d”,&op);
switch(op)
{
case 1:
insertion();
break;
case 2:
deletion();
break;
case 3:
show();
break;
default:
printf(“Invalid Option. Try again.”);
}
}
while(op!=4);
printf(“\n ENTER A VALID CHOICE!n”);
}
OUTPUT:
SEARCHING
✓ Write a program to implement Linear Search.
CODE:
#include <stdio.h>
int linearSearch(int a[], int n, int val)
{
for (int i = 0; i < n; i++)
{
if (a[i] == val)
return i+1;
}
return -1;
}
int main()
{
int a[] = {70, 40, 30, 11, 57, 41, 25, 14, 52};
int val = 41; // value to be searched
int n = sizeof(a) / sizeof(a[0]);
int res = linearSearch(a, n, val);
printf("The elements of the array are - ");
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\nElement to be searched is - %d", val);
if (res == -1)
printf("\nElement is not present in the array");
else
printf("\nElement is present at %d position of array", res);
return 0;
}
OUTPUT:
✓ Write a program to implement Binary Search.
CODE:
#include <stdio.h>
int binarySearch(int a[], int beg, int end, int val)
{
int mid;
if(end >= beg)
{
mid = (beg + end)/2;
if(a[mid] == val)
{
return mid+1;
}
else if(a[mid] < val)
{
return binarySearch(a, mid+1, end, val);
}
else
{
return binarySearch(a, beg, mid-1, val);
}
}
return -1;
}
int main()
{
int a[] = {11, 14, 25, 30, 40, 41, 52, 57, 70};
int val = 40; // value to be searched
int n = sizeof(a) / sizeof(a[0]);
int res = binarySearch(a, 0, n-1, val);
printf("The elements of the array are - ");
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\nElement to be searched is - %d", val);
if (res == -1)
printf("\nElement is not present in the array");
else
printf("\nElement is present at %d position of array", res);
return 0;
}
OUTPUT:
BINARY SEARCH
TREE
(BST)
✓ Write a program to search a node in Binary Search Tree.
CODE:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node*left;
struct node*right;
};
typedef struct node BST;
BST *LOC, *PAR;
void search(BST *root, int item)
{
BST *save,*ptr;
if (root == NULL)
{
LOC = NULL;
PAR=NULL;
}
if (item == root -> info)
{
LOC = root;
PAR = NULL;
return;
}
if (item < root->info)
{
save = root;
ptr = root->left;
}
else
{
save = root;
ptr = root -> right;
}
while( ptr != NULL)
{
if (ptr -> info == item)
{
LOC = ptr;
PAR = save;
return;
}
if(item < ptr->info)
{
save = ptr;
ptr = ptr->left;
}
else
{
save = ptr;
ptr = ptr->right;
}
}
LOC = NULL;
PAR = save;
return;
}
struct node* findmin(struct node*r)
{
if (r == NULL)
return NULL;
else if (r->left!=NULL)
return findmin(r->left);
else if (r->left == NULL)
return r;
}
struct node*insert(struct node*r, int x)
{
if (r == NULL)
{
r = (struct node*)malloc(sizeof(struct node));
r->info = x;
r->left = r->right = NULL;
return r;
}
else if (x < r->info)
r->left = insert(r->left, x);
else if (x > r->info)
r->right = insert(r->right, x);
return r;
}
struct node* del(struct node*r, int x)
{
struct node *t;
if(r == NULL)
printf("\nElement not found");
else if (x < r->info)
r->left = del(r->left, x);
else if (x > r->info)
r->right = del(r->right, x);
else if ((r->left != NULL) && (r->right != NULL))
{
t = findmin(r->right);
r->info = t->info;
r->right = del(r->right, r->info);
}
else
{
t = r;
if (r->left == NULL)
r = r->right;
else if (r->right == NULL)
r = r->left;
free(t);
}
return r;
}
int main()
{
struct node* root = NULL;
int x, c = 1, z;
int element;
char ch;
printf("\nEnter an element: ");
scanf("%d", &x);
root = insert(root, x);
printf("\nDo you want to enter another element :y or n");
scanf(" %c",&ch);
while (ch == 'y')
{
printf("\nEnter an element:");
scanf("%d", &x);
root = insert(root,x);
printf("\nPress y or n to insert another element: y or n: ");
scanf(" %c", &ch);
}
while(1)
{
printf("\n1 Insert an element ");
printf("\n2 Delete an element");
printf("\n3 Search for an element ");
printf("\n4 Exit ");
printf("\nEnter your choice: ");
scanf("%d", &c);
switch(c)
{
case 1:
printf("\nEnter the item:");
scanf("%d", &z);
root = insert(root,z);
break;
case 2:
printf("\nEnter the info to be deleted:");
scanf("%d", &z);
root = del(root, z);
break;
case 3:
printf("\nEnter element to be searched: ");
scanf("%d", &element);
search(root, element);
if(LOC != NULL)
printf("\n%d Found in Binary Search Tree !!\n",element);
else
printf("\nIt is not present in Binary Search Tree\n");
break;
case 4:
printf("\nExiting...");
return;
default:
printf("Enter a valid choice: ");
}
}
return 0;
}
OUTPUT:
✓ Write a program of Inserting a node in Binary Search
Tree.
CODE:
#include<stdio.h>
#include<stdlib.h>
void insert(int);
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *root;
void main ()
{
int choice,item;
do
{
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
insert(item);
printf("\nPress 0 to insert more ?\n");
scanf("%d",&choice);
}
while(choice == 0);
}
void insert(int item)
{
struct node *ptr, *parentptr , *nodeptr;
ptr = (struct node *) malloc(sizeof (struct node));
if(ptr == NULL)
{
printf("can't insert");
}
else
{
ptr -> data = item;
ptr -> left = NULL;
ptr -> right = NULL;
if(root == NULL)
{
root = ptr;
root -> left = NULL;
root -> right = NULL;
}
else
{
parentptr = NULL;
nodeptr = root;
while(nodeptr != NULL)
{
parentptr = nodeptr;
if(item < nodeptr->data)
{
nodeptr = nodeptr -> left;
}
else
{
nodeptr = nodeptr -> right;
}
}
if(item < parentptr -> data)
{
parentptr -> left = ptr;
}
else
{
parentptr -> right = ptr;
}
}
printf("Node Inserted");
}
}
OUTPUT:
✓ Write a program for Deleting a leaf node, node having one
or two children in Binary Search Tree.
CODE:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left, *right;
}
*root;
struct node *newNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = item;
temp->left = temp->right = NULL;
return temp;
}
struct node * minValueNode(struct node* node)
{
struct node* current = node;
while (current->left != NULL)
current = current->left;
return current;
}
struct node* delete_node(struct node* root, int data)
{
if (root == NULL)
return root;
if (data < root->data)
root->left = delete_node(root->left, data);
else if (data > root->data)
root->right = delete_node(root->right, data);
else
{
if (root->left == NULL)
{
struct node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
struct node *temp = root->left;
free(root);
return temp;
}
struct node* temp = minValueNode(root->right);
root->data = temp->data;
root->right = delete_node(root->right, temp->data);
}
return root;
}
void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
struct node* insert(struct node* node, int data)
{
if (node == NULL)
return newNode(data);
if (data < node->data)
node->left = insert(node->left, data);
else if (data > node->data)
node->right = insert(node->right, data);
return node;
}
int main()
{
int n;
root = NULL;
printf("\nEnter the number of nodes : ");
scanf("%d", &n);
int i;
int data;
printf("\nInput the nodes of the binary search tree : ");
if(n > 0)
{
scanf("%d", &data);
root = insert(root, data);
}
for(i = 1; i < n; i++)
{
scanf("%d", &data);
insert(root, data);
}
printf("\nInorder traversal of the BST : ");
inorder(root);
printf("\n");
int del_ele;
printf("\nEnter the node to be deleted : ");
scanf("%d", &del_ele);
delete_node(root, del_ele);
printf("\nInorder traversal after deletion : ");
inorder(root);
printf("\n");
return 0;
}
OUPUT:
SORTING
✓ Write a program to implement Bubble Sort.
CODE:
#include<stdio.h>
void print(int a[], int n)
{
int i;
for(i = 0; i < n; i++)
{
printf("%d ",a[i]);
}
}
void bubble(int a[], int n)
{
int i, j, temp;
for(i = 0; i < n; i++)
{
for(j = i+1; j < n; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
void main ()
{
int i, j,temp;
int a[5] = { 10, 35, 32, 13, 26};
int n = sizeof(a)/sizeof(a[0]);
printf("Before sorting array elements are - \n");
print(a, n);
bubble(a, n);
printf("\nAfter sorting array elements are - \n");
print(a, n);
}
OUTPUT:
✓ Write a program to implement Heap Sort.
CODE:
#include <stdio.h>
void heapify(int a[], int n, int i)
{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && a[left] > a[largest])
largest = left;
if (right < n && a[right] > a[largest])
largest = right;
if (largest != i)
{
int temp = a[i];
a[i] = a[largest];
a[largest] = temp;
heapify(a, n, largest);
}
}
void heapSort(int a[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
heapify(a, n, i);
for (int i = n - 1; i >= 0; i--)
{
int temp = a[0];
a[0] = a[i];
a[i] = temp;
heapify(a, i, 0);
}
}
void printArr(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
printf("%d", arr[i]);
printf(" ");
}
}
int main()
{
int a[] = {48, 10, 23, 43, 28, 26, 1};
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
heapSort(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
}
OUTPUT:
✓ Write a program to implement Insertion sort.
CODE:
#include <stdio.h>
int i, j, temp;
temp = a[i];
j = i - 1;
a[j+1] = a[j];
j = j-1;
a[j+1] = temp;
int i;
int main()
{
printArr(a, n);
insert(a, n);
printArr(a, n);
return 0;
OUTPUT:
✓ Write a program to implement Merge Sort.
CODE:
#include <stdio.h>
void merge(int a[], int beg, int mid, int end)
{
int i, j, k;
int n1 = mid - beg + 1;
int n2 = end - mid;
int LeftArray[n1], RightArray[n2]; //temporary arrays
for (int i = 0; i < n1; i++)
LeftArray[i] = a[beg + i];
for (int j = 0; j < n2; j++)
RightArray[j] = a[mid + 1 + j];
i = 0;
j = 0;
k = beg;
while (i < n1 && j < n2)
{
if(LeftArray[i] <= RightArray[j])
{
a[k] = LeftArray[i];
i++;
}
else
{
a[k] = RightArray[j];
j++;
}
k++;
}
while (i<n1)
{
a[k] = LeftArray[i];
i++;
k++;
}
while (j<n2)
{
a[k] = RightArray[j];
j++;
k++;
}
}
void mergeSort(int a[], int beg, int end)
{
if (beg < end)
{
int mid = (beg + end) / 2;
mergeSort(a, beg, mid);
mergeSort(a, mid + 1, end);
merge(a, beg, mid, end);
}
}
void printArray(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}
int main()
{
int a[] = { 12, 31, 25, 8, 32, 17, 40, 42 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArray(a, n);
mergeSort(a, 0, n - 1);
printf("After sorting array elements are - \n");
printArray(a, n);
return 0;
}
OUTPUT:
✓ Write a program to implement Quick Sort.
CODE:
#include <stdio.h>
int partition (int a[], int start, int end)
{
int pivot = a[end]; // pivot element
int i = (start - 1);
for (int j = start; j <= end - 1; j++)
{
if (a[j] < pivot)
{
i++;
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
int t = a[i+1];
a[i+1] = a[end];
a[end] = t;
return (i + 1);
}
void quick(int a[], int start, int end)
{
if (start < end)
{
int p = partition(a, start, end); //p is the partitioning index
quick(a, start, p - 1);
quick(a, p + 1, end);
}
}
void printArr(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
}
int main()
{
int a[] = { 24, 9, 29, 14, 19, 27 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
quick(a, 0, n - 1);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
}
OUPUT:
✓ Write a program to implement Selection Sort.
CODE:
#include <stdio.h>
void selection(int arr[], int n)
{
int i, j, small;
for (i = 0; i < n-1; i++)
{
small = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[small])
small = j;
int temp = arr[small];
arr[small] = arr[i];
arr[i] = temp;
}
}
void printArr(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
}
int main()
{
int a[] = { 12, 31, 25, 8, 32, 17 };
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: