-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path92dcllDisplay.c
More file actions
195 lines (178 loc) · 3.66 KB
/
92dcllDisplay.c
File metadata and controls
195 lines (178 loc) · 3.66 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include<stdio.h>
#include<stdlib.h>
struct node{
struct node* prev;
int data;
struct node* next;
};
struct node* newNode(int no);
int insertFirst(struct node** src,struct node** tail,int no);
int insertLast(struct node** src,struct node** tail,int no);
int insertAtPos(struct node**,struct node**,int,int);
void display(struct node**);
struct node* findTail(struct node**);
int deleteFirst(struct node**,struct node**);
int deleteLast(struct node**,struct node**);
int deleteAtPos(struct node**,struct node**,int pos);
int main(){
int itr1,num,data;
struct node* head=NULL,*tail=NULL;
printf("Insert number of nodes to be created.\n");
scanf("%d",&num);
for(itr1=0;itr1<num;itr1++){
printf("Enter element %d: ",itr1+1);
scanf("%d",&data);
insertLast(&head,&tail,data);
}
display(&head);
tail=findTail(&head);
return 0;
}
int deleteAtPos(struct node** Head,struct node** Tail,int pos){
struct node* temp=*Head;
if(*Head==NULL){
printf("List is empty.\n");
return -1;
}
if(pos==1){
deleteFirst(Head,Tail);
return 0;
}else{
while(pos>1){
temp=temp->next;
pos--;
}
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
free(temp);
temp=NULL;
return 0;
}
}
int deleteLast(struct node** Head,struct node** Tail){
struct node* temp1=*Head;
struct node* temp2=*Tail;
if(*Head==NULL){
printf("List is empty.\n");
return -1;
}
if(*Head==*Tail){
free(*Head);
*Head=NULL;
}
else{
temp2->prev->next=temp2->next;
temp2->next->prev=temp2->prev;
temp2->next=temp2->prev=NULL;
free(temp2);
temp2=NULL;
}
return 0;
}
int deleteFirst(struct node** Head,struct node** Tail){
struct node* temp1=NULL;
if(*Head==NULL){
printf("List is empty.\n");
return -1;
}
if((*Head)->next==*Head){
temp1=*Head;
free(temp1);
temp1=NULL;
*Head=temp1;
}else{
temp1=*Head;
temp1->next->prev=temp1->prev;
temp1->prev->next=temp1->next;
*Head=temp1->next;
free(temp1);
temp1=NULL;
}
}
int insertAtPos(struct node** src,struct node** tail,int pos,int no){
struct node* temp1=*src;
struct node* temp2=NULL;
if(*src==NULL){
printf("list is empty.\n");
}
if(pos==1){
insertFirst(src,tail,no);
}else{
while(pos>1){
temp1=temp1->next;
pos--;
}
temp2=newNode(no);
temp2->next=temp1;
temp2->prev=temp1->prev;
temp1->prev=temp2;
temp2->prev->next=temp2;
}
}
int insertLast(struct node** src,struct node** tail,int no){
struct node* temp1=*src;
struct node* Ttemp=*tail;
struct node* temp2;
if(*src==NULL){
temp2=newNode(no);
temp2->next=temp2->prev=temp2;
*src=temp2;
*tail=temp2->prev;
}else{
temp2=newNode(no);
temp2->next=Ttemp->next;
Ttemp->next=temp2;
temp2->prev=Ttemp;
(*src)->prev=temp2;
*tail=temp2;
}
}
int insertFirst(struct node** src,struct node** tail,int no){
struct node* temp1=*src;
struct node* temp2;
if(*src==NULL){
temp1=newNode(no);
temp1->prev=temp1->next=temp1;
*src=temp1;
*tail=temp1->prev;
return 0;
}else{
temp2=newNode(no);
temp2->next=temp1;
temp2->prev=temp1->prev;
temp1->prev=temp2;
temp2->prev->next=temp2;
*src=temp2;
*tail=temp2->prev;
}
}
struct node* findTail(struct node** head){
struct node* temp=*head;
if(*head==NULL){
printf("list is empty.\n");
return NULL;
}
while(temp->next!=*head){
temp=temp->next;
}
return temp;
}
void display(struct node** head){
struct node* temp=*head;
if(*head==NULL){
printf("List is empty.\n");
return;
}
printf("Output:\n");
while(temp->next!=*head){
printf("|%d|<=>",temp->data);
temp=temp->next;
}
printf("|%d|\n",temp->data);
}
struct node* newNode(int no){
struct node* new=(struct node*)malloc(sizeof(struct node));
new->data=no;
new->next=new->prev=NULL;
return new;
}