-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp4.c
More file actions
216 lines (206 loc) · 3.83 KB
/
p4.c
File metadata and controls
216 lines (206 loc) · 3.83 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define MS 5
int c;
struct student
{
char name[20],branch[20],usn[20],phon[20];
int sem;
float mark1,mark2,mark3,total,average;
struct student*next;
struct student*prev;
};
typedef struct student node;
int countnodes(node*head)
{
node*p;
p=head;
c=0;
while(p!=NULL)
{
p=p->next;
c++;
}
return c;
}
node*getnode()
{
node*nn;
nn=(node*)malloc(sizeof(node));
printf("enter the details\n");
printf("Name:\n");
scanf("%s",nn->name);
printf("Branch:\n");
scanf("%s",nn->branch);
printf("usn:\n");
scanf("%s",nn->usn);
printf("phone:\n");
scanf("%s",nn->phon);
printf("Sem:\n");
scanf("%d",&nn->sem);
printf("mark1,mark2,mark3\n");
scanf("%f%f%f",&nn->mark1,&nn->mark2,&nn->mark3);
nn->next=nn->prev=NULL;
return nn;
}
node*display(node*head)
{
node*p;
if(head==NULL)
printf("empty\n");
else
{
p=head;
printf("Name\tusn\t\tbranch\tphon\tsem\tmark1\tmark2\tmark3\ttotal\taverage\n");
while(p!=NULL)
{
p->total=p->mark1+p->mark2+p->mark3;
p->average=p->total/3;
printf("%s\t%s\t%s\t%s\t%d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n",p->name,p->usn,p->branch,p->phon,p->sem,p->mark1,p->mark2,p->mark3,p->total,p->average);
p=p->next;
}
}
printf("the number of node in a list %d",countnodes(head));
return head;
}
node*create(node*head)
{
node*nn,*p;
if(countnodes(head)==MS)
{
printf("full\n");
}
else if(head==NULL)
{
nn=getnode(head);
head=nn;
}
else
{
p=head;
nn=getnode(head);
while(p->next!=NULL)
{
p=p->next;
}
p->next=nn;
nn->prev=p;
}
return head;
}
node*insertfront(node*head)
{
node*nn;
if(countnodes(head)==MS)
{
printf("full\n");
}
else if(head=NULL)
{
nn=getnode(head);
head=nn;
}
else
{
nn=getnode(head);
nn->next=head;
head->prev=nn;
head=nn;
}
return head;
}
node*insertrear(node*head)
{
if(countnodes(head)==MS)
{
printf("full\n");
}
else
{
head=create(head);
}
return head;
}
node*deletefront(node*head)
{
node*p;
if(head==NULL)
{
printf("full\n");
}
else if(countnodes(head)==1)
{
p=head;
head=NULL;
free(p);
}
else
{
p=head;
((head->next)->next!=NULL);
head=head->next;
p->next=NULL;
free(p);
}
return head;
}
node*deleterear(node*head)
{
node*p,*q;
if(head==NULL)
{
printf("empty\n");
}
else if(countnodes(head)==1)
{
p=head;
head=NULL;
free(p);
}
else
{
p=head;
while(p->next!=NULL)
{
p=p->next;
}
q=p->prev;
q->next=NULL;
p->prev=NULL;
free(p);
}
return head;
}
void main()
{
int i,n,ch;
node*head;
head=NULL;
do
{
printf("\nMenu\t1.create\t2.display\t3.insertfront\t4.insertrear\t5.deletefront\t6.deleterear\t7.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch (ch)
{
case 1:printf("entr the number of student\n");
scanf("%d",&n);
for(i=0;i<n;i++)
head=create(head);
break;
case 2: head=display(head);
break;
case 3: head=insertfront(head);
break;
case 4: head=insertrear(head);
break;
case 5: head=deletefront(head);
break;
case 6: head=deleterear(head);
break;
case 7:exit(0);
default:printf("invalid choice");
}
}while(ch>=1&&ch<=7);
}