Sei sulla pagina 1di 61

INDEX

S.NO PROGRAM SIGNATURE


1 (a) Program to implement arrays all operations of arrays.
(b) Program to implement linear search.
(c) Program to implement binary search.

2 Program to implement stacks.

3 (a) Program to implement queues.


(b) Program to implement infix to postfix expression.
(c) Program to evaluate postfix expression.

4 (a) Program to implement a Queue using an Array


(b) Program to implement Dequeue.

5 Program to implement all operations of Linked List.

6 Program to implement all operations of doubly Linked List.

7 (a) Program to implement stacks using linked list.


(b) Program to implement queues using linked list.

8 Program to demonstrate insert and display operation in binary search tree

9 (a)Program to implement Bubble Sort.


(b)Program to implement Selection sort.
(c)Program to implement Insertion sort.
(d)Program to implement Merge sort.
(e)Program to implement Quick sort.

10 Program to implement String concatenation, compare, and Substring


PROGRAM : 1(A)

AIM : To implement arrays all operations of arrays

#include<stdio.h>
int insert(int ar[],int n, int num);
void display(int ar[],int n);
void sorting(int ar[],int n);
void max(int ar[],int n);
void min(int ar[],int n);
int uar[1000];
int f=0;
int n;
int main()
{ int u;
int ar[1000];
char ch='y';
printf("Pls enter the array size u want\n");
scanf("%d",&n);
while(ch=='y'){
printf("option one is compulsory\n");
printf("MENU\n1.ARRAY
CREATION\n2.INSERTION\n3.SORTING\n4.MAX\n5.MIN\n6.DISPLAY\n");
scanf("%d",&u);
switch (u) {
case 1:
{
int i;
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
}
break;
case 2:{int num;
printf("Enter the value to be inserted\n");
scanf("%d",&num);
n=insert(ar,n,num);
}break;
case 3:{
sorting(ar, n);
}break;
case 4:{max(ar, n);}break;
case 5:{min(ar,n);}break;

case 6:{display(ar, n);}break;


default:{printf("pls enter the correct option\n");}
break;
}
printf("Do you want to continue\n");
scanf("%s",&ch);
}
return 0;
}
int insert(int ar[],int n, int num){
int position;
int c;
int h;
if(f==1)
{
printf("Do u want the sorted array or the unsorted(1 or 2)");
scanf("%d",&h);
switch(h)
{
case 1:{printf("Enter the location where you wish to insert an
element\n");
scanf("%d", &position);

for (c = n - 1; c >= position - 1; c--)


{ar[c+1] = ar[c];}

ar[position-1] = num;
n++;
printf("Resultant array is\n");
display(ar, n);}
break;
case 2:{
printf("Enter the location where you wish to insert an
element\n");
scanf("%d", &position);

for (c = n - 1; c >= position - 1; c--)


{uar[c+1] = uar[c];}

uar[position-1] = num;
n++;
printf("Resultant array is\n");
display(uar, n);

}break;
default:{printf("pls choose from one or two\n");
}break;
}

}
else{
printf("Enter the location where you wish to insert an element\n");
scanf("%d", &position);

for (c = n - 1; c >= position - 1; c--)


{ar[c+1] = ar[c];}

ar[position-1] = num;
n++;
printf("Resultant array is\n");
display(ar, n);
}
return n;
}

void display(int ar[],int n)


{ int c;
int h;
if(f==1)
{
printf("Do u want the sorted array or the unsorted(1 or 2)");
scanf("%d",&h);
switch(h)
{
case 1:{for (c = 0; c < n; c++)
printf("%d\n", ar[c]);}
break;
case 2:{for (c = 0; c < n; c++)
printf("%d\n", uar[c]);
}break;
default:{printf("pls choose from one or two\n");
}break;
}
}
else
{for (c = 0; c < n; c++)
printf("%d\n", ar[c]);}
}
void sorting(int ar[],int n)
{ int i,j;
int key;
if(f==0)
{for(i=0;i<n;i++)
{
uar[i]=ar[i];
}
printf("Now ther are two arrays\n");
f = 1;
}
for(i=1;i<n;i++)
{key= ar[i];
j=i-1;
while(j>=0 && ar[j]>key)
{
ar[j+1]=ar[j];
j=j-1;
}
ar[j+1]=key;
}

display(ar, n);//display sorted array


}
void max(int ar[],int n)
{
sorting(ar, n);
printf("The max term is %d\n",ar[n-1]);
}
void min (int ar[],int n)
{
sorting(ar,n);
printf("The min term is %d \n",ar[0]);
}

OUTPUT :
PROGRAM : 1(B)

AIM : Program to implement binary search.

#include <stdio.h>

int main()
{
int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");


scanf("%d",&n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d",&array[c]);

printf("Enter value to find\n");


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 isn't present in the list.\n", search);

return 0;
}
OUTPUT:

PROGRAM : 1(C)

AIM : Program to implement linear search.

#include <stdio.h>

int main()
{
int array[100], search, c, n;

printf("Enter number of elements in array\n");


scanf("%d", &n);

printf("Enter %d integer(s)\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

printf("Enter a number to search\n");


scanf("%d", &search);

for (c = 0; c < n; c++)


{
if (array[c] == search) /* If required element is found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.\n", search);

return 0;
}

OUTPUT :

PROGRAM : 2

AIM : Program to implement stacks.

#include <stdio.h>
#define max 10
int stack[max],top=-1,item;
void push(void);
void pop(void);
void display(void);

int main(void)
{

int choice;
printf("menu");
printf("\n1-push");
printf("\n2-pop");
printf("\n3-display");
do{
printf("\nenter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:printf("exit");
break;

}while(choice!=4);

return 0;
}

void push()
{
if(top>max)
{
printf("overflow");
}
else
{
printf("enter the no you want to be pushed to stack : ");
scanf("%d",&item);
top++;
stack[top]=item;

void pop()
{
if(top<=-1)
{
printf("\nstack underflow");
}
else
{
printf("\nthe popped elemnt is %d ",stack[top]);
top--;

void display()
{
for(int i=top;i>=0;i--)
printf("%d ",stack[i]);
}

OUTPUT :

PROGRAM : 3(A)

AIM : Program to implement queues.

#include <stdio.h>
void toh(int ,char ,char ,char );
int main(void)
{
int n;
char a='A';
char b='B';
char c='C';
printf("Enter the number of hoops in one tower\n");
scanf("%d",&n);
toh(n,a,b,c);
return 0;
}
void toh(int n, char a, char b, char c)
{
if(n==1)
{
printf("%c->%c\n",a,c);
return;
}
toh(n-1,a,c,b);//transfer n-1 elements to last
toh(1,a,b,c);
toh(n-1,b,a,c);
}
OUTPUT :

PROGRAM : 3(B)

AIM : Program to implement infix to postfix expression.

#define SIZE 50 /* Size of Stack */


#include <ctype.h>
char s[SIZE];
int top=-1; /* Global declarations */

push(char elem)
{ /* Function for PUSH operation */
s[++top]=elem;
}

char pop()
{ /* Function for POP operation */
return(s[top--]);
}

int pr(char elem)


{ /* Function for precedence */
switch(elem)
{
case '#': return 0;
case '(': return 1;
case '+':
case '-': return 2;
case '*':
case '/': return 3;
}
}

int main()
{ /* Main Program */
char infx[50],pofx[50],ch,elem;
int i=0,k=0;
printf("\n\nRead the Infix Expression - ");
scanf("%s",infx);
push('#');
while( (ch=infx[i++]) != '\0')
{
if( ch == '(') push(ch);
else
if(isalnum(ch)) pofx[k++]=ch;
else
if( ch == ')')
{
while( s[top] != '(')
pofx[k++]=pop();
elem=pop(); /* Remove ( */
}
else
{ /* Operator */
while( pr(s[top]) >= pr(ch) )
pofx[k++]=pop();
push(ch);
}
}
while( s[top] != '#') /* Pop from stack till empty */
pofx[k++]=pop();
pofx[k]='\0'; /* Make pofx as valid string */
printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n",infx,pofx);
return 0;
}

OUTPUT :

PROGRAM : 3(C)
AIM : Program to evaluate postfix expression.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define max 100
char ex[max];
int stk[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int top=-1;
int n;
void push(int);
int pop(void);
int main() {int i,num;
int result=0;
int a,b;
printf("Enter the expression length\n");
scanf("%d",&n);
printf("Enter the exp you want to convert\n");
for (i=0; i<n; i++) {
scanf("%c",&ex[i]);
}
strcat(ex, ")");
i=0;
while (ex[i]!=')') {
if (isdigit(ex[i])) {
num = ex[i] -'0';
push(num);
}
else if (ex[i]=='+'||ex[i]=='-
'||ex[i]=='/'||ex[i]=='*'||ex[i]=='^')
{
a=pop();
b=pop();
if (ex[i]=='+') {
result= b+a;
printf("result= %d\n",result); }
else if (ex[i]=='-') {
result=b-a;
printf("result= %d\n",result); }
else if (ex[i]=='/')
{
result =b/a;
printf("result= %d\n",result); }
else if (ex[i]=='*')
{
result= b*a;
printf("result= %d\n",result); }
if(ex[i]=='^')
{
result= pow(b,a);
printf("pow");}
push(result);
}
i+=1;
}
printf("result= %d\n",result);
return 0;
}
void push(int num)
{ if(n==max)
{printf("overflow\n");}
top+=1;
stk[top]=num;
printf("push ok\n");
}
int pop(void)
{
int p=0;
p=stk[top];
printf("pop%d\n",stk[top]);
top-=1;
return p;
}

OUTPUT :
PROGRAM : 4(A)

AIM : Program to implement a Queue using an Array.

#include <stdio.h>

#define MAX 50

void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
} /* End of switch */
} /* End of while */
} /* End of main() */

void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /* End of insert() */

void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
} /* End of delete() */

void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
}

OUTPUT :

PROGRAM : 4(B)

AIM : Program to implement a Dequeue.


#include<stdio.h>
#define MAX 10

int deque[MAX];
int left=-1, right=-1;

void insert_right(void);
void insert_left(void);
void delete_right(void);
void delete_left(void);
void display(void);

int main()
{
int choice;
do
{
printf("\n1.Insert at right ");
printf("\n2.Insert at left ");
printf("\n3.Delete from right ");
printf("\n4.Delete from left ");
printf("\n5.Display ");
printf("\n6.Exit");
printf("\n\nEnter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert_right();
break;
case 2:
insert_left();
break;
case 3:
delete_right();
break;
case 4:
delete_left();
break;
case 5:
display();
break;
}
}while(choice!=6);
return 0;
}
//-------INSERT AT RIGHT-------
void insert_right()
{
int val;
printf("\nEnter the value to be added ");
scanf("%d",&val);
if( (left==0 && right==MAX-1) || (left==right+1) )
{
printf("\nOVERFLOW");
}
if(left==-1) //if queue is initially empty
{
left=0;
right=0;
}
else
{
if(right==MAX-1)
right=0;
else
right=right+1;
}
deque[right]=val;
}

//-------INSERT AT LEFT-------
void insert_left()
{
int val;
printf("\nEnter the value to be added ");
scanf("%d",&val);
if( (left==0 && right==MAX-1) || (left==right+1) )
{
printf("\nOVERFLOW");
}
if(left==-1) //if queue is initially empty
{
left=0;
right=0;
}
else
{
if(left==0)
left=MAX-1;
else
left=left-1;
}
deque[left]=val;
}

//-------DELETE FROM RIGHT-------


void delete_right()
{
if(left==-1)
{
printf("\nUNDERFLOW");
return;
}
printf("\nThe deleted element is %d\n", deque[right]);
if(left==right) //Queue has only one element
{
left=-1;
right=-1;
}
else
{
if(right==0)
right=MAX-1;
else
right=right-1;
}
}

//-------DELETE FROM LEFT-------


void delete_left()
{
if(left==-1)
{
printf("\nUNDERFLOW");
return;
}
printf("\nThe deleted element is %d\n", deque[left]);
if(left==right) //Queue has only one element
{
left=-1;
right=-1;
}
else
{
if(left==MAX-1)
left=0;
else
left=left+1;
}
}
//-------DISPLAY-------
void display()
{
int front=left, rear=right;
if(front==-1)
{
printf("\nQueue is Empty\n");
return;
}
printf("\nThe elements in the queue are: ");
if(front<=rear)
{
while(front<=rear)
{
printf("%d\t",deque[front]);
front++;
}
}
else
{
while(front<=MAX-1)
{
printf("%d\t",deque[front]);
front++;
}
front=0;
while(front<=rear)
{
printf("%d\t",deque[front]);
front++;
}
}
printf("\n");
}

OUTPUT :
PROGRAM : 5

AIM : Program to implement all operations of Linked List.

#include <stdio.h>
#include <stdlib.h>

void append();
int length();
void display();
void reverse();
void insbeg();
void deletebeg();
void deletepos();
void insertpos();
void deleteend();

struct node
{
int data;
struct node*link;
};

struct node*root=NULL;
int len;

int main()
{
int ch;char ans='y';
do
{
printf("\nmenu");
printf("\n1.append\n2.find length\n3.display\n4.reverse\n5.insert at
beg\n6.delete at beg\n7.delete from specified loc;\n8.insert at specified
position\n9.delete from end");
printf("\nenter your choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:append();
break;
case 2:len=length();
printf("the length of the linked list is = %d",len);
break;
case 3:display();
break;
case 4:reverse();
break;
case 5:insbeg();
break;
case 6:deletebeg();
break;
case 7:deletepos();
break;
case 8:insertpos();
break;
case 9:deleteend();
}
printf("\nwant to enter more? ");
scanf("%s",&ans);
}while(ans=='y');

return 0;
}

void append()
{
struct node* temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("enter the data in the node ");
scanf("%d",&temp->data);
temp->link=NULL;
if(root==NULL)
{
root=temp;
}
else
{
struct node* p;
p=root;
while(p->link!=NULL)
{
p=p->link;
}
p->link=temp;
}

int length()
{
int count=0;
struct node* temp;
temp=root;
while(temp!=NULL)
{
temp=temp->link;
count++;
}
return count;
}

void display()
{
struct node* temp;
temp=root;
if(temp==NULL)
{
printf("list is empty");
}
else
{
while(temp!=NULL)
{
printf("%d-->",temp->data);
temp=temp->link;
}
printf("\n\n");
}

void reverse()
{
int i,j,temp;
len=length();
i=0;j=len-1;
struct node*p,*q;
p=q=root;
while(i<j)
{
int k=0;
while(k<j)
{
q=q->link;
k++;
}
temp=p->data;
p->data=q->data;
q->data=temp;
i++;
p=p->link;
j--;

q=root;
}
display();

void insbeg()
{
struct node*temp;
temp=(struct node*)malloc(sizeof(struct node));

if(temp==NULL)
{
printf("out of memory");
}
printf("enter the data in node ");
scanf("%d",&temp->data);
temp->link=root;
root=temp;

void deletebeg()
{
struct node* temp;
temp=root;
root=temp->link;
temp->link=NULL;
free(temp);

void deletepos()
{
int loc;
printf("enter the location you want to delete the node from");
scanf("%d",&loc);
struct node* p=root,*q;
int i=1;
while(i<loc-1)
{
p=p->link;
i++;
}

q=p->link;
p->link=q->link;
q->link=NULL;
free(q);

void insertpos()
{
int loc;
printf("enter the pos where you want to insert the node ");
scanf("%d",&loc);
struct node *temp,*p=root;
temp=(struct node*)malloc(sizeof(struct node));
printf("\nenter the data in the node you want to insert ");
scanf("%d-->",&temp->data);
temp->link=NULL;
int i=1;
while(i<loc)
{
p=p->link;
i++;
}
temp->link=p->link;
p->link=temp;

void deleteend()
{
struct node* temp;struct node * secLast;
temp=root;
while(temp->link!=NULL)
{
secLast = temp;
temp=temp->link;
}
secLast->link=NULL;
free(temp);
}

OUTPUT
PROGRAM : 6

AIM : Program to implement all operations of doubly Linked List.

#include<stdio.h>
#include<stdlib.h>
struct Node;
typedef struct Node * PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;

struct Node
{
int e;
Position previous;
Position next;
};

void Insert(int x, List l, Position p)


{
Position TmpCell;
TmpCell = (struct Node*) malloc(sizeof(struct Node));
if(TmpCell == NULL)
printf("Memory out of space\n");
else
{
TmpCell->e = x;
TmpCell->previous = p;
TmpCell->next = p->next;
p->next = TmpCell;
}
}

int isLast(Position p)
{
return (p->next == NULL);
}

Position Find(int x, List l)


{
Position p = l->next;
while(p != NULL && p->e != x)
p = p->next;
return p;
}

void Delete(int x, List l)


{
Position p, p1, p2;
p = Find(x, l);
if(p != NULL)
{
p1 = p -> previous;
p2 = p -> next;
p1 -> next = p -> next;
if(p2 != NULL) // if the node is not the last node
p2 -> previous = p -> previous;
}
else
printf("Element does not exist!!!\n");
}

void Display(List l)
{
printf("The list element are :: ");
Position p = l->next;
while(p != NULL)
{
printf("%d -> ", p->e);
p = p->next;
}
}

void main()
{
int x, pos, ch, i;
List l, l1;
l = (struct Node *) malloc(sizeof(struct Node));
l->previous = NULL;
l->next = NULL;
List p = l;
printf("DOUBLY LINKED LIST IMPLEMENTATION OF LIST ADT\n\n");
do
{
printf("\n\n1. INSERT\t 2. DELETE\t 3. FIND\t 4. PRINT\t 5.
QUIT\n\nEnter the choice :: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
p = l;
printf("Enter the element to be inserted :: ");
scanf("%d",&x);
printf("Enter the position of the element :: ");
scanf("%d",&pos);
for(i = 1; i < pos; i++)
{
p = p->next;
}
Insert(x,l,p);
break;

case 2:
p = l;
printf("Enter the element to be deleted :: ");
scanf("%d",&x);
Delete(x,p);
break;

case 3:
p = l;
printf("Enter the element to be searched :: ");
scanf("%d",&x);
p = Find(x,p);
if(p == NULL)
printf("Element does not exist!!!\n");
else
printf("Element exist!!!\n");
break;

case 4:
Display(l);
break;
}
}
while(ch<5);
}

OUTPUT :
PROGRAM : 7(A)

AIM : Program to implement stacks using linked list.

#include<stdio.h>
void push();
void pop();
void display();
struct node
{
int data;
struct node *link;
};
struct node*top=NULL;
int main()
{
int choice;char ans='y';
do
{

printf("menu\n1.push\n2.pop\n3.display");
printf("\nenter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
}
printf("\ndo you want to enter more ");
scanf("%s",&ans);

}
while(ans=='y');
}

void push()
{
struct node * temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("enter the data in the node ");
scanf("%d",&temp->data);
temp->link=top;
top=temp;
}

void pop()
{
struct node* temp;
if(top==NULL)
{
printf("the list is empty");
}
else
{
temp=top;
printf("the node deleted is %d ",temp->data);
top=temp->link;
temp->link=NULL;
free(temp);
}

void display()
{
struct node *temp;
temp=top;
if(temp==NULL)
{
printf("\nempty");
}
while(temp!=NULL)
{
printf("\n%d<--",temp->data);
temp=temp->link;
}

OUTPUT

PROGRAM : 7(B)
AIM : Program to implement queues using linked list.

#include <stdio.h>
#include<stdlib.h>
void insert(void);
void delete(void);
void disp(void);
struct que {
int info;
struct que *new;
};
struct que * front=NULL;
struct que * temp=NULL;
struct que * rear=NULL;
//struct que * news;
int d;
int main() {
int c=0;
char ans='y';
while(ans=='y'){
printf(" 1.insert\n 2.Delete\n 3.Display\n 4.Exit\n");
scanf("%d",&c);
switch (c) {
case 1:
{insert();}
break;
case 2:{delete();}break;
case 3:{disp();}break;
default:
break;
}
printf("Do you want to continue\n");
scanf("%s", &ans);
}
return 0;
}
void insert(void)
{
struct que* news=NULL;
news = (struct que*) malloc(sizeof(struct que));
printf("Enter the info u want to add\n");
scanf("%d",&d);
news->info=d;
news->new=NULL;
if(rear==NULL&&front==NULL)
{ front=rear=news;
}
else
{ rear->new=news;
rear=news;
}

}
void delete()
{
if(front==NULL)
{printf("The que is empty\n");}
if(front!=NULL)
{
struct que *temp=front;
free(temp);
front=front->new;
printf("The first element has been deleted\n");

}
}
void disp()
{
if(front==NULL)
{printf("EMPTY");}
else
{ temp=front;
while(temp!=NULL)
{
printf("%d\n",temp->info);
temp= temp->new;
}
}
}

OUTPUT
#include<stdio.h>
void pop();
void push();
void display();
struct node
{
int data;
struct node *link;
};
struct node*front=NULL;
struct node*rear=NULL;

int main()
{
int choice;char ans='y';
do
{

printf("menu\n1.push\n2.pop\n3.display");
printf("\nenter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
}
printf("\ndo you want to enter more ");
scanf("%s",&ans);

}
while(ans=='y');

void push()
{

struct node* temp;


temp=(struct node*)malloc(sizeof(struct node));
printf("enter the data ");
scanf("%d",&temp->data);
temp->link=NULL;
if(front==NULL)
{
front=rear=temp;
}
else
{
rear->link=temp;
rear=temp;
}

void pop()
{
if(front==NULL)
{
printf("the queue is empty");
}
else
{
printf("the element popped is %d ",front->data);
front=front->link;

void display()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else
{
struct node* temp;
temp=front;
while(temp!=NULL)
{
printf("%d-->",temp->data);
temp=temp->link;
}

PROGRAM : 8

AIM : Program to demonstrate insert and display operations in binary search tree.

#include <stdio.h>
#include<stdlib.h>

void insert();

struct node
{
int data;
struct node * left;
struct node * right;
};
struct node * root=NULL;

void inorder(struct node * root)


{
if(root->left!=NULL)
{
inorder(root->left);
}
printf("%d ",root->data);
if(root->right!=NULL)
{
inorder(root->right);
}

void postorder(struct node * root)


{
if(root->left!=NULL)
{
postorder(root->left);
}
if(root->right!=NULL)
{
postorder(root->right);
}
printf("%d",root->data);
}

int main()
{
int choice;char ans='y';
do
{

printf("menu\n1.insert\n2.inorder traversal\n3.postorder traversal ");


printf("\nenter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:insert();
break;
case 2:inorder(root);
break;
case 3:postorder(root);
break;

}
printf("\ndo you want to enter more ");
scanf("%s",&ans);

}
while(ans=='y');

void insert()
{
struct node * temp;struct node *p;
temp=(struct node*)malloc(sizeof(struct node));
printf("enter the value ");
scanf("%d",&temp->data);
temp->right=NULL;
temp->left=NULL;
p=root;
if(root==NULL)
{
root=temp;
}
else
{
struct node * curr;
curr=root;
while(curr)
{
p=curr;
if(temp->data > curr->data)
{
curr=curr->right;
}
else
{
curr=curr->left;
}

}
if(temp->data>p->data)
{
p->right=temp;
}
else
{
p->left=temp;
}

OUTPUT :
PROGRAM : 9(A)

AIM : Program to implement bubble sort.

#include <stdio.h>

int main()
{
int array[100], n, c, d, swap;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

for (c = 0 ; c < n - 1; c++)


{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}

printf("Sorted list in ascending order:\n");

for (c = 0; c < n; c++)


printf("%d\n", array[c]);

return 0;
}

OUTPUT:

PROGRAM : 9(B)

AIM : Program to implement selection sort.


#include <stdio.h>

int main()
{
int a[100], n, min,temp;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n");
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

for(int i=0;i<n-1;i++)
{
min=i;
for(int j=i+1 ;j<n;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}

printf("Sorted list in ascending order:\n");

for (int i = 0; i < n; i++)


printf("%d\n", a[i]);

return 0;
}

OUTPUT :
PROGRAM : 9(C)

AIM : Program to implement insertion sort.

#include<stdio.h>
int main()
{
int a[5];
int key;
int j;

printf("enter elements ");

for(int i=0;i<5;i++)
{scanf("%d ",&a[i]);}

for(int i=1;i<5;i++)
{
key=a[i];
j=i-1;

while(j>=0 && a[j]>key)


{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=key;
}
printf("the sorted ");
printf("\n ");
for(int i=0;i<5;i++)
{
printf("%d ",a[i]);
}
}

PROGRAM : 9(D)

AIM : Program to implement merge sort.

#include <stdio.h>

void merge(int [], int, int [], int, int []);

int main() {
int a[100], b[100], m, n, c, sorted[200];

printf("Input number of elements in first array\n");


scanf("%d", &m);

printf("Input %d integers\n", m);


for (c = 0; c < m; c++) {
scanf("%d", &a[c]);
}

printf("Input number of elements in second array\n");


scanf("%d", &n);

printf("Input %d integers\n", n);


for (c = 0; c < n; c++) {
scanf("%d", &b[c]);
}

merge(a, m, b, n, sorted);

printf("Sorted array:\n");

for (c = 0; c < m + n; c++) {


printf("%d\n", sorted[c]);
}

return 0;
}

void merge(int a[], int m, int b[], int n, int sorted[]) {


int i, j, k;

j = k = 0;

for (i = 0; i < m + n;) {


if (j < m && k < n) {
if (a[j] < b[k]) {
sorted[i] = a[j];
j++;
}
else {
sorted[i] = b[k];
k++;
}
i++;
}
else if (j == m) {
for (; i < m + n;) {
sorted[i] = b[k];
k++;
i++;
}
}
else {
for (; i < m + n;) {
sorted[i] = a[j];
j++;
i++;
}
}
}
}

OUTPUT :

#include<stdio.h>

void mergesort(int a[],int i,int j);


void merge(int a[],int i1,int j1,int i2,int j2);

int main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");

for(i=0;i<n;i++)
scanf("%d",&a[i]);

mergesort(a,0,n-1);

printf("\nSorted array is :");


for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}

void mergesort(int a[],int i,int j)


{
int mid;

if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid); //left recursion
mergesort(a,mid+1,j); //right recursion
merge(a,i,mid,mid+1,j); //merging of two sorted sub-arrays
}
}

void merge(int a[],int i1,int j1,int i2,int j2)


{
int temp[50]; //array used for merging
int i,j,k;
i=i1; //beginning of the first list
j=i2; //beginning of the second list
k=0;

while(i<=j1 && j<=j2) //while elements in both lists


{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}

while(i<=j1) //copy remaining elements of the first list


temp[k++]=a[i++];

while(j<=j2) //copy remaining elements of the second list


temp[k++]=a[j++];

//Transfer elements from temp[] back to a[]


for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}

PROGRAM : 9(E)
AIM : Program to implement quick sort.

#include<stdio.h>

void quicksort(int number[25],int first,int last)


{
int i, j, pivot, temp;

if(first<last)
{
pivot=first;
i=first;
j=last;

while(i<j)
{
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j)
{
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}

temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);

}
}

int main()
{
int i, count, number[25];

printf("enter the no of elements: ");


scanf("%d",&count);

printf("Enter %d elements: \n", count);


for(i=0;i<count;i++)
scanf("\n%d",&number[i]);

quicksort(number,0,count-1);

printf("Order of Sorted elements: ");


for(i=0;i<count;i++)
printf(" %d",number[i]);

return 0;
}

OUTPUT :

#include <stdio.h>

void quick_sort(int[],int,int);
int partition(int[],int,int);

int main()
{
int a[50],n,i;
printf("How many elements?");
scanf("%d",&n);
printf("\nEnter array elements:");

for(i=0;i<n;i++)
scanf("%d",&a[i]);

quick_sort(a,0,n);
printf("\nArray after sorting:");

for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}

void quick_sort(int a[],int l,int u)


{
int j;
if(l<u)
{
j=partition(a,l,u);
quick_sort(a,l,j);
quick_sort(a,j+1,u);
}
}

int partition(int a[],int l,int u)


{
int v,i,j,temp;
v=a[l];
i=l;
j=u;

while(i<j)
{
do
{ i++;
}
while(a[i]<v);

do
{ j--;
}
while(a[j]>v);

if(i<j)
{ temp=a[i];
a[i]=a[j];
a[j]=temp;
}

a[l]=a[j];
a[j]=v;

return(j);
}
PROGRAM : 10

AIM : Program to implement string concatenation,compare and substring.

#include<stdio.h>
#include<string.h>

void SubString(char string[])


{
int position,length,c=0;
char sub[25];
printf("Enter the position and length of substring\n");
scanf("%d %d", &position, &length);

while (c < length) {


sub[c] = string[position+c-1];
c++;
}
sub[c] = '\0';

printf("Required substring is \"%s\"\n", sub);

}
int main()
{
char a[25];
char b[25];
int i=0;
printf("Enter String A\n");
gets(a);
printf("Enter String B\n");
gets(b);
char ans='y';
while(ans=='y'||ans=='Y')
{

printf("\n1. String Concatenate\n");


printf("\n2. String Compare\n");
printf("\n3. String Reverse A\n");
printf("\n4. String SubString A\n ");
scanf("%d",&i);
switch (i)
{
case 1: strcat(a,b);
printf("Your Concatenated String: %s\n",a);
break;
case 2: if(strcmpi(a,b)==0)
{
printf("Match: True\n");
}
else
{
printf("False!\n");
}
break;
case 3: strrev(a);
printf("\nReversed String :\n");
printf("%s\n",a);
break;
case 4: SubString(a);break;
default:
break;
}
printf("\nDo you want to check more?\n");
scanf(" %c",&ans);
}
}

OUTPUT
Binary tree menuuu

#include<stdio.h>
#include<stdlib.h>
struct tree {
int info;
struct tree *left;
struct tree *right;
};
struct tree *insert(struct tree *,int);
void inorder(struct tree *);
void postorder(struct tree *);
void preorder(struct tree *);
struct tree *delet(struct tree *,int);
struct tree *search(struct tree *);
int main(void) {
struct tree *root;
int choice, item,item_no;
root = NULL;
/* rear = NULL;*/
do {
do {
printf("\n \t 1. Insert in Binary Tree ");
printf("\n\t 2. Delete from Binary Tree ");
printf("\n\t 3. Inorder traversal of Binary tree");
printf("\n\t 4. Postorder traversal of Binary tree");
printf("\n\t 5. Preorder traversal of Binary tree");
printf("\n\t 6. Search and replace ");
printf("\n\t 7. Exit ");
printf("\n\t Enter choice : ");
scanf(" %d",&choice);
if(choice<1 || choice>7)
printf("\n Invalid choice - try again");
}
while (choice<1 || choice>7);
switch(choice) {
case 1:
printf("\n Enter new element: ");
scanf("%d", &item);
root= insert(root,item);
printf("\n root is %d",root->info);
printf("\n Inorder traversal of binary tree is : ");
inorder(root);
break;
case 2:
printf("\n Enter the element to be deleted : ");
scanf(" %d",&item_no);
root=delet(root,item_no);
inorder(root);
break;
case 3:
printf("\n Inorder traversal of binary tree is : ");
inorder(root);
break;
case 4:
printf("\n Postorder traversal of binary tree is : ");
postorder(root);
break;
case 5:
printf("\n Preorder traversal of binary tree is : ");
preorder(root);
break;
case 6:
printf("\n Search and replace operation in binary tree ");
root=search(root);
break;
default:
printf("\n End of program ");
}
/* end of switch */
}
while(choice !=7);
return(0);
}
struct tree *insert(struct tree *root, int x) {
if(!root) {
root=(struct tree*)malloc(sizeof(struct tree));
root->info = x;
root->left = NULL;
root->right = NULL;
return(root);
}
if(root->info > x)
root->left = insert(root->left,x); else {
if(root->info < x)
root->right = insert(root->right,x);
}
return(root);
}
void inorder(struct tree *root) {
if(root != NULL) {
inorder(root->left);
printf(" %d",root->info);
inorder(root->right);
}
return;
}
void postorder(struct tree *root) {
if(root != NULL) {
postorder(root->left);
postorder(root->right);
printf(" %d",root->info);
}
return;
}
void preorder(struct tree *root) {
if(root != NULL) {
printf(" %d",root->info);
preorder(root->left);
preorder(root->right);
}
return;
}
/* FUNCTION TO DELETE A NODE FROM A BINARY TREE */
struct tree *delet(struct tree *ptr,int x) {
struct tree *p1,*p2;
if(!ptr) {
printf("\n Node not found ");
return(ptr);
} else {
if(ptr->info < x) {
ptr->right = delet(ptr->right,x);
/*return(ptr);*/
} else if (ptr->info >x) {
ptr->left=delet(ptr->left,x);
return ptr;
} else
/* no. 2 else */ {
if(ptr->info == x)
/* no. 2 if */ {
if(ptr->left == ptr->right)
/*i.e., a leaf node*/ {
free(ptr);
return(NULL);
} else if(ptr->left==NULL)
/* a right subtree */ {
p1=ptr->right;
free(ptr);
return p1;
} else if(ptr->right==NULL)
/* a left subtree */ {
p1=ptr->left;
free(ptr);
return p1;
} else {
p1=ptr->right;
p2=ptr->right;
while(p1->left != NULL)
p1=p1->left;
p1->left=ptr->left;
free(ptr);
return p2;
}
}
/*end of no. 2 if */
}
/* end of no. 2 else */
/* check which path to search for a given no. */
}
return(ptr);
}
/* function to search and replace an element in the binary tree */
struct tree *search(struct tree *root) {
int no,i,ino;
struct tree *ptr;
ptr=root;
printf("\n Enter the element to be searched :");
scanf(" %d",&no);
fflush(stdin);
while(ptr) {
if(no>ptr->info)
ptr=ptr->right; else if(no<ptr->info)
ptr=ptr->left; else
break;
}
if(ptr) {
printf("\n Element %d which was searched is found and is = %d",no,ptr-
>info);
printf("\n Do you want replace it, press 1 for yes : ");
scanf(" %d",&i);
if(i==1) {
printf("\n Enter new element :");
scanf(" %d",&ino);
ptr->info=ino;
} else
printf("\n\t It's okay");
} else
printf("\n Element %d does not exist in the binary tree",no);
return(root);
}

Potrebbero piacerti anche