-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path89dcllDeleteFirst.c
152 lines (137 loc) · 2.88 KB
/
89dcllDeleteFirst.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
#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 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);
deleteFirst(&head,&tail);
display(&head);
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;
}