-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinklist.c
171 lines (169 loc) · 3.77 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
167
168
169
170
171
#include<stdio.h> //link list practice
#include<stdlib.h>
void create_node();
void insert_begin();
void insert_end();
void insert_pos();
void display();
struct node // creating a node
{
int info;
struct node *next; //one note is pointing another node . same data type can point same data type. //struct node *prev; for double link list . so it can go forward and backward
};
struct node *head=NULL;
int main()
{
int choice;
while(1) //while 1 mean it always true and run infinite time and we add a thing to stop.
{
printf("\n enter 1 for create \n 2 for insertion in beginning \n 3 for insertion in last \n 4 for insertion AFTER THE position YOU WANT\n 5 for display\n 6 for exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
create_node();
break;
case 2:
insert_begin();
break;
case 3:
insert_end();
break;
case 4:
insert_pos();
break;
case 5:
display();
break;
case 6:
exit(0);
default:
printf("you have enter wrong no:\n");
}
}
return 0;
}
void create_node()
{
struct node *temp,*ptr;
temp=(struct node*) malloc(sizeof (struct node)); //Malloc is used for dynamic memory allocation
if(temp==NULL) //and is useful when you don't know the amount of memory needed during compile time
{
printf("\n memory not allocated");
exit(0);
}
else{
printf("\n enter data/info:");
scanf("%d",&temp->info);
temp->next=NULL;
}
if(head==NULL)
{
head=temp;
}
else
{
ptr=head;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
}
}
void insert_begin()
{
struct node*temp;
temp=(struct node*)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\n memory not allocated");
exit(0);
}
printf("\n enter data:");
scanf("%d",&temp->info);
temp->next=NULL;
if(head==NULL)
{
head=temp;
}
else
{
temp->next=head;
head=temp;
}
}
void insert_end()
{
struct node*temp,*ptr;
temp=(struct node*)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\n memory not allocated");
exit(0);
}
printf("\n enter data:");
scanf("%d",&temp->info);
temp->next=NULL;
if(head==NULL)
{
head=temp;
}
else
{
ptr=head;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
}
}
void insert_pos()
{
int i,loc,item;
struct node *ptr, *temp;
ptr =(struct node *)malloc(sizeof(struct node));
if(ptr==NULL)
{
printf("\n OVERFLOW");
}
else
{
printf("\n Enter element value");
scanf("%d",&item);
ptr->info=item;
printf("\n Enter the location after which you want to insert");
scanf("\n%d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp=temp->next;
if(temp==NULL)
{
printf("\n can't insert\n");
return;
}
}
ptr ->next= temp->next;
temp->next=ptr;
printf("\n Node inserted ");
}
}
void display()
{
struct node *ptr;
ptr=head;
if(ptr==NULL)
{
printf("nothing to print");
}
else{
printf("\n printing value.......\n");
while(ptr!=NULL)
{
printf("\n%d",ptr->info);
ptr=ptr->next;
}
}
}