链式存储的队列

今天由于一些事情耽误了,本来可以写得更多的,但是却只完成了一部分,那就从下周开始加快进度吧。

对于链式的队列,我们需声明一个头指针和尾指针front和rear,当进队的时候,我们就用尾插法插进链表,当出队的时候,我们就从头开始出。

下面就是相关的代码,由于今天的一些事情耽误了(懒才是最重要的原因),所以就没有打,若有看不懂的,可以在评论区评论。

typedef struct SNode {
int data;
struct SNode *next;
}*queue;

typedef struct QNode{
queue front;
queue rear;
}*Queue;

Queue Create ();
void En (Queue q,int x);
int De (Queue q);
int IsEmpty (Queue q);

int main ()
{
Queue q = Create();
int i;
for(i= 1;i < 6;i++){
En(q,i);
}
for(i = 0;i < 4;i++){
printf("%d ",De(q));
}

}

int IsEmpty (Queue q)
{
return (q->front == q->rear);
}
int De (Queue q)
{
if(IsEmpty(q)) {//若满就不能删除。
printf("The queue is empty!");
return -1;
}else {
queue p;
p = q->front->next;//从队头依次出队。
q->front = q->front->next;
return (p->data);
}
}
void En (Queue q, int x )
{
queue p = (queue)malloc(sizeof(struct SNode));
p->data = x;
p->next = NULL;
q->rear->next= p;//利用尾指针插入q。
q->rear= p;//插入后,尾指针再指向队尾。
}

Queue Create ()
{
Queue q = (Queue)malloc(sizeof(struct QNode));
q->front = q->rear = (queue)malloc(sizeof(struct SNode));
q->front->next = NULL;

return q;
}

函数的名字等等设计得不怎么好,以后我会慢慢修改方面得错误。

今天就写到这里吧,还有一些任务未完成,全堆到一起了,看来下面今天不怎么轻松,加油,期待下一次!

posted @ 2020-10-11 21:53  jianzhui  阅读(151)  评论(0编辑  收藏  举报