谁做过c语言的 队列及其操作啊

东莞理工计算机学院的寒假作业
队列(Queue):也是运算受限的线性表。是一种先进先出(First In First Out ,简称FIFO)的线性表。只允许在表的一端进行插入,而在另一端进行删除。队首(front) :允许进行删除的一端称为队首。队尾(rear) :允许进行插入的一端称为队尾。队列中没有元素时称为空队列。在空队列中依次加入元素a1, a2, …, an之后,a1是队首元素,an是队尾元素。显然退出队列的次序也只能是a1, a2, …, an ,即队列的修改是依先进先出的原则进行的。队列的链式存储结构简称为链队列,它是限制仅在表头进行删除操作和表尾进行插入操作的单链表。需要两类不同的结点:数据元素结点,队列的队首指针和队尾指针的结点,链队的基本形式如上图

实现要求:
⑴ 链队列基本操作的实现:链队列的初始化,生成一个空链队列;链队列的撤消,即删除队列中的所有结点,仅留下指针结点;
⑵ 链队列的入队操作,即在已知队列的队尾插入一个元素e,即修改队尾指针;
⑶ 链队列的出队操作,即返回队首结点的元素值并删除队首结点;
⑷ 设计一个菜单,具有上述要求的所有功能、退出系统等最基本的功能。

我自己写了一个,练练手
由于没有指定数据类型,在第三行处我用float类型了
如果改成其它类型,代码中有//Type处,涉及输入输出,也要相应的改一下 
这个代码只做了简单的测试,不保证正确性

#include<stdio.h>                                                                                                                                                                                                                                                               
#include<malloc.h>                                                                                                                                                                                                                                                              
#define Type float                                                                                                                                                                                                                                                              
struct node{                                                                                                                                                                                                                                                                    
        Type data;                                                                                                                                                                                                                                                              
        struct node *next;                                                                                                                                                                                                                                                      
};                                                                                                                                                                                                                                                                              
struct queue{                                                                                                                                                                                                                                                                   
        struct node *front,*rear;                                                                                                                                                                                                                                               
};                                                                                                                                                                                                                                                                              
struct node * push(Type t,struct queue *q)                                                                                                                                                                                                                                      
{                                                                                                                                                                                                                                                                               
        struct node *last;                                                                                                                                                                                                                                                      
        last=(struct node *)malloc(sizeof(struct node));                                                                                                                                                                                                                        
        last->data=t;                                                                                                                                                                                                                                                           
        last->next=NULL;                                                                                                                                                                                                                                                        
        if(q->rear!=NULL){
                q->rear->next=last;
                q->rear=last;
        }else{
                q->front=last;
                q->rear=last;
        }
        return last;
}
Type pop(struct queue *q)
{
        struct node nd;
        if(q->front==NULL){
                printf("<<<Error:queue is blank\n");
                return 0;
        }
        nd=*(q->front);
        free(q->front);
        if(q->front!=q->rear)
                q->front=nd.next;
        else{
                q->front=NULL;
                q->rear=NULL;
        }
        return nd.data;
}
void clear(struct queue *q)
{
        struct node *np,*np1;
        if(q->front!=NULL){
                np=q->front;
                while(1){
                        np1=np;
                        free(np);
                        if(np==q->rear)
                                break;
                        np=np1->next;
                }
                q->front=NULL;
                q->rear=NULL;
        }
        printf("<<<The queue has been cleared\n");
}
struct queue * init()
{
        struct queue *q=(struct queue *)malloc(sizeof(struct queue));
        q->front=NULL;
        q->rear=NULL;
        return q;
}
void display(struct queue *q)
{
        struct node *np;
        np=q->front;
        if(np==NULL){
                printf("<<<The queue is blank\n");
                return;
        }
        while(1){
                printf(">>>%f\n",np->data);//Type 
                if(np==q->rear)
                        break;
                np=np->next;
        }
}
int main()
{
        struct queue *q;
        unsigned int c;
        Type t;
        q=init();
        printf("<<<One blank queue has been created\n");
        while(1){
                printf("Menu:\
                                \n*******************************************************\
                                \n\t1:push an element to queue\
                                \n\t2:pop an element from queue\
                                \n\t3:clear queue\
                                \n\t4:display queue\
                                \n\t5:exit\
                                \n*******************************************************\
                                \nPlease select operator\n");
                scanf("%d",&c);
                switch(c){
                        case 1:
                                printf("Please input the element pushed to queue:\n");
                                scanf("%f",&t);//Type 
                                push(t,q);
                                break;
                        case 2:
                                printf(">>>The element poped from queue is %f\n",pop(q));//Type
                                break;
                        case 3:
                                clear(q);
                                break;
                        case 4:
                                display(q);
                                break;
                        case 5:
                                clear(q);
                                free(q);
                                break;
                        default:
                                printf("Invalid input\n");
                                break;
                }
                if(c==5)
                        break;
        }
        return 0;
}

追问

大神谢谢你了

温馨提示:内容为网友见解,仅供参考
无其他回答

用C语言编写队列的各种基本操作,我不是非常明白:注释里有些问题:请大家...
ont)进行删除操作,而在表的后端(rear)进行插入操作。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。 在队列这种数据结构中,最先插入在元素将是最先被删除;反之最后插入的元素将最后被删除,因此队列又称为“先进先出”(FIFO—first in first out)...

C语言的队列如何实现和表示
删除元素:head-a==N?head=a:head++;当然要检查队列是否溢出,可以设变量n=0;每次添加元素n++ 每次删除元素n-- 当n<0后n>N数据溢出

C语言中,队列是什么意思,有什么用途
队列一种可以实现“先进先出”的存储结构,即“一端入,一端出”,队首(front)出队,队尾(rear)入队,若front指向队首,则rear指向队尾最后一个有效元素的下一个元素;若rear指向队尾,则front指向队首第一个有效元素的下一个元素。队列特殊之处在于它只允许在表的前端(front)进行删除操作,而在...

c语言队列操作
pq->rear->next = pnew这个代码从队列的尾部增加新节点,然后pq->rear = pnew更新队列尾部指针。队列的数据结构形式就是由一个头front指针,一个尾rear指针来表征,items的设计是用空间换时间,涉及队列大小的操作会非常方便。队列的特征是先进先出,你给出的链式实现,其实就跟一个链表一样,链表的...

C语言消息队列
发送端首先申请Key,使用msgget函数创建或打开消息队列。然后使用msgsnd函数向消息队列发送消息。该函数接受消息队列ID、消息缓冲区地址、消息正文长度以及标志位参数。当使用MSG_NOWAIT标志位时,msgsnd函数在消息队列满时将不等待,而是立即返回。接收端从消息队列中接收消息使用msgrcv函数。该函数也需要消息队列...

如何创建一个字符循环队列,实现字符元素入队列、出队列、显示队列...
以下是一个简单的C语言实现,其中包括入队、出队和显示队列元素的操作:```c#include #define MAXSIZE 50typedef struct { char data[MAXSIZE]; int front; int rear;}Queue;void initQueue(Queue *q) { q->front = 0; q->rear = 0;}int isEmpty(Queue *q) { return ...

数据结构C语言描述的链队列的基本操作(初始化,判空,入队,出队,取对头...
} \/\/出队 int deQueue( LiQueue *&q,ElemType &e){QNode *t;if(q->rear==NULL)return 0;t=q->front;if(q->front==q->rear)q->front=q->rear=NULL;else q->front=q->front->next;e=t->data;break;free(t);return 1;} \/\/取队头 输出队列所有数就是出队 ...

c语言结构体那块的队列问题。我们书上没有,是能给我讲讲。把我讲懂我...
,如果没有到,那么head=head+1,出队结束。判队列是否为空操作,就看head是否等于tail,等于就是空的。最后,注意当队列为空时,试图删除一个元素,就会导致队列下溢,应该在删除前先判定队列是否为空。如果head=tail+1时(因为是环形),队列满,此时添加元素会溢出,应该在添加操作前判定。

C语言中使用队列
如果你用vc,#include<deque>就好了,但是注意要加上using naemspace std;我是当你用的c++的STL,STL中没有真正的队列和栈,他们都是通过对双端队列的改造得到的,所以包含的文件可能和你想的不一样。而且这些头文件都没有.h结尾!很特别 如果你不是vc,当我没说 ...

计算机c语言中什么是栈和队列
删除这一端为栈顶,另一端称为栈底。表中无元素时为空栈。栈的修改是按后进先出的原则进行的,栈又称为LIFO表。队列是一种运算受限的线性表,插入在表的一端进行,而删除在表的另一端进行,允许删除的一端称为队头,允许插入的 一端称为队尾,队列的操作原则是先进先出的,又称作FIFO表。

相似回答