forked from Aashutoshsharma002/algo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
linklist.c
166 lines (164 loc) · 3.58 KB
/
linklist.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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node{
int info;
struct node* link;
}*new_node,*start,*temp,*last;
void create()
{
int value;
int choice;
do
{
new_node = (struct node*)malloc(sizeof(struct node));
printf("enter value");
scanf("%d",&value);
new_node->info=value;
new_node->link=NULL;
if(start==NULL)
{
start=new_node;
last=new_node;
}
else
{
last->link=new_node;
last=new_node;
last->link=NULL;
}
printf("press 1 to continue 0 to exit");
scanf("%d",&choice);
}
while(choice==1);
}
void display()
{
printf("linklist : ");
temp=start;
while(temp->link!=NULL)
{
printf("%d->",temp->info);
temp=temp->link;
}
printf("%d->",temp->info);
printf("NULL");
}
void insert()
{
int choice,pos,item,i;
printf("\n***choice***\n1.insert at beg\n2. insert at end\n3. insert at specfic postion\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("enter element : ");
scanf("%d",&item);
new_node=(struct node*)malloc(sizeof(struct node));
if(new_node==NULL)
{
printf("out of memory\n");
exit(1);
}
new_node->info=item;
new_node->link=start;
start=new_node;
break;
case 2:
printf("enter element : ");
scanf("%d",&item);
new_node=(struct node*)malloc(sizeof(struct node));
if(new_node==NULL)
{
printf("out of memory\n");
exit(1);
}
new_node->info=item;
new_node->link=NULL;
last->link=new_node;
last=new_node;
break;
case 3:
printf("enter element and positon: ");
scanf("%d%d",&item,&pos);
new_node=(struct node*)malloc(sizeof(struct node));
if(new_node==NULL)
{
printf("out of memory\n");
exit(1);
}
new_node->info=item;
temp=start;
for(i=0;i<pos-1;i++)
{
temp=temp->link;
}
new_node->link=temp->link;
temp->link=new_node;
break;
default:
printf("wrong choice");
break;
}
}
void delete()
{
int i, pos, choice,value;
printf("\nenter node to delete");
printf("\n1.press 1 if you want to delete the value at begg \n2.press 2 delete the value at end\n3.press 3 delete at mid\n4.press 4 for exit delete operation ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{ printf("enter value to delete\n");
scanf("%d",&value);
new_node=(struct node*)malloc(sizeof(struct node));
if(start=NULL)
{
printf("out of memory");
}
temp=start;
start=start->link;
temp->info=NULL;
break;
}
case 2:
{
printf("enter value to delete\n");
scanf("%d",&value);
new_node=(struct node*)malloc(sizeof(struct node));
temp==start;
while(temp->link!=last)
{temp=temp->link;
}
temp->info = NULL;
last=temp;
break;
}
case 3:
{
printf("enter the value or pos in which you want to delete");
scanf("%d%d",&pos,&value);
new_node=(struct node*)malloc(sizeof(struct node));
temp=start;
for(i=0;i<=pos-1;i++)
{
temp=temp->link;
}
new_node->link=temp->link;
temp->link=new_node;
break;
}
}
}
int main()
{
//start=NULL;
create();
display();
insert();
display();
delete();
display();
return 0;
}