-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
71 lines (61 loc) · 1.24 KB
/
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
#include "stack.h"
#include <malloc.h>
#include <assert.h>
void init_stack(pstack sta)
{
assert(sta != NULL);
if( sta == NULL ) return;
sta->data = (pSeqList)malloc(sizeof(SeqList));
assert( sta->data != NULL);
init_seq(sta->data);
}
void destroy_stack(pstack sta)
{
assert(sta != NULL);
if( sta == NULL ) return;
destroy_seq(sta->data);
free(sta->data);
}
void clear_stack(pstack sta)
{
assert(sta != NULL);
if( sta == NULL || sta->data == NULL ) return;
clear_seq( sta->data);
}
int isempty_stack(pstack sta)
{
assert(sta != NULL);
if( sta == NULL || sta->data == NULL ) return -1;
if( count_seq(sta->data ) == 0)
{
return 1;
}
else
{
return 0;
}
}
int length_stack(pstack sta)
{
assert(sta != NULL);
if( sta == NULL || sta->data == NULL ) return -1;
return count_seq(sta->data );
}
ElemType stack_top(pstack sta)
{
assert(sta != NULL);
if( sta == NULL || sta->data == NULL ) return -1;
return find_seq_pos(sta->data, count_seq(sta->data) - 1);
}
void push_stack(pstack sta, ElemType val)
{
assert(sta != NULL);
if( sta == NULL || sta->data == NULL ) return;
insert_seq_tail(sta->data, val);
}
void pop_stack(pstack sta)
{
assert(sta != NULL);
if( sta == NULL || sta->data == NULL ) return;
delete_seq_tail(sta->data);
}