Ds Programs

Télécharger au format doc, pdf ou txt
Télécharger au format doc, pdf ou txt
Vous êtes sur la page 1sur 41

AIM: To Implement Circular Linked List.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
#define null 0
struct node
{
int info;
struct node *link;
}*start,*last;
void main()
{
int ch,n,m,position,i;
last=null;
clrscr();
while(1)
{
printf("\n1.enter elments \n2.addat starting \n3.add at position
\n4.delete \n5.display \n6.exit\n");
printf("enter ur choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter no of elemnts u want to enter:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the element");
scanf("%d",&m);
create(m);
}
break;

case 2:
printf("enter the element");
scanf("%d",&m);
addat(m);
break;
case 3:
printf("enter the element");

Ankur Gaur IT -S[10] 1161563108


scanf("%d",&m);

printf("enter the position");


scanf("%d",&position);
addbt(m,position);
break;
case 4:
if(last==null)
{
printf("list is empty");
continue;
}

printf("enter the element for deletion");


scanf("%d",&m);
del(m);
break;
case 5:
disp();
break;
case 6:
exit(0);
break;
default:
printf("wrong choice");
}}}

create(int data)
{
struct node *q,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;

tmp->link=null;
if(last==null)
{
last=tmp;
tmp->link=last;
}
else
{
tmp->link=last->link;
last->link=tmp;
last=tmp;
}}

addat(int data)

Ankur Gaur IT -S[10] 1161563108


{

struct node *q,*tmp;


tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=last->link;
last->link=tmp;
}

addbt(int data,int pos)


{
struct node *tmp,*q;
int i;
q=last->link;;
for(i=1;i<pos-1;i++)
{
q=q->link;
if(q==last->link)
{
printf("there are less than %d elements",pos);
return;
}
}
tmp=(struct node *)malloc(sizeof(struct node));
tmp->link=q->link;

tmp->info=data;
q->link=tmp;
if(q==last)
last=tmp;
}
del(int data)
{
struct node *tmp,*q;
if(last->link==last&&last->info==data)
{
tmp=last;
last=null;
free(tmp);
return;
}
q=last->link;
if(q->info==data)
{
tmp=q;
last->link=q->link;
free(tmp);

Ankur Gaur IT -S[10] 1161563108


return;
}
while(q->link!=last)
{
if(q->link->info==data)
{
tmp=q->link;
q->link=tmp->link;

free(tmp);
printf("element %d is deleted",data);
}
if(q->link->info=data)
{
tmp=q->link;
q->link=last->link;

free(tmp);
last=q;
return;}
printf("element%d is not found",data);
}}

disp()
{
struct node *q;
if(last==null)
{
printf("list is empty");
return;
}q=last->link;
while(q!=last)
{
printf("%d",q->info);
q=q->link;
}
printf("%d",last->info);
}

1.enter elments
2.addat starting
3.add at position
4.delete

Ankur Gaur IT -S[10] 1161563108


5.display
6.exit
enter ur choice:
1
enter no of elemnts u want to enter:
3
enter the element7
enter the element5
enter the element3

1.enter elments
2.addat starting
3.add at position
4.delete
5.display
6.exit
enter ur choice:
2
enter the element1

1.enter elments
2.addat starting
3.add at position
4.delete
5.display
6.exit
enter ur choice:
3
enter the element2
enter the position2

1.enter elments
2.addat starting
3.add at position
4.delete
5.display
6.exit
enter ur choice:
5
12753

1.enter elments
2.addat starting
3.add at position
4.delete
5.display
6.exit

Ankur Gaur IT -S[10] 1161563108


enter ur choice:
4
enter the element for deletion7

1.enter elments
2.addat starting
3.add at position
4.delete
5.display
6.exit
enter ur choice:
5
1253
1.enter elments
2.addat starting
3.add at position
4.delete
5.display
6.exit
enter ur choice:
6

AIM: To Implement Doubly Linked List.

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

typedef struct node


{
struct node *prev;
int data;
struct node *next;
}
Node;
Node *MakeNode(int data)
{
Node *p=malloc(sizeof (Node));
p->data = data;
return p;
}
void Insert(int pos, int data, Node **start)
{

Ankur Gaur IT -S[10] 1161563108


if (pos<1)
{
puts("Invalid position");
return;
}
if (*start == NULL)
{
*start = MakeNode(data);
(*start)->prev = NULL;
(*start)->next = NULL;
}
else
{
Node *curr=*start;
for (;curr->next && pos>1; pos--)

curr = curr->next;
if (pos > 1 )
{
Node *newnode = MakeNode(data);
newnode->prev = curr;
newnode->next = curr->next;
curr->next = newnode;
}
else if (curr==*start)
{
Node *newnode = MakeNode(data);
newnode->prev = curr->prev;
newnode->next = curr;
curr->prev = newnode;
*start = newnode;
}
else
{
Node *newnode = MakeNode(data);
newnode->prev = curr->prev;
newnode->next = curr;
curr->prev->next = newnode;
curr->prev = newnode;
}}}

void DeleteByPosition(int pos, Node **start)


{
if (pos<1 || *start==NULL)
puts("Invalid position");
else if (pos==1)
{

Ankur Gaur IT -S[10] 1161563108


Node *temp = *start;
*start = (*start)->next;
free(temp);
}
else
{
Node *temp = *start;
for (;temp && pos>1; pos--)

temp = temp->next;
if (temp==NULL)
puts("Invalid position");
else
{
temp->prev->next = temp->next;
if (temp->next)
temp->next->prev = temp->prev;
free(temp);
}}}

void DeleteByValue(int value, Node **start)


{ if ((*start)->data == value)
{
Node *temp = *start;
*start = (*start)->next;
free(temp);
}
else
{
Node *temp = (*start)->next;
for (; temp && temp->data!=value; temp=temp->next)
;
if (temp==NULL)
puts("Element not found");
else
{
temp->prev->next = temp->next;
if (temp->next)
temp->next->prev = temp->prev;
free(temp);
}}}

void Traverse(Node *x)


{
int cnt=1;
while (x)
{

Ankur Gaur IT -S[10] 1161563108


printf("%d: %d\n", cnt++, x->data);

x = x->next;
}}

void TraverseInReverse(Node *x)


{
int cnt=1;
if (x)
{ while (x->next)
{
x = x->next;
cnt++;
}
while (x)
{
printf("%d: %d\n", cnt--, x->data);
x = x->prev;
}}}

int menu(void)
{
int choice;
puts("1-Insert data at end");
puts("2-Insert at beginning");
puts("3-Insert at position");
puts("4-Delete by position");
puts("5-Delete by element");
puts("6-Display");
puts("7-Display in reverse");
puts("8-Exit");
printf("Enter choice: ");
scanf("%d", &choice);
while (getchar()!='\n');
return choice;
}
int main(void)
{
Node *start = NULL;
char input[10];
int pos;

while(1)
{
switch(menu())
{
case 1:

Ankur Gaur IT -S[10] 1161563108


puts("Enter numbers, blank line to stop");
while (gets(input)[0])
Insert(INT_MAX, atoi(input), &start);
break;
case 2:
printf("Enter number: ");
Insert(1, atoi(gets(input)), &start);
break;
case 3:
printf("Enter position: ");
pos = atoi(gets(input));
printf("Enter number: ");
Insert(pos, atoi(gets(input)), &start);
break;
case 4:
printf("Enter position: ");
DeleteByPosition(atoi(gets(input)), &start);
break;
case 5:
printf("Enter value: ");
DeleteByValue(atoi(gets(input)), &start);
break;
case 6:
Traverse(start);
break;
case 7:
TraverseInReverse(start);
break;
case 8:
exit(0);
}}}

Enter choice: 1
Enter numbers, blank line to stop
8
4
2

1-Insert data at end


2-Insert at beginning
3-Insert at position
4-Delete by position

Ankur Gaur IT -S[10] 1161563108


5-Delete by element
6-Display
7-Display in reverse
8-Exit
Enter choice: 2
Enter number: 1

1-Insert data at end


2-Insert at beginning
3-Insert at position
4-Delete by position
5-Delete by element
6-Display
7-Display in reverse
8-Exit
Enter choice: 3
Enter position: 3
Enter number: 3

1-Insert data at end


2-Insert at beginning
3-Insert at position
4-Delete by position
5-Delete by element
6-Display
7-Display in reverse
8-Exit
Enter choice: 6
1: 1
2: 8
3: 3
4: 4
5: 2

1-Insert data at end


2-Insert at beginning
3-Insert at position
4-Delete by position
5-Delete by element
6-Display
7-Display in reverse
8-Exit
Enter choice: 4
Enter position: 2

1-Insert data at end


2-Insert at beginning

Ankur Gaur IT -S[10] 1161563108


3-Insert at position
4-Delete by position
5-Delete by element
6-Display
7-Display in reverse
8-Exit
Enter choice: 5
Enter value: 4

1-Insert data at end


2-Insert at beginning
3-Insert at position
4-Delete by position
5-Delete by element
6-Display
7-Display in reverse
8-Exit
Enter choice: 6
1: 1
2: 3
3: 2

1-Insert data at end


2-Insert at beginning
3-Insert at position
4-Delete by position
5-Delete by element
6-Display
7-Display in reverse
8-Exit
Enter choice: 7
3: 2
2: 3
1: 1
1-Insert data at end
2-Insert at beginning
3-Insert at position
4-Delete by position
5-Delete by element
6-Display
7-Display in reverse
8-Exit
Enter choice: 8

Ankur Gaur IT -S[10] 1161563108


AIM: To Implement ReverseLinklist.

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

#define MAX 10

struct lnode
{
int number;
struct lnode *next;
};

void llist_add_begin(struct lnode **n, int val);


void llist_reverse(struct lnode **n);
void llist_display(struct lnode *n);

int main(void)
{
struct lnode *new = NULL;
int i = 0;
clrscr();

for(i = 0; i <= MAX; i++)


llist_add_begin(&new, i);

printf("linked list before reversal:");


llist_display(new);
llist_reverse(&new);
printf("\nlinked list after reversal:");
llist_display(new);

return 0;
}

void llist_add_begin(struct lnode **n, int val) {


struct lnode *temp = NULL;
temp = malloc(sizeof(struct lnode));
temp->number = val;
temp->next = *n;
*n = temp;
}

void llist_reverse(struct lnode **n)


{

Ankur Gaur IT -S[10] 1161563108


struct lnode *a = NULL;
struct lnode *b = NULL;
struct lnode *c = NULL;
a = *n, b = NULL;

while(a != NULL){
c = b, b = a, a = a->next;
b->next = c;
}

*n = b;
}

void llist_display(struct lnode *n) {


while(n != NULL)
printf(" %d", n->number), n = n->next;

printf("\n");
getch();
}

linked list before reversal: 10 9 8 7 6 5 4 3 2 1 0

linked list after reversal: 0 1 2 3 4 5 6 7 8 9 10

AIM :To Implement Binary Search.

#include<stdio.h>
#include<conio.h>
#define MAX 11
main()
{
int top,bottom,middle,i,key;
int data[]={31,45,57,88,143,201,204,327,328,414,499};

printf("sorted list:\n");
for (i=0;i<MAX;i++) printf("%d\n",data[i]);
printf("Enter search key: ");
scanf("%d",&key);

Ankur Gaur IT -S[10] 1161563108


top=0;
bottom=MAX-1;
middle=(top+bottom)/2;
do{
if (data[middle]<key)
top=middle+1;
else
bottom=middle;
middle=(top+bottom)/2;
}while(top<bottom);
if (data[middle]==key)
printf("%d was found at location %d\n",key,middle);
else
printf("%d was not found\n",key);
}

sorted list:
31
45
57
88
143
201
204
327
328
414
499
Enter search key: 31
31 was found at location 0

AIM: To Implement Bubble Sort.

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 10

char name[MAX][15];
void sort(int n)
{

Ankur Gaur IT -S[10] 1161563108


int pa,cp,i,j,k,kk=0;
char temp[15];
pa=n-1;
cp=n-1;
for(i=1;i<=pa;i++)
{
for(j=1;j<=cp;j++)
{
kk=kk+1;
if(strcmp(name[j],name[j+1])>0)
{
strcpy(temp,name[j]);
strcpy(name[j],name[j+1]);
strcpy(name[j+1],temp);
}
}
printf("\n\n List after %d pass is ",i);
for(k=1;k<=n;k++)
printf("\n\t\t %s",name[k]);
getch();
}
printf("\nTotal Comparisions Done : %d",kk);
}

void main()
{
int n,i,j;
clrscr();
printf("Enter How Many Names : ");
scanf("%d",&n);
if(n>MAX)
printf("\n\t\tArray Size IS Only %d",MAX);
else
{
printf("\n\t\tEnter %d Names :\n",n);
for(i=1;i<=n;i++)
{
printf("\t\t");
scanf("%s",name[i]);
}
sort(n);
printf("\n\n\tSorted List ");
for(i=1;i<=n;i++)
printf("\n\t\t%s",name[i]);
}
getch();
}

Ankur Gaur IT -S[10] 1161563108


Enter How Many Names : 5

Enter 5 Names :
ankur
rinku
nitesh
sarita
garima

List after 1 pass is


ankur
nitesh
rinku
garima
sarita

List after 2 pass is


ankur
nitesh
garima
rinku
sarita

List after 3 pass is


ankur
garima
nitesh
rinku
sarita

List after 4 pass is


ankur
garima
nitesh
rinku
sarita

Total Comparisions Done : 16

Sorted List
ankur
garima
nitesh

Ankur Gaur IT -S[10] 1161563108


rinku
sarita

AIM: To Implement Heap Sort.

#include <stdio.h>
#include <conio.h>

void swap(int *x,int *y)


{
int temp;
temp=*x;
*x = *y;
*y = temp;
}

void heapsort(int a[],int n)


{
int i,s,f;
for(i=1;i< n;++i)
{
s=i;
f=(s-1)/2;
while( a[f]< a[s])
{
swap(&a[f],&a[s]);
s=f;
if(s==0)
break;
f=(s-1)/2;
}
}
for(i=n-1;i>=1;--i)
{
swap(&a[0],&a[i]);
f=0;
s=1;
if(i==1)
break;
if(i>2)if(a[2]>a[1])s=2;
while( a[f]< a[s] )
{
swap(&a[f],&a[s]);

Ankur Gaur IT -S[10] 1161563108


f=s;
s=2*f+1;
if(i>s+1 )if(a[s+1]>a[s])s=s+1;
if (s>=i)
break;
}
}
}

void main()
{
int a[100],n,i;
clrscr();
printf("\t\tHEAP SORT\n");
printf("\nEnter The Number Of Elements\t: ");
scanf("%d",&n);
printf("\nEnter Elements\n");
for(i=0;i< n;++i)
scanf("%d",&a[i]);
heapsort(a,n);
printf("\n\t\t\tSorted List\n");
for(i=0;i< n;++i)
printf("\t%d",a[i]);
getch();
}

Enter The Number Of Elements : 6

Enter Elements
45
12
3
1
78
6

Sorted List
1 3 6 12 45 78

Ankur Gaur IT -S[10] 1161563108


AIM: To Implement Insertion Sort.

#include<stdio.h>
#include<conio.h>
void insertion(int a[],int n)
{int i,j,x,k;
for(i=1;i<=n-1;i++)
{
j=i;
x=a[i];
while(a[j-1]>x && j>0)
{a[j]=a[j-1];
j=j-1;
}
a[j]=x;
printf("\n\n The array after pass no.%d: ",i);
for(k=0;k<=n-1;k++)
printf("%4d",a[k]);}
}
void main()
{int a[1000],n,i;
clrscr();
printf("\n\nEnter an integer value for total no.s of elements to
be sorted: ");
scanf("%3d",&n);
for(i=0;i<=n-1;i++)
{
printf("\n\nEnter an integer value for element no.%d: ",i+1);
scanf("%4d",&a[i]);
}
insertion(a,n);
printf("\n\n\nFinally sorted array is : ");
for(i=0;i<=n-1;i++)
printf("%4d",a[i]);
}

Enter an integer value for total no.s of elements to be sorted: 6

Enter an integer value for element no.1: 67

Enter an integer value for element no.2: 34

Enter an integer value for element no.3: -23

Ankur Gaur IT -S[10] 1161563108


Enter an integer value for element no.4: 100

Enter an integer value for element no.5: 0

Enter an integer value for element no.6: -68

The array after pass no.1: 34 67 -23 100 0 -68

The array after pass no.2: -23 34 67 100 0 -68

The array after pass no.3: -23 34 67 100 0 -68

The array after pass no.4: -23 0 34 67 100 -68

The array after pass no.5: -68 -23 0 34 67 100

Finally sorted array is : -68 -23 0 34 67 100

AIM: To Implement Merge Sort.

#include<stdio.h>
#include<conio.h>

void mergesort(int a[],int n)


{
int b[50],c,low1,high1,high2,low2;
int i,k,j;
c=1;
while(c< n)
{
low1=0;
k=0;
while(low1+c< n)
{
low2=low1+c ;
high1=low2-1;
if(low2+c-1< n)
high2=low2+c-1;
else
high2=n-1;
i=low1;
j=low2;

Ankur Gaur IT -S[10] 1161563108


while(i<=high1 && j<=high2)
{
if(a[i]<=a[j])
b[k++]= a[i++];
else
b[k++]= a[j++];

}
while(i<=high1)
b[k++]=a[i++];
while(j<=high2)
b[k++] =a[j++];
low1=high2+1;
}
i=low1;
while(k<n)
b[k++] =a[i++];
for(i=0;i<n;i++)
a[i]=b[i];
c=c*2;
}
}

main()
{
int a[20],i,n;
clrscr();
printf("\n\n");

printf("Enter The number Of Elements\t: ");


scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("\nElement %d\t: ",i+1);
scanf("%d",&a[i]);
}

printf("\nArray Before Sorting : ");


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

printf("\nArray After Sorting : ");


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

Ankur Gaur IT -S[10] 1161563108


}

Enter The number Of Elements : 10

Element 1 : 12

Element 2 : 54

Element 3 : 98

Element 4 : 65

Element 5 : 45

Element 6 : 12

Element 7 : 5

Element 8 : 1

Element 9 : 156

Element 10 : 21

Array Before Sorting : 12 54 98 65 45 12 5 1 156 21


Array After Sorting : 1 5 12 12 21 45 54 98 65 156

AIM: To Implement Radix Sort.

#define NUMELTS 100


# include<stdio.h>
#include<conio.h>
#include<math.h>
void radixsort(int a[],int);
void main()

Ankur Gaur IT -S[10] 1161563108


{
int n,a[20],i;
clrscr();

printf("\nenter the number: ");


scanf("%d",&n);
printf("ENTER THE DATA: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
radixsort(a,n);
getch();
}
void radixsort(int a[],int n)
{
int rear[10],front[10],first,p,q,exp,k,i,y,j;
struct
{
int info;
int next;
}node[NUMELTS];
for(i=0;i<n-1;i++)
{
node[i].info=a[i];
node[i].next=i+1;
}
node[n-1].info=a[n-1];
node[n-1].next=-1;
first=0;

for(k=1;k<=2;k++)
{
for(i=0;i<10;i++)
{
front[i]=-1;
rear[i]=-1;
}

while(first!=-1)
{
p=first;
first=node[first].next;
y=node[p].info;
exp=pow(10,k-1);
j=(y/exp)%10;
q=rear[j];

Ankur Gaur IT -S[10] 1161563108


if(q==-1)
front[j]=p;
else
node[q].next=p;
rear[j]=p;
}
for(j=0;j<10&&front[j]==-1;j++)
;
first=front[j];
while(j<=9)
{
for(i=j+1;i<10&&front[i]==-1;i++)
;
if(i<=9)
{
p=i;
node[rear[j]].next=front[i];
}
j=i;
}
node[rear[p]].next=-1;
}
for(i=0;i<n;i++)
{
a[i]=node[first].info;
first=node[first].next;

}
textcolor(YELLOW);
printf("\nDATA AFTER SORTING:");
for(i=0;i<n;i++)
printf("%d\n"a[i]);
}

enter the number: 5


ENTER THE DATA: 9
5
4
8
6

DATA AFTER SORTING:4


5
6

Ankur Gaur IT -S[10] 1161563108


8
9

AIM : To Implement Shell Sort.

#include<stdio.h>
#include<conio.h>

void shellsort(int a[],int n)


{
int j,i,k,m,mid;
for(m = n/2;m>0;m/=2)
{
for(j = m;j<n;j++)
{
for(i=j-m;i>=0;i-=m)
{
if(a[i+m]>=a[i])
break;
else
{
mid = a[i];
a[i] = a[i+m];
a[i+m] = mid;
}
}
}
}
}

main()
{
int a[10],i,n;
clrscr();

printf("Enter The number Of Elements\t: ");


scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nElement %d\t: ",i+1);
scanf("%d",&a[i]);
}

printf("\nArray Befor Sorting : ");

Ankur Gaur IT -S[10] 1161563108


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

printf("\nArray After Sorting : ");


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

Enter The number Of Elements : 5

Element 1 : 21

Element 2 : 36

Element 3 : 54

Element 4 : 2

Element 5 :10

Array Befor Sorting : 21 36 54 2 10


Array After Sorting : 10 2 21 36 54

AIM: To Implement Selection Sort.


#include<stdio.h>
#include<conio.h>

void simplesel(int a[],int b[],int n)


{
int j,i=0,k,H;
int L=32760;
k=0;
L=H;
while(i<n)
{for(j=0;j<n;j++)
{if(a[j]<L)

Ankur Gaur IT -S[10] 1161563108


{L = a[j];
k = j;
}
}
a[k] = H;
b[i] = L;
L = H;
i++;
}
}

main()
{
int a[10],b[10],i,n;
clrscr();

printf("Enter The number Of Elements\t: ");


scanf("%d",&n);
for(i=0;i<n;i++
{
printf("\nElement %d\t: ",i+1);
scanf("%d",&a[i]);
}

printf("\nArray Before Sorting : ");


for(i=0;i<n;i++)
printf("%5d",a[i]);
simplesel(a,b,n);

printf("\nArray After Sorting : ");


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

Enter The number Of Elements : 10

Element 1 : 21

Element 2 : 25

Element 3 : 63

Ankur Gaur IT -S[10] 1161563108


Element 4 : 45

Element 5 : 1

Element 6 : 147

Element 7 : 10

Element 8 : 78

Element 9 : 96

Element 10 : 5
Array Before Sorting : 21 25 63 45 1 147 10 78 96 5
Array After Sorting : 1 5 10 21 25 45 63 78 96 147

AIM: To Implement Quick Sort.

#include<stdio.h>
#include<conio.h>

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


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

main()
{
int a[20],p,q,i,n;
clrscr();

printf("Enter The number Of Elements\t: ");


scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("\nElement %d\t: ",i+1);
scanf("%d",&a[i]);
}

p=0;
q=n-1;
printf("\nArray Befor Sorting : ");
for(i=0;i<n;i++)

Ankur Gaur IT -S[10] 1161563108


printf("%5d",a[i]);
quicksort(a,p,q);

printf("\nArray After Sorting : ");


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

void quicksort(int a[],int p,int q)


{
int j;
if(p<q)
{
j=partition(a,p,q+1);
quicksort(a,p,j-1);
quicksort(a,j+1,q);
}}

int partition(int a[],int m,int p)


{
int v,i,j;
int temp;
v=a[m];
i=m;j=p;
do
{
do{i += 1;}
while(a[i]< v);
do{j -= 1;}
while(a[j]>v);

if(i< j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}}
while(i< j);
a[m] =a[j];
a[j] = v;
return j;
}

Ankur Gaur IT -S[10] 1161563108


Enter The number Of Elements :10

Element 1 : 12

Element 2 : 54

Element 3 : 98

Element 4 : 65

Element 5 : 45

Element 6 : 12

Element 7 : 5

Element 8 : 1

Element 9 : 156

Element 10 : 21

Array Befor Sorting : 12 54 98 65 45 12 5 1 156 21


Array After Sorting : 1 5 12 12 21 45 54 98 65 156

AIM: To Implement Linear Search.

#include<stdio.h>
#include<conio.h>

Ankur Gaur IT -S[10] 1161563108


void main(){

int a[10],i,n,m,c=0;

clrscr();

printf("Enter the size of an array");

scanf("%d",&n);

printf("\nEnter the elements of the array");

for(i=0;i<=n-1;i++){

scanf("%d",&a[i]);

printf("\nThe elements of an array are");

for(i=0;i<=n-1;i++){

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

printf("\nEnter the number to be search");

scanf("%d",&m);

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

if(a[i]==m)

c=1;

break;

Ankur Gaur IT -S[10] 1161563108


if(c==0)

printf("\nThe number is not in the list");

else

printf("\nThe number is found");

getch();

Enter the size of an array10

Enter the elements of the array


9
8
6
5
7
15
45
92
78
36
The elements of an array are 9 8 6 5 7 15 45 92 78 36
Enter the number to be search 94
The number is not in the list

Enter the size of an array 10


Enter the elements of the array

Ankur Gaur IT -S[10] 1161563108


9
8
6
5
7
15
45
92
78
36
The elements of an array r 9 8 6 5 7 15 45 92 78 36
Enter the number to be search 15
The number is found

/* C Program on binary Search Tree */

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<dos.h>
#include<stdlib.h>

struct node
{
char data[15];
struct node *left,*right;
};

void insert(struct node *r,struct node *p)


{
if((r->right==NULL)&&(strcmp(p->data,r->data)>0))
r->right=p;
else if((r->right!=NULL)&&(strcmp(p->data,r->data)>0))
insert(r->right,p);
if((r->left==NULL)&&(strcmp(p->data,r->data)< 0))
r->left=p;
else if((r->left!=NULL)&&(strcmp(p->data,r->data)< 0))
insert(r->left,p);

void tree(struct node *r,int c)


{
int top,flag;
struct node *w,*stack[20];
if(r!=NULL)

Ankur Gaur IT -S[10] 1161563108


{
if(c!=4)
{
if(c == 1)
printf(" %s ",r->data);
tree(r->left,c);
if(c == 2)
printf(" %s ",r->data);
tree(r->right,c);
if(c == 3)
printf(" %s ",r->data);
}
}
if(c == 4)
{
top = 0;
w = r;
flag = 0;
while((top != -1)&&(w!=NULL))
{
while((flag == 0) && (w->left!=NULL))
{
stack[top] = w;
top++;
w = w->left;
}
printf(" %s ",w->data);
if(w->right != NULL)
{
w = w->right;
flag = 0;
}
else
{
top--;
w = stack[top];
flag = 1;
}
}
}
}
void main()
{
int choice,c,i,flag;
char temp='N',temp1[15];
struct node *s,*root,*r,*q;
root = NULL;

Ankur Gaur IT -S[10] 1161563108


do
{
clrscr();
printf("\n 1. Enter");
printf("\n 2. Delete ");
printf("\n 3. Search ");
printf("\n 4. Display");
printf("\n 5. Exit");
printf("\nEnter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("***** Data Entry ***** ");
do
{
s=malloc(sizeof(struct node));
s->left=NULL;
s->right=NULL;
printf("\nEnter Data : ");
scanf("%s",&s->data);
if(root==NULL)
root=s;
else
insert(root,s);
printf("\nEnter Your Elements[y/n] : ");
scanf("%c",&temp);
}
while(temp=='y');
break;

case 2:printf("****** Delete Operation *******\n");


do
{
printf("\nEnter Element To Be Deleted : ");
scanf("%s",temp1);
s=root;i=0;flag=0;
do
{
if(strcmp(s->data,temp1)>0)
{
r=s;
s=s->left;
i=2;
}
if(strcmp(s->data,temp1)==0)
{
flag=1;

Ankur Gaur IT -S[10] 1161563108


if(i==0)
{
if(root->right!=NULL)
{
q=root->left;
root=root->right;
insert(root,q);
}
if(root->right==NULL)
root=root->left;
}
else
{
if(i==1)
{
q=s->left;
r->right=s->right;
if(s->left!=NULL)
insert(r,q);
}
if(i==2)
{
q=s->right;
r->left=s->left;
if(s->right!=NULL)
insert(r,q);
}
}
}
}
while(flag==0&&s!=NULL);
printf("\n Delete Any More[Y/N] : ");
scanf("%c",&temp);
}
while(temp=='y');
break;
case 3:printf("****** Search Operation *******\n");
do
{
printf("\n Enter Name To Be Searched");
scanf("%s",temp1);
i=0;
s=root;
while(s!=NULL&&i==0)
{
if(strcmp(s->data,temp1)< 0)
s=s->right;

Ankur Gaur IT -S[10] 1161563108


if(strcmp(s->data,temp1)>0)
s=s->left;
if(strcmp(s->data,temp1)==0)
i=1;
}
if(i==0)
printf("\nElement Not Found\n");
else
printf("\nElement Found\n");
printf("\nEnter More Elements[Y/N] : ");
scanf("%c",&temp);
}
while(temp=='y');
break;
case 4:clrscr();
do
{
clrscr();
printf("\n 1. Preorder\n 2. Inorder \n 3. Postorder \n 4.
Non Recursion \n 5. Exit");
printf("\nEnter Your Choice : ");
scanf("%d",&c);
if(root==NULL)
printf("Tree Not Started Yet");
else
tree(root,c);
printf("\n Press Any Key To Continue......");
getch();
}
while(c!=5);
break;
}
}
while(choice!=5);
}
/****************** OUTPUT ****************

1. Enter
2. Delete
3. Search
4. Display
5. Exit
Enter Your Choice : 1
/****** Data Entry *******

Enter Data : Lionel


Enter More Elements(Y/N) : y

Ankur Gaur IT -S[10] 1161563108


Enter Data : Dylan
Enter More Elements(Y/N) : y
Enter Data : Daniel
Enter More Elements(Y/N) : y
Enter Data : Malcolm
Enter More Elements(Y/N) : y
Enter Data : Cyril
Enter More Elements(Y/N) : y
Enter Data : Jason
Enter More Elements(Y/N) : n

1. Enter
2. Delete
3. Search
4. Display
5. Exit
Enter Your Choice : 4

1. Preorder
2. Inorder
3. Postorder
4. Non Recursion
5. Exit
Enter Your Choice : 1
Lionel Dylan Malcolm Cyril Daniel Jason

Press Any Key to Contiue....

1. Preorder
2. Inorder
3. Postorder
4. Non Recursion
5. Exit
Enter Your Choice : 2
Malcolm Cyril Dylan Lionel Jason Daniel

Press Any Key to Contiue....

1. Preorder
2. Inorder
3. Postorder
4. Non Recursion
5. Exit
Enter Your Choice : 3
Cyril Malcolm Dylan Lionel Jason Daniel

Press Any Key to Contiue....

Ankur Gaur IT -S[10] 1161563108


1. Preorder
2. Inorder
3. Postorder
4. Non Recursion
5. Exit
Enter Your Choice : 4
Malcolm Cyril Dylan Lionel Jason Daniel

Press Any Key to Contiue....

1. Preorder
2. Inorder
3. Postorder
4. Non Recursion
5. Exit
Enter Your Choice : 5

Press Any Key To Continue.....

1. Enter
2. Delete
3. Search
4. Display
5. Exit
Enter Your Choice : 3

Enter Name To Be Searched : Dylan


Element Found

Enter More Elements(Y/N) : n

1. Enter
2. Delete
3. Search
4. Display
5. Exit
Enter Your Choice : 2

****** Delete Operation *******


Enter Element To Be Deleted : Dylan
Delete Any More(Y/n) : n

1. Enter
2. Delete
3. Search

Ankur Gaur IT -S[10] 1161563108


4. Display
5. Exit
Enter Your Choice : 5
*/

Ankur Gaur IT -S[10] 1161563108

Vous aimerez peut-être aussi