-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack1.c
110 lines (102 loc) · 2.33 KB
/
stack1.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
//
// main.c
// AM
//
// Created by 余彬 on 15/7/10.
// Copyright (c) 2015年 余彬. All rights reserved.
//
#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
char c;
struct Node *pNext;
}Node,*pNode;
typedef struct Stack{
pNode pBase;
pNode pOver;
}Stack,*pStack;
int Push(pStack pS,char c){
pNode pNew;
pNew=(pNode)malloc(sizeof(Node));
if(pNew==NULL){
printf("Fail!\n");
return 0;
}else {
pNew->c=c;
pNew->pNext=NULL;
if(pS->pBase==NULL){
pS->pBase=pS->pOver=pNew;
}else{
pS->pOver->pNext=pNew;
pS->pOver=pNew;
}
return 1;
}
}
int Pop(pStack pS,char *c){
if(pS->pBase==NULL)
return 0;
else {
*c=pS->pOver->c;
if(pS->pBase==pS->pOver){
free(pS->pOver);
pS->pBase=pS->pOver=NULL;
}else {
pNode pTmp=pS->pBase;
while(pTmp->pNext!=pS->pOver)
pTmp=pTmp->pNext;
free(pS->pOver);
pTmp->pNext=NULL;
pS->pOver=pTmp;
}
return 1;
}
}
int main(){
char c,c1;
Stack s;
s.pBase=s.pOver=NULL;
printf("Please enter an expression:\n");
scanf("%c",&c);
while(c!='$'){
if(c>='0' && c<='9'){
while(c>='0' && c<='9'){
printf("%c",c);
scanf("%c",&c);
}
printf(" ");
continue;
}else {
switch (c) {
case '+':
case '-':
while(Pop(&s,&c1) && c1!='('){
printf("%c ",c1);
}
if(c1=='(')
Push(&s, '(');
Push(&s,c);
break;
case '*':
case '/':
case '(':
Push(&s,c);
break;
case ')':
Pop(&s,&c1);
while(c1!='('){
printf("%c ",c1);
Pop(&s, &c1);
}
break;
default:
break;
}
}
scanf("%c",&c);
}
while(Pop(&s, &c1))
printf("%c ",c1);
printf("\n");
return 1;
}