-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLink_list.c
82 lines (80 loc) · 1.7 KB
/
Link_list.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<stdio.h>
struct List *memory(int);
struct List //creating Structure fro data and next Pointer
{
int data;
struct List *next; //next contain address for next node
};
struct List *start=NULL; //contain address of starting node
void add()
{
int data;
struct List *ptr,*prt1;
char ch;
do
{
printf("enter data ");
scanf("%d",&data);
ptr=memory(data);
if(start==NULL)
{
start=ptr;
}
else
{
prt1=start;
while(prt1->next!=NULL)
{
prt1=prt1->next;
}
prt1->next=ptr;
}
printf("\ndo you want to add more (y/n)? ");
fflush(stdin);
ch=getchar();
}while(ch=='y'|| ch=='Y');
}
void traverse()
{
struct List *ptr;
if(start==NULL)
{
printf("\nlist is empty");
}
else
{
ptr=start;
while(ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr=ptr->next;
}
}
}
struct List *memory(int data) //create a node of structure type and also allocate memory
{
struct List *ptr=(struct List *)malloc(sizeof(struct List)); //allocate dynamic memory to structure
ptr->data=data;
ptr->next=NULL;
return ptr;
}
void main()
{
int ch;
while(1)
{
printf("\n1. add node");
printf("\n2.traverse");
printf("\n3. exit");
printf("\nenter your choice ");
scanf("\n%d",&ch);
switch(ch)
{
case 1:add();
break;
case 2:traverse();
break;
case 3:exit(0);
}
}
}