链式栈的实现
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int value;
struct Node *next;
};
struct stack
{
int size; //栈的容量大小
struct Node *bottom;
struct Node *top;
};
struct Node *createNode(int value)
{
struct Node *newnode;
newnode = (struct Node *)malloc(sizeof(struct Node));
if(newnode == NULL)
{
return NULL;
}
newnode->next = NULL;
newnode->value = value;
return newnode;
}
int push(struct stack *sp, int data)
{
struct Node *ptr = createNode(data);
if(ptr == NULL)
{
printf("create error");
return -1;
}
if(sp->size == 0)
{
sp->top = ptr;
sp->bottom = ptr;
}else{
ptr->next = sp->top;
sp->top = ptr;
}
sp->size++;
}
void print(struct stack *sp)
{
int i = sp->size;
struct Node *ptr = sp->top;
while(i != 0)
{
printf("%d",ptr->value);
i--;
ptr = ptr->next;
}
printf("\n");
}
int main()
{
struct stack Stack = {0,NULL,NULL};
//push(&Stack);
for(int i = 1; i < 6; i++)
{
push(&Stack,i);
}
print(&Stack);
return 0;
}
基于栈的计算器
#include <stdio.h> //链式栈
#include <stdlib.h>
struct Node //节点结构体
{
int value;
struct Node *next;
};
struct stack //栈的管理结构体
{
int size; //栈的容量大小
struct Node * bottom; //栈底指针
struct Node * top; //栈顶指针
};
struct Node * createNode(int value)
{
struct Node * newnode;
newnode = (struct Node *)malloc(sizeof(struct Node));
if(NULL == newnode)
{
return NULL;
}
newnode->next = NULL;
newnode->value = value;
return newnode;
}
int gettop(struct stack *sp)
{
if(sp->top == NULL)
{
return 0;
}
return sp->top->value;
}
int push(struct stack *sp,int data)
{
struct Node *ptr = createNode(data);
if(ptr == NULL)
{
printf("createNode error!\n");
return -1;
}
if(sp->size == 0)
{
sp->top = ptr;
sp->bottom = ptr;
}else{
ptr->next = sp->top;
sp->top = ptr;
}
sp->size++;
}
int pop(struct stack *sp)
{
if(sp->size == 0)
{
printf("stack is empty!\n");
return -1;
}
struct Node *ptr = sp->top;
sp->top = ptr->next;
sp->size--;
int data = ptr->value;
free(ptr);
return data;
}
void print(struct stack *sp)
{
int i = sp->size;
struct Node * ptr = sp->top;
while(i != 0)
{
printf("%d ",ptr->value);
i--;
ptr = ptr->next;
}
printf("\n");
}
char precede(char ch1, char ch2)
{
int i,j;
char pre[7][7] = {
{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=','0'},
{'>','>','>','>','0','>','>'},
{'<','<','<','<','<','0','='},
};
switch(ch1)
{
case '+':i = 0;break;
case '-':i = 1;break;
case '*':i = 2;break;
case '/':i = 3;break;
case '(':i = 4;break;
case ')':i = 5;break;
case '=':i = 6;break;
}
switch(ch2)
{
case '+':j = 0;break;
case '-':j = 1;break;
case '*':j = 2;break;
case '/':j = 3;break;
case '(':j = 4;break;
case ')':j = 5;break;
case '=':j = 6;break;
}
return pre[i][j];
}
int operate(int a,int b,char ch)
{
int num;
switch(ch)
{
case '+':return a + b;
case '-':return a - b;
case '*':return a * b;
case '/':
if(b == 0)
{
printf("invalid devide!\n");
exit(1);
}
return a / b;
}
}
int chop(char c)
{
switch(c)
{
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '=':
return 1;
default:
return 0;
}
}
int numop(char c)
{
if(c <= '9' && c >= '0')
{
return 1;
}else{
return 0;
}
}
int calculate(char *str)
{
struct stack Num = {0, NULL, NULL};
struct stack oper = {0, NULL, NULL};
int i;
char ch, ch2;
push(&oper, '=');
while(ch != '=' || gettop(&oper) != '=')
{
ch = str[i];
if(chop(ch))
{
switch(precede(gettop(&oper),ch))
case '<':
push(ch,oper);
i++;
ch = str[i];
}
int main()
{
char str[30];
int result;
scanf("%s",str);
result = calculate(str);
printf("result = %d\n",result);
return 0;
}