链队列的入队、出队

来源:互联网 发布:全民淘宝客能赚钱嘛 编辑:程序博客网 时间:2024/04/30 05:43

// test_demo.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "stdio.h"#include "windows.h"#define QueueSize 100  /*假定预分配的队列空间最多为100个元素*/  typedef char DataType ; /*假定队列元素的数据类型为字符*/typedef struct node{DataType data;struct node *next;}QueueNode;typedef struct{QueueNode *front;  /*头指针*/QueueNode *rear;}LinkQueue;/* 置队列空*/void Initial(LinkQueue *Q)/*将顺序队列置空*/{    Q->front=Q->rear=NULL;} /*判队列空*/int IsEmpty(LinkQueue *Q){return Q->front==NULL&&Q->rear==NULL;}/*进队列*/void Push(LinkQueue *Q,DataType x){/*将元素x插入链队列尾部*/QueueNode *p=(QueueNode *)malloc(sizeof(QueueNode));/*申请新结点*/p->data=x;p->next=NULL;if(IsEmpty(Q))Q->front=Q->rear=p;  /*将x插入空队列*/else { /*x插入非空队列的尾*/Q->rear->next=p;     /*p链到原队尾结点后*/Q->rear=p;           /*队尾指针指向新的尾*/}}/*出队列*/DataType Pop(LinkQueue *Q){DataType x;QueueNode *p;if(IsEmpty(Q)){printf("队列为空");/*下溢*/exit(1);}p=Q->front;                   /*指向对头结点*/x=p->data;                    /*保存对头结点的数据*/Q->front=p->next;             /*将对头结点从链上摘下*/if(Q->rear==p)                /*原队中只有一个结点,删去后队列变空,此时队头指针已为空*/Q->rear=NULL;free(p);   /*释放被删队头结点*/return x;  /*返回原队头数据*/}/* 取队列顶元素*/DataType Front(LinkQueue *Q){if(IsEmpty(Q)){printf("队列为空"); /*下溢,退出运行*/exit(1);}printf("Q->front->data:%c \n",Q->front->data);//测试 若数据不为基本类型,则用其他方式显示return Q->front->data;}int _tmain(int argc, _TCHAR* argv[]){LinkQueue s;DataType first,sec;Initial(&s);Push(&s,'a');Push(&s,'b');first=Front(&s);Pop(&s);sec=Front(&s);Pop(&s);while(1);return 0;}


以上代码为  数据结构算法实现(严蔚敏版配套实现程序)


0 0
原创粉丝点击