链表类

链表结点类编写好了,我们可以向链表类进军了。链表是由一个个链表结点组成的,所以我们会在链表类中使用到链表结点类。链表结点类是一个很简单的类,链表类是一个功能更为强大的类。 正是将一个个类不断地组合与扩充,使得面向对象的程序功能越来越强大。

让我们感兴趣的是,假设我们编写的链表需要有一个头结点作为成员数据,那么是先有链表呢,还是先有头结点?我们又该如何在给链表作初始化的同时初始化头结点呢?

当一个对象中包含别的对象时,我们可以在它的构造函数定义中用以下格式调用其成员对象的构造函数:
    类名::构造函数名(参数表):成员对象名1(参数表)[,……成员对象名n(参数表)]
前一段和普通的构造函数一样,冒号之后则表示该类中的成员对象怎样调用各自的构造函数。

下面我们来看一个简单的面向对象的链表程序:(程序15.3)
//node.h同程序15.2.2
//linklist.h
#include "node.h"//需要使用链表结点类
#include <iostream>
using namespace std;
class Linklist
{
   public:
   Linklist(int i,char c);//链表类构造函数
   bool Locate(int i);//根据整数查找结点
   bool Locate(char c);//根据字符查找结点
   bool Insert(int i=0,char c='0');//在当前结点之后插入结点
   bool Delete();//删除当前结点
   void Show();//显示链表所有数据
   void Destroy();//清除整个链表
   private:
   Node head;//头结点
   Node * pcurrent;//当前结点指针
};
Linklist::Linklist(int i,char c):head(i,c)//类名::构造函数名(参数表):成员对象名1(参数表),链表类构造函数,调用head对象的构造函数重载1,详见Node.h文件
{
   cout<<"Linklist constructor is running..."<<endl;
   pcurrent=&head;
}
bool Linklist::Locate(int i)
{
   Node * ptemp=&head;
   while(ptemp!=NULL)
   {
      if(ptemp->readi()==i)
      {
         pcurrent=ptemp;//将当前结点指针指向找到的结点
         return true;
      }
      ptemp=ptemp->readn();//查找下一个结点
   }
   return false;
}
bool Linklist::Locate(char c)
{
   Node * ptemp=&head;
   while(ptemp!=NULL)
   {
      if(ptemp->readc()==c)
      {
         pcurrent=ptemp;
         return true;
      }
      ptemp=ptemp->readn();
   }
   return false;
}
bool Linklist::Insert(int i,char c)
{
   if(pcurrent!=NULL)
   {
      Node * temp=new Node(i,c,pcurrent,pcurrent->readn());//调用Node类构造函数重载2
      if (pcurrent->readn()!=NULL)
      {
         pcurrent->readn()->setp(temp);
      }
      pcurrent->setn(temp);
      return true;
   }
   else
   {
      return false;
    }
}
bool Linklist::Delete()
{
   if(pcurrent!=NULL && pcurrent!=&head)//head结点不能删除
   {
      Node * temp=pcurrent;
      if (temp->readn()!=NULL)
      {
         temp->readn()->setp(pcurrent->readp());
      }
      temp->readp()->setn(pcurrent->readn());//先连
      pcurrent=temp->readp();
      delete temp;//后断
      return true;
   }
   else
   {
      return false;
   }
}
void Linklist::Show()
{
   Node * ptemp=&head;
   while (ptemp!=NULL)//链表的遍历
   {
      cout <<ptemp->readi() <<'\t' <<ptemp->readc() <<endl;
      ptemp=ptemp->readn();
   }
}
void Linklist::Destroy()
{
   Node * ptemp1=head.readn();
   while (ptemp1!=NULL)
   {
      Node * ptemp2=ptemp1->readn();
      delete ptemp1;
      ptemp1=ptemp2;
   }
   head.setn(NULL);//头结点之后没有其他结点
}
//main.cpp
#include "Linklist.h"
#include <iostream>
using namespace std;
int main()
{
   int tempi;
   char tempc;
   cout <<"请输入一个整数和一个字符:" <<endl;
   cin >>tempi >>tempc;
   Linklist a(tempi,tempc);//创建一个链表,头结点数据由tempi和tempc确定
   a.Locate(tempi);
   a.Insert(1,'C'); 
   a.Insert(2,'B');
   a.Insert(3,'F');
   cout <<"After Insert" <<endl;
   a.Show();
   a.Locate('B');
   a.Delete();
   cout <<"After Delete" <<endl;
   a.Show();
   a.Destroy();
   cout <<"After Destroy" <<endl;
   a.Show();
   return 0;
}

运行结果:
请输入一个整数和一个字符:
4 G
Node constructor is running...
Linklist constructor is running...
Node constructor is running...
Node constructor is running...
Node constructor is running...
After Insert
4 G
3 F
2 B
1 C
After Delete
4 G
3 F
1 C
After Destroy
4 G
根据程序的运行结果,我们发现头结点的构造函数比链表的构造函数优先运行。这也不难理解: 构造函数的目的是要初始化成员数据,初始化成员数据的时候这个成员数据是必须存在的。所以当一个成员数据是一个对象的时候,应当先产生这个成员对象,于是就先调用了成员对象的构造函数。


原文:http://see.xidian.edu.cn/cpp/biancheng/view/84.html

面向对象程序设计课程作业 1. 请创建一个数据型为T的链表模板List,实现以下成员函数: 1) 默认构造函数List(),将该链表初始化为一个空链表(10分) 2) 拷贝构造函数List(const List& list),根据一个给定的链表构造当前链表(10分) 3) 析构函数~List(),释放链表中的所有节点(10分) 4) Push_back(T e)函数,往链表最末尾插入一个元素为e的节点(10分) 5) operator<<()友元函数,将链表的所有元素按顺序输出(10分) 6) operator=()函数,实现两个链表的赋值操作(10分) 7) operator+()函数,实现两个链表的连接,A=B+C(10分) 2. 请编写main函数,测试该模板的正确性: 1) 用List模板定义一个List型的模板对象int_listB,从键盘读入m个整数,调用Push_back函数将这m个整数依次插入到该链表中;(4分) 2) 用List模板定义一个List型的模板对象int_listC,从键盘读入n个整数,调用Push_back函数将这n个整数依次插入到该链表中;(4分) 3) 用List模板定义一个List型的模板对象int_listA,调用List的成员函数实现A = B + C;(4分) 4) 用cout直接输出int_listA的所有元素(3分) 5) 用List模板定义List型的模板对象double_listA, double_listB, double_listC,重复上述操作。(15分) 3. 输入输出样例: 1) 输入样例 4 12 23 34 45 3 56 67 78 3 1.2 2.3 3.4 4 4.5 5.6 6.7 7.8 2) 输出样例 12 23 34 45 56 67 78 1.2 2.3 3.4 4.5 5.6 6.7 7.8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值