(摘)面向对象c++数据结构之基本数据结构-链表-使用链表管理器(连载中)

3.2.4 使用链表管理器

使用单链表管理器的应用程序类可以在程序3-11和程序3-12中找到。与使用数组和向量管理器的程序相同,该应用程序类的构造函数创建了一个链表管理器对象。类的其余成员函数则负责处理用户界面和基本的链表操作。

 

【程序3-11】单链表演示程序应用程序类的声明

  #ifndef APP

  #define APP

 

  #include “listmgr.h”

  #define   INSERT 1

  #define   REMOVE_THING 2

  #define   VIEW   3

  #define   QUIT  9

 

 

  Class AppClass

  {

private:

   listMgr*theList;

   int menu();

   void addThing();

   void removeveThing();

   void view();

public:

   AppClass();

   void run();

 };

#endif

 

【程序3-12】单链表演示程序应用程序类的具体实现

 #include appclass.h”

 #inlcude “listmgr.h”

 #include “thing.h”

 #include <iostream.h>

 

AppClass ::AppClass()

{

  thelist=new ListMgr();

 }

 

void AppClass::run()

{

 int choice =menu();

 While(choice=!=QUIT)

{

                  switch(choice)

                  {

                  case  INSERT

                  addThing();

                  break;

                  case   REMOVE_THING;

                  removeThing();

                  break;

                  case   VIEW;

                  view();

                  break;

                  case   QUIT;

                  BREAK;

                  default:

                 cout<<”Unrecognized menu option.”<<endl;

}

}

}

 

Int AppClass::menu()

{

 Cout<<”/n1.Add an element”;

 Cout<<”/n2.Remove an element.”;

 Cout<<”/n3.View elements”;

 Cout<<”/n9.Quit”;

 Cout<<”/n/nChoice:”;

int choice;

cin>>choice;

return choice;

 

}

 

Void AppClass::addThing()

{

 Thing*newThing;

 int ID;

 char name[26];

 

 cout<<”/NID:”;

 cin>>ID;

 cin.get();//eat CR in keyboard buffer

 cout<<”Name:”;

 cin.getline(name,26);

newThing=new Thing(ID,name);

theList->insert(newThing);

 

}

 

void AppClass::removeThing()

{

 Int ID;

 

 cout<<”ID number to remove:”

 cin>>ID;

 int result=thelist->remove(ID);

 if(result)

 cout<<”Remove successful”;

 else

 cout<<”Remove unsuccessful”;

 }

 Void Appclass ::view()

 {

  Thing*current;

  ListItr*theIterator;

  theItarator=new ListLTr(theList);

  current=theIterator->getNext();

  cout<<endl;//just a blank line for spacing

 

 while(current!=0)

  {

   cout<<*ID<<current->getID()<<*;Name=*

   <<current->getName()<<”.”<<endl;

   Current=theIterator->getnext90;

  }

}

 

1.     添加和删除元素

 

为了输入一个元素,程序需要收集被链接到链表中的对象数据(在本例中是Thing对象),然后,然后程序创建一个对象,接着调用链表管理器的insert函数。为了删除一个元素,程序只需要知道该元素的输入的关键字即可,然后它将该关键字传递给链表管理器的remove函数。链表管理器负责链表操作的各种细节。

 

2.     链表的遍历

 

 同数组和向量一样,链表管理器使用一个迭代对象处理链表的遍历。单链表的链表迭代对象(见程序3-13和程序3-14)从链表管理器中取得链表的头节点以取得对自身进行初始化,然后,它按照每个节点的“next“指针遍历整个链表。

 

 

 

【程序3-13】单链表迭代类的声明

 #ifndef   LISTITR

 #define   LISTITR

 

 #include “node.h”

 #include “listmgr.h”

 

 

 Class ListItr

{

  Private:

         Node*current;

         ListMgr*theList;

  Public:

        ListItr (ListMgr*)

        Thing*getNext();

 };

 

#endif

 

【程序3-14】单链表迭代类的具体实现

 #include

 ListItr::ListItr(ListMgr*whichList)

 {
      current=0;

      theList=whichList;

  }

 

 

 Thing*ListItr::getNext()

{

 if(current==0)

         current=theList->getFirst();

 else

         current=current->getNext();

 if (current!=0)

         return current->getThing90;

 else

         return   0;

}

 

当然,迭代对象是否达到链表末尾(换句话说,在getnext返回0的时候)的判断工作需要由使用迭代对象的函数负责(在本例中,该函数为程序3-12中应用程序类的view函数)

 

注意:本书并没有提供演示程序引用程序类其余部分的完整代码。这主要出自两个原因:第一,如果包括这些代码,单单是简单的I/O部分就会使本书增加将近60页的内容,第二,这些代码可以从作者的web站点(HTTP//wwww.blackgryphonltd.com)下载。

 

 

《面向对象c++数据结构》【美】Jan Harrington 著 陈博译(仅供学习)

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值