c语言实现链队列
#include <stdio.h> #include <stdlib.h> #define MAXQSIZE 100 typedef int QElemType; typedef struct QNode{ QElemType data; QNode *next; }QNode,*QLinkPtr; typedef struct{ QLinkPtr front; QLinkPtr rear; }QueueLink; void Init(QueueLink &Q){ Q.front= Q.rear = (QLinkPtr)malloc(sizeof(QNode)); Q.front -> next = NULL; printf("init success \n"); } int Get(QueueLink Q){ QueueLink p; if (Q.front == Q.rear){ printf("empty\n"); return 0; } p = Q; p.front = p.front->next; while(p.front){ printf("data is %d\n",p.front->data); p.front = p.front->next; } return 0; } int enQueueLink(QueueLink &Q,QElemType e){ QLinkPtr p; p = (QLinkPtr)malloc(sizeof(QNode)); p -> data = e; p -> next = NULL; Q.rear -> next = p; Q.rear = p; return 0; } int outQueueLink(QueueLink &Q){ QLinkPtr p; if (Q.front == Q.rear){ printf("empty\n"); return 0; } p = Q.front->next; if (Q.rear == p){ free(p); Q.rear = Q.front; return 0; } Q.front->next = Q.front->next->next; free(p); return 0; } void DropQueueLink(QueueLink &Q){ QLinkPtr p; while(Q.front){ p = Q.front -> next; free(Q.front); Q.front = p; } } int main(){ QueueLink Q; Init(Q); // for (int i=0;i<10;i++){ // enQueueLink(Q,i+1); // } enQueueLink(Q,100); outQueueLink(Q); // outQueueLink(Q); // DropQueueLink(Q); Get(Q); }