0% found this document useful (0 votes)
27 views

Data Structure

Uploaded by

jkjaikumar2424
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Data Structure

Uploaded by

jkjaikumar2424
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 86

PROGRAM :

#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
void enqueue();
void dequeue();
void show();
int inp_arr[SIZE];
int rear = -1;
int front = -1;
int i;
int main() {
int ch;
clrscr();
while (1) {
printf("1. Enqueue operation\n");
printf("2. Dequeue operation\n");
printf("3. Display operation\n");
printf("4. Exit\n");
printf("Enter your choice of operation: ");
scanf("%d", &ch);
switch (ch) {
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
show();
break;
case 4:
exit(0);
default:
printf("\nIncorrect choice\n");
}
}
}
void enqueue() {
int insert_item;
if (rear == SIZE - 1) {
printf("overflow \n");
} else
{
if (front == -1) {
front = 0;
}
printf("Element to be inserted in the queue: ");
scanf("%d", &insert_item);
rear = rear + 1;
inp_arr[rear] = insert_item;
}
}
void dequeue() {
if (front == -1 || front > rear) {
printf("Underflow\n");
return;
} else
{
printf("Element deleted from the queue: %d\n", inp_arr[front]);
if (front == rear) {
front = -1;
rear = -1;
} else {
front = front + 1;
}
}
}
void show() {
if (front == -1) {
printf("Empty queue\n");
} else
{
printf("Queue: ");
for (i = front; i <= rear; i++) {
printf("%d ", inp_arr[i]);
}
printf("\n");
getch();
}
}
PROGRAM :
#include<stdio.h>
#define max 6
int queue[max];
int front=-1;
int rear=-1;
void enqueue(int element)
{
if(front==-1&&rear==-1)
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front)
printf("queue is overflow");
else
{
rear=(rear+1)%max;
queue[rear]=element;
}
}
int dequeue()
{
if((front==-1)&&(rear==-1))
printf("\nQueue is underflow");
else if(front==rear)
{
printf("\nThe dequeue elements is %d",queue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe dequeued element is %d",queue[front]);
front=(front+1)%max;
}
}
void display()
{
int i=front;
if(front==-1&&rear==-1)
printf("\nQueue is empty");
else
{
printf("\nElements in a Queue are:");
while(i<=rear)
{
printf("%d",queue[i]);
i=(i+1)%max;
}
}
}
int main()
{
int choice=1,x;
clrscr();
while(choice<4&&choice!=0)
{
printf("\nPress1:Insert an element");
printf("\npress2:Delete an element");
printf("\nPress3:Display the element");
printf("\nPress4:Exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the element which is to be inserted:");
scanf("%d",&x);
enqueue(x);
break;
case 2:
dequeue(x);
break;
case 3:
display();
}
}
return 0;
}
PROGRAM :
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node{
int info;
struct node*link;
};
struct node *start =NULL;
void createlist()
{
int n,i;
if(start ==NULL){
printf("\nEnter the number of nodes:");
scanf("%d",&n);
if(n!=0)
{
int data;
struct node*newnode;
struct node*temp;
newnode =malloc (sizeof(struct node));
start =newnode;
temp =start;
printf("\n Enter number to be inserted:");
scanf("%d",&data);
start->info =data;
for(i=2;i<n;i++)
{
newnode=malloc(sizeof(struct node));
temp->link =newnode;
printf("\n Enter number to be inserted:");
scanf("%d",&data);
newnode->info=data;
temp =temp->link;
}
}
printf("\n the list is created\n");
}
else
printf("\n the list is already created\n");
}
void traverse()
{
struct node*temp;
if(start==NULL)
printf("\n List is empty\n");
else{
temp=start ;
while(temp!=NULL)
{
printf("Data =%d\n",temp->info);
temp=temp->link;
}
}
getch();
}
void insertAtFront()
{
int data;
struct node* temp;
temp =malloc(sizeof(struct node));
printf("\n Enter number to be inserted");
scanf("%d",&data);
temp->info =data;
temp->link =start;
start =temp;
}
void insertAtEnd ()
{
int data;

struct node *temp, * head;


temp = malloc (sizeof(struct node));
printf ("\n Enter number to be inserted:");
scanf ("%d", &data);
temp->link =0;
temp->info = data;
head = start;
while (head->link != NULL)
head= head->link;
head->link = temp;
}
void insertAtPosition ()
{
struct node * temp, * newnode;
int pos, data, i=1;
newnode = malloc(sizeof (struct node));
printf ("\n Enter position : ");
scanf("%d",&pos);
printf ("\n Enter data : ");
scanf ("%d", &data);
temp = start;
newnode->info = data;
newnode->link =0;
while (i<pos-1)
{
temp = temp-> link;
i++;
}
newnode->link = temp -> link;
temp->link =newnode;
}
void deleteFirst ()
{
struct node*temp;
if (start == NULL)
printf ("\n List in Empty\n");
else {
temp = start;
start =start ->link;
free (temp);
}
}
void deleteEnd ()
{
struct node *temp, * prevnode;
if (start ==NULL)
printf ("\n List is Empty \n" );
else
{
temp = start;
while (temp->link != 0){
prevnode =temp;
temp = temp -> link ;
}
free (temp);
prevnode -> link =0;
}
}
void deletePosition ()
{
struct node * temp, * position;
int i=1, pos;
if (start == NULL)
printf ("\n List is empty \n");
else
{
printf (" \n Enter index");
scanf ("%d", &pos);
position= malloc(sizeof (struct node));
temp = start;
while (i< pos-1)
{
temp =temp->link;
i++ ;
}
position = temp-> link;
temp-> link = position->link;
free (position);
}
}
void Maximum ()
{
int a[10];
int i,max;
struct node * temp;
if (start == NULL)
printf ("\n List is Empty \n");
else {
temp = start;
max =temp-> info;
while (temp != NULL) {
if (max<temp->info)
max = temp-> info;
temp = temp-> link;
}
printf ("\n maximum number is: %d", max);
}
getch();
}
void mean ()
{
int a [10];
int i,sum,count;
float m;
struct node*temp;
if (start ==NULL)
printf("\n list is empty\n");
else {
temp = start;
sum=0, count =0;
while (temp != NULL) {
sum= sum+temp->info;
temp = temp -> link;
count ++;
}
m= sum/count;
printf (" \n Mean is %f ", m) ;
}
getch();
}
void sort ()
{
struct node *current = start;
struct node *index=NULL;
int temp;
if (start ==NULL)
return;
else {
while (current != NULL)
{
index = current -> link;
while (index!= NULL )
{
if (current ->info > index->info)
{ temp = current -> info;
current-> info = index-> info;
index-> info = temp;
}
index = index ->link;
}
current = current-> link;
}}}
void reverseLL()
{
struct node *t1, *t2, *temp;
t1 =t2 = NULL;
if (start == NULL)
printf ("\n list is empty\n");
else
{
while (start != NULL) {
t2 = start-> link;
start -> link =t1;
t1 = start;
start=t2;
}
start =t1;
temp = start;
printf ("\n Reverse linked list is:");
while ( temp != NULL)
{
printf ("%d\t", temp->info);
temp = temp->link;
}
}}

void main()
{
int choice;
clrscr();
while (1) {
printf ( " \n 1 To see list ");
printf ("\n 2 For insertion at starting ");
printf ("\n 3 For Insertion at End ");
printf ("\n 4 for insert at any positions");
printf ("\n 5 For delete First element ");
printf ("\n 6 For delete last element ");
printf ( "\n 7 for delete element at any position");
printf ("\n 8 For Find maximum of elements ");
printf ("\n 9 To find mean for elements ");
printf ("\n 10 To sort the elements ");
printf ("\n 11 To reverse the linked list ");
printf ("\n 12 To exit ");
printf ("\n Enter choice : ");
scanf ("%d", &choice);
switch (choice) {
case 1:
traverse();
break;
case 2:
insertAtFront ();
break;
case 3:
insertAtEnd ();
break;
case 4:
insertAtPosition ();
break;
case 5:
deleteFirst();
break;
case 6:
deleteEnd();
break;
case 7:
deletePosition ();
break;
case 8 :
Maximum ();
break;
case 9:
mean ();
break;
case 10:
sort ();
break;
case 11:
reverseLL ();
break;
case 12:
exit (1);
break;
default :
printf ("Incorrect choice \n");
}
}
}
PROGRAM :
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node {
int info;
struct node *ptr;
} *top, *temp;
int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
int count = 0;
void main() {
int no, ch, e;
clrscr();
printf("\n1 - Push");
printf("\n2 - Pop");
printf("\n3 - Top");
printf("\n4 - Empty");
printf("\n5 - Exit");
printf("\n6 - Display");
printf("\n7 - Stack Count");
printf("\n8 - Destroy Stack");
create();
while (1) {
printf("\nEnter choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter data: ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No element in stack\n");
else {
e = topelement();
printf("\nTop element: %d\n", e);
}
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default:
printf("Wrong choice, please enter the right choice:\n");
break;
}
}
getch(); // Pauses the output
}
void create() {
top = NULL;
}
void stack_count() {
printf("\nNo. of elements: %d", count);
}
void push(int data) {
if (top == NULL) {
top = (struct node *)malloc(sizeof(struct node));
top->ptr = NULL;
top->info = data;
} else {
temp = (struct node *)malloc(sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
}
void display() {
struct node *top1 = top;
if (top1 == NULL) {
printf("Stack is empty\n");
return;
}
while (top1 != NULL) {
printf("%d ", top1->info);
top1 = top1->ptr;
}
printf("\n");
}
void pop() {
struct node *top1;
if (top == NULL) {
printf("\nError: Trying to pop from an empty stack\n");
return;
} else {
top1 = top->ptr;
printf("\nPopped value: %d", top->info);
free(top);
top = top1;
count--;
}
}
int topelement() {
return (top->info);
}
void empty() {
if (top == NULL)
printf("\nStack is empty\n");
else
printf("\nStack is not empty, contains %d elements\n", count);
}
void destroy() {
struct node *top1;
while (top != NULL) {
top1 = top->ptr;
free(top);
top = top1;
}
printf("\nAll stack elements destroyed\n");
count = 0;
top = NULL;
}
PROGRAM :
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *front = NULL;
struct node *rear = NULL;
void delQueue() {
struct node *temp, *var = rear;
if (var == rear) {
rear = rear->next;
free(var);
} else {
printf("\nQueue is empty");
}
}
void push(int value) {
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = value;
if (front == NULL) {
front = temp;
front->next = NULL;
rear = front;
} else {
front->next = temp;
front = temp;
front->next = NULL;
}
}
void display() {
struct node *var = rear;
if (var != NULL) {
printf("\nElements are as:");
while (var != NULL) {
printf("\t%d", var->data);
var = var->next; // Increment the pointer to move to the next node
}
printf("\n");
} else {
printf("\nQueue is empty");
}
}
int main() {
int i = 0;
clrscr(); // Clear the screen
printf("\n1. Push to the queue");
printf("\n2. Pop from the queue");
printf("\n3. Display data from the queue");
printf("\n4. Exit\n");
while (1) {
printf("\nChoose an option: ");
scanf("%d", &i);
switch (i) {
case 1: {
int value;
printf("\nEnter a value to push into the queue: ");
scanf("%d", &value);
push(value);
display();
break;
}
case 2: {
delQueue();
display();
break;
}
case 3: {
display();
break;
}
case 4:
exit(0);
default:
printf("\nWrong option");
}
}
getch(); // Wait for key press
return 0;
}
PROGRAM :
#include <stdio.h>
#include <stdlib.h>
struct link {
int coeff;
int pow;
struct link *next;
};
struct link *poly1 = NULL, *poly2 = NULL, *poly = NULL;
void create(struct link **node) {
char ch;
struct link *temp;
do {
temp = (struct link *)malloc(sizeof(struct link));
if (temp == NULL) {
printf("Memory allocation failed!\n");
return;
}
printf("Enter coefficient: ");
scanf("%d", &temp->coeff);
printf("Enter the power: ");
scanf("%d", &temp->pow);
temp->next = NULL;
if (*node == NULL) {
*node = temp;
} else {
struct link *current = *node;
while (current->next != NULL) {
current = current->next;
}
current->next = temp;
}
printf("Continue (y/n): ");
scanf(" %c", &ch); // Add a space before %c to consume any leftover newline
} while (ch == 'y' || ch == 'Y');
}
void display(struct link *node) {
while (node != NULL) {
printf("%d^%d", node->coeff, node->pow);
node = node->next;
if (node != NULL) {
printf(" + ");
}
}
printf("\n");
}
void polyadd(struct link *poly1, struct link *poly2, struct link **result) {
struct link *temp;
while (poly1 != NULL && poly2 != NULL) {
temp = (struct link *)malloc(sizeof(struct link));
if (temp == NULL) {
printf("Memory allocation failed!\n");
return;
}
if (poly1->pow > poly2->pow) {
temp->pow = poly1->pow;
temp->coeff = poly1->coeff;
poly1 = poly1->next;
} else if (poly1->pow < poly2->pow) {
temp->pow = poly2->pow;
temp->coeff = poly2->coeff;
poly2 = poly2->next;
} else {
temp->pow = poly1->pow;
temp->coeff = poly1->coeff + poly2->coeff;
poly1 = poly1->next;
poly2 = poly2->next;
}
temp->next = NULL;
if (*result == NULL) {
*result = temp;
} else {
struct link *current = *result;
while (current->next != NULL) {
current = current->next;
}
current->next = temp;
}
}
wile (poly1 != NULL) {
temp = (struct link *)malloc(sizeof(struct link));
if (temp == NULL) {
printf("Memory allocation failed!\n");
return;
}
temp->pow = poly1->pow;
temp->coeff = poly1->coeff;
temp->next = NULL;
if (*result == NULL) {
*result = temp;
} else {
struct link *current = *result;
while (current->next != NULL) {
current = current->next;
}
current->next = temp;
}
poly1 = poly1->next;
}
while (poly2 != NULL) {
temp = (struct link *)malloc(sizeof(struct link));
if (temp == NULL) {
printf("Memory allocation failed!\n");
return;
}

temp->pow = poly2->pow;
temp->coeff = poly2->coeff;
temp->next = NULL;

if (*result == NULL) {
*result = temp;
} else {
struct link *current = *result;
while (current->next != NULL) {
current = current->next;
}
current->next = temp;
}
poly2 = poly2->next;
}
}
int main() {
clrscr();
poly1 = NULL;
poly2 = NULL;
poly = NULL;
printf("Enter the First Polynomial:\n");
create(&poly1);
printf("First Polynomial: ");
display(poly1);

printf("Enter the Second Polynomial:\n");


create(&poly2);
printf("Second Polynomial: ");
display(poly2);
polyadd(poly1, poly2, &poly);
printf("Addition of Two Polynomials: ");
display(poly);
getch();
return 0;
}
PROGRAM :
#include <stdio.h>
#include <stdlib.h>
struct node {
int coefficient;
int exponent;
struct node *next;
};
struct node *hPtr1 = NULL, *hPtr2 = NULL, *hPtr3 = NULL;
struct node *buildNode(int coefficient, int exponent) {
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr->coefficient = coefficient;
ptr->exponent = exponent;
ptr->next = NULL;
return ptr;
}
void polynomial_insert(struct node **myNode, int coefficient, int exponent) {
struct node *IPtr = buildNode(coefficient, exponent);
struct node *pPtr, *qPtr = *myNode;
if (*myNode == NULL || (*myNode)->exponent < exponent) {
IPtr->next = qPtr;
*myNode = IPtr;
return;
}
while (qPtr) {
pPtr = qPtr;
qPtr = qPtr->next;
if (!qPtr) {
pPtr->next = IPtr;
break;
} else if (exponent < pPtr->exponent && exponent > qPtr->exponent) {
IPtr->next = qPtr;
pPtr->next = IPtr;
break;
}
}
}
void polynomial_add(struct node **n1, int coefficient, int exponent) {
struct node *x = NULL, *temp = *n1;
if (*n1 == NULL || (*n1)->exponent < exponent) {
*n1 = x = buildNode(coefficient, exponent);
x->next = temp;
} else {
while (temp) {
if (temp->exponent == exponent) {
temp->coefficient += coefficient;
return;
}
if (temp->exponent > exponent && (temp->next == NULL || temp->next->exponent < exponent)) {
x = buildNode(coefficient, exponent);
x->next = temp->next;
temp->next = x;
return;
}
temp = temp->next;
}
x->next = NULL;
temp->next = x;
}
}
void polynomial_multiply(struct node **n1, struct node *n2, struct node *n3) {
struct node *temp;
int coefficient, exponent;
temp = n3;
if (!n2 && !n3)
return;
if (!n2) {
*n1 = n3;
} else if (!n3) {
*n1 = n2;
} else {
while (n2) {
struct node *currentN3 = n3;
while (currentN3) {
coefficient = n2->coefficient * currentN3->coefficient;
exponent = n2->exponent + currentN3->exponent;
polynomial_add(n1, coefficient, exponent);
currentN3 = currentN3->next;
}
n2 = n2->next;
}
}
}
struct node *polynomial_deleteList(struct node *ptr) {
struct node *temp;
while (ptr) {
temp = ptr->next;
free(ptr);
ptr = temp;
}
return NULL;
}
void polynomial_view(struct node *ptr) {
int flag = 0;
while (ptr) {
if (ptr->coefficient != 0) {
if (ptr->coefficient > 0 && flag > 0) {
printf("+");
}
if (ptr->exponent == 0) {
printf("%d", ptr->coefficient);
} else {
printf("%dx^%d", ptr->coefficient, ptr->exponent);
}
flag++;
}
ptr = ptr->next;
}
printf("\n");
}
int main() {
int coefficient, exponent, count, i;

printf("Enter the number of coefficients in the multiplicand: ");


scanf("%d", &count);
for (i = 0; i < count; i++) {
printf("Enter the coefficient part: ");
scanf("%d", &coefficient);
printf("Enter the exponent part: ");
scanf("%d", &exponent);
polynomial_insert(&hPtr1, coefficient, exponent);
}
printf("Enter the number of coefficients in the multiplier: ");
scanf("%d", &count);
for (i = 0; i < count; i++) {
printf("Enter the coefficient part: ");
scanf("%d", &coefficient);
printf("Enter the exponent part: ");
scanf("%d", &exponent);
polynomial_insert(&hPtr2, coefficient, exponent);
}
printf("Polynomial Expression 1: ");
polynomial_view(hPtr1);
printf("Polynomial Expression 2: ");
polynomial_view(hPtr2);
polynomial_multiply(&hPtr3, hPtr1, hPtr2);
printf("Output:\n");
polynomial_view(hPtr3);
hPtr1 = polynomial_deleteList(hPtr1);
hPtr2 = polynomial_deleteList(hPtr2);
hPtr3 = polynomial_deleteList(hPtr3);
return 0;
}
PROGRAM :
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <conio.h>
#define MAX 10
typedef struct node {
char data;
struct node *next;
} stack;
stack *topstack = NULL;
int push(char value);
int pop(char *value);
void intoPost(char inStr[], char postStr[]);
int indexpriority(char p[][2], char data);
char iP[MAX][2] = {
{'(', 0}, {')', 0}, {'\0', 0}, {'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}, {'%', 2}, {'^', 3}
};
char sP[MAX][2] = {
{'(', 0}, {')', -1}, {'\0', 0}, {'+', 1}, {'-', 1}, {'*', 2}, {'%', 2}, {'^', 3}
};
void main() {
char inStr[20], postStr[20];
clrscr();
printf("Enter the infix expression: ");
scanf("%s", inStr);
intoPost(inStr, postStr);
printf("The postfix expression is: %s\n", postStr);
getch();
}
int push(char value) {
stack *newnode = (stack *)malloc(sizeof(stack));
if (newnode == NULL)
return -1;
newnode->data = value;
newnode->next = topstack;
topstack = newnode;
return 0;
}
int pop(char *value) {
stack *temp;
if (topstack == NULL)
return -1;
temp = topstack;
topstack = topstack->next;
*value = temp->data;
free(temp);
return 0;
}
void intoPost(char inStr[], char postStr[]) {
char ch, item;
int i = 0, st = 0, spr, ipr;
push('\0');
while ((ch = inStr[st++]) != '\0') {
if (tolower(ch) >= 'a' && tolower(ch) <= 'z') {
postStr[i++] = ch;
} else if (ch == '(') {
push(ch);
} else if (ch == ')') {
pop(&item);
while (item != '(') {
postStr[i++] = item;
pop(&item);
}
} else {
pop(&item);
spr = indexpriority(sP, item);
ipr = indexpriority(iP, ch);
while (sP[spr][1] >= iP[ipr][1]) {
postStr[i++] = item;
pop(&item);
spr = indexpriority(sP, item);
}
push(item);
push(ch);
}
}
while (pop(&item) == 0)
postStr[i++] = item;
postStr[i] = '\0';
}
int indexpriority(char p[][2], char data) {
int index;
for (index = 0; index < MAX; index++)
if (p[index][0] == data)
return index;
return -1;
}
PROGRAM :
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define stacksize 10
struct stack {
int top;
int item[stacksize];
};
void push(struct stack *sptr, int data);
int pop(struct stack *sptr);
int main()
{
char exp[10];
char postex;
int len, val, result, a, b, c, i;
struct stack s;
s.top = -1;
clrscr();
printf("\nEnter the postfix expression: ");
scanf("%s", exp);
len = strlen(exp);
printf("\nLength: %d", len);
printf("\nPostfix expression: %s", exp);
for (i = 0; i < len; i++) {
postex = exp[i];
if (postex == '+' || postex == '-' || postex == '*' || postex == '/')
{
a = pop(&s);
b = pop(&s);
switch (postex) {
case '+':
c = b + a;
break;
case '-':
c = b - a;
break;
case '*':
c = a * b;
break;
case '/':
if (a != 0)
c = b / a;
Else
{
printf("\nError: Division by zero!");
return 1;
}
break;
}
push(&s, c);
result = c;
} else {
printf("\nEnter the value of %c: ", postex);
scanf("%d", &val);
push(&s, val);
}
}
printf("\nResult = %d", result);
getch(); // Wait for a key press
return 0;
}
void push(struct stack *sptr, int data)
{
if (sptr->top == stacksize - 1)
printf("\nStack overflow");
else {
sptr->top++;
sptr->item[sptr->top] = data;
}
}
int pop(struct stack *sptr) {
if (sptr->top == -1) {
printf("\nStack underflow");
return -1;
} else
return sptr->item[sptr->top--];
}
PROGRAM :
#include <stdio.h>
#include <stdlib.h>
typedef struct bst {
int data;
struct bst *left, *right;
} node;
void insert(node **root, node *newNode);
void inorder(node *root);
node *search(node *root, int key, node **parent);
void del(node **root, int key);
node *getnode();
int main() {
int choice;
char ans = 'N';
int key;
node *root = NULL;
node *parent,*temp;
clrscr();
do {
printf("\n1. Create\n2. Search\n3. Delete\n4. Display\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
do {
node *s = getnode();
printf("Enter the element: ");
scanf("%d", &s->data);
insert(&root, s);
printf("Do you want to enter more elements? (y/n): ");
ans = getch();
printf("\n");
} while (ans == 'y' || ans == 'Y');
break;
case 2:
printf("Enter the element to search: ");
scanf("%d", &key);
parent = NULL;
temp = search(root, key, &parent);
if (temp) {
printf("Element %d found with parent %d\n", temp->data, parent ? parent->data : -1);
} else {
printf("Element %d not found.\n", key);
}
break;
case 3:
printf("Enter the element to delete: ");
scanf("%d", &key);
del(&root, key);
break;
case 4:
if (root == NULL) {
printf("Tree is not created.\n");
} else {
printf("Inorder traversal: ");
inorder(root);
printf("\n");
}
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 5);
return 0;
}
node *getnode() {
node *temp = (node *)malloc(sizeof(node));
temp->left = NULL;
temp->right = NULL;
return temp;
}
void insert(node **root, node *newNode) {
if (*root == NULL) {
*root = newNode;
} else if (newNode->data < (*root)->data) {
insert(&(*root)->left, newNode);
} else {
insert(&(*root)->right, newNode);
}
}
node *search(node *root, int key, node **parent) {
node *temp = root;
*parent = NULL;
while (temp != NULL) {
if (temp->data == key) {
return temp;
}
*parent = temp;
if (key < temp->data) {
temp = temp->left;
} else {
temp = temp->right;
}
}
return NULL;
}
void del(node **root, int key) {
node *temp, *parent;
temp = search(*root, key, &parent);
if (temp == NULL) {
printf("Element %d not found.\n", key);
return;
}
if (temp->left == NULL && temp->right == NULL) {
if (parent == NULL) {
*root = NULL; // Tree becomes empty
} else if (parent->left == temp) {
parent->left = NULL;
} else {
parent->right = NULL;
}
free(temp);
}
// Case 2: One child
else if (temp->left == NULL || temp->right == NULL) {
node *child = (temp->left != NULL) ? temp->left : temp->right;
if (parent == NULL) {
*root = child; // Tree becomes root's child
} else if (parent->left == temp) {
parent->left = child;
} else {
parent->right = child;
}
free(temp);
}
else {
node *succParent = temp;
node *succ = temp->right;
while (succ->left != NULL) {
succParent = succ;
succ = succ->left;
}
temp->data = succ->data;
if (succParent->left == succ) {
succParent->left = succ->right;
} else {
succParent->right = succ->right;
}
free(succ);
}
printf("Element %d deleted.\n", key);
}
void inorder(node *temp) {
if (temp != NULL) {
inorder(temp->left);
printf("%d ", temp->data);
inorder(temp->right);
}
}
PROGRAM :
#include <stdio.h>
#include <stdlib.h>
typedef enum { FALSE, TRUE } bool;
struct node {
int info;
int balance;
struct node* lchild;
struct node* rchild;
};
struct node* insert(int info, struct node* pptr, bool* ht_inc);
struct node* search(struct node* ptr, int info);
void display(struct node* ptr, int level);
void inorder(struct node* ptr);
void main() {
bool ht_inc;
int info;
int choice;
struct node* root = NULL;
clrscr();
printf("1. Insert\n");
printf("2. Display\n");
printf("3. Exit\n");
while (1) {
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to be inserted: ");
scanf("%d", &info);
if (search(root, info) == NULL) {
root = insert(info, root, &ht_inc);
} else {
printf("Duplicate value ignored\n");
}
break;
case 2:
if (root == NULL) {
printf("Tree is empty\n");
} else {
printf("Tree is: \n");
display(root, 1);
printf("\nInorder traversal is: ");
inorder(root);
printf("\n");
}
break;
case 3:
exit(0);
default:
printf("Wrong choice\n");
}
}
}
struct node* search(struct node* ptr, int info) {
if (ptr != NULL) {
if (info < ptr->info) {
return search(ptr->lchild, info);
} else if (info > ptr->info) {
return search(ptr->rchild, info);
} else {
return ptr;
}
}
return NULL;
}
struct node* insert(int info, struct node* pptr, bool* ht_inc) {
struct node* aptr;
struct node* bptr;
if (pptr == NULL) {
pptr = (struct node*)malloc(sizeof(struct node));
pptr->info = info;
pptr->lchild = NULL;
pptr->rchild = NULL;
pptr->balance = 0;
*ht_inc = TRUE;
return pptr;
}
if (info < pptr->info) {
pptr->lchild = insert(info, pptr->lchild, ht_inc);
if (*ht_inc == TRUE) {
switch (pptr->balance) {
case -1:
pptr->balance = 0;
*ht_inc = FALSE;
break;
case 0:
pptr->balance = 1;
break;
case 1:
aptr = pptr->lchild;
if (aptr->balance == 1) {
printf("Left to left rotation\n");
pptr->lchild = aptr->rchild;
aptr->rchild = pptr;
pptr->balance = 0;
pptr = aptr;
} else {
printf("Left to right rotation\n");
bptr = aptr->rchild;
aptr->rchild = bptr->lchild;
bptr->lchild = aptr;
pptr->lchild = bptr->rchild;
bptr->rchild = pptr;
if (bptr->balance == 1) {
pptr->balance = -1;
aptr->balance = 0;
} else if (bptr->balance == 0) {
pptr->balance = 0;
aptr->balance = 0;
} else {
pptr->balance = 0;
aptr->balance = 1;
}
pptr = bptr;
}
pptr->balance = 0;
*ht_inc = FALSE;
break;
}
}
} else if (info > pptr->info) {
pptr->rchild = insert(info, pptr->rchild, ht_inc);
if (*ht_inc == TRUE) {
switch (pptr->balance) {
case 1:
pptr->balance = 0;
*ht_inc = FALSE;
break;
case 0:
pptr->balance = -1;
break;
case -1:
aptr = pptr->rchild;
if (aptr->balance == -1)
{
printf("Right to right rotation\n");
pptr->rchild = aptr->lchild;
aptr->lchild = pptr;
pptr->balance = 0;
pptr = aptr;
} else {
printf("Right to left rotation\n");
bptr = aptr->lchild;
aptr->lchild = bptr->rchild;
bptr->rchild = aptr;
pptr->rchild = bptr->lchild;
bptr->lchild = pptr;

if (bptr->balance == -1) {
pptr->balance = 1;
aptr->balance = 0;
} else if (bptr->balance == 0) {
pptr->balance = 0;
aptr->balance = 0;
} else {
pptr->balance = 0;
aptr->balance = -1;
}
pptr = bptr;
}
pptr->balance = 0;
*ht_inc = FALSE;
break;
}
}
}
return pptr;
}
void display(struct node* ptr, int level)
{
int i;
if (ptr != NULL) {
display(ptr->rchild, level + 1);
printf("\n");
for (i = 0; i < level; i++) printf(" ");
printf("%d", ptr->info);
display(ptr->lchild, level + 1);
}
}
void inorder(struct node* ptr)
{
if (ptr != NULL) {
inorder(ptr->lchild);
printf("%d ", ptr->info);
inorder(ptr->rchild);
}
}
PROGRAM :
#include <stdio.h>
void main() {
int array[100], search, c, number,found;
clrscr();
printf("Enter the number of elements in array: ");
scanf("%d", &number);
printf("Enter %d numbers:\n", number);
for (c = 0; c < number; c++) {
scanf("%d", &array[c]);
}
printf("Enter the number to search: ");
scanf("%d", &search);
found = 0;
for (c = 0; c < number; c++) {
if (array[c] == search) {
printf("%d is present at location %d\n", search, c + 1);
found = 1;
break;
}
}
if (!found) {
printf("%d is not present in array\n", search);
}
getch();
}
PROGRAM :
#include <stdio.h>
void main() {
int n, c, first, last, middle, search, array[100];
clrscr();
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d integers:\n", n);
for (c = 0; c < n; c++) {
scanf("%d", &array[c]);
}
printf("Enter value to find: ");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first + last) / 2;
while (first <= last) {
if (array[middle] < search) {
first = middle + 1;
} else if (array[middle] == search) {
printf("%d found at location %d\n", search, middle + 1);
break;
} else {
last = middle - 1;
}
middle = (first + last) / 2;
}
if (first > last) {
printf("Not found! %d is not present in the list\n", search);
}
getch();
}
PROGRAM :
#include <stdio.h>
int* insertionSort(int array[], int n) {
int i, j, temp;
for (i = 1; i < n; i++) {
temp = array[i];
j = i - 1;
while (j >= 0 && array[j] > temp) {
array[j + 1] = array[j];
j--;
}
array[j + 1] = temp;
}
return array;
}
void main() {
int array[1000], n, i;
clrscr();
printf("Enter the number of elements you want to sort: ");
scanf("%d", &n);
printf("Enter elements in the list:\n");
for (i = 0; i < n; i++) {
scanf("%d", &array[i]);
}
insertionSort(array, n);
printf("Sorted array:\n");
for (i = 0; i < n; i++) {
printf("%d\t", array[i]);
}
printf("\n");
getch();
}
PROGRAM :
#include <stdio.h>
void selectionSort(int array[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
int min_idx = i;
for (j = i + 1; j < n; j++) {
if (array[j] < array[min_idx]) {
min_idx = j;
}
}
temp = array[min_idx];
array[min_idx] = array[i];
array[i] = temp;
}
}
void main() {
int array[1000], n,i;
clrscr();
printf("Enter the number of elements you want to sort: ");
scanf("%d", &n);

printf("Enter elements in the list: ");


for ( i = 0; i < n; i++) {
scanf("%d", &array[i]);
}
selectionSort(array, n);
printf("Sorted list: ");
for ( i = 0; i < n; i++) {
printf("%d\t", array[i]);
}
getch();
}
PROGRAM :
#include <stdio.h>
#include <math.h>
#define MAX 100
void swap(int *p, int *q);
void display(int a[], int n);
void insert(int a[], int *n, int data);
int del_hi_priori(int a[], int *n);
int parent(int i);
int left(int i);
int right(int i);
int main() {
int choice, num, n = 0, a[MAX], data, s;
while (1) {
printf(".....MAIN MENU.....\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Quit\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to be inserted: ");
scanf("%d", &data);
insert(a, &n, data);
break;
case 2:
s = del_hi_priori(a, &n);
if (s != 0)
printf("The deleted value is: %d\n", s);
break;
case 3:
display(a, n);
break;
case 4:
return 0;
default:
printf("Invalid choice\n");
}
}
}
void insert(int a[], int *n, int data)
{
int i;
if (*n >= MAX) {
printf("Queue is Full\n");
return;
}
i = (*n)++;
a[i] = data;
while (i != 0 && a[parent(i)] < a[i]) {
swap(&a[i], &a[parent(i)]);
i = parent(i);
}
}
int del_hi_priori(int a[], int *n) {
int root,i;
if (*n <= 0) {
printf("Queue is Empty!\n");
return 0;
}
root = a[0];
a[0] = a[--(*n)];
i = 0;
while (1) {
int l = left(i);
int r = right(i);
int largest = i;
if (l < *n && a[l] > a[largest])
largest = l;
if (r < *n && a[r] > a[largest])
largest = r;
if (largest == i)
break;
swap(&a[i], &a[largest]);
i = largest;
}
return root;
}
int parent(int i) {
return (i - 1) / 2;
}
int left(int i) {
return 2 * i + 1;
}
int right(int i) {
return 2 * i + 2;
}
void display(int a[], int n) {
int i;
if (n == 0) {
printf("Queue is Empty!\n");
return;
}
for ( i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
}
void swap(int *p, int *q) {
int temp = *p;
*p = *q;
*q = temp;
}
PROGRAM :
#include <limits.h>
#include <stdio.h>
#define bool int
#define true 1
#define false 0
#define V 9
int minDistance(int dist[],bool sptSet[]) {
int min = INT_MAX, min_index,v;
for (v = 0; v < V; v++)
if (!sptSet[v] && dist[v] <= min) {
min = dist[v];
min_index = v;
}
return min_index;
}
void printSolution(int dist[]) {
int i;
printf("Vertex\t\tDistance from Source\n");
for ( i = 0; i < V; i++)
printf("%d\t\t%d\n", i, dist[i]);
}
void dijkstra(int graph[V][V], int src) {
int dist[V];
bool sptSet[V];
int i,count,v;
for (i = 0; i < V; i++) {
dist[i] = INT_MAX;
sptSet[i] = false;
}
dist[src] = 0;
for ( count = 0; count < V - 1; count++) {
int u = minDistance(dist, sptSet);
sptSet[u] = true;
for ( v = 0; v < V; v++)
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
printSolution(dist);
}
void main() {
int graph[V][V] = {
{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 0, 10, 0, 2, 0, 0},
{0, 0, 0, 14, 10, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0}
};
clrscr();
dijkstra(graph, 0);
getch();
}
PROGRAM :
#include <limits.h>
#include <stdio.h>
#define bool int
#define true 1
#define false 0
#define V 5
int minKey(int key[], bool mstSet[]) {
int min = INT_MAX, min_index,v;
for ( v = 0; v < V; v++)
if (!mstSet[v] && key[v] < min) {
min = key[v];
min_index = v;
}
return min_index;
}
void printMST(int parent[], int graph[V][V]) {
int i;
printf("Edge \tWeight\n");
for ( i = 1; i < V; i++)
printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]);
}
void primMST(int graph[V][V]) {
int parent[V];
int key[V];
bool mstSet[V];
int i,count,v;
for (i = 0; i < V; i++) {
key[i] = INT_MAX;
mstSet[i] = false;
}
key[0] = 0;
parent[0] = -1;
for ( count = 0; count < V - 1; count++) {
int u = minKey(key, mstSet);
mstSet[u] = true;
for ( v = 0; v < V; v++)
if (graph[u][v] && !mstSet[v] && graph[u][v] < key[v]) {
parent[v] = u;
key[v] = graph[u][v];
}
}
printMST(parent, graph);
}
void main() {
int graph[V][V] = {
{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}
};
clrscr();
primMST(graph);
getch();
}
PROGRAM :
#include <stdio.h>
#include <stdlib.h>
void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int i,j,k;
int L[10];
int R[10];
for ( i = 0; i < n1; i++)
L[i] = arr[l + i];
for ( j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
void printArray(int A[], int size) {
int i;
for (i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
void main() {
int n,i,arr[20];
clrscr();
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter %d elements of the array:\n", n);
for ( i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Given array is: \n");
printArray(arr, n);
mergeSort(arr, 0, n - 1);
printf("Sorted array is: \n");
printArray(arr, n);
getch();
}
PROGRAM :
#include <stdio.h>
#include <stdlib.h>
#define TABLE_SIZE 10
int h[TABLE_SIZE] = {NULL};
void insert() {
int key, index, i, flag = 0, hkey;
printf("\nEnter a value to insert into the hash table: ");
scanf("%d", &key);
hkey = key % TABLE_SIZE;
for (i = 0; i < TABLE_SIZE; i++) {
index = (hkey + i) % TABLE_SIZE;
if (h[index] == NULL) {
h[index] = key;
break;
}
}
if (i == TABLE_SIZE) {
printf("\nElement cannot be inserted\n");
}
}
void search() {
int key, index, i, flag = 0, hkey;
printf("\nEnter the element to search: ");
scanf("%d", &key);
hkey = key % TABLE_SIZE;
for (i = 0; i < TABLE_SIZE; i++) {
index = (hkey + i) % TABLE_SIZE;
if (h[index] == key) {
printf("Value is found at index %d\n", index);
break;
}
}
if (i == TABLE_SIZE) {
printf("\nValue is not found\n");
}
}
void display() {
int i;
printf("\nElements in the hash table are:\n");
for (i = 0; i < TABLE_SIZE; i++) {
printf("At index %d: value = %d\n", i, h[i]);
}
}
void main() {
int opt;
clrscr();
while (1) {
printf("\nPress 1. Insert\t2. Display\t3. Search\t4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &opt);
switch (opt) {
case 1:
insert();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:
exit(0);
default:
printf("Invalid option! Please try again.\n");
}
}
getch();
}
PROGRAM :
#include <stdio.h>
#include <stdlib.h>
#define TABLE_SIZE 10
int h[TABLE_SIZE] = {NULL};
void insert() {
int key, index, i, hkey;
printf("\nEnter the number to insert in the hash table: ");
scanf("%d", &key);
hkey = key % TABLE_SIZE;
for (i = 0; i < TABLE_SIZE; i++) {
index = (hkey + i * i) % TABLE_SIZE;
if (h[index] == NULL) {
h[index] = key;
break;
}
}
if (i == TABLE_SIZE) {
printf("\nElement cannot be inserted\n");
}
}
void search() {
int key, index, i, hkey;
printf("\nEnter the element to search: ");
scanf("%d", &key);
hkey = key % TABLE_SIZE;
for (i = 0; i < TABLE_SIZE; i++) {
index = (hkey + i * i) % TABLE_SIZE;
if (h[index] == key) {
printf("Value is found at index %d\n", index);
return;
}
}
if (i == TABLE_SIZE) {
printf("\nValue is not found\n");
}
}
void display() {
printf("\nElements in the hash table are:\n");
for (int i = 0; i < TABLE_SIZE; i++) {
printf("At index %d: value = %d\n", i, h[i]);
}
}
void main() {
int opt,i;
clrscr();
while (1) {
printf("\nPress 1. Insert\t2. Display\t3. Search\t4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &opt);

switch (opt) {
case 1:
insert();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:
exit(0);
default:
printf("Invalid option! Please try again.\n");
}
}
getch();
}

You might also like