-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathq_degug.c
140 lines (122 loc) · 2.99 KB
/
q_degug.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
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
struct Node *prev;
};
void InitQ(struct Node **head){
*head = 0;
}
void dispPtrs(struct Node **head){
printf("Pointer - %p\nPrev Pointer - %p\nNext Pointer - %p\n",(void *)(*head),(void *)(*head)->prev,(void *)(*head)->next);
}
void AddQ(struct Node **head, int data){
printf("Adding Node %d\n",data);
struct Node *temp = (struct Node *) malloc (sizeof (struct Node));
temp->data = data;
//When the head pointer is null
if(*head == 0){
temp->next = 0;
temp->prev = 0;
printf("temp pointer\n");
dispPtrs(&temp);
*head = temp;
printf("head pointer\n");
dispPtrs(head);
}
else if((*head)->prev == 0){
(*head)->next = temp;
(*head)->prev = temp;
temp->next = *head;
temp->prev = *head;
printf("temp pointer\n");
dispPtrs(&temp);
printf("head pointer\n");
dispPtrs(head);
}
else{
(*head)->prev->next = temp;
temp->prev = (*head)->prev;
(*head)->prev = temp;
temp->next = *head;
printf("temp pointer\n");
dispPtrs(&temp);
printf("head pointer\n");
dispPtrs(head);
}
}
struct Node * DelQ(struct Node **head){
printf("Inside Delete\n");
if((*head) == 0)
return 0;
struct Node *temp = *head;
printf("temp pointer\n");
dispPtrs(&temp);
if((*head)->next == 0){
//When there is at least one node
printf("One node\n");
*head = (*head)->next;
if((*head) == 0){
printf("Head pointer is null\n");
}
}
else if((*head)->prev == (*head)->next){
//When there are two nodes
printf("Two nodes\n");
*head = (*head)->next;
(*head)->prev = 0;
(*head)->next = 0;
printf("head pointer\n");
dispPtrs(head);
}
else if((*head)->prev != (*head)->next){
//When there are more than two nodes
printf("Greater than two nodes\n");
*head = (*head)->next;
temp->prev->next = *head;
(*head)->prev = temp->prev;
printf("head pointer\n");
dispPtrs(head);
}
return temp;
}
void RotateQ(struct Node **head){
if(*head != 0)
(*head) = (*head)->next;
}
int main(){
struct Node *head;
InitQ(&head);
printf("Initialized queue\n\n");
AddQ(&head,4);
printf("Displaying head after adding one node\n");
dispPtrs(&head);
//printf("Added 4\n");
printf("Checking added data - %d\n\n",head->data );
AddQ(&head,5);
printf("Displaying head after adding second node\n");
dispPtrs(&head);
//printf("Added 5\n");
printf("Checking added data - %d\n\n",head->next->data);
AddQ(&head,6);
printf("Displaying head after adding second node\n");
dispPtrs(&head);
//printf("Added 5\n");
printf("Checking added data - %d\n\n",head->next->data);
printf("Deleting Node 4\n");
struct Node *temp = DelQ(&head);
printf("Check deleted node - %d\n\n",temp->data);
printf("Deleting Node 5\n");
temp = DelQ(&head);
printf("Check deleted node - %d\n\n",temp->data);
printf("Deleting Node 6\n");
temp = DelQ(&head);
printf("Check deleted node - %d\n\n",temp->data);
printf("Deleting Empty Node\n");
temp = DelQ(&head);
if(temp==0)
printf("Node is empty\n\n");
printf("Exiting Program\n");
}