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

C++链表有关问题

2012-03-06 
求助C++链表问题list.h #ifndefLIST_H#defineLIST_H#includeiostream.h#includeconio.hstructperson

求助C++链表问题
"list.h "
#ifndef   LIST_H
#define   LIST_H
#include   <iostream.h>
#include   <conio.h>
struct   person
{
char   name[20];
char   num[20];
};
struct       Node      
    {      
    struct   person   pe;    
    Node       *next;      
    };      
class     list      
  {      
    public:      
    list(){head=NULL;}    
    Node*   setlastnode();
    void     Creat();
    void     display();
    void     deletenode();
    private:      
    Node     *head;            
  };
#endif

"list.cpp "
#include   "List.h "
Node*   list::setlastnode()
{
Node*   p;
p=head;
while(p!=NULL)
{
p=p-> next;
}
p=new   Node;
return   p;
}
void     list::Creat()      
  {
    Node*   p;
    if(head==NULL)
    {
    head=new     Node;      
    p=head;
    }
    else
    p=list::setlastnode();
    cout < < "请输入姓名: ";
    cin> > p-> pe.name;
    cout < < "请输入学号: ";
    cin> > p-> pe.num;      
  }

  void   list::display()
  {
    int   i=1;
    Node       *p=head;      
    while(p!=NULL)  
    {
    cout < < "第   " < <i < < "   个学生\n " < < "姓名: ";
    cout < <p-> pe.name < <endl;
    cout < < "学号: " < <p-> pe.num < <endl;
            p=p-> next;    
    i++;
    }
  }
  void   list::deletenode()
  {
  Node*   p;
  while(head!=NULL)
  {
    p=head;
    head=head-> next;
    delete   p;
  }
 
  }

"main.cpp "
#include   "List.h "

void   main()
{
list   a;
int   i=1;
        while(i)
{
a.Creat();
cout < < "退出按   ESC         其它继续 " < <endl;
i=getch();
switch(i)
{
case   27:
i=0;
break;
default:
i=1;
}
}
a.display();           //显示
a.deletenode();     //释放内存
}

编译没有问题       但是运行时   程序会死掉,不知道是什么原因
还有   不知道这样释放内存是否正确

[解决办法]
#include <iostream>

using namespace std;



#define NOFOUND -1
#define FOUND 0
#define SUCCEED 0
#define FAIL -1
typedef struct {
int x,y;
}ElemType;
class Item
{
public:
int mx,my;
Item(int a=0,int b=0)
{
mx=a;my=b;
}
Item *mNxtItem; //Point to Next Element
};
//Define Abstract Data Type-----List
class List
{
public:
//Element Set
Item *mList;
//Operator Set
void mfInitList(List &l);
void mfDestoryList(List &l);
void mfClearList(List &l);
bool mfListEmpty(List l);
int mfListLength(List &l);
int mfGetElem(List l,int i,Item &e);
int mfLocateElem(List l,Item e,bool compare(Item e1,Item e2));
void mfPrior(List l,int i,Item &e);
void mfNextElem(List l,int i,Item &e);
int mfListInsert(List &l,int i,Item e);
void mfListDelete(List &l,int i,Item &e);
void mfListTraverse(List l,void visit(Item e));
};
//Define Global Function
bool gfEqual(Item e1,Item e2)
{
return((e1.mx==e2.mx)&&(e1.my==e2.my));
};
void gfDisplay(Item e)
{
cout < < "mx= " < <e.mx < < ";my= " < <e.my < <endl;
}
//End Define Global Function
void List::mfInitList(List &l)
{
l.mList=NULL;
};
bool List::mfListEmpty(List l)
{
return(l.mList==NULL);
};
void List::mfDestoryList(List &l)
{
delete l.mList;
};
void List::mfClearList(List &l)
{
Item *p,*temp;
p=l.mList;
while(p)
{
temp=p-> mNxtItem;
delete p;
p=temp;
}
delete p;
l.mList=NULL;
};
int List::mfListLength(List &l)
{
int ilen=0;
Item *p=l.mList;
while(p)
{
ilen++;
}
return ilen;
};
int List::mfGetElem(List l,int i,Item &e)
{
int j=1;
Item *p;
p=l.mList;
while(p&&j!=i)
{
p=p-> mNxtItem;
j++;
}
if(p) {e=*p;return FOUND;}
return NOFOUND;
};
int List::mfLocateElem(List l,Item e,bool compare(Item e1,Item e2))
{
Item *p=l.mList;
int i=0;
while(p)
{
i++;
if(compare(*p,e)) break;
p=p-> mNxtItem;
}
if(p) return i;
return NOFOUND;
};
int List::mfListInsert(List &l,int i,Item e)
{
Item *p=l.mList;
Item *insP,*nxtP;
int j=1;
if(!l.mList) {insP=new Item();insP-> mx=e.mx;insP-> my=e.my;insP-> mNxtItem=NULL;l.mList=insP;return SUCCEED;}
if(i==1) {
insP=new Item();insP-> mx=e.mx;insP-> my=e.my;nxtP=l.mList;
l.mList=insP;insP-> mNxtItem=nxtP;return SUCCEED;
}
while(p&&j <i-1)
{
j++;
p=p-> mNxtItem;
}
if(p){
insP=new Item();
if(insP==NULL) return FAIL;
insP-> mx=e.mx;insP-> my=e.my;
nxtP=p-> mNxtItem;
insP-> mNxtItem=nxtP;
p-> mNxtItem=insP;
return SUCCEED;
}
return FAIL;
};
void List::mfListTraverse(List l,void visit(Item e))
{
Item *p=l.mList,e;
if(!l.mList) {cout < < "List Is Empty,Traverse Failed! " < <endl;return;}
while(p)
{
e.mx=p-> mx;e.my=p-> my;
visit(e);
p=p-> mNxtItem;
}
}
void List::mfListDelete(List &l,int i,Item &e)
{
Item *p,*preP,*nxtP;
p=l.mList;
if(!l.mList) {cout < < "ERROR,The List Is Empty! " < <endl;
return ; }
if(i==1) {l.mList=p-> mNxtItem;delete p;return;}


while(p&&i> 2)
{
p=p-> mNxtItem;
i--;
}
preP=p; //指向删除结点的前一个结点
nxtP=p-> mNxtItem-> mNxtItem; //指向删除结点的后一个结点
preP-> mNxtItem=nxtP; //要删除的结点前一个结点的指针域,指向删除结点的后一个结点
p=p-> mNxtItem;
delete p;
}

int main()
{
int i;
List l;
//List *k=new List();
Item e;
l.mfInitList(l);
for(i=1;i <=10;i++)
{
e.mx=i;e.my=i;
l.mfListInsert(l,i,e);
}
l.mfListTraverse(l,gfDisplay);
l.mfListDelete(l,10,e);
l.mfListTraverse(l,gfDisplay);

system( "pause ");
return 0;
}
[解决办法]
#include <iostream.h>
#include <conio.h>
struct person
{
char name[20];
char num[20];
};

struct Node
{
struct person pe;
Node *next;

Node()
{
next = NULL;
}
};

class list
{
public:
list(){head=NULL;}
void setlastnode(Node* p);
void Creat();
void display();
void deletenode();
private:
Node *head;
};

void list::setlastnode(Node* p)
{
Node *tmp;
tmp=head;
while(tmp-> next != NULL)
{
tmp = tmp-> next;
}

tmp-> next = p;
}

void list::Creat()
{
Node* p;
if(head==NULL)
{
head=new Node;
p=head;
}
else
{
p = new Node;
setlastnode(p);
}

cout < < "请输入姓名: ";
cin> > p-> pe.name;
cout < < "请输入学号: ";
cin> > p-> pe.num;
}

void list::display()
{
int i = 1;
Node *p = head;
do
{
cout < < "第 " < <i++ < < " 个学生\n " < < "姓名: ";
cout < <p-> pe.name < <endl;
cout < < "学号: " < <p-> pe.num < <endl;
p = p-> next;
}while(p != NULL);

}

void list::deletenode()
{
Node* p;
while(head!=NULL)
{
p=head;
head=head-> next;
delete p;
}

}

void main()
{
list a;

while(1)
{
a.Creat();
cout < < "退出按 ESC 其它继续 " < <endl;
if(27 == getch())
break;
}
a.display(); //显示
a.deletenode(); //释放内存
}

热点排行