本文共 1327 字,大约阅读时间需要 4 分钟。
#include "stdafx.h"#includeusing namespace std;typedef struct node{ char data; struct node *link;//指向后缀结点的指针};typedef struct Queue{ node *first,*rear;//定义队列的头和尾指针};Queue * InsertQueue(Queue *Q,char value){ node*newNode=(node*)malloc(sizeof(node)); newNode->data=value; newNode->link=NULL; if(Q->first==NULL) Q->first=Q->rear=newNode; else { Q->rear->link=newNode; Q->rear=newNode; } return Q;}Queue * DeleteQueue(Queue *Q){ node*ptemp; if(Q->first==NULL) cout<<"队列已空!"< first; if(Q->first==Q->rear) { Q->first=NULL; Q->rear=NULL; } else Q->first=Q->first->link; free(ptemp); } return Q;}Queue * InitQueue(){ char ch; node*newNode; Queue *QL=(Queue*)malloc(sizeof(Queue));//初始化队列 QL->first=(node*)malloc(sizeof(node));//队列中的第一个元素,头尾指针均指向该结点 QL->first->link=NULL; QL->rear=QL->first; cout<<"请输入字符串并回车,初始化队列如下:"< data=ch; newNode->link=NULL; if(QL->first==NULL) QL->first=QL->rear=newNode; else { QL->rear->link=newNode; QL->rear=newNode; } ch=getchar(); } return QL; }int length(Queue *Q){ int count=0; node*ptemp=Q->first; while(ptemp->link!=NULL) { count++; ptemp=ptemp->link; } return count;}void DisplayQueue(Queue *QL){ node*ptemp; ptemp=QL->first->link; if(ptemp==NULL) cout<<"队列已空!"< data; ptemp=ptemp->link; if(ptemp!=NULL) cout<<"——> "; } cout<
转载地址:http://zdsvi.baihongyu.com/