icoding数据结构——队列栈

2.1 

#include <stdio.h>
#include <stdlib.h>
#include "list.h" // 请不要删除,否则检查不通过


int compute_reverse_polish_notation(char *str){
    Stack S;
    init_stack(&S);
    int i=0,x,y,res,number_to_push;
    while(str[i]!='\0'){
        
        if (str[i] != ' ') {
        if(str[i]>='0'&&str[i]<='9'){
            number_to_push=0;
            while (str[i] != ' ' && str[i]) {
                number_to_push = number_to_push * 10 + (str[i] - '0');
                i++;}
            push(&S,number_to_push);
        }else {
            pop(&S,&x);
            pop(&S,&y);
            switch(str[i]){
                case '+':{ res =x+y; break;}
                case '-': {res =y-x; break;}
                case '*': {res =x*y;break;}
                case'/' :{res =y/x;break;  }     
                 case'%' :{res =y%x;break;}}
                 push(&S,res);
        }
        
        
    }
    i++;
    }
    pop(&S,&res);
    return res;
}

2.2 

#include <stdio.h>
#include <stdlib.h>
#include "list.h" // 请不要删除,否则检查不通过

bool init_queue(LinkQueue *LQ)
{
	   *LQ=(LinkQueue)malloc(sizeof(LinkQueueNode));
	   if((*LQ)==NULL) return 0;
	   	   (*LQ)->next=*LQ;
	    return 1;
}

bool enter_queue(LinkQueue *LQ, ElemType x)
{
	LinkQueue p;
	p=(LinkQueue)malloc(sizeof(LinkQueueNode));
	if(!p) return 0;
	p->data=x;
	p->next=(*LQ)->next;
	(*LQ)->next=p;
	*LQ=p;

	return 1;
}

bool leave_queue(LinkQueue *LQ, ElemType *x)
{
	LinkQueue p;
	if((*LQ)->next==*LQ)
	return 0;
	p=(*LQ)->next->next;//指向第一个元素 
	*x=p->data;
	if((*LQ)->next->next==(*LQ)){//两个节点 
        *LQ=(*LQ)->next;
        (*LQ)->next=*LQ;free(p);
	}   else{
	    (*LQ)->next->next=(*LQ)->next->next->next;
	    free(p);
	}

	return 1;
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值