1.自己完成 终于到达学期初梦想的彼岸
#include"stdafx.h" #include#include using namespace std; typedef int Elemtype; typedef struct Node{ Elemtype data; struct Node *next; }QueueNode,*PQueueNode; typedef struct { QueueNode *front; QueueNode* rear; }linkQueue,*PlinkQueue; //购买节点 QueueNode *BuyNode(){ QueueNode *s = (QueueNode*)malloc(sizeof(QueueNode)); if (s == NULL) exit(1); s->next = NULL; return s; } //初始化 void Init_Queue(linkQueue *q){ assert(q != NULL); QueueNode *s = BuyNode(); q->front = s; q->rear = s; } //入队 bool Push_rear(linkQueue *q, Elemtype val){ assert(q != NULL); QueueNode *s=BuyNode(); s->data = val; q->rear->next = s; q->rear = s; s->next = NULL; return true; } //打印 void Print_Queue(linkQueue *q){ assert(q != NULL); QueueNode *s = q->front->next ; while (s != NULL){ cout << s->data << endl; s = s->next; } } //出队 bool Pop_Front(linkQueue *q){ assert(q != NULL); QueueNode *s = q->front->next; q->front->next = s->next; free(s); return true; } //判空 bool Empty_Queue(linkQueue *q){ assert(q != NULL); if (q->rear == q->front){ return true; } else return false; } //清空队列 bool Clear_Queue(linkQueue *q){ assert(q != NULL); while (!Empty_Queue(q)){ Pop_Front(q); } return true; } //摧毁队列 bool Destory_Queue(linkQueue *q){ assert(q != NULL); Clear_Queue(q); free(q->front); return true; } //获取队头元素的值 Elemtype *Get_Front(linkQueue *q, Elemtype *s){ assert(q != NULL); if (s == NULL) return NULL; *s = q->front->next->data ; return s; } //队列 int main(){ linkQueue q; Init_Queue(&q); cout << "入队后的元素为" << endl; for (int i = 1; i <= 10; i++){ Push_rear(&q, i); } Print_Queue(&q); cout << "队列清空后:" << endl; Clear_Queue(&q); Print_Queue(&q); int s; //Get_Front(&q, &s); //cout << endl<<"栈顶元素为:" << endl; //cout << s << endl; cout << "时光作渡,眉目成书,从此深情,不被辜负。" << endl; }



