-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdll.h
86 lines (55 loc) · 1.34 KB
/
dll.h
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
struct node{
int data;
struct node *next;
struct node*prev;
};
struct node*create_node(struct node*head,struct node*tail,int num){
struct node*temp=tail;
if (head==NULL){
//allocate memory for head
head=(struct node*)malloc(sizeof(struct node));
//head and temp are the same, make temp point to head
temp=head;
//assign values to temp
temp->data=num;
//make the previous half of head point of null
temp->prev=NULL;
//assign second half of temp to null
temp->next=NULL;
tail=temp;
}
//to add other nodes
else{
//assign memory for temp->next where there is currently NULL
temp->next=(struct node*)malloc(sizeof(struct node));
//establish a backward link
temp->next->prev=temp;
//move temp to the next block, where the value will be held
temp=temp->next;
temp->data=num;
//make the next block null
temp->next=NULL;
tail=temp;
}
temp->next=NULL;
tail=temp;
return temp;
}
struct node* delete_at_tail(struct node* head, struct node*tail){
struct node*temp;
if (head->next ==NULL){
temp=head;
printf("%d is popped",temp->data);
head=NULL;
free(temp);
return head;
}
else{
temp=tail;
tail->prev->next=NULL;
printf("%d is popped",temp->data);
tail=tail->prev;
free(temp);
return tail;
}
}