c++队列问题 急!~
// 队列.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "iostream"using namespace std;struct node { int date; node* next;};struct list { node* front; node* rear;};void init(list* p){ node* temp = new node; p->front = temp; p->rear = temp; temp->next = NULL; temp->date = 0;}void insert(list* p,int date){ node* temp = new node; temp->next = p->front; temp->date = date; p->front = temp;}void del(list* p){ node* temp = new node; temp->next = p->rear; node* d = temp->next; temp->next = NULL; p->rear = temp; delete d;}void show(list p){ node* temp = p.front; while (temp->next != NULL) { cout<<temp->date<<endl; temp = temp->next; }}int _tmain(int argc, _TCHAR* argv[]){ list test; init(&test); insert(&test,11); insert(&test,22); insert(&test,33); del(&test); show(test); system("pause"); return 0;}void del(list* p){ node* temp; int data; if(p->front == NULL) { printf("队列为空,无法删除! "); return; } data = p->front->date; temp = p->front; p->front = temp->next; if(p->front == NULL) { p->rear = NULL; } delete temp;}
[解决办法]
队列是先进先出的void del(list* p){ node* temp = p->front; if(p->front == NULL) { return; } while (temp->next != p->rear) { temp = temp->next; } node *d = temp->next; p->rear=temp; delete d; temp->next=NULL;}int _tmain(int argc, _TCHAR* argv[]){ list test; init(&test); insert(&test,11); insert(&test,22); insert(&test,33); cout<<"before delete"<<endl; show(test);//增加一个 del(&test); cout<<"after delete"<<endl; show(test); system("pause"); return 0;}
[解决办法]
队列是先进先出....写链表next不要指错了,不要释放了还要输出,不要没有申请内存还要使用。。。