#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *link;
}NODE;
NODE * get_node()
{
NODE *p;
p = (NODE*)malloc(sizeof(NODE));
printf("Enter data:");
scanf("%d",&p->data);
p->link = NULL;
return p;
}
NODE * create_sll()
{
int n,i;
NODE *first,*last,*q;
printf("Enter no.of nodes:");
scanf("%d",&n);
first = last = NULL;
for(i=1;i<=n;i++)
{
q = get_node();
if(first==NULL)
first = q;
else
last->link = q;
last = q;
}
return first;
}
void display(NODE *h)
{
while(h!=NULL)
{
printf("%4d",h->data);
h=h->link;
}
printf("\n");
}
int search(NODE *h, int key)
{
int i=1;
while(h!=NULL && h->data!=key)
{
i++;
h=h->link;
}
if(h==NULL)
return -1;
else
return i;
}
void main()
{
NODE *h=NULL;
int ch,key,i;
while(1)
{
printf("1.Create\n");
printf("2.Display\n");
printf("3.Search\n");
printf("4.Exit\n");
printf("Enter your choice (1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1:
h = create_sll();
break;
case 2:
display(h);
break;
case 3:
printf("Enter search key:");
scanf("%d",&key);
i = search(h,key);
if(i==-1)
printf("Key %d not found\n",key);
else
printf("Key %d found at position %d\n",key,i);
break;
case 4:
exit(0);
}
}
}
Output-
1.Create
2.Display
3.Search
4.Exit
Enter your choice (1-4): 1
Enter no.of nodes:2
Enter data:2
Enter data:3
1.Create
2.Display
3.Search
4.Exit
Enter your choice (1-4):2
2 3
1.Create
2.Display
3.Search
4.Exit
Enter your choice (1-4):3
Enter search key:3
Key 3 found at position 2
1.Create
2.Display
3.Search
4.Exit
Enter your choice (1-4):4
Output-
1.Create
2.Display
3.Search
4.Exit
Enter your choice (1-4): 1
Enter no.of nodes:2
Enter data:2
Enter data:3
1.Create
2.Display
3.Search
4.Exit
Enter your choice (1-4):2
2 3
1.Create
2.Display
3.Search
4.Exit
Enter your choice (1-4):3
Enter search key:3
Key 3 found at position 2
1.Create
2.Display
3.Search
4.Exit
Enter your choice (1-4):4
Output-
1.Create
2.Display
3.Search
4.Exit
Enter your choice (1-4): 1
Enter no.of nodes:2
Enter data:2
Enter data:3
1.Create
2.Display
3.Search
4.Exit
Enter your choice (1-4):2
2 3
1.Create
2.Display
3.Search
4.Exit
Enter your choice (1-4):3
Enter search key:3
Key 3 found at position 2
1.Create
2.Display
3.Search
4.Exit
Enter your choice (1-4):4
Recent Posts
Posted on 2019-07-18
Posted on 2019-07-18
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-05-28
Posted on 2019-05-24
Posted on 2019-05-24
Posted on 2019-05-23
Posted on 2019-05-23
Posted on 2019-05-23
Posted on 2019-05-23
Posted on 2019-05-23
Posted on 2019-05-23
Posted on 2019-05-23
Posted on 2019-05-23
Posted on 2019-05-23