C++中的链表类的设计

本文详细介绍了如何使用C++类实现链表的封装,包括成员变量定义、操作方法实现及关键操作如插入、删除、查找、反转、大小获取等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mylist.h   //头文件
struct node
{ int idata_item;
  struct node *pnode;}   //结点的定义
class mylist
{ private:                      //成员变量的说明
      struct node* _at_front;
      struct node* _at_end;     //定义该变量是为了链表的连结
      int          _size;
  public:
      struct node* get_front(){return _at_front;}
      struct node* get_end(){return _at_end;}
      int size(void){return _size;}
      void insert_front(int)
      void insert_end(int)
      bool insert(struct node*,int)
      int  remove(int)
      void  remove_front(void)
      void  remove_end(void)
      struct node* find(int)
      void display(void)
      void reverse(void)
      bool equality(mylist&)
      mylist& concat(mylist&)
      mylist():_at_front(0),_at_end(0),_size(0) //构造函数
      ~mylist()
       
}
链表的实现如下:
mylist.cpp  //链表实现文件名
//insert实现代码
bool mylist::insert(struct node* pnode,int a)
{assert(pnode!=0);
 struct node* temp=new struct node;
 if(!temp)
 {temp->idata_item=a;
  temp->pnext=pnode->pnext;
  pnode->pnext=temp;}
 return true;
 else
   cerr<<"non memory allocate"<<endl;
   return false;}
 
//display实现代码
void mylist::display(void)
{if(_size==0)
   cout<<"mylist is empty"<<endl;
 else
   { struct node *iter=_at_front;
       for(int  i=1;i<=_size;i++)
       { cout<<iter->idata_item<<" ";
         iter=iter->pnext;}
     }
 }       
 //reverse实现代码
void mylist::reverse(void)
  {struct node *temp;
   temp=_at_front;
   _at_front=_at_end;
   _at_end=temp;
   while(
  }       
//remove实现代码
int  mylist::remove(int a)
 { struct node *iter1=_at_front;
   struct node *iter2=0;
       for(int  i=1;i<=_size;i++)
       { if(iter1->idata_item!=a)
           iter2=iter1;
           iter1=iter1->pnext;
         else
            iter2=iter1->pnext;
            delete iter1;
            _size--;
            return 1;
        }
   
    return 0;}   
 
//insert_end实现代码
void mylist::insert_end(int a)
{ struct node* temp=new struct node;
  temp->idata_item=a;
  if(!_at_end)
      { _at_front=_at_end=temp;
       _size++;      
       }
   else
      {_at_end->pnext=temp;
       _at_end=temp;
       _size++;
      }
 }
 //insert_front实现代码
void mylist::insert_front(int a)
{struct node* temp=new struct node;
 temp->idata_item=a;
 if(!_at_front)
   {_at_front=_at_end=temp;
    _size++;
    }
 else
    {temp->pnext=_at_front;
     _at_front=temp;
    _size++;}
}

 

链表是数据结构的知识,现在我们用C++的类来实现封装.
对链表类分析如下.
链表类的成员变量(private)
struct node *_at_front;
struct node *_at_end;
int   _size;
链表中结点,所以定义结点如下:
struct node
{ int idata_item;
  struct node *pnext;}
链表所支持的操作:
insert 插入一个结点到指定的结点后;
remove 移去一个结点;
find   查找一个结点;
reverse 翻转一个链表;
size   得到链表中结点的个数;
display 链表的输出;
equality 链表相等的判断;
concat   两个链表连结在一起;
以上是链表类的有关操作,另外加上构造函数和析构函数;
链表的声明如下:
 
面向对象程序设计课程作业 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、付费专栏及课程。

余额充值