-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.txt
More file actions
100 lines (83 loc) · 1.77 KB
/
Stack.txt
File metadata and controls
100 lines (83 loc) · 1.77 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
// this code is made to implemet Stack.
// Stack
// I did my best for this subject, but screwed up all for oversleeping
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STACK 100
typedef enum { false, true } bool;
typedef int Data;
typedef struct
{
Data item[MAX_STACK];
int top;
}Stack;
void InitStack(Stack* pstack);
bool IsFull(Stack* pstack);
bool IsEmpty(Stack* psatck);
Data Peek(Stack* pstack);
void Push(Stack* pstack);
void Pop(Stack* psatck);
void InitStack(Stack* pstack)
{
pstack->top = -1;
}
bool IsFull(Stack* pstack)
{
return pstack->top == MAX_STACK - 1;
}
bool IsEmpty(Stack* pstack)
{
return pstack->top == -1;
}
Data Peek(Stack* pstack)
{
if (IsEmpty(pstack)) exit(1);
return pstack->item[pstack -> top];
}
void Push(Stack* pstack, Data item)
{
if (IsFull(pstack)) exit(1);
pstack->item[++pstack->top] = item;
}
void Pop(Stack* pstack)
{
if (IsEmpty(pstack)) exit(1);
--(pstack->top);
}
void ReversePrint(char* s, int len)
{
Stack stack;
char ch;
InitStack(&stack);
for (int i = 0; i < len; i++)
{
Push(&stack, s[i]);
}
while (!IsEmpty(&stack))
{
ch = Peek(&stack);
printf("%c", ch);
Pop(&stack);
}
}
bool IsParanbBalanced(char* exp, int len)
{
Stack stack;
InitStack(&stack);
for (int i = 0; i < len; i++)
{
if (exp[i] == '(') Push(&stack, exp[i]);
else if (exp[i] == ')')
{
if (IsEmpty(&stack)) return false;
else Pop(&stack);
}
}
if (IsEmpty(&stack)) return true;
else return false;
}
int main()
{
}