// 链队.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "stdio.h"
#include "malloc.h"
typedef struct linkduilie {
struct link *top;
struct link *foot;
}LinkDuiLie;
typedef struct link {
struct link *next;
int data;
}Link;
//初始化
LinkDuiLie *Init() {
LinkDuiLie *Q;
Q= (LinkDuiLie*)malloc(sizeof(linkduilie));
Link *head;
head = (Link*)malloc(sizeof(link));
head->next = NULL;
Q->top = head; //将head放入top
Q->foot = head; //将head放入foot
return Q;
}
//判断空
int Kong(LinkDuiLie *Q) {
if (Q->foot == Q->top) {
return 1;
}
return 0;
}
//入队
int Add(LinkDuiLie *Q, int x) {
Link *p = (Link*)malloc(sizeof(link)); //申请一个空间
p->data = x; //将数据放入 p中
p->next = NULL;
Q->foot->next = p;
Q->foot = p;
return 1;
}
//出队
int Out(LinkDuiLie *Q) {
if (Kong(Q)) {
printf("队为空\n");
return 0;
}
Link *p = (Link*)malloc(sizeof(link)); //申请一个零时用的东西
p = Q->top->next; //将队列的top中的head的下一个放入p
//输出数
printf("%5d", p->data);
//往后挪动一个
Q->top->next = p->next; //将队列中的top中的下一个放p的下一个也就是head的下一个的下一个
free(p);
return 1;
}
int main()
{
LinkDuiLie *Q;
Q = Init();
printf("判断队列是否为空\n 空为1 非空为0\n%d\n",Kong(Q));
Add(Q, 10); Add(Q, 20);
Add(Q, 30); Add(Q, 40);
printf("判断队列是否为空\n 空为1 非空为0\n%d\n", Kong(Q));
Out(Q); Out(Q);
Out(Q); Out(Q);
return 0;
}
链队——c
最新推荐文章于 2022-03-27 21:32:27 发布