DS All Programs
DS All Programs
[Link] Date:
AIM:
To implement list concept using array and performing the following operations.
ALGORITHM:
Steps:
Steps:
Steps:
Steps:
Steps:
OUTPUT:
IENGINEERINGCOLLEGE
(Autonomous)
MAX. MARKS
DESCRIPTION
MARKS AWARDED
Preparation&
20
Conduction
Observation &Results 10
Record Completion 05
Viva Voce 05
TOTAL 40
RESULT:
Thus, the above program for List Implementation using array is executed
and all of its operations are implemented successfully.
Linked List Implementation of Singly Linked List.
[Link] Date:
AIM:
To implement singly and Doubly linked list and performing insert, search, display and
delete
operations.
ALGORITHMS:
Steps:
Steps:
Steps:
Steps:
Steps:
Steps:
Steps:
Steps:
void create()
{
intval;
char ch;
do
{
new=(struct node *)malloc(sizeof(struct node));
printf("Enter the node value:");
scanf("%d",&val);
new->data=val;
new->next=NULL;
if(head==NULL)
{
head=new;
tail=new;
}
else
{
tail->next=new;
tail=new;
}
void insertatbegin()
{
int value;
new=(struct node*)malloc(sizeof( struct node));
printf("Enter the node value to insert:\n");
scanf("%d",&value);
new->data=value;
new->next=head;
head=new;
}
void insertatend()
{
int value;
new=(struct node*)malloc(sizeof( struct node));
printf("Enter the node value to insert:\n");
scanf("%d",&value);
new->data=value;
new->next=NULL;
tail->next=new;
tail=new;
}
void insertatspecific()
{
intvalue,pos,I;
temp=head;
new=(struct node*)malloc(sizeof( struct node));
printf("Enter the node value to insert:\n");
scanf("%d",&value);
new->data=value;
printf("Enter the position to Insert:\n");
scanf("%d",&pos);
for(I=0;I<pos-1; I++)
{
temp=temp->next;
}
new->next=temp->next;
temp->next=new;
}
OUTPUT:
Singly Linked List Creation, Display, Insertion:
1. Create
2. Display
3. Insertion at the Beginning
4. Insertion at the Ending
5. Insertion at the Specific Position
6. Exit
1. Linked list Creation
Enter your Choice: 1
Enter the node value: 10
Do you want to continue: y
Enter the node value: 20
Do you want to continue: y
Enter the node value: 30
Do you want to continue: y
Enter the node value: 40
Do you want to continue: n
2. Display:
Enter your Choice: 2
Nodes of Singly linked list: 10 20 30 40
3. Insertion at the
Beginning: Enter
your Choice: 3
Enter the node value to insert: 5
Enter your Choice: 2
Nodes of Singly linked list: 5 10 20 30 40
4. Insertion at the
Ending: Enter your
Choice: 4
Enter the node value to insert: 50
Enter your Choice: 2
Nodes of Singly linked list: 5 10 20 30 40 50
[Link] at the Specific Position:
Enter your Choice: 5
Enter the node value to insert: 25
Enter the position to insert: 3
Enter your Choice: 2
Nodes of Singly linked list: 5 10 20 25 30 40 50
6. Exit:
Enter your Choice: 6
PROGRAM: 2
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void create();
void display();
void deleteatbegin();
void deleteatend();
void deleteatspecific();
struct node
{
int data;
struct node *next;
};
struct node *head;
struct node *tail;
struct node *new;
struct node *temp;
void main()
{
int n;
do
{
printf("* Singly Lined List *\n"); printf("_\n\
n");
printf("[Link] \n");
printf("[Link] \n");
printf("[Link] at the Beginning \n");
printf("[Link] at the Ending \n");
printf("[Link] at the specific Location:\n");
printf("[Link] \n");
printf("Enter your choice:\n");
scanf("%d",&n);
switch(n)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
deleteatbegin();
break;
case 4:
deleteatend();
break;
case 5:
deleteatspecific();
break;
case 6:
exit(0);
break;
}
}while(1);
}
void create()
{
intval;
char ch;
do
{
new=(struct node *)malloc(sizeof(struct node));
printf("Enter the node value:\n;");
scanf("%d",&val);
new->data=val;
new->next=NULL;
if(head==NULL)
{
head=new;
tail=new;
}
else
{
tail->next=new;
tail=new;
}
void display()
{
//Node current will point to head
temp=head;
printf("Nodes of singly linked list: \n");
while(temp != NULL) {
//Prints each node by incrementing pointer
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
void deleteatbegin()
{
temp=head;
head=head->next;
temp->next=NULL;
}
void deleteatend()
{
temp=head;
while(temp->next!=tail)
{
temp=temp->next;
}
temp->next=NULL;
tail=temp;
}
void deleteatspecific()
{
intpos,I;
printf("Enter the position to Delete:\n");
scanf("%d",&pos);
temp=head;
for(I=0;I<pos-1; I++)
{
temp=temp->next;
}
temp->next=temp->next->next;
}
OUTPUT:
6. Exit:
Enter your Choice: 6
IENGINEERINGCOLLEGE
(Autonomous)
MAX. MARKS
DESCRIPTION
MARKS AWARDED
Preparation&
20
Conduction
Observation &Results 10
Record Completion 05
Viva Voce 05
TOTAL 40
RESULT:
Thus, the above Singly Linked List are created and all of its
AIM:
To implement singly and Doubly linked list and performing insert, search, display and
delete
operations.
ALGORITHMS:
Steps:
Steps:
Steps:
Steps:
Steps:
Steps:
Steps:
Steps:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void create();
void display();
void insertatbegin();
void insertatend();
void insertatspecific();
struct node
{
int data;
struct node *next;
struct node *prev;
};
struct node *head;
struct node *tail;
struct node *new;
struct node *temp;
void main()
{
int n;
clrscr();
do
{
printf("* Doubly Linked List *\n");
printf(" \n\n");
printf("[Link] \n");
printf("[Link] \n");
printf("[Link] at the Beginning \
n"); printf("[Link] at the Ending \
n");
printf("[Link] at the specific Location:\n");
printf("[Link] \n");
printf("Enter your choice:\n");
scanf("%d",&n);
switch(n)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
insertatbegin();
break;
case 4:
insertatend();
break;
case 5:
insertatspecific();
break;
case 6:
exit(0);
break;
}
}while(1);
}
void create()
{
intval;
char ch;
do
{
new=(struct node *)malloc(sizeof(struct node));
printf("Enter the node value:");
scanf("%d",&val);
new->data=val;
new->next=NULL;
new->prev=NULL;
if(head==NULL)
{
head=new;
tail=new;
}
else
{
tail->next=new;
new->prev=tail;
tail=new;
}
void insertatbegin()
{
int value;
new=(struct node*)malloc(sizeof( struct node));
printf("Enter the node value to insert:\n");
scanf("%d",&value);
new->data=value;
new->prev=NULL;
new->next=head;
head->prev=new;
head->prev=NULL;
head=new;
}
void insertatend()
{
int value;
new=(struct node*)malloc(sizeof( struct node));
printf("Enter the node value to insert:\n");
scanf("%d",&value);
new->data=value;
new->next=NULL;
new->prev=tail;
tail->next=new;
tail=new;
}
void insertatspecific()
{
intvalue,pos,I;
temp=head;
new=(struct node*)malloc(sizeof( struct node));
printf("Enter the node value to insert:\n");
scanf("%d",&value);
new->data=value;
printf("Enter the position to Inser:\n");
scanf("%d",&pos);
for(I=0;I<pos-1; I++)
{
temp=temp->next;
}
new->next=temp->next;
temp->next->prev=new;
temp->next=new;
new->prev=temp;
}
OUTPUT:
Doubly Linked List Creation, Display, Insertion:
1. Create
2. Display
3. Insertion at the Beginning
4. Insertion at the Ending
5. Insertion at the Specific Position
6. Exit
1. Linked list Creation
Enter your Choice: 1
Enter the node value: 10
Do you want to continue: y
Enter the node value: 20
Do you want to continue: y
Enter the node value: 30
Do you want to continue: y
Enter the node value: 40
Do you want to continue: n
2. Display:
Enter your Choice: 2
Nodes of Doubly linked list: 10 20 30 40
3. Insertion at the
Beginning: Enter
your Choice: 3
Enter the node value to insert: 5
Enter your Choice: 2
Nodes of Doubly linked list: 5 10 20 30 40
4. Insertion at the
Ending: Enter your
Choice: 4
Enter the node value to insert: 50
Enter your Choice: 2
Nodes of Doubly linked list: 5 10 20 30 40 50
[Link] at the Specific Position:
Enter your Choice: 5
Enter the node value to insert: 25
Enter the position to insert: 3
Enter your Choice: 2
Nodes of Doubly linked list: 5 10 20 25 30 40 50
6. Exit:
Enter your Choice: 6
PROGRAM: 2
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void create();
void display();
void deleteatbegin();
void deleteatend();
void deleteatspecific();
struct node
{
int data;
struct node *next;
struct node *prev;
};
struct node *head;
struct node *tail;
struct node *new;
struct node *temp;
void main()
{
int n;
clrscr();
do
{
printf("* Doubly Linked List *\n");
printf(" \n\n");
printf("[Link] \n");
printf("[Link] \n");
printf("[Link] at the Beginning \
n"); printf("[Link] at the Ending \
n");
printf("[Link] at the specific Location:\n");
printf("[Link] \n");
printf("Enter your choice:");
scanf("%d",&n);
switch(n)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
deleteatbegin();
break;
case 4:
deleteatend();
break;
case 5:
deleteatspecific();
break;
case 6:
exit(0);
break;
}
}while(1);
}
void create()
{
intval;
char ch;
do
{
new=(struct node *)malloc(sizeof(struct node));
printf("Enter the node value:");
scanf("%d",&val);
new->data=val;
new->next=NULL;
new->prev=NULL;
if(head==NULL)
{
head=new;
tail=new;
}
else
{
tail->next=new;
new->prev=tail;
tail=new;
}
void deleteatbegin()
{
temp=head;
head=head->next;
temp->next=NULL;
head->prev=NULL;
}
void deleteatend()
{
temp=tail;
tail=tail->prev;
temp->prev=NULL;
tail->next=NULL;
}
void deleteatspecific()
{
intpos,I;
printf("Enter the position to delete:\n");
scanf("%d",&pos);
temp=head;
for(I=0;I<pos-1; I++)
{
temp=temp->next;
}
temp->next=temp->next->next;
temp->next->prev=temp;
}
OUTPUT:
Doubly Linked List Creation, Display, Deletion:
1. Create
2. Display
3. Deletion at the Beginning
4. Deletion at the Ending
5. Deletion at the Specific Position
6. Exit
1. Linked list Creation
Enter your Choice: 1
Enter the node value: 10
Do you want to continue: y
Enter the node value: 20
Do you want to continue: y
Enter the node value: 30
Do you want to continue: y
Enter the node value: 40
Do you want to continue: n
2. Display:
Enter your Choice: 2
Nodes of Doubly linked list: 10 20 30 40
3. Deletion at the
Beginning: Enter
your Choice: 3 Enter
your Choice: 2
Nodes of Doubly linked list: 20 30 40
4. Deletion at the
Ending: Enter your
Choice: 4 Enter your
Choice: 2
Nodes of Doubly linked list: 20 30
6. Exit:
Enter your Choice: 6
IENGINEERINGCOLLEGE
(Autonomous)
MAX. MARKS
DESCRIPTION
MARKS AWARDED
Preparation&
20
Conduction
Observation &Results 10
Record Completion 05
Viva Voce 05
TOTAL 40
RESULT:
Thus the above Doubly Linked List are created and all of its
[Link] Date:
AIM:
To implement the program for Push, Pop and Display operations in Stack using
Array.
ALGORITHMS:
Step 1: If Top=Max-1
End If
Step 2: Top=Top+1
Step 3: Stack[TOP]=Element
Step 4: End
End if
Step 3: Top=Top-1
Step 4: Del_Element
Step 5: End
PROGRAM:
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
//clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t ");
printf("\n\t [Link]\n\t [Link]\n\t [Link]\n\t [Link]");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}
OUTPUT:
1. PUSH
2. POP
3. DISPLAY
4. EXIT
98
24
12
24
12
EXIT POINT
IENGINEERINGCOLLEGE
(Autonomous)
MAX. MARKS
DESCRIPTION
MARKS AWARDED
Preparation&
20
Conduction
Observation &Results 10
Record Completion 05
Viva Voce 05
TOTAL 40
RESULT:
Thus, the program for Push, Pop and Display operations in Stack was implemented
and executed successfully.
IMPLEMENTATION OF EVALUATING THE POSTFIX EXPRESSION
[Link] Date:
AIM:
ALGORITHM:
#include<stdio.h>
#include<ctype.h>
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;
}
OUTPUT:
(Autonomous)
MAX. MARKS
DESCRIPTION
MARKS AWARDED
Preparation&
20
Conduction
Observation &Results 10
Record Completion 05
Viva Voce 05
TOTAL 40
RESULT:
Thus, the evaluation of postfix expression is successfully implemented using stack ADT.
IMPLEMENTATION OF CONVERTING INFIX TO POSTFIX EXPRESSION
[Link] Date:
AIM:
To write the C Program in implementing the conversion of infix to postfix expression using
Stack ADT.
ALGORITHM:
#include<stdio.h>
#include<ctype.h>
char stack[100];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}
int main()
{
char exp[100];
char *e, x;
printf("Enter the expression : ");
scanf("%s",exp);
printf("\n");
e = exp;
while(*e != '\0')
{
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
}
while(top != -1)
{
printf("%c ",pop());
}return 0;
}
OUTPUT:
ab+c*da-+
IENGINEERINGCOLLEGE
(Autonomous)
MAX. MARKS
DESCRIPTION
MARKS AWARDED
Preparation&
20
Conduction
Observation &Results 10
Record Completion 05
Viva Voce 05
TOTAL 40
RESULT:
Thus, the conversion of infix to postfix expression is successfully implemented using stack
ADT.
IMPLEMENTATION OF QUEUE AND ITS OPERATIONS
[Link] Date:
AIM:
To write the C Program in implementing the queue and its operations using Array.
ALGORITHMS:
#include<stdio.h>
#include<stdlib.h>
#define n 5
int main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\[Link] \[Link] \[Link] \[Link]");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
return 0;
}
OUTPUT:
Queue using Array
[Link]
[Link]
[Link]
[Link]
Enter the Choice:1
Enter no 1:2
Enter the Choice:1
Enter no 2:3
Enter the Choice:1
Enter no 3:4
Enter the Choice:1
Enter no 4:5
Enter the Choice:2
Deleted Element is 2
Enter the Choice:3
Queue Elements are:
3
4
5
Enter the Choice:4
IENGINEERINGCOLLEGE
(Autonomous)
MAX. MARKS
DESCRIPTION
MARKS AWARDED
Preparation&
20
Conduction
Observation &Results 10
Record Completion 05
Viva Voce 05
TOTAL 40
RESULT:
Thus, the queue and its operations is successfully implemented using Array.
APPLICATIONS OF QUEUE ADTs.
[Link] Date:
Aim:
To write the C Program in implementing the Applications of Queue.
ALGORITHMS:
#include <stdio.h>
#include <stdlib.h>
while (1) {
printf("\nQueue Operations Menu:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display Queue\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter element to enqueue: ");
scanf("%d", &element);
enqueue(element);
break;
case 2:
dequeue();
break;
case 3:
displayQueue();
break;
case 4:
exit(0);
break;
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
OUTPUT:
Queue Operations Menu:
1. Enqueue
2. Dequeue
3. Display Queue
4. Exit
Enter your choice: 1
Enter element to enqueue: 10
Enqueued: 10
(Autonomous)
MAX. MARKS
DESCRIPTION
MARKS AWARDED
Preparation&
20
Conduction
Observation &Results 10
Record Completion 05
Viva Voce 05
TOTAL 40
RESULT:
Thus, the applications of queue were executed successfully and the Output is verified.
IMPLEMENTATION OF BINARY SEARCH TREE
[Link] Date:
AIM:
To write the program to implement the Binary Search Tree and its operations.
ALGORITHM:
1. Read integers
2. Create binary tree with the functions insert, del, findmin, display.
FOR INSERTION
If(T==null)
Newnode -> data=X;
Newnode->left=Null;
Newnode -> right=Null;
FOR DELETION
If(T== null)
Print(“element not found”)
FINDMIN
If(T!=null)
If(t->left==null)
Return T;
DISPLAY
If(T!=null)
Display T->left;
Printf(“%d\t”,T->data);
Display(T->right);
4. Visit in the order left, root, right,
5. Display the visited nodes
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct tree *node;
node insert(int,node T);
node findmin(node T);
node del(int,node T);
void display(node T);
struct tree
{
int data;
struct tree *right,*left;
}*root;
void main()
{
node T=NULL;
int data,n,i=0;
char c;
clrscr();
printf("\n\n Enter the number of elements in the tree:");
scanf("%d", &n);
printf("\n Enter the elements to insert:\n");
while(i<n)
{
scanf("%d", &data);
T=insert(data,T);
i++;
}
printf("\n Elements displayed in inorder format are:\n");
display(T);
printf("\n Enter the element to delete:\n");
scanf("%d",&data);
T=del(data,T);
printf("\nThe contents of tree after deletion:\n");
display(T);
getch();
}
node insert(int X, node T)
{
struct tree *newnode;
newnode=malloc(sizeof (struct tree));
if(newnode==NULL)
printf("\n Out of space\n");
else
{
if(T==NULL)
{
newnode->data=X;
newnode->left=NULL;
newnode->right=NULL;
T=newnode;
}
else
{
if(X<T->data)
T->left=insert(X,T->left);
else
T->right=insert(X,T->right);
}
}
return T;
}
node del(int X,node T)
{
node tmpcell;
if(T==NULL)
printf("\nElement not found");
else if(X<T->data)
T->left=del(X,T->left);
else if(X>T->data)
T->right=del(X,T->right);
else if(T->left&&T->right)
{
tmpcell=findmin(T->right);
T->data=tmpcell->data;
T->right=del(T->data,T->right);
}
else
{
tmpcell=T;
if(T->left==NULL)
T=T->right;
else if(T->right==NULL)
T=T->left;
free(tmpcell);
}
return T;
}
node findmin(node T)
{
if(T!=NULL)
{
if(T->left==NULL)
return T;
else
return findmin(T->left);
}
return(0);
}
void display(node T)
{
if(T!=NULL)
{
display(T->left);
printf("%d\t",T->data);
display(T->right);
}
}
OUTPUT:
(Autonomous)
MAX. MARKS
DESCRIPTION
MARKS AWARDED
Preparation&
20
Conduction
Observation &Results 10
Record Completion 05
Viva Voce 05
TOTAL 40
RESULT:
Thus, the program for Binary Search Tree operations was implemented and
executed successfully.
IMPLEMENTATION OF AVL TREE
[Link] Date:
AIM:
To write the program to implement AVL Tree and its operation.
ALGORITHMS:
#include <stdio.h>
#include <stdlib.h>
// Allocate a new node with the given key and NULL left and right pointers
struct Node* newNode(int key) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1; // new node is initially added at leaf
return(node);
}
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = max(height(y->left), height(y->right)) + 1;
x->height = max(height(x->left), height(x->right)) + 1;
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;
// Recursive function to insert a key in the subtree rooted with node and returns the new root
of the subtree
struct Node* insert(struct Node* node, int key) {
// Perform the normal BST insertion
if (node == NULL)
return(newNode(key));
// Get the balance factor of this ancestor node to check whether this node became
unbalanced
int balance = getBalance(node);
// Function to find the node with the minimum value (used for deletion)
struct Node* minValueNode(struct Node* node) {
struct Node* current = node;
return current;
}
// Recursive function to delete a node with a given key from the subtree with the given root
struct Node* deleteNode(struct Node* root, int key) {
// Perform standard BST delete
if (root == NULL)
return root;
// No child case
if (temp == NULL) {
temp = root;
root = NULL;
} else // One child case
*root = *temp; // Copy the contents of the non-empty child
free(temp);
}
else {
// Node with two children: Get the inorder successor (smallest in the right subtree)
struct Node* temp = minValueNode(root->right);
return root;
}
// A utility function to print the preorder traversal of the tree
void preOrder(struct Node *root) {
if (root != NULL) {
printf("%d ", root->key);
preOrder(root->left);
preOrder(root->right);
}
}
int main() {
struct Node *root = NULL;
/* Constructing tree */
root = insert(root, 10);
root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 25);
// Deleting node
root = deleteNode(root, 50);
return 0;
}
OUTPUT:
(Autonomous)
MAX. MARKS
DESCRIPTION
MARKS AWARDED
Preparation&
20
Conduction
Observation &Results 10
Record Completion 05
Viva Voce 05
TOTAL 40
RESULT:
[Link] Date:
AIM:
To write the program to implement the Graph Traversal.
ALGORITHM:
Step 2 − If no adjacent vertex is found, remove the first vertex from the queue.
Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack. (It will pop up all the
vertices from the stack, which do not have adjacent vertices.)
while(!isStackEmpty())
{
//get the unvisited vertex of vertex which is at top of the stack
int unvisitedVertex = getAdjUnvisitedVertex(peek());
addVertex('S'); // 0
addVertex('A'); // 1
addVertex('B'); // 2
addVertex('C'); // 3
addVertex('D'); // 4
addEdge(0, 1); // S - A
addEdge(0, 2); // S - B
addEdge(0, 3); // S - C
addEdge(1, 4); // A - D
addEdge(2, 4); // B - D
addEdge(3, 4); // C - D
printf("Depth First Search: ")
depthFirstSearch();
return 0;
}
OUTPUT:
(Autonomous)
MAX. MARKS
DESCRIPTION
MARKS AWARDED
Preparation&
20
Conduction
Observation &Results 10
Record Completion 05
Viva Voce 05
TOTAL 40
RESULT:
Thus, the program Graph Traversal was implemented and executed successfully.
IMPLEMENTATION OF LINER SEARCH
[Link] Date:
AIM:
To write a C program for implementing linear search and to perform the various operations.
ALGORITHM:
1. Start
2. Input: Array arr[] of size n, element x to search.
3. Set: i = 0
4. While i < n (iterate over the array):
o If arr[i] == x Then:
Print "Element found at index i" // Element found
Return i // Return the index of the found element.
o Else: Increment i (i = i + 1)
5. End While
6. If the loop completes and no element is found:
o Print "Element not found in the array"
7. End
PROGRAM:
#include <stdio.h>
int main() {
int n, x, result;
return 0;
}
OUTPUT:
(Autonomous)
MAX. MARKS
DESCRIPTION
MARKS AWARDED
Preparation&
20
Conduction
Observation &Results 10
Record Completion 05
Viva Voce 05
TOTAL 40
RESULT:
Thus, the above liner search is implemented successfully.
IMPLEMENTATION OF BINARY SEARCH
[Link] Date:
AIM:
To write a C program for implementing binary search and to perform the various operations.
ALGORITHM:
1. Start
2. Input: A sorted array arr[] of size n, element x to search.
3. Initialize: low = 0, high = n - 1
4. While low <= high:
o Set mid = (low + high) / 2 // Find the middle element
o If arr[mid] == x:
Print "Element found at index mid"
Return mid // Return the index where the element is found.
o Else If arr[mid] < x:
Set low = mid + 1 // Search the right half.
o Else:
Set high = mid - 1 // Search the left half.
5. End While
6. If element is not found:
o Print "Element not found in the array"
7. End
PROGRAM:
#include <stdio.h>
int main() {
int n, x, result;
return 0;
}
OUTPUT:
(Autonomous)
MAX. MARKS
DESCRIPTION
MARKS AWARDED
Preparation&
20
Conduction
Observation &Results 10
Record Completion 05
Viva Voce 05
TOTAL 40
RESULT:
Thus, the above liner search was implemented and the output are verified successfully.
IMPLEMENTATION OF INSERTION SORT
[Link] Date:
AIM:
To write a C program for implementing insertion sort.
ALGORITHM:
1. Start
2. Input: Array arr[] of size n.
3. For i = 1 to n-1:
Set key = arr[i] (element to be inserted).
Set j = i - 1
While j >= 0 and arr[j] > key:
Set arr[j + 1] = arr[j] (shift element to the right).
Decrement j = j - 1
Set arr[j + 1] = key (insert the key element in its correct position).
4. End For
5. End
PROGRAM:
#include <stdio.h>
// Function to perform insertion sort
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i]; // Element to be inserted
int j = i - 1;
// Shift elements of arr[0..i-1], that are greater than key, to one position ahead
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key; // Insert key in its correct position
}
}
// Function to print the array
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int n;
return 0;
}
OUTPUT:
64 25 12 22 11 90
Sorted array:
11 12 22 25 64 90
IENGINEERINGCOLLEGE
(Autonomous)
MAX. MARKS
DESCRIPTION
MARKS AWARDED
Preparation&
20
Conduction
Observation &Results 10
Record Completion 05
Viva Voce 05
TOTAL 40
RESULT:
Thus, the above insertion sort was implemented and the output are verified successfully.
IMPLEMENTATION OF BUBBLE SORT
[Link] Date:
AIM:
To write a C program for implementation of bubble sort.
PROGRAM:
#include <stdio.h>
int main() {
int n;
return 0;
}
OUTPUT:
(Autonomous)
MAX. MARKS
DESCRIPTION
MARKS AWARDED
Preparation&
20
Conduction
Observation &Results 10
Record Completion 05
Viva Voce 05
TOTAL 40
RESULT:
Thus, the above bubble sort was implemented and the output are verified successfully.
IMPLEMENTATION OF HASHING WITH SEPARATE CHAINING
[Link] Date:
AIM:
To write a C program for implementation of hashing with separate chaining.
ALGORITHM:
1. Initialization
Create a hash table of size TABLE_SIZE where each index points to the head of a linked list.
Initialize all entries in the hash table to NULL.
2. Hash Function
Input: Key k
Output: Index index
Compute the hash value:
index = (sum of ASCII values of characters in k) % TABLE_SIZE
3. Insertion (Insert)
Input: Key k, Value v
Compute the index using the hash function:
index = hash(k)
Create a new node with the key k and value v.
If table[index] is NULL:
Set table[index] to point to the new node (insert as the head of the list).
Else:
Traverse the linked list at table[index] to find the end of the list.
Append the new node to the end of the list.
4. Search (Search)
Input: Key k
Compute the index using the hash function:
index = hash(k)
Traverse the linked list at table[index]:
While the current node is not NULL:
If the key of the current node matches k, return the value.
Move to the next node.
If no match is found, return NULL or an indication that the key is not found.
5. Display (Display)
For each index in the hash table:
Print the index and traverse the linked list at that index.
Print all key-value pairs in the list.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Hash function
int hash(char* key) {
int hashValue = 0;
for (int i = 0; key[i] != '\0'; i++) {
hashValue += key[i]; // Simple hash function
}
return hashValue % TABLE_SIZE; // Modulus to fit within table size
}
return 0;
}
OUTPUT:
Index 0:
Index 1:
Index 2:
Index 3:
Index 4: -> [banana: 60]
Index 5: -> [apple: 10]
Index 6:
Index 7:
Index 8: -> [orange: 30] -> [mango: 40]
Index 9: -> [grape: 50]
Value for key 'banana': 60
IENGINEERINGCOLLEGE
(Autonomous)
MAX. MARKS
DESCRIPTION
MARKS AWARDED
Preparation&
20
Conduction
Observation &Results 10
Record Completion 05
TOTAL 40
Thus, the above program for
implementation of hashing with separate chaining was executed and the output are verified
successfully.