栈和队列基本操作实验报告

#include #include #include typedef int elemtype; typedef struct node //队列结点类型定义 { elemtype data; //队列的数据元素类型 struct node *next; //指向后继结点的指针 }NODE; typedef struct { //定义链队 NODE *front,*rear;//定义链队队头和队尾指针 }LINKQUEUE; void initqueue(LINKQUEUE *QL)//队列的初始化 { QL->front=(NODE *)malloc(sizeof(NODE));//队列为带头结点的链队列 QL->front->next=NULL; QL->rear=QL->front; } LINKQUEUE *pushqueue(LINKQUEUE *QL,elemtype x) { //将元素x插入到链队列QL中,作为QL的新队尾 QL->rear->next=(NODE *)malloc(sizeof(NODE));

QL->rear->next->data=x; QL->rear=QL->rear->next; QL->rear->next=NULL; return QL; } elemtype popqueue(LINKQUEUE *QL) { //若链队列不为空,则删除队头元素,返回其元素值 NODE *newnode; newnode=QL->front->next; if(newnode==NULL) return 0; newnode=QL->front; QL->front=QL->front->next; free(newnode); return(QL->front->data); } void printqueue(LINKQUEUE *QL)//队列的显示 { NODE *p;

p=QL->front->next; if(p==NULL) printf(\队列空!\ while(p!=NULL) { if(p->next==NULL) printf(\ else printf(\ p=p->next; } printf(\} void main() { LINKQUEUE *p; int choice,elemdata,x=0; p=(LINKQUEUE *)malloc(sizeof(LINKQUEUE)); initqueue(p); while(1) { printf(\欢迎使用队列操作小程序:\\n\

printf(\、元素入队\\n\ printf(\、元素出队\\n\ printf(\、显示队列\\n\ printf(\、清屏幕\\n\ printf(\、退出程序\\n\ printf(\请选择你的操作:\ scanf(\ switch(choice) { case 1:printf(\请输入进队元素:\ scanf(\ p=pushqueue(p,elemdata); printf(\队列中的元素为:\\n\ printqueue(p); system(\ break; case 2:x=popqueue(p); if(x!=0) printf(\元素%d出队!\\n\ printf(\队列中的元素为:\\n\ printqueue(p); system(\ break;

case 3:printf(\队列中的元素分别为:\\n\ printqueue(p); system(\ break; case 4:system(\ break; case 5:return; } system(\ } }

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4