学海无涯苦做舟,天天7.起来奋斗。
道路遥遥无期需作伴加QQ845264178
点个赞谢谢
#include<iostream>
using namespace std;
#define MAX 1204
#define ERROR -1
#define OK 0
typedef int ElemType;
typedef int Status;
typedef struct node
{
ElemType elem;
struct node* next;
}Node;
typedef struct Queue
{
Node* front;
Node* rear;
int count;
}QueueLink;
//初始化
QueueLink* InitQueueLink()
{
QueueLink* q = (QueueLink*)malloc(sizeof(QueueLink));
if (q != NULL)
{
q->front = NULL;
q->rear = NULL;
q->count = 0;
}
return q;
}
bool empty(QueueLink* q)
{
return(q->count == 0);
}
//进队
Status EnQueueLink(QueueLink* q, ElemType e)
{
Node* s = (Node*)malloc(sizeof(Node));
if (s!=NULL )
{
s->elem = e;
s->next = NULL;
}
if (empty(q))
q->front = s;
else
q->rear->next = s;
q->rear = s;
q->count++;
return OK;
}
//出队
Status OutQueueLink(QueueLink* q, ElemType* e)
{
if (q->rear == NULL)
{
cout << "empty" << endl;
return ERROR;
}
*e = q->front->elem;
Node* p = q->front;
q->front = q->front->next;
if (q->count==1)
{
q->rear = NULL;
}
free(p);
return OK;
}
//创建
void CreateQueueLink(QueueLink* q)
{
int n;
cout << "输入你要创建的个数" << endl;
cin >> n;
for (int i = 0;i < n;i++)
{
EnQueueLink(q, i);
//cout << "n=" << i << endl;
}
}
//销毁
void DelQueueLink(QueueLink* q)
{
Node* p = q->front;
q->front = q->rear = NULL;
Node* tmp;
while (p != NULL)
{
tmp = p->next;
free(p);
p = tmp;
}
}
//打印
void Show(QueueLink *q)
{
Node *p = q->front;
while (p != NULL)
{
cout << p->elem << endl;
p = p->next;
//cout << "show行号=" << __LINE__ << endl;
}
}
int main()
{
QueueLink* q = InitQueueLink();
CreateQueueLink(q);
Show(q);
cout << "----------------------" << endl;
EnQueueLink(q, 666);
EnQueueLink(q, 777);
int e;
OutQueueLink(q, &e);
cout << "e=" << e << endl;
Show(q);
}