一个简单的链表实现队列
作者:互联网
#include <stdio.h>
#include <stdlib.h>
typedef int datatype;
typedef struct link_node{
datatype info;
struct link_node* next;
}node;
typedef node* linklist;
typedef struct link_node2{
linklist front, rear;
}node2;
typedef node2* linkqueue;
void SetNull(linkqueue q){
q->front = (linklist)malloc(sizeof(node));
q->front->next = NULL;
q->rear = q->front;
}
int Empty(linkqueue q){
if(q->front == q->rear) return -1;
else return 0;
}
int Front(linkqueue q){
if(Empty(q)){
//TODO
printf("queue is empty!\n");
return -1;
}
else return q->front->next->info;
}
void insert (linkqueue q, int x){
linklist newnode = (linklist)malloc(sizeof(node2));
newnode->info = x;
q->rear->next = newnode;
q->rear = newnode;
q->rear->next = NULL;
}
int pop(linkqueue q){
int tmp;
linklist s;
if(Empty(q)){
printf("queue is empty!\n");
return -1;
}
else {
s = q->front->next;
if(s->next == NULL){
q->front->next = NULL;
q->rear = q->front;
}
else q->front->next = s->next;
tmp = s->info;
return tmp;
}
}
int main(){
node2 q;
SetNull(&q);
insert(&q, 2);
insert(&q, 3);
insert(&q, 8);
pop(&q);
pop(&q);
int a = 0;
a = Front(&q);
printf("%d\n",a);
}
原文:http://blog.csdn.net/tengweitw/article/details/17170743
作者:nineheadedbird
标签:return,队列,linkqueue,next,链表,int,简单,front,rear 来源: https://www.cnblogs.com/01am/p/15943181.html