-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathq.c
80 lines (73 loc) · 1.47 KB
/
q.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
#include <stdio.h>
#include <stdlib.h>
#include <TCB.h>
struct Node
{
int data;
struct Node *next;
struct Node *prev;
};
void InitQ(struct Node **head){
*head = 0;
}
void AddQ(struct Node **head, int data){
struct Node *temp = (struct Node *) malloc (sizeof (struct Node));
temp->data = data;
if(*head == 0){
//When the head pointer is null
temp->next = 0;
temp->prev = 0;
*head = temp;
}
else if((*head)->prev == 0){
//When there is one node
(*head)->next = temp;
(*head)->prev = temp;
temp->next = *head;
temp->prev = *head;
}
else{
//When there is more than one node
(*head)->prev->next = temp;
temp->prev = (*head)->prev;
(*head)->prev = temp;
temp->next = *head;
}
}
struct Node * DelQ(struct Node **head){
if((*head) == 0)
return 0;
struct Node *temp = *head;
if((*head)->next == 0){
//When there is one node
*head = (*head)->next;
}
else if((*head)->prev == (*head)->next){
//When there are two nodes
*head = (*head)->next;
(*head)->prev = 0;
(*head)->next = 0;
}
else if((*head)->prev != (*head)->next){
//When there are more than two nodes
*head = (*head)->next;
temp->prev->next = *head;
(*head)->prev = temp->prev;
}
return temp;
}
void RotateQ(struct Node **head){
if(*head != 0)
(*head) = (*head)->next;
}
int main(){
struct Node *head;
InitQ(&head);
AddQ(&head,4);
AddQ(&head,5);
AddQ(&head,6);
struct Node *temp = DelQ(&head);
temp = DelQ(&head);
temp = DelQ(&head);
temp = DelQ(&head);
}