-
Notifications
You must be signed in to change notification settings - Fork 0
/
linklist3.c
160 lines (144 loc) · 2.37 KB
/
linklist3.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include<stdio.h>
#include<stdlib.h>
struct node
{ //structure of node
int info;
struct node *link;
};
int count=0;
struct node *START = NULL; //start pointer to control the link list
struct node * createnode();
void insertnode(); //insert at last
void deletenode(); //delete at first
void viewlist();
int menu();
void insertatfirst();
int getlength();
struct node * createnode() //create node dynamically
{
struct node *n;
n = malloc(sizeof(struct node));
return(n);
}
void insertnode()
{
struct node *temp,*t;
temp = createnode();
printf("Enter the data in node \n");
scanf("%d",&temp->info);
temp->link=NULL;
if(START==NULL)
{
START = temp;
}
else
{
t = START;
while(t->link!=NULL)
{
t = t->link;
}
t->link=temp;
}
}
void deletenode()
{
if(START==NULL)
printf("List is empty\n");
else
{
struct node *q;
q=START;
START=START->link;
free(q);
}
}
void viewlist()
{
struct node *t;
if(START==NULL)
printf("No List is there\n");
else
{
t=START;
while(t!=NULL)
{
printf("%d",t->info);
t=t->link;
}
}
}
void insertatfirst()
{
struct node *newnode;
newnode=createnode();
printf("Enter the data in node\n");
scanf("%d",&newnode->info);
if(START==NULL)
START=newnode;
else
{
newnode->link=START;
START=newnode;
}
}
int getlength()
{
struct node *t;
if(t==NULL)
printf("List is EMPTY\n");
else
{
t=START;
while(t!=NULL)
{
count+=1;
t=t->link;
}
return(count);
}
}
int menu()
{
int ch;
printf("\t\t\t 1. Insert NODE at last in LINK LIST\n");
printf("\t\t\t 2. View LIST in LINK LIST\n");
printf("\t\t\t 3. Delete NODE at first in LINK LIST\n");
printf("\t\t\t 4. Insert NODE at first in LINK LIST\n");
printf("\t\t\t 5. To see the length\n");
printf("\t\t\t 6. Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
return(ch);
}
int main()
{
int k;
while(1)
{
switch(menu())
{
case 1:
insertnode();
break;
case 2:
viewlist();
break;
case 3:
deletenode();
break;
case 4:
insertatfirst();
break;
case 5:
k=getlength();
printf("The length of link list is %d",k);
break;
case 6:
exit(0);
break;
default:
printf("\nThis choice doesn't exist.\n");
}
}
}