% gcc -o stack stack.c > tcc stack.c % stack > h h(elp; q(uit; p(ush x; P(op; d(isplay; i(nfix s; + (add); * (mult)
Run Data File with Test Cases: % stack stack.dat
> p 5 p 10 p 3 d Stack (top=2):
5 10 3
> P Pop: 3
> P Pop: 10
> P Pop: 5
Solution program for infix to postfix: % stack2 stack.dat
Infix : a+b*c+(d*e+f)*g Postfix: abc*+de*f+g*+
Array Stacks: Postfix Calculator [22]
Postfix: 6 5 2 3 + 8 * + 3 + *
+: push(pop(S)+pop(S),S)
*: push(pop(S)*pop(S),S)
> p 6 p 5 p 2 p 3 d Stack (top=3):
6 5 2 3
> + d Stack (top=2):
6 5 5
> p 8 d Stack (top=3):
6 5 5 8
> * d Stack (top=2):
6 5 40
> + d Stack (top=1):
6 45
> p 3 d Stack (top=2):
6 45 3
> + d Stack (top=1):
6 48
> * d Stack (top=0):
288
> P d Pop: 288
Stack (top=-1):
Array Stacks: Infix to Postfix [23]
| operand : | |
| `+` : | pop/print until empty but do not pop `(`; push |
| `*` : | push |
| `(` : | push |
| `)` : | pop/print until `(` |
| NULL : | pop/print until empty |
infix stack postfix
a+b*c+(d*e+f)*g
+b*c+(d*e+f)*g a
b*c+(d*e+f)*g + a
*c+(d*e+f)*g + ab
c+(d*e+f)*g +* ab
+(d*e+f)*g +* abc
(d*e+f)*g + abc*+
d*e+f)*g +( abc*+
*e+f)*g +( abc*+d
e+f)*g +(* abc*+d
+f)*g +(* abc*+de
f)*g +(+ abc*+de*
)*g +(+ abc*+de*f
*g + abc*+de*f+
g +* abc*+de*f+
+* abc*+de*f+g
+ abc*+de*f+g*
abc*+de*f+g*+
Array Stacks: stack.c [24]
#define STACK_SIZE 100
#define EMPTY_TOS -1
typedef int element_type;
struct stack_record {
int top_of_stack;
element_type stack_array[STACK_SIZE];
};
typedef struct stack_record *STACK;
STACK create_stack() {
STACK S;
S = (STACK) malloc(sizeof(struct stack_record));
if (S == NULL) printf("Out of space!\n");
else S->top_of_stack = EMPTY_TOS;
return(S);
}
int is_empty(STACK S) {
return(S->top_of_stack == EMPTY_TOS);
}
int is_full(STACK S) {
return(S->top_of_stack == STACK_SIZE-1);
}
void make_null(STACK S) {
S->top_of_stack = EMPTY_TOS;
}
void push(element_type x, STACK S) {
if (is_full(S))
printf("Full stack\n");
else
S->stack_array[++S->top_of_stack] = x;
}
element_type pop(STACK S) {
if (is_empty(S))
printf("Empty stack");
else
return(S->stack_array[S->top_of_stack--]);
}
void add(STACK S) {
push(pop(S)+pop(S),S);
}
void mult(STACK S) {
push(pop(S)*pop(S),S);
}
ANALYSIS:
To push(x,S) a new element on the stack: O( )
To pop(S) an element from the stack: O( )
Pointer Stacks: stack1.c [26]
typedef int element_type;
typedef struct node *node_ptr;
struct node {
element_type element;
node_ptr next;
};
typedef node_ptr STACK;
int is_empty(STACK S) {
return(S->next == NULL);
}
STACK create_stack() { /* make a header node */
STACK S;
S = (STACK) malloc(sizeof(struct node));
if (S == NULL) printf("Out of space!\n");
return S;
}
void make_null(STACK S) { /* call this after create_stack */
if (S != NULL)
S->next = NULL;
else
printf("Must use create_stack first\n");
}
void push(element_type x, STACK S) { /* put new element at head of list */
node_ptr tmp_cell;
tmp_cell = (node_ptr) malloc(sizeof(struct node));
if (tmp_cell == NULL) printf("Out of space!\n");
else {
tmp_cell->element = x;
tmp_cell->next = S->next;
S->next = tmp_cell;
}
}
element_type top(STACK S) { /* examine the head of the list */
if (is_empty(S)) printf("Empty stack\n");
else
return S->next->element;
}
void pop(STACK S) { /* get LIFO element at head of list */
node_ptr first_cell;
if (is_empty(S)) printf("Empty stack\n");
else {
first_cell = S->next;
S->next = S->next->next;
free(first_cell);
}
}