forked from sarthakd999/Hacktoberfest2021-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Doubly_linked_list.c
172 lines (144 loc) · 3.63 KB
/
Doubly_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
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
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
struct node *prev;
};
struct node *start,*tail,*temp,*newnode,*loc;
void insert_at_beginning(){
int d;
newnode=malloc(sizeof(struct node));
printf("enter thedata to be inserted:\n");
scanf("%d",&d);
newnode->next=NULL;
newnode->data=d;
newnode->prev=NULL;
if(start==NULL){
start=newnode;
}
else{
newnode -> next=start;
newnode->prev=NULL;
start->prev=newnode;
start=newnode;
}
printf("%d is Successfully inserted at the beginning\n",newnode->data);
}
void insert_at_end()
{
int d;
printf("Enter data to insert into list\n");
scanf("%d", &d);
newnode=malloc(sizeof(struct node));
newnode->next=NULL;
newnode->data=d;
newnode->prev=NULL;
if (start == NULL)
{
start=newnode;
}
else
{
temp=start;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=newnode;
newnode->prev=temp;
}
printf("%d is Successfully inserted at the end\n",d);
}
void insert_after_particular_node(){
int d,pos,i=0;
newnode=malloc(sizeof(struct node));
printf("Enter the data to be inserted:");
scanf("%d",&d);
printf("Enter the position to insert the new element:");
scanf("%d",&pos);
newnode->next=NULL;
newnode->data=d;
newnode->prev=NULL;
temp=start;
while (temp != NULL && i+1 < pos)
{
temp = temp->next;
i++;
}
if (temp == NULL)
{
printf("That many nodes are not present in the list\n");
}
else{
newnode->next=temp->next;
newnode->prev=temp;
temp->next=newnode;
}
printf("%d is Successfully inserted after %d position\n",d,pos);
}
void delete_Node_containg_particular_item(){
int d;
struct node *loc,*nextnode;
printf("Enter the item that is to be deleted: ");
scanf("%d",&d);
temp=start;
while(temp!=NULL && temp->data!=d){
loc=temp;
temp=temp->next;
}
if(temp==start){
printf("the element deleted is %d\n",start->data);
if(temp->next==NULL){
start=NULL;
}else{
nextnode=temp->next;
nextnode->prev=NULL;
start=nextnode;
return;
}
}
if(temp!=NULL){
loc->next=temp->next;
nextnode=temp->next;
if(nextnode!=NULL){
nextnode->prev=loc;
}
printf("The element deleted is %d\n",temp->data);
}else{
printf("Unsuccessful\n");
}
}
void display(){
struct node *temp ;
temp=start;
printf("Elements in the linked list are: ");
while (temp->next!= NULL)
{
printf("%d, ", temp->data);
temp = temp->next;
}
printf("%d",temp->data);
}
void main(){
int ch;
do{
printf("\nchoose the operation you want to perform\n");
printf("1.Insert at the beginning 2. insert at the end 3. insert after a node\n4.Delete node containing value\t5.Display\t6.exit \n");
scanf("%d",&ch);
switch(ch){
case 1: insert_at_beginning();
break;
case 2: insert_at_end();
break;
case 3: insert_after_particular_node();
break;
case 4: delete_Node_containg_particular_item();
break;
case 5: display();
break;
case 6:printf("existing");
exit(0);
default:printf("Invalid choice");
}
}while(ch!=6);
}