-
Notifications
You must be signed in to change notification settings - Fork 0
/
Link1.c
206 lines (193 loc) · 4.24 KB
/
Link1.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
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
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int num;
struct node *next;
}node;
int size=sizeof(node);
void add(node* head)
{
int a;
printf("Enter a Number: ");
scanf("%d",&a);
node* temp=malloc(sizeof(node));
node* ptr=head;
temp->num=a;
temp->next=NULL;
while(ptr->next!=NULL){
ptr=ptr->next;
}
ptr->next=temp;
}
void printlist(node* x)
{
int i=1;
while(x!=NULL)
{
printf("Element %d is : %d \n",i,x->num);
x=x->next;
i++;
}
}
void delete(node* n)//frees memory
{
if(n==NULL)
{
return;
}
delete(n->next);
free(n);
}
//inserting element at the front
node* insertelementf(node* n)
{
int a;
node* y=malloc(sizeof(node));
printf("Enter a Number: ");
scanf("%d",&a);
y->num=a;
y->next=n;
return y;
}
node* insertelement()
{
int a;
node* y=malloc(sizeof(node));
if(y==NULL)//memory check
{
printf("Exiting :(\n");
exit(1);
}
printf("Enter a Number: ");
scanf("%d",&a);
y->num=a;
y->next=NULL;
return y;
}
int addf(node* head,int numinp)
{
int flag;
node* new1;
while(head!=NULL)
{
if(head->next->num==numinp)
{
flag=0;
new1=insertelement(head);
new1->next=head->next;
head=new1;
break;
}
else
flag++;
head=head->next;
}
return flag;
}
int main(void)
{
int a;
printf("Let's Make a Linked List\nEnter the size of the List: ");
scanf("%d",&a);
node* ele[a];
ele[0]=insertelement();
for (int i = 1; i < a; i++)
{
ele[i]=insertelement();
ele[i-1]->next=ele[i];
}
printf("\nYour List\n");
printlist(ele[0]);
int inpt=0;
printf("\nWould you like to input a new element(press 1/0): ");
scanf("%d",&inpt);
if(inpt == 1)
{
node* new=insertelement();
ele[a-1]->next=new;
printf("\nNew List\n");
printlist(ele[0]);
//delete(ele[0]);
}
else
{
printf("\nBye\n");
delete(ele[0]);
exit(0);
}
printf("\nWould you like to delete element(press 1 TO DELETE THE FIRST)(press 0 TO DELETE THE LAST): ");
scanf("%d",&inpt);
if(inpt==1)
{
node* tmp=ele[0];
ele[0]=ele[0]->next;
free(tmp);
printf("\nNew List\n");
printlist(ele[0]);
//delete(ele[0]);
}
else if(inpt==0)
{
node* tmp=ele[a-1]->next;
ele[a-1]->next=NULL;
free(tmp);
printf("\nNew List\n");
printlist(ele[0]);
//delete(ele[0]);
}
else
{
printf("\nBye\n");
delete(ele[0]);
exit(0);
}
printf("Would you like to add an element before a specific node(1/0):");
scanf("%d",&inpt);
if(inpt==1)
{
int numinp;
printf("Enter the element before which you wanna add the number:");
scanf("%d",&numinp);
int flag=0;
node* new1;
if(ele[0]->num==numinp)
{
flag=0;
new1=insertelementf(ele[0]);
ele[0]=new1;
}
else
{
/*while(ele[0]!=NULL)
{
if(ele[0]->next->num==numinp)
{
flag=0;
new1=insertelement(ele[0]);
new1->next=ele[0]->next;
ele[0]=new1;
break;
}
else
flag++;
ele[0]=ele[0]->next;
}*/
flag=addf(ele[0],numinp);
}
if(flag==0)
{
printf("\nNew List\n");
printlist(ele[0]);
delete(ele[0]);
}
else
printf("Number Not found\n");
}
else
{
printf("\nBye\n");
delete(ele[0]);
exit(0);
}
}