-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedlistSortedSearch.c
61 lines (59 loc) · 1.23 KB
/
LinkedlistSortedSearch.c
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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
void main() {
struct node *p,*q,*start,*loc;
int item,found=0,ele;
char choice='y';
start=NULL;
while (choice=='y')
{
p=malloc(sizeof(struct node));
printf("Enter the element");
scanf("%d",&item);
p->data=item;
p->link=NULL;
if(start==NULL) {
start=p;
q=p;
}
else {
q->link=p;
q=p;
}
printf("Enter another element(y/n)");
getchar();
choice=getchar();
}
printf("Searching in Sorted list\n");
printf("Enter the element to be searched:");
scanf("%d",&ele);
printf("Linked list elements\n");
p=start;
while (p!=NULL)
{
printf("Item %d is at address %p\n",p->data,p);
p=p->link;
}
p=start;
loc=start;
while (p!=NULL)
{
if(p->data!=ele)
p=p->link;
else if(p->data==ele) {
loc=p;
printf("Element %d found at %p\n",ele,loc);
found=1;
break;
}
}
if(found==0) {
printf("Element does not exist\n");
loc->link=NULL;
}
}