-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoublelinklist.c
180 lines (178 loc) · 3.74 KB
/
doublelinklist.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
#include<stdio.h>
#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;
struct node *prev;
};
struct node *head=NULL;
int main()
{
int choice;
while(1){
printf("\n enter 1 creat node: \n 2 for insert in beginning:\n 3 for insert at end:\n 4 for insert at position:\n 5 for display:\n and 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");
}
}
}
void create_node()
{
struct node *temp,*ptr; //ptr point that node
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:");
scanf("%d",&temp->info);
temp->next=NULL;
temp->prev=NULL;
}
if(head==NULL)
{
head=temp;
}
else
{
ptr=head;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
temp->next=NULL;
temp->prev=ptr;
}
}
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;
temp->prev=NULL;
if(head==NULL)
{
head=temp;
}
else
{
temp->next=head;
head->prev=temp;
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;
temp->prev=NULL;
if(head==NULL)
{
head=temp;
}
else
{
ptr=head;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
temp->prev=head;
temp->next=NULL;
}
}
void insert_pos()
{
int i,loc,item;
struct node *ptr1,*ptr2,*temp;
ptr1=(struct node *)malloc(sizeof(struct node));
if(ptr1==NULL)
{
printf("\n OVERFLOW");
}
else
{
printf("\n Enter element value \n");
scanf("%d",&item);
ptr1->info=item;
printf("\n Enter the location after which you want to insert \n");
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;
}
}
ptr1 ->next= temp->next;
ptr2 ->prev=temp->prev;
temp->next=ptr1;
temp->prev=ptr1;
printf("\n Node inserted \n");
}
}
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;
}
}
}