【icoding 栈与队列】习题

1、队列 循环链表表示队列

题目:
假设以带头结点的循环链表表示队列,并且只设一个指针指向队尾元素结点(注意不设头指针),请完成下列任务:
1: 队列初始化,成功返回真,否则返回假:bool init_queue(LinkQueue *LQ);
2: 入队列,成功返回真,否则返回假: bool enter_queue(LinkQueue *LQ, ElemType x);
3: 出队列,成功返回真,且*x为出队的值,否则返回假 bool leave_queue(LinkQueue *LQ, ElemType *x);
相关定义如下:

typedef struct _QueueNode {
    ElemType data;          /*数据域*/
    struct _QueueNode *next;      /*指针域*/
}LinkQueueNode, *LinkQueue;

答案代码:

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

bool init_queue(LinkQueue* LQ){
    *LQ=(LinkQueue)malloc(sizeof(LinkQueueNode));
    if(*LQ!=NULL){
        (*LQ)->next=*LQ;
        return true;
    }else{
        return false;
    }
}

bool enter_queue(LinkQueue* LQ, ElemType x){
    LinkQueue new_node, temp;
    new_node=(LinkQueue)malloc(sizeof(LinkQueueNode));
    if(new_node!=NULL){
        //new_node插入到队头temp 头插法
        new_node->data=x;
        new_node->next=temp;//新的连在头的前面
        (*LQ)->next=new_node;//尾连新头
        *LQ=new_node;//更新尾指针
        return true;
    }else{
        return false;
    }
}
//???
bool leave_queue(LinkQueue* LQ, ElemType* x){
    LinkQueue temp, temp1;
    if((*LQ)->next!=NULL&&(*LQ)->next!=*LQ){
        temp=(*LQ)->next->next;//指向队头结点
        *x=temp->data;//取队头值
        (*LQ)->next->next=temp->next;
        temp1=temp->next;
        free(temp);
        if(temp1->next==temp1) *LQ=temp1;
        return true;
    }else{
        return false;
    }
}

2、后缀表达式

题目:
请使用已定义好的栈完成后缀表达式计算:
(1)如果是操作数,直接入栈
(2)如果是操作符op,连续出栈两次,得到操作数x 和 y,计算 x op y,并将结果入栈。
后缀表达式示例如下:
9 3 1 - 3 * + 10 2 / +
13 445 + 51 / 6 -
操作数、操作符之间由空格隔开,操作符有 +,-,*, /, %共 5 种符号,所有操作数都为整型。
栈的定义如下:

#define Stack_Size 50
typedef struct{
    ElemType elem[Stack_Size];
    int top;
}Stack;

bool push(Stack* S, ElemType x);
bool pop(Stack* S, ElemType *x);
void init_stack(Stack *S);

其中,栈初始化的实现为:

void init_stack(Stack *S){
    S->top = -1;
}

需要完成的函数定义为:int compute_reverse_polish_notation(char *str);
函数接收一个字符指针,该指针指向一个字符串形式的后缀表达式,函数返回该表达式的计算结果。
答案代码:

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

int compute_reverse_polish_notation(char* str){
    Stack numbers;
    init_stack(&numbers);
    ElemType num1, num2, push_num;
    while(str[i]!='\0'){
        if(str[i]!=' '){
            if(str[i]>='0'&&str[i]<='9'){ //遇到操作数
                //以空格为分界线,一个多位数不能拆开(比如5 445中445需要通过下列算法合并)
                push_num=0;
                while(str[i]!=' '){
                    push_num=push_num*10+str[i]-'0';
                    i++;
                }
                push(&numbers, push_num);
            }else{//遇到操作符
                pop(&numbers, &num2);
                pop(&numbers, &num1);
                switch(str[i]){
                    case '+':{
                        num1+=num2; break;
                    }
                    case '-':{
                        num1-=num2; break;
                    }
                    case '*':{
                        num1*=num2; break;
                    }
                    case '/':{
                        num1/=num2; break;
                    }
                    case '%':{
                        num1%=num2; break;
                    }
                }
                push(&numbers, num1);
            }
        }
        //最外层遍历字符串的循环
        i++;
    }
    //最终返回结果
    pop(&numbers, &num1);
    return num1;
}

你可能感兴趣的:(数据结构)