0% found this document useful (0 votes)
12 views29 pages

DS Programs

Download as pdf or txt
0% found this document useful (0 votes)
12 views29 pages

DS Programs

Download as pdf or txt
Download as pdf or txt
You are on page 1/ 29

Data Structure Using C(Programs)

1. Reverse of a string using pointer


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char *s;
int len,i;
printf(“\nEnter a string: ”);
gets(s);
len=strlen(s);
printf(“\nReverse of the string is : ”);
for(i=len;i>=0;i--)
printf(“%c”,*(s+i));
getch();
}

Output

Enter a string: Computer


Reverse of the string is: retupmoC
******************************************************************************

2. Implement Pattern Matching Algorithm


#include<stdio.h>
#include<conio.h>
#include<string.h>
int match(char[ ],char[ ]);
void main()
{
clrscr();
char a[100],b[100];
int pos;
printf(“\nEnter some text:”);
gets(a);
printf(“\nEnter a string to find:”);
gets(b);
pos=match(a,b);
if(pos!=-1)
{
printf(“\nFound at location:%d”,pos+1);
}
else printf(“\nNot Found.”);
getch();
}
int match(char text[],char pattern[])
{
int c, d, e, text_len, patt_len, pos=-1;
text_len=strlen(text);
patt_len=strlen(pattern);
if(patt_len>text_len)
return-1;
for(c=0;c<=text_len,patt_len;c++)
{
pos=e=c;
for(d=0;d<patt_len;d++)
{
If(pattern[d]==text[e]) e++;
else break;
}
If(d==patt_len) return pos;
}
return -1;
}

Output

Enter some text: Computer science


Enter a string to find: n

Found at location:14
******************************************************************************

3. Search an element in 2-dimensional array


#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int row_size,col_size;
printf(“\nEnter the row size of the matrix: ”);
scanf(“%d”,&row_size);
printf(“\nEnter the column size of the matrix: ”);
scanf(“%d”,&col_size);
int matrix[5][2];
int i,j;
printf(“\nEnter the matrix element:\n”);
for(i=0;i<row_size;i++)
{
for(j=0;j<col_size;j++)
{
scanf(“%d”,&matrix[i][j]);
}
}
int search_ele;
printf(“\nEnter the search element: “);
scanf(“%d”,&search_ele);
int flag=0,row,col;
for(i=0;i<row_size;i++)
{
for(j=0;j<col_size;j++)
{
If(matrix[i][j]==search_ele)
{
flag=1;
row=i;
col=j;
}
}
}
If(flag==1)
printf(“\nPosition of search element %d is (%d,%d)”,search_ele,row,col);
else printf(“\nGiven search element is not found!”);
getch();
}

Output

Enter the row size of the matrix: 2


Enter the column size of the matrix: 2

Enter the matrix elements:


6 7
8 9

Enter the search element: 8

Position of search element 8 is (1,0)


******************************************************************************

4. Append 2 Arrays
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int arr1[50],arr2[50],size1,size2,i,k,merge[100];
printf(“\nEnter array 1 size:”);
scanf(“%d”,&size1);
printf(“\nEnter array 1 elements:”);
for(i=0;i<size1;i++)
{
scanf(“%d”,&arr1[i]);
merge[i]=arr1[i];
}
k=i;
printf(“\nEnter array 2 size:”);
scanf(“%d”,&size2);
printf(“\nEnter array 2 elements:”);
for(i=0;i<size2;i++)
{
scanf(“%d”,&arr2[i]);
merge[k]=arr2[i];
k++;
}
printf(“\nThe new array after merging is :\n”);
for(i=0;i<k;i++)
printf(“%d”,merge[i]);
getch();
return 0;
}

Output

Enter array 1 size: 2


Enter array 1 elements: 3 4
Enter array 2 size: 3
Enter array 2 elements: 5 6 7

The new array after merging is:


34567
******************************************************************************

5. Search an element in the array using binary search


#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,low,high,mid,n,key,arr[100];
printf(“\nEnter number of elements:”);
scanf(“%d”,&n);
printf(“\nEnter %d integers:”,n);
for(i=0;i<n;i++)
scanf(“%d”,&arr[i]);
printf(“\nEnter value to find: “);
scanf(“%d”,&key);
low=0;
high=n-1;
mid=(low+high)/2;
while(low<=high)
{
if(arr[mid]<key) low=mid+1;
else if(arr[mid]==key)
{
printf(“\n%d found at location %d”,key,mid+1);
break;
}
else high=mid-1;
mid=(low+high)/2;
}
if(low>high)
printf(“\nNot found ! %d isn’t present in the list”,key);
getch();
}

Output

Enter number of elements: 4

Enter 4 elements:
5
6
7
8

Enter the value to find: 7

7 found at location: 3
******************************************************************************

6. Read a sparse matrix and display its triplet representation using array
#include<stdio.h>
#include<conio.h>
void main()
{
int A[10][10],B[10][3],m,n,i,j,s=0;
clrscr();
printf(“\nEnter the order of mxn of the sparse matrix: “);
scanf(“%d%d”,&m,&n);
printf(“\nEnter the elements in the sparse matrix(mostly zeros): “);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“\n%d rows and %d columns: “,i,j);
scanf(“%d”,&A[i][j]);
}
}
printf(“\nThe given matrix is: \n“);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%5d”,A[i][j]);
}
printf(“\n”);
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
If(A[i][j]!=0)
{
B[s][0]= A[i][j]);
B[s][1]=i;
B[s][2]=j;
s++;
}
}
}
printf(“\nThe sparse matrix is given by: \n“);
for(i=0;i<s;i++)
{
for(j=0;j<3;j++)
{
printf(“%5d”,B[i][j]);
}
printf(“\n”);
}
getch();
}

Output

Enter the order of mXn of the sparse matrix:2 2

Enter the elements in the sparse matrix(mostly zeros):


0 rows 0 columns :1
0 rows 1 columns :0
1 rows 0 columns :0
1 rows 1 columns :5

The given matrix is:


1 0
0 5
The sparse matrix is given by:
1 0 0
5 1 1
******************************************************************************

7. Create a singly linked list of n nodes and display it


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head,*tail=NULL;
void addNode(int data)
{
struct node *newNode=(struct node*)malloc(sizeof(struct node));
newNode->data=data;
newNode->next=NULL;
if(head==NULL)
{
head=newNode;
tail=newNode;
}
else
{
tail->next=newNode;
tail=newNode;
}
}
void display()
{
struct node *current=head;
if(head==NULL)
{
printf(“List is empty”);
return;
}
printf(“\nNodes of Singly Linked List:”);
while(current!=NULL)
{
printf(“\n%d”,current->data);
current=current->next;
}
printf(“\n”);
getch();
}
int main()
{
int i, num;
while(1)
{
printf(“\nList Operations:”);
printf(“\n1.Insert a New Node.”);
printf(“\n2.Display.”);
printf(“\n3.Exit.”);
printf(“\n\nChoose an option: ”);
scanf(“%d”,&i):
if(i<=0) printf(“\nEnter a valid option!!”);
else
{
switch(i)
{
case 1:
{
printf(“\nEnter the value to be inserted:”);
scanf(“%d”,&num);
addNode(num);
break;
}
case 2: display();
case 3: return 0;
default: printf(“\nInvalid Option!!”);
}
}
}
}

Output:

List Operations:
1.Insert a New Node.
2.Display.
3.Exit.
Choose an option: 1
Enter value to be inserted:7
List Operations:
1.Insert a New Node.
2.Display.
3.Exit.
Choose an option: 2
Nodes of Singly Linked List:
7
******************************************************************************

8. Delete a given node from a singly linked list


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head,*tail=NULL;
void addNode(int data)
{
struct node *newNode=(struct node *)malloc(sizeof(struct node));
newNode->data=data;
newNode->next=NULL;
if(head==NULL)
{
head=newNode;
tail=newNode;
}
else
{
tail->next=newNode;
tail=newNode;
}
}
int deleteNode(int num)
{
struct node *temp,*prev;
temp=head;
while(temp!=NULL)
{
if(temp->data==num)
{
if(temp==head)
{
head=temp->next;
free(temp);
return 1;
}
else
{
prev->next=temp->next;
free(temp);
return 1;
}
}
else
{
prev=temp;
temp=temp->next;
}
}
return 0;
}
void display()
{
struct node *current=head;
if(head==NULL)
{
printf(“List is empty!”);
return;
}
printf(“\nNodes of singly linked list:”);
while(current!=NULL)
{
printf(“\n%d”,current->data);
current=current->next;
}
printf(“\n”);
getch();
}
int main()
{
int i,num;
while(1)
{
printf(“\nList Operations:”);
printf(“\n1.Insert a New Node.”);
printf(“\n2.Display.”);
printf(“\n3.Delete a node.”);
printf(“\n4.Exit.”);
printf(“\n\nChoose an option: ”);
scanf(“%d”,&i);
switch(i)
{
case 1:
{
printf(“\nEnter the value to be inserted:”);
scanf(“%d”,&num);
addNode(num);
break;
}
case 2: display();break;
case 3:
{
if(head==NULL)printf(“\nList is empty.”);
else printf(“\nEnter the number to delete:”);
scanf(“%d”,&num);
if(deleteNode(num))printf(“\n%d deleted successfully!”,num);
else printf(“\n%d not found!”,num);
}
case 4: return 0;
default: printf(“\nInvalid Option!!”);
}
}
}

Output:

List Operations:
1.Insert a New Node.
2.Display.
3.Delete a node.
4.Exit.
Choose an option: 2
Nodes of Singly Linked List:
7
8
9

List Operations:
1.Insert a New Node.
2.Display.
3.Delete a node.
4.Exit.
Choose an option: 3
Enter number to delete: 8
8 deleted successfully!
List Operations:
1.Insert a New Node.
2.Display.
3.Delete a node.
4.Exit.
Choose an option: 2
Nodes of Singly Linked List:
7
9
******************************************************************************

9. Creating a doubly linked list of integers and display in forward and backward direction
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *prev;
struct node *next;
};
struct node *head,*tail=NULL;
void addNode(int data)
{
struct node *newNode=(struct node*)malloc(sizeof(struct node));
newNode->data=data;
if(head==NULL)
{
head=tail=newNode;
head->prev=NULL;
tail->next=NULL;
}
else
{
tail->next=newNode;
newNode->prev=tail;
tail=newNode;
tail->next=NULL;
}
}
void display()
{
struct node *current=head;
if(head==NULL)
{
printf(“\nList is empty!”);
return;
}
printf(“\nNodes of doubly linked list :\n”);
while(current!=NULL)
{
printf(“%d”,current->data);
current=current->next;
printf(“\t”);
}
}
void reverse()
{
struct node *current=head,*temp;
while(current!=NULL)
{
temp=current->next;
current->next=current->prev;
current->prev=temp;
current=current->prev;
}
temp=head;
head=tail;
tail=temp;
display();
}
int main()
{
int i,num;
while(1)
{
printf(“\nList Operations”);
printf(“\n1.Insert a node.”);
printf(“\n2Display.”);
printf(“\n3.Display in reverse order.”);
printf(“\n4.Exit.”);
printf(“\n\nEnter your choice:”);
scanf(“%d”,&i);
if(i<=0) printf(“\nInvalid Option”);
else switch(i)
{
case 1: {
printf(“\nEnter the value to be inserted:”);
scanf(“%d”,&num);
addNode(num);
break;
}
case 2:display();break;
case 3:printf(“\nIn reverse order”);reverse();break;
case 4:return 0;
default: printf(“\nInvalid option !”);
}
}
}
Output:

List Operations:
1.Insert a node.
2.Display.
3.Display in reverse order.
4.Exit.
Enter your choice:1

Enter the value to be inserted:6

List Operations:
1.Insert a node.
2.Display.
3.Display in reverse order.
4.Exit.
Enter your choice:1

Enter the value to be inserted:7


List Operations:
1.Insert a node.
2.Display.
3.Display in reverse order.
4.Exit.
Enter your choice:1

Enter the value to be inserted:8

List Operations:
1.Insert a node.
2.Display.
3.Display in reverse order.
4.Exit.
Enter your choice:2

Nodes of doubly linked list :


6 7 8

List Operations:
1.Insert a node.
2.Display.
3.Display in reverse order.
4.Exit.
Enter your choice:3

Nodes of doubly linked list:


8 7 6
******************************************************************************

10. Stack using Array


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define SIZE 10
void push(int);
void pop();
void display();
int stack[SIZE],top=-1;
void main()
{
int value,i;
clrscr();
while (1)
{
printf(“\n***MENU***\n”);
printf(“\n1.Push.\n2.Pop.\n3.Display.\n4.Exit.”);
printf(“\nEnter your choice: “);
scanf(“%d”,&i);
switch(i)
{
case 1:printf(“\nEnter the value:”);
scanf(“%d”,&value);
push(value);break;
case 2:pop();break;
case 3:display();break;
case 4:exit(0);
default:printf(“\nWrong Selection!”);
}
}
}
void push(int value)
{
if(top==SIZE-1)
printf(“\nStack is full !! Insertion is not possible.”);
else
{
top++;
stack[top]=value;
printf(“\nInsertion Success !!”);
}
}
void pop()
{
if(top==-1)
printf(“\nThe stack is empty.Deletion is not possible !”);
else
{
printf(“\nDeleted %d”,stack[top]);
top--;
}
}
void display()
{
if(top==-1)
printf(“\nStack is empty!!”);
else
{
int i;
printf(“\nStack elements are:\n”);
for(i=top;i>=0;i--)
printf(“%d\n”,stack[i]);
}
}
Output

***MENU**
1.Push.
2.Pop.
3.Display.
4.Exit.
Enter your choice:1

Enter the value:6


Insertion Success!!

***MENU**
1.Push.
2.Pop.
3.Display.
4.Exit.
Enter your choice:1

Enter the value:7


Insertion Success!!

***MENU**
1.Push.
2.Pop.
3.Display.
4.Exit.
Enter your choice:1

Enter the value:8


Insertion Success!!

***MENU**
1.Push.
2.Pop.
3.Display.
4.Exit.
Enter your choice:3

Stack elements are:


8
7
6

***MENU**
1.Push.
2.Pop.
3.Display.
4.Exit.
Enter your choice:2

Deleted 8
******************************************************************************

11. Stack using Linked List


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;
void main()
{
clrscr();
int choice=0;
printf(“\n**Stack Using Linked List**”);
while(choice!=4)
{
printf(“\n\nChoose an option:”);
printf(“\n1.Push.\n2.Pop.\n3.Show.\n4.Exit.”);
printf(“\nEnter your choice:”);
scanf(“%d”,&choice);
switch(choice)
{
case 1:push();break;
case 2:pop();break;
case 3:display();break;
case 4:exit(0);
default:printf(“\nPlease enter valid choice..”);
}
}
}
void push()
{
int val;
struct node *ptr=(struct node*)malloc(sizeof(struct node));
if(ptr==NULL)
{
printf(“\nNot able to push the element.”);
}
else
{
printf(“\nEnter the value: ”);
scanf(“%d”,&val);
if(head==NULL)
{
ptr->val=val;
ptr->next=NULL;
head=ptr;
}
else
{
ptr->val=val;
ptr->next=head;
head=ptr;
}
printf(“\nItem Pushed!!”);
}
}
void pop()
{
int item;
struct node *ptr;
if(head==NULL)
{
printf(“\nStack Underflow !!”);
}
else
{
item=head->val;
ptr=head;
head=head->next;
free(ptr);
printf(“\nItem Popped.”);
}
}
void display()
{
struct node *ptr;
ptr=head;
if(ptr==NULL)
{
printf(“\nStack is empty..”);
}
else
{
printf(“\nPrinting stack element:\n”);
while(ptr!=NULL)
{
printf(“%d\n”,ptr->val);
ptr=ptr->next;
}
}
}

Output

**Stack Using Linked List**

Choose an Option
1.Push.
2.Pop.
3.Show.
4.Exit.
Enter your choice:1

Enter the value: 6


Item Pushed !!

Choose an Option
1.Push.
2.Pop.
3.Show.
4.Exit.
Enter your choice:1

Enter the value: 7


Item Pushed !!

Choose an Option
1.Push.
2.Pop.
3.Show.
4.Exit.
Enter your choice:1

Enter the value: 8


Item Pushed !!

Choose an Option
1.Push.
2.Pop.
3.Show.
4.Exit.
Enter your choice:3

Printing stack element:


8
7
6

Choose an Option
1.Push.
2.Pop.
3.Show.
4.Exit.
Enter your choice:2

Item Popped.
******************************************************************************

12. Evaluation of Postfix


#include<stdio.h>
#include<ctype.h>
#include<conio.h>
int stack[20];
int top = -1;

void push(int x)
{
stack[++top] = x;
}

int pop()
{
return stack[top--];
}

int main()
{
clrscr();
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;
case '^' :n3 = n2 ^ n1; break;
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
getch();
return 0;
}

Output

Enter the expression: 483*+

The result of the expression 483*+=28


******************************************************************************

13. Queue using array

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define n 5
void main()
{
int queue[n],ch=1,front=0,rear=0,i,j=0,x=n;
printf(“\nQueue using Array”);
printf(“\n1.Insertion.\n2.Deletion.\n3.Display.\n4.Exit.”);
while(ch)
{
printf(“\nEnter your choice:”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:{
if(rear==x) printf(“\nQueue is full.”);
else
{
printf(“\nEnter no %d”,j++);
scanf(“%d”,&queue[rear++]);
}
break;
}
case 2:
{
if(front==rear) printf(“\nQueue is empty.”);
else
{
printf(“\nDeleted elements is %d”,queue[front++]);
x++;
}
break;
}
case 3:
{
printf(“\nQueue elements are:\n”);
if(front==rear) printf(“\nQueue is empty.”);
else
{
for(i=front;i<rear;i++)
{
printf(“%d”,queue[i]);
printf(“\n”);
}
}
break;
}
case 4:exit(0);
default: printf(“\nWrong Choice.Please see the options.”);
}
}
getch();
}

Output

Queue using Array


1.Insertion.
2.Deletion.
3.Display.
4.Exit
Enter the choice:1

Enter no 1:10

Enter the choice:1

Enter no 2:54

Enter the choice:1


Enter no 3:98

Enter the choice:1

Enter no 4:234

Enter the choice:3


Queue elements are:
10
54
98
234

Enter the choice:2

Deleted elements is 10

Enter the choice:3

Queue elements are:


54
98
234
******************************************************************************

14. Queue using linked list

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*rear, *front;
void dequeue()
{
struct node *var=front;
if(front==NULL)
printf("\nQueue is empty");
else
{
rear->next;
front=front->next;
free(var);
}
}
void enqueue(int value)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=value;
if(rear==NULL)
{
rear=temp;
rear->next=NULL;
front=rear;
}
else
{
rear->next=temp;
rear=temp;
rear->next=NULL;
}
}
void display()
{
struct node *var=front;
if(var!=NULL)
{
printf("\nElements are:");
while(var!=NULL)
{
printf("\t%d",var->data);
var=var->next;
}
printf("\n");
}
else printf("\nQueue is empty.");
}
void main()
{
int i=0;
clrscr();
front=NULL;
while(1)
{
printf("\n1.Enqueue.\n2.Dequeue. \n3.Display.\n4.Exit.");
printf("\nChoose an option:");
scanf("%d", &i);
switch(i)
{
case 1:{
int value;
printf("\nEnter a value to enqueue:");
scanf("%d", &value);
enqueue(value);
display();
break;
}
case 2:dequeue();display();break;
case 3:display();break;
case 4:exit(0);
default:printf("\nInvalid Option!");
}
}
}

Output

1.Enqueue.
2.Dequeue.
3.Display.
4.Exit.
Choose an option:1

Enter the value: 5


Elements are:5

1.Enqueue.
2.Dequeue.
3.Display.
4.Exit.
Choose an option:1

Enter the value: 6


Elements are: 5 6

1.Enqueue.
2.Dequeue.
3.Display.
4.Exit.
Choose an option:1

Enter the value: 7


Elements are: 5 6 7

1.Enqueue.
2.Dequeue.
3.Display.
4.Exit.
Choose an option:2

Elements are: 6 7

******************************************************************************

15. Search an element using Binary Search Tree

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct tnode
{
int data;
struct tnode *l,*r;
};
struct tnode *root=NULL;
struct tnode *create(int data)
{
struct tnode *newnode;
newnode=(struct tnode*)malloc(sizeof(struct tnode));
newnode->data=data;
newnode->l=NULL;
newnode->r=NULL;
return(newnode);
}

void insert(struct tnode **node,int data)


{
if(*node==NULL)
{
*node=create(data);
}
else if(data<(*node)->data)
{
insert(&(*node)->l,data);
}
else if(data>(*node)->data)
{
insert(&(*node)->r,data);
}
}

void deletion(struct tnode **node, struct tnode **parent, int data)


{
struct tnode *temp,*tempparent;
if(*node==NULL)
{
return;
}
if((*node)->data==data)
{
if(!(*node)->l && !(*node)->r)
{
if(parent)
{
If((*parent)->l==*node)
{
(*parent)->l=NULL;
}
else
{
(*parent)->r=NULL;
free(*node);
}
}
else
{
free(*node);
}
}
else if(!(*node)->r && (*node)->l)
{
temp=*node;
(*parent)->r=(*node)->l;
free(temp);
*node=(*parent)->r;
}
else if((*node)->r && !(*node)->l)
{
temp=*node;
(*parent)->l=(*node)->r;
free(temp);
(*node)=(*parent)->l;
}
else if(!(*node)->r->l)
{
temp=*node;
(*node)->r->l=(*node)->l;
(*parent)->l=(*node)->r;
free(temp);
*node=(*parent)->l;
}
else
{
temp=(*node)->r;
while(temp->l)
{
tempparent=temp;
temp=temp->l;
}
tempparent->l=temp->r;
temp->l=(*node)->l;
temp->r=(*node)->r;
free(*node);
*node=temp;
}
}
else if(data<(*node)->data)
{
deletion(&(*node)->l,node,data);
}
else if(data>(*node)->data)
{
deletion(&(*node)->r,node,data);
}
}

void search(struct tnode *node,int data)


{
if(!node)
{
printf(“\nNot Found!”);
return;
}
else if(data<node->data)
{
search(node->l,data);
}
else if(data>node->data)
{
search(node->r,data);
}
else printf(“\nData found %d.\n);
return;
}

void traverse(struct tnode *node)


{
if(node!=NULL)
{
traverse(node->l);
printf(“%d”,node->data);
traverse(node->r);
}
return;
}
int main()
{
while(1)
{
int ch,data;
printf(“\n\n1.Insertion.\n2.Deletion.\n3.Search.\n4.Traversal.\n5.Exit\n”);
printf(“\nEnter your choice:”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:{
while(ch==1)
{
printf(“\nEnter your data:”);
scanf(“%d”,&data);
insert(&root,data);
printf(“\nSuccessfully inserted data.”);
printf(“\nContinue insertion?(1/0)”);
scanf(“%d”,&ch);
}
break;
}
case 2:{
printf(“\nEnter data for deletion:”);
scanf(%d”,&data);
deletion(&root,NULL,data);
printf(“\nDeletion Succcessfull.”);
break;
}
case 3:{
printf(“\nEnter value for search:”);
scanf(“%d”,&data);
search(root,data);
break;
}
case 4:{
printf(“\nInorder Traversal”);
traversal(root);
break;
}
case 5:exit(0);
default:printf(“\nInvalid Option.”);
}
}
}

Output

1.Insertion.
2.Deletion.
3.Search.
4.Traversal.
5.Exit
Enter your choice:1
Enter your data:9

Successfully inserted data.


Continue insertion?(1/0):1

Enter your data:4

Successfully inserted data.


Continue insertion?(1/0):1

Enter your data:7

Successfully inserted data.


Continue insertion?(1/0):0

1.Insertion.
2.Deletion.
3.Search.
4.Traversal.
5.Exit
Enter your choice:4

Inorder Traversal
479

1.Insertion.
2.Deletion.
3.Search.
4.Traversal.
5.Exit
Enter your choice:3

Enter data for deletion:7

Data found 7.
******************************************************************************
16. Implement Quick sort
#include<stdio.h>
#include<conio.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);
}
}
void main()
{
int i, count, number[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
getch();
}

Output:
How many elements are u going to enter?: 5

Enter 5 elements: 88 0 -9 98 27

Order of Sorted elements: -9 0 27 88 98


**************************************************************************************

17. Implement Selection sort

#include<stdio.h>
#include<conio.h>
void main()
{
int array[100],n,c,d,position,swap;
printf("\n Enter no of elements:");
scanf("%d",&n);
printf("\n Enter %d integers: \n",n);
for(c=0;c<n;c++)
scanf("%d",&array[c]);
for(c=0;c<(n-1);c++)
{
position=c;
for(d=c+1;d<n;d++)
{
if(array[position]>array[d])
position=d;
}
if(position!=c)
{
swap=array[c];
array[c]=array[position];
array[position]=swap;
}
}
printf("\n Sorted list in ascending order:\n");
for(c=0;c<n;c++)
printf("%d\n",array[c];
getch();
}

Output:
Enter no of elements:4
Enter 4 integers:
4
2
7
1

Sorted list in ascending order:


1
2
4
7

**************************************************************************************

18. Implement Insertion sort


#include<stdio.h>
#include<conio.h>
void main()
{
int n, array[1000], c, d, t, flag=0;
printf("enter no of elements\n");
scanf("%d", &n);
printf("enter %d integers\n", n);
for(c=0; c<n; c++)
scanf("%d", &array[c]);
for(c=1; c<=n-1; c++)
{
t=array[c];
for(d=c-1; d>=0; d--)
{
if(array[d]>t)
{
array[d+1]=array[d];
flag=1;
}
else break;
}
if(flag) array[d+1]=t;
}
printf("sorted list in ascending order:\n");
for(c=0; c<=n-1; c++)
{
printf("%d\n", array[c]);
}
getch();
}

Output:

Enter no of elements:
5

Enter 5 integers:
4
3
-1
2
1

Sorted list in ascending order:


-1
1
2
3
4
**************************************************************************************

You might also like