首先转载一篇别人的亲测能用程序,并做出了相应的改动;
//--2017-04-10-将队列模式以链表的形式实现
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<conio.h>
using namespace std;
#define Queue_TYPE int
typedef struct NODE{
int data;
struct NODE *next;
}Node;
/*--入列(尾部插入的方式操作)
每次尾插新值后指向队尾的指针后移一位*/
Node* insert(Node *rear,Queue_TYPE value)
{
Node *p;
p = (Node *)malloc(sizeof(Node));
p->data = value;
p->next = rear->next;
rear->next = p;
rear = rear->next;
return rear;
}
int Delete(Node *rear)
{
Node *p = rear->next; /*rear的下一个结点是头结点*/
int dat;
p = p->next; /*p此时才指向第一个有数据的结点(表头结点的下一个结点)*/
dat = p->data;
rear->next->next = p->next; /*等价于head->next = p->next,因为rear的下一个结点是头结点*/
free(p);
return dat;
}
void print_quence(Node *rear)
{
Node *p = rear->next;
p = p->next; /*指向第一个有数据的单元*/
do
{
printf("%d ", p->data);
p = p->next;
}while(p != rear->next);
}
int main()
{
int date[6] = {1,2,3,4,5,6};
int i;
Node *rear;
rear = (Node *)malloc(sizeof(Node)); //表头的创建
rear->data = NULL;
rear->next = rear;
for(i = 0; i < 6; i++)
{
rear = insert(rear, date[i]);
print_quence(rear);
printf("\n");
}
printf("%d is out Quence.\n", Delete(rear)); /*出队一个元素*/
print_quence(rear);
}
原文连接http://blog.csdn.net/laoniu_c/article/details/38372569
http://blog.csdn.net/keheinash/article/details/51143573