-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.c
95 lines (94 loc) · 1.74 KB
/
linked_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
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<stdio.h>
#include<stdlib.h>
struct node{
int element;
struct node *next;
};
static struct node *current;
static struct node *head;
void add(int n){
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->next=NULL;
if(current!=NULL){
current->next=temp;
current=temp;
current->element=n;
}
else{
head=temp;
current=head;
current->element=n;
}
}
void itr(){
struct node *iter=head;
struct node* temp;
while(iter!=NULL){
printf("%d\n",iter->element);
iter = iter->next;
}
}
void insert(int num,int position){
struct node *temp;
struct node *bu;
struct node *iter=head;
temp=(struct node*)malloc(sizeof(struct node));
temp->element=num;
for(int i=0;i<position;i++){
bu=iter;
iter=iter->next;
}
bu->next=temp;
temp->next=iter;
if(temp->next==NULL)
current=temp;
}
void del(int num){
struct node *iter=head,*bf;
while(iter!=NULL){
bf=iter;
iter=iter->next;
if(iter->element==num||bf->element==num){
if(bf->element==num)
head=iter;
bf->next=iter->next;
break;
}
}
}
int main(){
int num,pos;
do{
printf("operations available:\n\t\t1.add\n\t\t2.insertion\n\t\t3.deletion\n\t\t4.traversal\nyour choice : ");
scanf("%d",&num);
switch(num){
case 1:{
printf("\n\nenter the number to be added to list : ");
scanf("%d",&num);
add(num);
break;
}
case 2:{
printf("\n\nenter the element an position of insertion : ");
scanf("%d%d",&num,&pos);
insert(num,pos);
break;
}
case 3:{
printf("\n\nenter the number to be deleted : ");
scanf("%d",&num);
del(num);
break;
}
case 4:{
printf("\n\nthe list values are:\n");
itr();
break;
}
}
printf("enter 1 to continue ......");
scanf("%d",&pos);
}while(pos==1);
return 0;
}