首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c++队列有关问题 急

2012-03-08 
c++队列问题 急!~C/C++ code// 队列.cpp : 定义控制台应用程序的入口点。//#include stdafx.h#include i

c++队列问题 急!~

C/C++ code
// 队列.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;}

del()出现了问题 不知该如何解决!

[解决办法]
队列只有出队,没有删除。。。而且只能front出队
C/C++ code
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;}
[解决办法]
队列是先进先出的
C/C++ code
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不要指错了,不要释放了还要输出,不要没有申请内存还要使用。。。

热点排行