用循环链表实现队列

                                                    用循环链表实现队列
请编写程序用循环链表实现队列,设置一个指针指向队尾元素(注意不设置头指针),该队列具有如下五个接口功能,并编写主程序测试队列的各个接口,测试方法:将一串字符入队后再依次出队并输出出队元素,以检验队列先进先出的操作特点。
(1) 创建空队列;
(2) 判断队列是否为空;
(3) 入队算法;
(4) 出队算法;
(5) 取队头元素;

源代码:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Queue{
    char c;
    struct Queue * link;
};
typedef struct Queue * Pqueue;
Pqueue creatnullqueue();
Pqueue Insert(Pqueue ,char );
Pqueue Outqueue(Pqueue );
void get_head(Pqueue );
int main(){
    Pqueue queue;
    char ch;
    queue = creatnullqueue();
    while(1){
        scanf("%c",&ch);
        if(ch=='\n')  break;
        queue=Insert(queue,ch);
    }
    get_head(queue);
    Outqueue(queue);
    return 0;
}
Pqueue creatnullqueue(){
    Pqueue head,front;
    head=front= (Pqueue)malloc(sizeof(struct Queue));
    if(front==NULL) printf("creat fail!\n");
    front->c =NULL;
    front->link=head;
 //   printf("creat ok!\n");
    return front;
}
Pqueue Insert(Pqueue queue,char ch){
    Pqueue q,p;
    p=queue;
    q = (Pqueue)malloc(sizeof(struct Queue));
    p->c = ch;
    q->c=NULL;
    q->link=p->link;
    p->link=q;
    p=q;
    return p;
}
void get_head(Pqueue queue){
    if(queue->link->c==NULL)  printf("队列为空\n");
    else printf("队列不为空,头元素为%c\n",queue->link->c);
}
Pqueue Outqueue(Pqueue queue){//全部让其输出
    Pqueue ps;
    while(1){
        ps=queue->link;
        if(queue->link->c==NULL) break;
        printf("%c ",queue->link->c);
        queue->link=queue->link->link;
        free(ps);
    }
}

猜你喜欢

转载自blog.csdn.net/huang1600301017/article/details/85621900