队列的链表算法——C语言版
//队列的链表也是分带头结点的和不带头结点的
#include<stdio.h>
#include<stdlib.h>
typedef struct jiedian
{
int data;
struct jiedian* next;
}jiedian;
typedef struct lianbiaozhizhen
{
jiedian* front, * rear;
}lianbiaozhizhen;
void chushihua(lianbiaozhizhen& q)
{
q.front = q.rear = (jiedian*)malloc(sizeof(jiedian));
q.front->next = NULL;
}
//判空
bool iskong(lianbiaozhizhen q)
{
if (q.front == q.rear) return true;
else return false;
}
//入队
void rudui(lianbiaozhizhen& q, int x)
{
jiedian *s = (jiedian*)malloc(sizeof(jiedian));
s->data = x;
s->next = NULL;
q.rear->next = s;
q.rear = s;
}
//不带头结点入队
void budaitoujiedian(lianbiaozhizhen q, int x)
{
jiedian* s = (jiedian*)malloc(sizeof(jiedian));
s->data = x;
s->next = NULL;
if (q.front == NULL)
{
q.front = s;
q.rear = s;
}
else {
q.rear->next = s;
q.rear = s;
}
}
//出队
bool chudui(lianbiaozhizhen q, int& x)
{
if (q.front == q.rear)return false;
jiedian* p = q.front->next;
x = p->data;
q.front->next = p->next;
if (q.rear == p)
q.rear = q.front;
free(p);
return true;
}
int main()
{
lianbiaozhizhen q;
chushihua(q);
for (int i = 1; i <= 10; i++)
{
rudui(q, i);
}
int x = 0;
if (chudui(q, x))printf("%d\n", x);
else printf("出队失败");
return 0;
}