赞
踩
要求:
思路
#include <vector> #include <cstring> #include <ctime> #include <iostream> #include <iomanip> #include <cmath> #include <algorithm> #include <stdio.h> using namespace std; //定义链节 typedef struct QNode { int data; struct QNode *next; }QNode,*QueuePtr; //定义队列的指针类型 typedef struct LinkQueue { QueuePtr front; QueuePtr rear; }; //初始化队列,加上头结点 int InitQueue(LinkQueue &Q) { Q.front = Q.rear = new QNode; Q.front->next = NULL; return 1;//初始化成功返回1 } //入队 int EnQueue(LinkQueue &Q,int e) { QNode *p = new QNode; p->data = e; p->next = NULL; Q.rear->next = p; Q.rear = p; return 1;//入队成功返回1 } //出队 int DeQueue(LinkQueue &Q,int &e) { if (Q.front == Q.rear) return -1;//队列为空返回-1 QNode *p = Q.front->next; Q.front->next = p->next; e = p->data; delete p; return 1;//出队成功返回1 } //查找元素 int FindElem(LinkQueue &Q, int e) { if (Q.front == Q.rear) return -1; QNode *p = new QNode; p = Q.front->next; while (p) { if (p->data == e) return 1;//成功返回1 else { p = p->next; } } return 0;//失败返回0 } //翻转链表 void OverTurnQueue(LinkQueue &Q) { QNode *pre = new QNode; QNode *pcur = new QNode; QNode *pnext = new QNode; pre = Q.front->next; pcur = pre->next; pnext = pcur->next; Q.rear = pre; while (pnext) { pcur->next = pre; pre = pcur; pcur = pnext; pnext = pcur->next; } pcur->next = pre; Q.front->next = pcur; Q.rear->next = NULL; } //打印当前队列元素 void PrintElem(LinkQueue &Q) { QNode *p = Q.front->next; while (p) { cout << p->data << '\t'; p = p->next; } cout << endl; delete p; } int main() { srand(time(0)); int e; LinkQueue *Q = new LinkQueue(); InitQueue(*Q); cout << "原始元素:" << endl; for (int i = 0; i < 10; i++) { e = rand()%(99 - 10 + 1) + 10; cout << e << '\t'; EnQueue(*Q, e); } cout << endl; cout << "打印队列" << endl; PrintElem(*Q); OverTurnQueue(*Q); cout << "打印翻转队列" << endl; PrintElem(*Q); DeQueue(*Q, e); cout << "打印出队元素" << endl; cout << e << endl; cout << "打印队列" << endl; PrintElem(*Q); }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。