该程序的功能是实现单向链表的基本操作,现要求编写一个成员函数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);
}

解决方案 »

  1.   

    大概是这样,没有测试
    你测试一下!#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;
    }
    }
      

  2.   

    什么烂题目。我服了,从没有见过,根本就没有规范,不过很佩服你的朋友的学习劲头,所以我帮帮他!
    #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);
    }
      

  3.   

    #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;
    }
    };
    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; }
    帮他做了个显示链表的方法.测试一下???
      

  4.   

    对了应该释放内存空间
    如下所示
    #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;
    }