// 鍊隊.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;
}