-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsortinglinklistselectionsort.c
More file actions
134 lines (130 loc) · 2.21 KB
/
sortinglinklistselectionsort.c
File metadata and controls
134 lines (130 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data ;
struct node *next;
};
struct node *head=NULL;
void add()
{
struct node *temp,*new;
int da,c;
printf("how many node you want to insert---- ");
scanf("%d",&c);
while(c--)
{
if (head==NULL)
{ head=(struct node*)malloc(sizeof(struct node));
printf("enter the data\n---");
scanf("%d",&da);
head->data=da;
head->next=NULL;
temp=head;
}
else
{ struct node *temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
new=(struct node*)malloc(sizeof(struct node));
printf("enter the data\n---");
scanf("%d",&da);
new->data=da;
new->next=NULL;
temp->next=new;
}
}
}
void display()
{
struct node *temp=head;
while(temp!=NULL)
{printf("data->%d\n",temp->data);
temp=temp->next;
}
}
void sort()
{
struct node *temp=head;
struct node *temp1;
int t;
while(temp->next->next!=NULL)
{temp1=temp->next;
while(temp1!=NULL)
{
if(temp->data>temp1->data)
{t=temp->data;
temp->data=temp1->data;
temp1->data=t;
}
temp1=temp1->next;
}
temp=temp->next;
}
}
struct node* binary(int v)
{
struct node *start=head;
struct node *end=NULL;
struct node *mid;
do
{ mid=middle(start,end);
if(mid==NULL)
{return NULL;}
if(mid->data==v)
{return mid;}
else if(mid->data<v)
{start=mid->next;}
else
end=mid;
}while(end==NULL || end!=start);
return NULL;
}
struct node* middle(struct node * start,struct node * end)
{
struct node *t1=start;
struct node *mid=start;
while(t1!=end)
{
t1=t1->next;
if(t1!=end)
{t1=t1->next;
mid=mid->next;
}
}
return mid;
}
int main()
{
int choice=1;
while (choice==1)
{
int op;
printf("enter the choice \n 1 for adding node\n 2 for display\n 3 for sort item\n 4 for search \n----");
scanf("%d",&op);
if (op==1)
{
add();
}
else if(op==2){
display();
}
else if(op==3)
{ sort();}
else
{ int v;
struct node *t;
printf("enter the data to find ---");
scanf("%d",&v);
t=binary(v);
if(t!=NULL)
{printf("FOUND \n");}
else
{printf("NOT FOUND \n");}
}
printf("to continue press 1---");
scanf("%d",&choice);
}
}