-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
134 lines (110 loc) · 1.89 KB
/
stack.c
File metadata and controls
134 lines (110 loc) · 1.89 KB
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
#define _CRT_SECURE_NO_WARNINGS
/*
typedef int Item; //×Ô¶¨ÒåÕ»ÄÚÈÝ
typedef struct node {
Item item;
struct node* next;
}Node;
typedef struct stack {
Node* top;
}Stack;
void InitializeStack(Stack* ps); //initialize a stack
bool StackIsFull(Stack* ps);
bool StackIsEmpty(Stack* ps);
int StackItemCount(Stack* ps);
bool StackPush(Item item, Stack* ps); //enter a element into the stack
bool StackPop(Item item, Stack* ps); //pop a element out of the stack
bool EmptyTheStack(Stack* ps);
*/
#include<stdio.h>
#include<stdlib.h>
#include"stack.h"
/*jvbu hanshu*/
void InitializeStack(Stack* ps)
{
ps->top = NULL;
}
bool StackIsFull(Stack* ps)
{
/*code*/
}
bool StackIsEmpty(const Stack* ps)
{
if (ps->top == NULL)
return true;
else
return false;
}
int StackItemCount(const Stack* ps)
{
int count = 0;
Node* pnode = ps->top;
if (pnode == NULL)
{
return 0;
}
if (pnode != NULL)
{
count++;
}
while (pnode->next != NULL)
{
count++;
pnode = pnode->next;
}
return count;
}
bool StackPush(Item item, Stack* ps)
{
Node* pnew;
pnew = (Node*)malloc(sizeof(Node));
if (pnew == NULL)
{
return false;
}
if (StackIsEmpty(ps)) //if the stack is empty
{
pnew->item = item;
pnew->next = NULL;
ps->top = pnew;
}
else //if the stack is not empty
{
pnew->item = item;
pnew->next = ps->top;
ps->top = pnew;
}
return true;
}
bool StackPop(Stack* ps)
{
Node* temp;
if (StackIsEmpty(ps))
{
fprintf(stderr, "the stack is empty now");
return false;
}
else
{
temp = ps->top;
ps->top = ps->top->next;
free(temp);
}
return true;
}
void EmptyTheStack(Stack* ps)
{
Node* temp;
Node* scan=ps->top;
if (scan == NULL)
{
return;
}
while ( scan->next!= NULL)
{
temp = scan;
scan = scan->next;
free(temp);
}
free(scan);
}