Step by step

My diary

...

Search

breakinformation. Powered by Blogger.

2019년 9월 22일 일요일

Linked stack _Alpha


"linked stack.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
#ifndef _LINKED_STACK_
#define _LINKED_STACK_
 
struct Stack_Node_Type {
 
    char data;
    struct Stack_Node_Type* Pointer_Next;
};
 
struct Linked_Stack_Type {
 
    int Current_Element_Count;
    struct Stack_Node_Type Header;
};
 
struct Linked_Stack_Type* Create_Linked_Stack(void);
int Push_Linked_Stack(struct Linked_Stack_Type* Pointer_Stack, struct Stack_Node_Type element);
struct Stack_Node_Type* Pop_Linked_Stack(struct Linked_Stack_Type* Pointer_Stack);
struct Stack_Node_Type* Peek_Linked_Stack(struct Linked_Stack_Type* Pointer_Stack);
void Delete_Linked_Stack(struct Linked_Stack_Type* Pointer_Stack);
int Is_Linked_Stack_Empty(struct Linked_Stack_Type* Pointer_Stack);
void Display_Linked_Stack(struct Linked_Stack_Type* Pointer_Stack);
 
#endif // !_LINKED_STACK_
 
#ifndef _COMMON_STACK_DEFAULT_
#define _COMMON_STACK_DEFAULT_
 
#define TRUE    1
#define FALSE    0
 
#endif // !_COMMON_STACK_DEFAULT_
 
 
cs
"linked stack.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linked stack.h"
 
struct Linked_Stack_Type* Create_Linked_Stack(void) {
 
    struct Linked_Stack_Type* Pointer_Return = (struct Linked_Stack_Type*malloc(1 * sizeof(struct Linked_Stack_Type));
    if (!(struct Linked_Stack_Type*)Pointer_Return) {
 
        printf("Error! The linked stack was empty. Create_Linked_Stack()\n");
        return NULL;
    }
 
    (void*)memset((struct Linked_Stack_Type*)Pointer_Return, 01 * sizeof(struct Linked_Stack_Type));
 
    return (struct Linked_Stack_Type*) Pointer_Return;
}
 
int Push_Linked_Stack(struct Linked_Stack_Type* Pointer_Stack, struct Stack_Node_Type element) {
 
    if (!(struct Linked_Stack_Type*)Pointer_Stack) {
 
        printf("Error! The linked stack was empty. Push_Linked_Stack()\n");
        return FALSE;
    }
    
    struct Stack_Node_Type* Pointer_Top_Node = (struct Stack_Node_Type*malloc(1 * sizeof(struct Stack_Node_Type));
    if (!(struct Stack_Node_Type*)Pointer_Top_Node) {
 
        printf("Error! The dynamic memory allocation failed. Push_Linked_Stack()\n");
        return FALSE;
    }
    (void*)memset((struct Stack_Node_Type*)Pointer_Top_Node, 01 * sizeof(struct Stack_Node_Type));
 
    *Pointer_Top_Node = *(struct Stack_Node_Type*&element;
    struct Stack_Node_Type* pointer_Header_Node = &Pointer_Stack->Header;
    Pointer_Top_Node->Pointer_Next = (struct Stack_Node_Type*) pointer_Header_Node->Pointer_Next;
    pointer_Header_Node->Pointer_Next = (struct Stack_Node_Type*)Pointer_Top_Node;
 
    Pointer_Stack->Current_Element_Count++;
 
    return TRUE;
}
 
struct Stack_Node_Type* Pop_Linked_Stack(struct Linked_Stack_Type* Pointer_Stack) {
 
    if (!(struct Linked_Stack_Type*)Pointer_Stack) {
 
        printf("Error! The linked stack was empty. Pop_Linked_Stack()\n");
        return NULL;
    }
    if (Is_Linked_Stack_Empty((struct Linked_Stack_Type*)Pointer_Stack)) {
 
        printf("Error! The linked stack was empty(Number of current node). Pop_Linked_Stack()\n");
        return NULL;
    }
 
    struct Stack_Node_Type* Pointer_Header_Node = (struct Stack_Node_Type*&Pointer_Stack->Header;
    struct Stack_Node_Type* Pointer_Return = (struct Stack_Node_Type*) Pointer_Header_Node->Pointer_Next;
    Pointer_Header_Node->Pointer_Next = (struct Stack_Node_Type*)Pointer_Return->Pointer_Next;
    Pointer_Return->Pointer_Next = NULL;
 
    Pointer_Stack->Current_Element_Count--;
 
    return (struct Stack_Node_Type*)Pointer_Return;
}
 
struct Stack_Node_Type* Peek_Linked_Stack(struct Linked_Stack_Type* Pointer_Stack) {
 
    if (!(struct Linked_Stack_Type*)Pointer_Stack) {
 
        printf("Error! The linked stack was empty. Peek_Linked_Stack()\n");
        return NULL;
    }
    if (Is_Linked_Stack_Empty((struct Linked_Stack_Type*)Pointer_Stack)) {
 
        printf("Error! The linked stack was empty(Number of current node). Peek_Linked_Stack()\n");
        return NULL;
    }
 
    return (struct Stack_Node_Type*)Pointer_Stack->Header.Pointer_Next;
}
 
void Delete_Linked_Stack(struct Linked_Stack_Type* Pointer_Stack) {
 
    if (!(struct Linked_Stack_Type*)Pointer_Stack) {
 
        printf("Error! The linked stack was empty. Delete_Linked_Stack()\n");
        return;
    }
 
    struct Stack_Node_Type* Pointer_Header_Node = (struct Stack_Node_Type*)&Pointer_Stack->Header;
    int loop = 0;
    for (loop = Pointer_Stack->Current_Element_Count; loop; loop--) {
 
        struct Stack_Node_Type* Pointer_Delete_Node = (struct Stack_Node_Type*)Pointer_Header_Node->Pointer_Next;
        Pointer_Header_Node->Pointer_Next = (struct Stack_Node_Type*) Pointer_Delete_Node->Pointer_Next;
        free((struct Stack_Node_Type*)Pointer_Delete_Node);
    }
    free((struct Linked_Stack_Type*)Pointer_Stack);
    Pointer_Stack = NULL;
 
    return;
}
 
int Is_Linked_Stack_Empty(struct Linked_Stack_Type* Pointer_Stack) {
 
    if (!(struct Linked_Stack_Type*)Pointer_Stack) {
 
        printf("Error! The linked stack was empty. Is_Linked_Stack_Empty()\n");
        return FALSE;
    }
 
    if (Pointer_Stack->Current_Element_Count == 0return TRUE;
    return FALSE;
}
 
void Display_Linked_Stack(struct Linked_Stack_Type* Pointer_Stack) {
 
    if (!(struct Linked_Stack_Type*)Pointer_Stack) {
 
        printf("Error! The linked stack was empty. Display_Linked_Stack()\n");
        return;
    }
 
    printf("Number of current node : %d\n", Pointer_Stack->Current_Element_Count);
    
    struct Stack_Node_Type* Pointer_Top_Node = (struct Stack_Node_Type*)Pointer_Stack->Header.Pointer_Next;
    int index = 0;
    for (index = 0; index < Pointer_Stack->Current_Element_Count; index++) {
 
        printf("[%d]-[%c]\n", Pointer_Stack->Current_Element_Count - index - 1, Pointer_Top_Node->data);
        Pointer_Top_Node = (struct Stack_Node_Type*)Pointer_Top_Node->Pointer_Next;
    }
 
    return;
}
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
47
48
49
50
51
#include <stdio.h>
#include <stdlib.h>
#include "linked stack.h"
 
int main(int argc, char* argv[]) {
 
    typedef struct Linked_Stack_Type Linked_Stack;
    typedef struct Stack_Node_Type Stack_Node;
 
    Linked_Stack* Pointer_Stack = (Linked_Stack*)Create_Linked_Stack();
    if (!(Linked_Stack*)Pointer_Stack) return -1;
 
    int loop = 0;
    for (loop = 0; loop < 10; loop++) {
 
        Stack_Node node = { 0, };
        node.data = 'A' + loop;
 
        Push_Linked_Stack((Linked_Stack*)Pointer_Stack, node);
    }
    Display_Linked_Stack((Linked_Stack*)Pointer_Stack);
 
    Stack_Node* Pointer_Node = (Stack_Node*)Pop_Linked_Stack((Linked_Stack*)Pointer_Stack);
    if ((Stack_Node*)Pointer_Node != NULL) {
 
        printf("\nPop-[%c]\n", Pointer_Node->data);
        free((Stack_Node*)Pointer_Node);
    }
    else {
 
        printf("Pop-NULL\n");
    }
    Display_Linked_Stack((Linked_Stack*)Pointer_Stack);
 
    Pointer_Node = (Stack_Node*)Peek_Linked_Stack((Linked_Stack*)Pointer_Stack);
    if ((Stack_Node*)Pointer_Node != NULL) {
 
        printf("\nPeek-[%c]\n", Pointer_Node->data);
    }
    else {
 
        printf("Peek-NULL\n");
    }
    Display_Linked_Stack((Linked_Stack*)Pointer_Stack);
 
    Delete_Linked_Stack((Linked_Stack*)Pointer_Stack);
 
    system("pause");
 
    return 0;
}
cs

0 개의 댓글:

댓글 쓰기