该程序的功能是实现单向链表的基本操作,现要求编写一个成员函数append(int),对一个空表追加链表项,追加的新表项被放在链尾部。
#include <iostream.h>
class list;
class Item{
public:
friend class list;
private:
Item(int d){data=d;next=0;}
Item *next;
int data;
};
class list{
private:
Item *end();
Item *list1;
public:
list(){list1=0;}
list(int d){list1=new Item(d);}
int append(int d=0);
};
int list::append(int d)
{
//请在此处将本函数补充完整
};
void main()
{
list list1;
int i;for(i=15;i<20;i++)
list1.append(i);
}
#include <iostream.h>
class list;
class Item{
public:
friend class list;
private:
Item(int d){data=d;next=0;}
Item *next;
int data;
};
class list{
private:
Item *end();
Item *list1;
public:
list(){list1=0;}
list(int d){list1=new Item(d);}
int append(int d=0);
};
int list::append(int d)
{
//请在此处将本函数补充完整
};
void main()
{
list list1;
int i;for(i=15;i<20;i++)
list1.append(i);
}
你测试一下!#include <iostream.h>
#include "StdAfx.h"
class list;
class Item{
public:
friend class list;
private:
Item(int d){data=d;next=0;}
Item *next;
int data;
};
class list{
private:
Item *end();
Item *list1,* p_pre,* p_next;
int iCount;
public:
list()
{
iCount = 1;
}
list(int d)
{
list1=new Item(d);
}
bool append(int d=0);
};
bool list::append(int d)
{
if (iCount == 1)
{
p_pre = new Item(d);
list1 = p_pre;
p_next = p_pre;
p_pre->next = 0 ;
iCount++;
return true;
}
else
{
p_pre= new Item(d);
p_next->next= p_pre;
p_next = p_pre;
p_next->next = 0;
iCount++;
return true;
}
}
void main()
{
list list1;
int i;
for(i=15;i<20;i++)
{
if (list1.append(i) == false)
return;
}
}
#include <iostream.h>
class list;
class Item{
public:
friend class list;
private:
Item(int d){data=d;next=0;}
Item *next;
int data;
};
class list{
private:
Item *list1,* p_pre,* p_next;
int iCount;
public:
list()
{
list1=0;
iCount = 1;
}
list(int d)
{
list1=new Item(d);
}
int append(int d=0);
};
int list::append(int d)
{
if (iCount == 1)
{
p_pre = new Item(d);
list1 = p_pre;
p_next = p_pre;
p_pre->next = 0 ;
iCount++;
return 0;
}
else
{
p_pre= new Item(d);
p_next->next= p_pre;
p_next = p_pre;
p_next->next = 0;
iCount++;
return 0;
}
}
void main()
{
list list1;
int i;
for(i=15;i<20;i++)
list1.append(i);
}
class list;
class Item{
public:
friend class list;
int getData()
{
return data;
}
Item * getItem()
{
return next;
}private:
Item(int d)
{
data=d;
next=0;
}
Item *next;
int data;
};
class list{
private:
Item *list1,* p_pre,* p_next;
int iCount;
public:
list()
{
list1=0;
iCount = 1;
}
list(int d)
{
list1=new Item(d);
}
int append(int d=0);
bool ShowLinkList(list * pList);
Item * GetHead()
{
return list1;
}
};
int list::append(int d)
{
if (iCount == 1)
{
p_pre = new Item(d);
list1 = p_pre;
p_next = p_pre;
p_pre->next = 0 ;
iCount++;
return 0;
}
else
{
p_pre= new Item(d);
p_next->next= p_pre;
p_next = p_pre;
p_next->next = 0;
iCount++;
return 0;
}
}bool ShowLinkList(list * pList)
{
if (pList == NULL)
return false; if (pList->GetHead() == NULL)
return false; Item * pListTemp = pList->GetHead();
while (pListTemp)
{
cout<<pListTemp->getData()<<endl;
pListTemp = pListTemp->getItem();
}
return true;
}
void main()
{
list list1;
int i;
for(i=15;i<20;i++)
list1.append(i);
//show the LinkList
if (ShowLinkList(&list1) == false)
return; }
帮他做了个显示链表的方法.测试一下???
如下所示
#include <iostream.h>
class list;
class Item{
public:
friend class list;
int getData()
{
return data;
}
Item * getItem()
{
return next;
}private:
Item(int d)
{
data=d;
next=0;
}
Item *next;
int data;
};
class list{
private:
Item *list1,* p_pre,* p_next;
int iCount;
public:
list()
{
list1=0;
iCount = 1;
}
list(int d)
{
list1=new Item(d);
}
int append(int d=0);
bool ShowLinkList(list * pList);
Item * GetHead()
{
return list1;
}
bool FreeMomery(list * pList);
};//create the linkList
int list::append(int d)
{
if (iCount == 1)
{
p_pre = new Item(d);
list1 = p_pre;
p_next = p_pre;
p_pre->next = 0 ;
iCount++;
return 0;
}
else
{
p_pre= new Item(d);
p_next->next= p_pre;
p_next = p_pre;
p_next->next = 0;
iCount++;
return 0;
}
}//show the linkList
bool ShowLinkList(list * pList)
{
if (pList == NULL)
return false; if (pList->GetHead() == NULL)
return false; Item * pListTemp = pList->GetHead();
while (pListTemp)
{
cout<<pListTemp->getData()<<endl;
pListTemp = pListTemp->getItem();
}
return true;
}//delete the point
bool FreeMomery(list * pList)
{
if (pList == NULL)
return false;
if (pList->GetHead() == NULL)
return false;
Item * pListTemp = pList->GetHead();
Item *temp = NULL;
Item *back = NULL;
if (pListTemp != NULL)
{
temp = pListTemp->getItem();
delete pListTemp;
pListTemp = NULL;
while (temp != NULL)
{
back = temp->getItem();
delete temp;
temp = back;
}
}
return true;
}
void main()
{
list list1;
int i;
for(i=15;i<20;i++)
list1.append(i);
//show the LinkList
if (ShowLinkList(&list1) == false)
return;
//free the memory
if (FreeMomery(&list1) == false)
return;
}