1.堆栈的顺序实现及后缀表达式的计算(后缀表达式只由0-9的数字和加减乘除组成)
#include
#include
#define maxsize 100
typedef enum{true,false
}bool;
typedef struct SNode * Stack;
typedef int ElementType;
//定义堆栈结构
struct SNode{
ElementType data[maxsize];
int top;
};
//生成空堆栈
Stack createStack(){
Stack stack=(Stack)malloc(sizeof(struct SNode));
stack->top=0;
}
// 判断堆栈是否已满
bool isFull(Stack s){
if(s->top==maxsize){
return true;
}
return false;
}
//入栈
bool push(Stack s,ElementType e){
if(s->top==maxsize){
printf("栈满\n");
return false;
}
else{
s->data[s->top]=e;
s->top++;
return true;
}
}
//出栈
ElementType pop(Stack s){
if(s->top==0){
printf("栈空\n");
return ;
}
ElementType e=s->data[s->top-1];
s->top--;
return e;
}
//判断是否为空
bool isEmpty(Stack s){
if(s->top==0){
return true;
}
return false;
}
//读取后缀表达式并计算其结果,只能计算不带括号的0-9数字的加减乘除
ElementType compute(char * str,Stack stack){
ElementType n1,n2;
for(int i=0;i
switch(c){
case '1':push(stack,1);break;
case '2':push(stack,2);break;
case '3':push(stack,3);break;
case '4':push(stack,4);break;
case '5':push(stack,5);break;
case '6':push(stack,6);break;
case '7':push(stack,7);break;
case '8':push(stack,8);break;
case '9':push(stack,9);break;
case '+':n1=pop(stack),n2=pop(stack);
push(stack,n1+n2);
break;
case '-':n1=pop(stack),n2=pop(stack);
push(stack,n2-n1);
break;
case '*':n1=pop(stack),n2=pop(stack);
push(stack,n2*n1);
break;
case '/':n1=pop(stack),n2=pop(stack);
push(stack,n2/n1);
break;
}
}
return pop(stack);
}
int main(int argc, char *argv[]) {
Stack stack=createStack();
char str[100];
printf("请输入后缀表达式:");
gets(str);
printf("结果为:%d\n",compute(str,stack));
return 0;
}
2.堆栈的l链表实现及后缀表达式的计算(后缀表达式只由0-9的数字和加减乘除组成)
#include
#include
//建立节点
typedef enum{true,false
}bool;
typedef int ElementType;
typedef struct SNode * Stack;
struct SNode{
ElementType data;
Stack next;
};
//建立空链表
Stack createStack(){
Stack stack=(Stack)malloc(sizeof(struct SNode));
stack->next=NULL;
return stack;
}
//判断是否为空栈
bool isEmpty(Stack stack){
if(stack->next==NULL){
return true;
}
else{
return false;
}
}
// 入栈
bool push(Stack stack,ElementType e){
Stack node=createStack();
node->data=e;
node->next=stack->next;
stack->next=node;
return true;
}
//出栈
ElementType pop(Stack stack){
if(isEmpty(stack)==true){
printf("空栈\n");
return -1;
}
else{
ElementType t;
Stack temp=stack->next;
t=temp->data;
stack->next=temp->next;
free(temp);
return t;
}
}
// 计算后缀表达式:表达式只由0-9的数字和加减乘除组成
ElementType compute(char *str,Stack stack){
ElementType n1,n2;
for(int i=0;i
switch(c){
case '1':push(stack,1);break;
case '2':push(stack,2);break;
case '3':push(stack,3);break;
case '4':push(stack,4);break;
case '5':push(stack,5);break;
case '6':push(stack,6);break;
case '7':push(stack,7);break;
case '8':push(stack,8);break;
case '9':push(stack,9);break;
case '+':n1=pop(stack),n2=pop(stack);
push(stack,n1+n2);
break;
case '-':n1=pop(stack),n2=pop(stack);
push(stack,n2-n1);
break;
case '*':n1=pop(stack),n2=pop(stack);
push(stack,n2*n1);
break;
case '/':n1=pop(stack),n2=pop(stack);
push(stack,n2/n1);
break;
}
}
return pop(stack);
}
int main(int argc, char *argv[]) {
Stack stack=createStack();
char str[100];
printf("请输入后缀表达式:");
gets(str);
printf("结果为:%d\n",compute(str,stack));
return 0;
}
3.队列的顺序实现
#include
#include
//定义节点
#define maxsize 5
typedef enum{false,true
}bool;
typedef int ElementType;
typedef struct QNode * Queue;
struct QNode{
ElementType data[maxsize];
int font;
int rear;
};
//创建队列
Queue createQueue(){
Queue queue=(Queue)malloc(sizeof(struct QNode));
queue->font=0;
queue->rear=0;
return queue;
}
//判断队列是否为空
bool isEmpty(Queue queue){
if(queue->font==queue->rear){
return true;
}
else
return false;
}
// 判断队列是否满
bool isFull(Queue queue){
if((queue->rear+1)%maxsize==queue->font){
return true;
}
return false;
}
//入队
bool add(ElementType e,Queue queue){
if(isFull(queue)==true){
printf("队满\n");
return false;
}
queue->rear=(queue->rear+1)%maxsize;
queue->data[queue->rear]=e;
return true;
}
//出队
ElementType delete(Queue queue){
if(isEmpty(queue)==true){
printf("队空\n");
return false;
}
queue->font=(queue->font+1)%maxsize;
return queue->data[queue->font];
}
int main(int argc, char *argv[]) {
Queue queue=createQueue();
if(isEmpty(queue)==true){
printf("队空\n");
}
add(1,queue);
add(2,queue);
add(3,queue);
add(4,queue);
if(isFull(queue)==true){
printf("队满\n");
}
for(int i=0;i
}
return 0;
}