-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedList.c
175 lines (154 loc) · 3.21 KB
/
linkedList.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
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *link;
};
struct node *root=NULL;
void atEnd(){
struct node *temp,*p;
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter data you want to store: ");
scanf("%d",&temp->data);
temp->link=NULL;
if(root==NULL){
root=temp;
}
else{
p=root;
while(p->link!=NULL){
p=p->link;
}
p->link=temp;
}
}
void atBeginning(){
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter Data you want to store: ");
scanf("%d",&temp->data);
temp->link=NULL;
if(root==NULL){
root=temp;
}
else{
temp->link=root;
root=temp;
}
}
int length(){
struct node *p;
int count=0;
if(root==NULL){
return 0;
}
else{
p=root;
while(p!=NULL){
p=p->link;
count++;
}
return count++;
}
}
int atAfter(){
int n,i=1;
int len=length();
struct node *temp,*p;
printf("Enter index after which you want to insert element: ");
scanf("%d",&n);
if(n>len){
printf("Index not Valid!\n");
return 0;
}
else{
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter data you want to store: ");
scanf("%d",&temp->data);
temp->link=NULL;
p=root;
while(i<n){
p=p->link;
i++;
}
temp->link=p->link;
p->link=temp;
return 1;
}
}
void display(){
int len=length();
struct node *p;
if(len==0){
printf("\nNothing to Display!!\n");
}
else{
p=root;
for(int i=1;i<=len;i++){
printf("%d->",p->data);
p=p->link;
}
}
}
void deleteElement(){
int n,i=1;
int len=length();
struct node *p=root,*temp;
if(len==0){
printf("Nothing to Delete!\n");
}
else{
printf("Enter index(1-%d) of element you want to delete: ",len);
scanf("%d",&n);
while(i<n-1){
p=p->link;
}
temp=p;
p=p->link;
p=p->link;
temp->link=p;
printf("DELETED!!\n");
}
}
int main(){
int O,v;
pick: printf("\n\nLLL--LinkedList--LLLLLLLL\n");
printf("-----------CHOICES-----------\n");
printf("1.At End\n2.At Beginning\n3.At After\n4.Length of List\n5.Display\n6.Delete element\n7.EXIT!!\n");
printf("------------------------------\n\n");
printf("ENTER YOUR CHOICE: ");
scanf("%d",&O);
switch(O){
case 1:
atEnd();
printf("\nAdded!\n");
break;
case 2:
atBeginning();
printf("\nAdded!\n");
break;
case 3:
v=atAfter();
if(v){
printf("\nAdded!!\n");
}
else{
printf("\nTry different Choice!\n");
}
break;
case 4:
v=length();
printf("\nLength= %d\n",v);
break;
case 5:
display();
break;
case 6:
deleteElement();
break;
case 7:
exit(0);
}
goto pick;
return 0;
}