Step by step

My diary

...

Search

breakinformation. Powered by Blogger.

2018년 12월 11일 화요일

linked queue


"linkedqueue.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
#ifndef _LINKED_QUEUE_
#define _LINKED_QUEUE_
 
typedef struct Queue_Node_Type {
 
    int data;
    struct Queue_Node_Type* Pointer_Next;
} Queue_Node;
 
typedef struct Linked_Queue_Type {
 
    int Current_Element_Count;
    Queue_Node* Pointer_Front_Node;
    Queue_Node* Pointer_Rear_Node;
} Linked_Queue;
 
Linked_Queue* Create_Linked_Queue(void);
int Enqueue_Linked_Queue(Linked_Queue* Pointer_Queue, Queue_Node element);
Queue_Node* Dequeue_Linked_Queue(Linked_Queue* Pointer_Queue);
Queue_Node* Peek_Linked_Queue(Linked_Queue* Pointer_Queue);
int Is_Linked_Queue_Full(Linked_Queue* Pointer_Queue);
int Is_Linked_Queue_Empty(Linked_Queue* Pointer_Queue);
 
#endif // !_LINKED_QUEUE_
 
#ifndef _COMMON_QUEUE_DEFAULT_
#define _COMMON_QUEUE_DEFAULT_
 
#define TRUE    1
#define FALSE    0
 
#endif // !_COMMON_QUEUE_DEFAULT_
 
cs
"display.h"
1
2
3
4
5
6
7
8
9
#ifndef _DISPLAY_
#define _DISPLAY_
 
#include "linkedqueue.h"
 
void Display_Linked_Queue(Linked_Queue* Pointer_Queue);
 
#endif // !_DISPLAY_
 
cs
"function.h"
1
2
3
4
5
6
7
8
#ifndef _FUNCTION_
#define _FUNCTION_
 
#include "linkedqueue.h"
int Enqueue_Linked_Queue_Integer(Linked_Queue* Pointer_Queue, int data);
 
#endif // !_FUNCTION_
 
cs
"linkedqueue.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linkedqueue.h"
 
Linked_Queue* Create_Linked_Queue(void) {
 
    Linked_Queue* Pointer_Return = (Linked_Queue*)malloc(1 * sizeof(Linked_Queue));
    if ((Linked_Queue*)Pointer_Return == NULL) {
 
        printf("Error! Dynamic memory allocation, Create_Linked_Queue()\n");
        return FALSE;
    }
 
    (void*)memset((Linked_Queue*)Pointer_Return, 0sizeof(Linked_Queue));
 
    return (Linked_Queue*)Pointer_Return;
}
 
int Enqueue_Linked_Queue(Linked_Queue* Pointer_Queue, Queue_Node element) {
 
    if ((Linked_Queue*)Pointer_Queue == NULL) {
 
        printf("Error! The linked queue is null. Enqueue_Linked_Queue()\n");
        return FALSE;
    }
 
    Queue_Node* Pointer_Node = (Queue_Node*)calloc(1sizeof(Queue_Node));
    if ((Queue_Node*)Pointer_Node == NULL) {
 
        printf("Error! Dynamic memory allocation, Enqueue_Linked_Queue()\n");
        return FALSE;
    }
 
    *(Queue_Node*)Pointer_Node = element;
    (Queue_Node*)Pointer_Node->Pointer_Next = NULL;
 
    if (Is_Linked_Queue_Empty((Queue_Node*)Pointer_Queue) == TRUE) {
 
        (Linked_Queue*)Pointer_Queue->Pointer_Front_Node = (Queue_Node*)Pointer_Node;
        (Linked_Queue*)Pointer_Queue->Pointer_Rear_Node = (Queue_Node*)Pointer_Node;
    }
    else {
 
        (Linked_Queue*)Pointer_Queue->Pointer_Rear_Node->Pointer_Next = (Queue_Node*)Pointer_Node;
        (Linked_Queue*)Pointer_Queue->Pointer_Rear_Node = (Queue_Node*)Pointer_Node;
    }
    (Linked_Queue*)Pointer_Queue->Current_Element_Count++;
 
    return TRUE;
}
 
Queue_Node* Dequeue_Linked_Queue(Linked_Queue* Pointer_Queue) {
 
    if ((Linked_Queue*)Pointer_Queue == NULL) {
 
        printf("Error! The linked queue is null. Dequeue_Linked_Queue()\n");
        return NULL;
    }
    else if (Is_Linked_Queue_Empty((Linked_Queue*)Pointer_Queue) != FALSE) {
 
        printf("Error! Dequeue_Linked_Queue()\n");
        return NULL;
    }
 
    Queue_Node* Pointer_Return = NULL;
    (Queue_Node*)Pointer_Return = (Linked_Queue*)Pointer_Queue->Pointer_Front_Node;
    (Linked_Queue*)Pointer_Queue->Pointer_Front_Node = (Linked_Queue*)Pointer_Queue->Pointer_Front_Node->Pointer_Next;
    (Queue_Node*)Pointer_Return->Pointer_Next = NULL;
 
    (Linked_Queue*)Pointer_Queue->Current_Element_Count--;
    if (Is_Linked_Queue_Empty((Linked_Queue*)Pointer_Queue) == TRUE) {
 
        (Linked_Queue*)Pointer_Queue->Pointer_Rear_Node = NULL;
    }
 
    return (Queue_Node*)Pointer_Return;
}
 
Queue_Node* Peek_Linked_Queue(Linked_Queue* Pointer_Queue) {
 
    if ((Linked_Queue*)Pointer_Queue == NULL) {
 
        printf("Error! The linked queue is null. Peek_Linked_Queue()\n");
        return NULL;
    }
    else if (Is_Linked_Queue_Empty((Linked_Queue*)Pointer_Queue) == TRUE) {
 
        printf("Error! Peek_Linked_Queue()\n");
        return NULL;
    }
 
    return (Linked_Queue*)Pointer_Queue->Pointer_Front_Node;
}
 
void Delete_Linked_Queue(Linked_Queue* Pointer_Queue) {
 
    if ((Linked_Queue*)Pointer_Queue == NULL) {
 
        printf("Error! The linked queue is null. Delete_Linked_Queue()\n");
        return;
    }
 
    Queue_Node* Pointer_Node = (Linked_Queue*)Pointer_Queue->Pointer_Front_Node;
    Queue_Node* Pointer_Delete_Node = NULL;
    while ((Queue_Node*)Pointer_Node != NULL) {
 
        (Queue_Node*)Pointer_Delete_Node = (Queue_Node*)Pointer_Node;
        (Queue_Node*)Pointer_Node = (Queue_Node*)Pointer_Node->Pointer_Next;
        free((Queue_Node*)Pointer_Delete_Node);
    }
    free((Linked_Queue*)Pointer_Queue);
 
    return;
}
 
int Is_Linked_Queue_Full(Linked_Queue* Pointer_Queue) {
 
    return FALSE;
}
 
int Is_Linked_Queue_Empty(Linked_Queue* Pointer_Queue) {
 
    if ((Linked_Queue*)Pointer_Queue == NULL) {
 
        return FALSE;
    }
    if ((Linked_Queue*)Pointer_Queue->Current_Element_Count == 0return TRUE;
 
    return FALSE;
}
cs
"display.c"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include "display.h"
#include "linkedqueue.h"
 
void Display_Linked_Queue(Linked_Queue* Pointer_Queue) {
 
    if ((Linked_Queue*)Pointer_Queue == NULL) {
 
        printf("Error! The linked queue is null. Display_Linked_Queue()\n");
        return;
    }
 
    printf("Current number of node : %d\n", (Linked_Queue*)Pointer_Queue->Current_Element_Count);
    Queue_Node* Pointer_Node = (Linked_Queue*)Pointer_Queue->Pointer_Front_Node;
    int index = 0;
    while ((Queue_Node*)Pointer_Node != NULL) {
 
        printf("[%d]-[%d]\n", index++, (Queue_Node*)Pointer_Node->data);
        (Queue_Node*)Pointer_Node = (Queue_Node*)Pointer_Node->Pointer_Next;
    }
    return;
}
cs
"function.c"
1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#include "function.h"
 
int Enqueue_Linked_Queue_Integer(Linked_Queue* Pointer_Queue, int data) {
 
    Queue_Node node = { 0, };
    node.data = data;
 
    return (int)Enqueue_Linked_Queue((Linked_Queue*)Pointer_Queue, node);
}
cs
"main.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
#include <stdio.h>
#include "display.h"
#include "function.h"
#include "linkedqueue.h"
 
int main(int argc, char** argv) {
 
    // create queue
    Linked_Queue* Pointer_Queue = Create_Linked_Queue();
    if ((Linked_Queue*)Pointer_Queue == NULLreturn -1;
 
    // queue
    Enqueue_Linked_Queue_Integer((Linked_Queue*)Pointer_Queue, 1);
    Enqueue_Linked_Queue_Integer((Linked_Queue*)Pointer_Queue, 3);
    Enqueue_Linked_Queue_Integer((Linked_Queue*)Pointer_Queue, 4);
    Enqueue_Linked_Queue_Integer((Linked_Queue*)Pointer_Queue, 2);
    Enqueue_Linked_Queue_Integer((Linked_Queue*)Pointer_Queue, 9);
    Enqueue_Linked_Queue_Integer((Linked_Queue*)Pointer_Queue, 5);
    Enqueue_Linked_Queue_Integer((Linked_Queue*)Pointer_Queue, 2);
 
    Display_Linked_Queue((Linked_Queue*)Pointer_Queue);
 
    Queue_Node* Pointer_Node = Dequeue_Linked_Queue((Linked_Queue*)Pointer_Queue);
    if ((Linked_Queue*)Pointer_Node != NULL) {
 
        printf("Dequeue Value-[%d]\n", (Queue_Node*)Pointer_Node->data);
        free((Queue_Node*)Pointer_Node);
    }
 
    (Queue_Node*)Pointer_Node = Dequeue_Linked_Queue((Linked_Queue*)Pointer_Queue);
    if ((Linked_Queue*)Pointer_Node != NULL) {
 
        printf("Dequeue Value-[%d]\n", (Queue_Node*)Pointer_Node->data);
        free((Queue_Node*)Pointer_Node);
    }
    Display_Linked_Queue((Linked_Queue*)Pointer_Queue);
 
    (Queue_Node*)Pointer_Node = (Queue_Node*)Peek_Linked_Queue((Linked_Queue*)Pointer_Queue);
    if ((Linked_Queue*)Pointer_Node != NULL) {
 
        printf("Dequeue Value-[%d]\n", (Queue_Node*)Pointer_Node->data);
    }
    Display_Linked_Queue((Linked_Queue*)Pointer_Queue);
 
    return 0;
}
cs

0 개의 댓글:

댓글 쓰기