链队列的操作
鏈隊列的基本操作:
#include <iostream> #include <string.h> #include <stdio.h>using namespace std;struct QNode {int val;QNode *next; };struct LinkQueue {QNode *front;QNode *rear; };int InitLinkQueue(LinkQueue *Q) {Q->front=new QNode();if(Q->front!=NULL){Q->rear=Q->front;Q->front->next=NULL;return 1;}return 0; }int EnterLinkQueue(LinkQueue *Q,int x) {QNode *newNode = new QNode();if(newNode!=NULL){newNode->val=x;newNode->next=NULL;Q->rear->next=newNode;Q->rear=newNode;return 1;}return 0; }int DelLinkQueue(LinkQueue *Q,int *x) {if(Q->front==Q->rear) return 0;QNode *q=Q->front->next;Q->front->next=q->next;if(Q->rear==q)Q->rear=Q->front;*x=q->val;delete q;return 1; }int GetHead(LinkQueue Q,int *x) {if(Q.front==Q.rear)return 0;*x=Q.front->next->val;return 1; }void ShowQueue(LinkQueue *Q) {QNode *p=Q->front->next;if(p==NULL) return;while(p!=NULL){printf("%d ",p->val);p=p->next;}cout<<endl; }int main() {int n,x,v;LinkQueue Q;InitLinkQueue(&Q);scanf("%d",&n);for(int i=1; i<=n; i++){scanf("%d",&x);EnterLinkQueue(&Q,x);}ShowQueue(&Q);if(GetHead(Q,&v) != 0)printf("%d\n",v);scanf("%d",&n);for(int i=1; i<=n; i++){DelLinkQueue(&Q,&x);printf("%d ",x);}cout<<endl;ShowQueue(&Q);return 0; }
?
總結(jié)