1、题目:利用链表实现一个先入后出的栈结构,并提供栈操作的push和pop的接口
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
#define BUTTOM -1
struct stack_date
{
int top;
int stack[MAX];
};
typedef struct stack_date Stack;
enum return_result {FULL_OK,FULL_NO,EMPTY_OK,EMPTY_NO,PUSH_OK,PUSH_NO,POP_OK,POP_NO};
void create_stack(Stack ** stack)
{
*stack = (Stack *)malloc(sizeof(Stack));
if(NULL == *stack)
{
printf("Malloc error!\n");
exit(-1);
}
}
void init_stack(Stack * stack)
{
stack->top = BUTTOM;
}
int is_full(Stack * stack)
{
if(stack->top == MAX-1)
{
return FULL_OK;
}
else
{
return FULL_NO;
}
}
int push_stack(Stack * stack,int num)
{
if(is_full(stack) == FULL_OK)
{
printf("Stack is full.\n");
return PUSH_NO;
}
else
{
stack->top++;
stack->stack[stack->top] = num;
return PUSH_OK;
}
}
int is_empty(Stack * stack)
{
if(stack->top == -1)
{
return EMPTY_OK;
}
else
{
return EMPTY_NO;
}
}
void gettop_stack(Stack * stack)
{
int i;
if(is_empty(stack) == EMPTY_OK)
{
printf("Stack is empty.\n");
}
else
{
for(i = stack->top; i > -1; i--)
{
printf("num = %d\n",stack->stack[i]);
}
}
}
int pop_stack(Stack * stack)
{
int n;
if(is_empty(stack) == EMPTY_OK)
{
printf("Stack is empty.Pop fail.\n");
return POP_NO;
}
else
{
stack->top--;
n = stack->top + 1;
return stack->stack[n];
}
}
int main()
{
Stack * stack;
int i,n;
create_stack(&stack);
init_stack(stack);
for(i = 0; i < 10; i++)
{
if(push_stack(stack,i+1) == PUSH_OK)
{
printf("PUSH OK %d !\n",i+1);
}
}
gettop_stack(stack);
for(i = MAX; i > -1; i--)
{
n = pop_stack(stack);
if(n != POP_NO)
{
printf("POP OK %d.\n",n);
}
}
gettop_stack(stack);
free(stack);
return 0;
}
3、题目:通过链栈实现的简易计算器
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
typedef struct Stack_num
{
int num;
struct Stack_num *next;
}Stack_num,*LinkStack_num_ptr;
typedef struct LinkStack_num
{
struct Stack_num* top;
int count;
}LinkStack_num;
typedef struct Stack_opt
{
char opt;
struct Stack_opt *next;
}Stack_opt,*LinkStack_opt_ptr;
typedef struct LinkStack_opt
{
struct Stack_opt* top;
int count;
}LinkStack_opt;
bool Push_num(LinkStack_num *S,int e)
{
LinkStack_num_ptr s=(LinkStack_num_ptr)malloc(sizeof(Stack_num));
s->num=e;
s->next=S->top;
S->top=s;
S->count++;
return OK;
}
bool Pop_num(LinkStack_num *S,int *e)
{
LinkStack_num_ptr p;
if(S->top==NULL)
{
return ERROR;
}
*e=S->top->num;
p=S->top;
S->top=S->top->next;
free(p);
S->count--;
return OK;
}
bool Push_opt(LinkStack_opt *S,char e)
{
LinkStack_opt_ptr s=(LinkStack_opt_ptr)malloc(sizeof(Stack_opt));
s->opt=e;
s->next=S->top;
S->top=s;
S->count++;
return OK;
}
bool Pop_opt(LinkStack_opt *S,char *e)
{
LinkStack_opt_ptr p;
if(S->top==NULL)
{
return ERROR;
}
*e=S->top->opt;
p=S->top;
S->top=S->top->next;
free(p);
S->count--;
return OK;
}
int Priority(char ch)/*优先级判断函数*/
{
switch(ch)
{
case '(':
return 3;
case '*':
case '/':
return 2;
case '+':
case '-':
return 1;
default:
return 0;
}
}
int main()
{
char opt[100];
LinkStack_num* stack_num=(LinkStack_num*)malloc(sizeof(LinkStack_num));
LinkStack_opt* stack_opt=(LinkStack_opt*)malloc(sizeof(LinkStack_opt));;
stack_num->top=NULL;
stack_num->count=0;
stack_opt->top=NULL;
stack_opt->count=0;
int i=0,tmp=0;
char option;
int num1,num2;
printf("please input : \n");
scanf("%s",opt);
while(opt[i]!='\0'||stack_opt->top!=NULL)
{
if(opt[i]>='0'&&opt[i]<='9')
{
tmp = tmp*10+opt[i]-'0';
i++;
if(opt[i]<'0'||opt[i]>'9')
{
Push_num(stack_num,tmp);
tmp = 0;
}
}
else
{
if(stack_opt->top==NULL||(stack_opt->top->opt=='('&&opt[i]!=')')||Priority(opt[i])>Priority(stack_opt->top->opt))
{
Push_opt(stack_opt,opt[i]);
i++;
continue;
}
if(stack_opt->top->opt=='('&&opt[i]==')')
{
char temp;
Pop_opt(stack_opt,&temp);
i++;
continue;
}
if((opt[i]=='\0'&&stack_opt->top!=NULL)||opt[i]==')'&&stack_opt->top->opt!='('||Priority(opt[i])<=Priority(stack_opt->top->opt))
{
Pop_opt(stack_opt,&option);
switch(option)
{
case '+':
{
Pop_num(stack_num,&num1);
Pop_num(stack_num,&num2);
Push_num(stack_num,(num1+num2));
break;
}
case '-':
{
Pop_num(stack_num,&num1);
Pop_num(stack_num,&num2);
Push_num(stack_num,(num2-num1));
break;
}
case '*':
{
Pop_num(stack_num,&num1);
Pop_num(stack_num,&num2);
Push_num(stack_num,(num1*num2));
break;
}
case '/':
{
Pop_num(stack_num,&num1);
Pop_num(stack_num,&num2);
Push_num(stack_num,(num2/num1));
break;
}
}
}
}
}
printf("result : %d\n",stack_num->top->num);
}