-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoubleLinkedList.c
166 lines (143 loc) · 3.08 KB
/
DoubleLinkedList.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<conio.h>
#include<stdlib.h>
struct node{
struct node *prev;
int data;
struct node *next;
};
struct node *head;
void display(){
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node *));
if(!head){
printf("\n\n**********EMPTY***********\n\n");
}
else{
temp=head;
printf("\n\n**********START PRINTING DATA************\n\n\n");
while(temp){
printf("\n\n%d\n\n",temp->data);
temp=temp->next;
}
printf("\n\n********ALL DATA PRINTED*****************\n\n\n");
}
};
void insertAtBegining(int num){
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node *));
if(!head){
temp->data=num;
temp->next=NULL;
temp->prev=NULL;
head=temp;
}else{
temp->data=num;
temp->prev=NULL;
temp->next=head;
head=temp;
}
};
void insertAtEnd(int num){
struct node *temp,*last;
temp=(struct node *)malloc(sizeof(struct node *));
if(!head){
temp->data=num;
temp->next=NULL;
temp->prev=NULL;
head=temp;
}else{
temp->data=num;
temp->next=NULL;
last=head;
while(last->next!=NULL){
last=last->next;
}
last->next=temp;
temp->prev=last;
}
};
void deleteAtBegining(){
if(!head){
printf("\n\n*******NOTHING TO DELETE**************\n\n");
}else{
printf("\n\ndeleted num is:%d \n\n",head->data);
struct node *first;
first=head;
head=head->next;
free(first);
}
};
void deleteAtEnd(){
if(!head){
printf("\n\n*******NOTHING TO DELETE**************\n\n");
}
else if(head->next==NULL){
struct node *temp;
temp=head;
printf("\n\ndeleted number is %d",temp->data);
head=head->next;
free(temp);
}
else{
struct node *last,*secondlast;
last=head;
secondlast=head;
while(last->next!=NULL){
secondlast=last;
last=last->next;
}
printf("\n\n delted number is %d",last->data);
secondlast->next=NULL;
free(last);
}
};
void main(){
printf("\n*********WELCOME TO DOUBLE LINKED LIST WORLD**************\n");
printf("\n\nINSERTING AT BEGINING\n\n");
insertAtBegining(5);
insertAtBegining(4);
insertAtBegining(3);
insertAtBegining(2);
insertAtBegining(1);
display();
printf("\n\nINSERTING AT END\n\n");
insertAtEnd(6);
insertAtEnd(7);
insertAtEnd(8);
display();
printf("\n\n********DELETING AT BEGINING\n\n");
deleteAtBegining();
deleteAtBegining();
deleteAtBegining();
deleteAtBegining();
deleteAtBegining();
deleteAtBegining();
deleteAtBegining();
deleteAtBegining();
deleteAtBegining();
deleteAtBegining();
printf("\n\nINSERTING AT BEGINING\n\n");
insertAtBegining(5);
insertAtBegining(4);
insertAtBegining(3);
insertAtBegining(2);
insertAtBegining(1);
display();
printf("\n\nINSERTING AT END\n\n");
insertAtEnd(6);
insertAtEnd(7);
insertAtEnd(8);
display();
printf("\n\n********DELETING AT END\n\n");
deleteAtEnd();
deleteAtEnd();
deleteAtEnd();
deleteAtEnd();
deleteAtEnd();
deleteAtEnd();
deleteAtEnd();
deleteAtEnd();
deleteAtEnd();
deleteAtEnd();
}