实验二 循环链表和双向链表的设计和实现

20141018

 

 

实验二 循环链表和双向链表的设计和实现

一.实验目的

深入了解循环链表和双向链表的特性,学会在实际问题下灵活运用它们。

二.问题描述

1.循环链表的实现:需实现循环链表的构造函数、析构函数、插入结点、删除结点、定位某个元素在循环链表中的位置、打印输出整个链表中元素的值等操作;并设计主函数验证设计的算法。

2.双向链表的实现:需实现双向链表的构造函数、析构函数、插入结点、删除结点、定位某个元素在双向链表中的位置、打印输出整个链表中元素的值等操作;并设计主函数验证设计的算法。

三.实验要求

1.根据单链表的操作及循环链表和双向链表的特点,实现相应操作,并设计适当的代码验证循环链表和双向链表中的实现的各种方法。

2.在实验过程中,对不同算法要考虑到在不同的存储结构下的时间复杂度和空间复杂度。

 四.实验环境

PC微机

DOS操作系统或Windows操作系统

Turbo C程序集成环境或Visual C++程序集成环境

五.实验步骤及结果

1.循环链表的实现:需实现循环链表的构造函数、析构函数、插入结点、删除结点、定位某个元素在循环链表中的位置、打印输出整个链表中元素的值等操作;并设计主函数验证设计的算法。

 

 

1.新建一个空工程,命名为“循环链表验证试验”,在该工程中新建一个头文件cirlist.h该头文件包括顺序表类 cirlist 的定义。如下图:

 

#ifndef cirlist_H

#define cirlist_H

 

template <class DataType>

struct Node

{

      DataType data;

      Node<DataType>*next;

};

template<class DataType>

class cirlist

{

      public:

             cirlist();

             cirlist(DataType a[],int n);

             ~cirlist();

             int Locate(DataType x);

             void Insert(int i,DataType x);

             DataType Delete(int i);

             void Printlist();

      private:

             Node<DataType>*first;

};

#endif

 

 

2.在工程“顺序表验证试验”中新建一个源程序文件 cirlist.cpp ,该文件包括类 cirlist 中程序函数的定义,如下图:    

 

#include<iostream>

using namespace std;

#include"cirlist.h"

template<class DataType>

cirlist<DataType>::cirlist()

{

      first=new Node<DataType>;

      first->next=NULL;

}

template<class DataType>

cirlist<DataType>::cirlist(DataType a[],int n)

{

      Node<DataType>*r,*s;

      first=new Node<DataType>;

      r=first;

      for(int i=0;i<n;i++)

      {

             s=new Node<DataType>;

             s->data=a[i];

             r->next=s;r=s;

      }

      r->next=first;

}

template<class DataType>

cirlist<DataType>::~cirlist()

{

      Node<DataType>*q=NULL;

      while(first!=NULL)

      {

             q=first;

             first=first->next;

             delete q;

      }

}

template<class DataType>

void cirlist<DataType>::Insert(int i,DataType x)

{

      Node<DataType>*p=first,*s=NULL;

      int count=0;

      while(p!=first&&count<i-1)

      {

             p=p->next;

             count++;

      }

      if(p==NULL) throw"位置";

      else {

             s=new Node<DataType>;s->data=x;

             s->next=p->next;p->next=s;

      }

}

template<class DataType>

DataType cirlist<DataType>::Delete(int i)

{

      Node<DataType>*p=first,*q=NULL;

      DataType x;

      int count=0;

      while(p!=first&&count<i-1)

      {

             p=p->next;

             count++;

      }

      if(p==NULL||p->next==first)

             throw"位置";

      else{

             q=p->next;x=q->data;

             p->next=q->next;

             delete q;

             return x;

      }

}

template<class DataType>

int cirlist<DataType>::Locate(DataType x)

{

      Node<DataType>*p=first->next;

      int count=1;

      while(p!=first)

      {

             if(p->data==x)return count;

             p=p->next;

             count++;

      }

      return 0;

}

template<class DataType>

void cirlist<DataType>::Printlist()

{

      Node<DataType>*p=first->next;

      while(p!=first)

      {

             cout<<p->data<<" ";

             p=p->next;

      }

      cout<<endl;

}

 

3、在工程中新建一个源程序文件 cirlist_main.cpp ,如下图:

#include<iostream>

using namespace std;

#include"cirlist.cpp"

void main()

{

      int r[5]={1,2,3,4,5};

      cirlist<int>L(r,5);

      cout<<"20134432祖培顺"<<endl;

      cout<<"执行插入操作前数据为:"<<endl;

      L.Printlist();

      try

      {

             L.Insert(2,3);

      }

      catch(char*s)

      {

             cout<<s<<endl;

      }

      cout<<"执行插入操作后数据为:"<<endl;

      L.Printlist();

      cout<<"值为5的元素位置为:";

      cout<<L.Locate(5)<<endl;

    cout<<"执行删除操作前数据为:"<<endl;

      L.Printlist();

      try

      {

             L.Delete(1);

      }

             catch(char*s)

             {

                    cout<<s<<endl;

             }

             cout<<"执行删除操作后数据为:"<<endl;

             L.Printlist();

}

 

2.双向链表的实现:需实现双向链表的构造函数、析构函数、插入结点、删除结点、定位某个元素在双向链表中的位置、打印输出整个链表中元素的值等操作;并设计主函数验证设计的算法。

1.新建一个空工程,命名为“循环链表验证试验”,在该工程中新建一个头文件dullist.h该头文件包括顺序表类 dullist 的定义。如下图:

#ifndef dullist_H

#define dullist_H

 

template <class DataType>

struct DulNode

{

      DataType data;

      DulNode<DataType>*prior,*next;

};

template<class DataType>

class dullist

{

      public:

             dullist();

             dullist(DataType a[],int n);

             ~dullist();

             int Locate(DataType x);

             void Insert(int i,DataType x);

             DataType Delete(int i);

             void Printlist();

      private:

             DulNode<DataType>*first;

};

#endif

2.在工程“循环列表验证试验”中新建一个源程序文件 dullist.cpp ,该文件包括类 dullist 中程序函数的定义,如下图:

#include<iostream>

using namespace std;

#include"dullist.h"

template<class DataType>

dullist<DataType>::dullist()

{

      first=new DulNode<DataType>;

      first->next=NULL;

}

template<class DataType>

dullist<DataType>::dullist(DataType a[],int n)

{

      DulNode<DataType>*r,*s;

      first=new DulNode<DataType>;

      r=first;

      for(int i=0;i<n;i++)

      {

             s=new DulNode<DataType>;

             s->data=a[i];s->prior=r;

             r->next=s;r=s;

      }

      r->next=NULL;

}

template<class DataType>

dullist<DataType>::~dullist()

{

      DulNode<DataType>*q=NULL;

      while(first!=NULL)

      {

             q=first;

             first=first->next;

             delete q;

      }

}

template<class DataType>

void dullist<DataType>::Insert(int i,DataType x)

{

      DulNode<DataType>*p=first,*s=NULL;

      int count=0;

      while(p!=NULL&&count<i-1)

      {

             p=p->next;

             count++;

      }

      if(p==NULL) throw"位置";

      else {

             s=new DulNode<DataType>;s->data=x;

             s->next=p->next;p->next->prior=s;

             p->next=s;

      }

}

template<class DataType>

DataType dullist<DataType>::Delete(int i)

{

      DulNode<DataType>*p=first;

      DataType x;

      int count=0;

      while(p!=NULL&&count<i-1)

      {

             p=p->next;

             count++;

      }

      if(p==NULL||p->next==NULL)

             throw"位置";

      else{

             x=p->data;

            

             (p->prior)->next=p->next;

             (p->next)->prior=p->prior;

             delete    p;

             return x;

      }

}

template<class DataType>

int dullist<DataType>::Locate(DataType x)

{

      DulNode<DataType>*p=first->next;

      int count=1;

      while(p!=NULL)

      {

             if(p->data==x)return count;

             p=p->next;

             count++;

      }

      return 0;

}

template<class DataType>

void dullist<DataType>::Printlist()

{

      DulNode<DataType>*p=first->next;

      while(p!=NULL)

      {

             cout<<p->data<<" ";

             p=p->next;

      }

      cout<<endl;

}

实验运行结果如下图:

 

六.实验小结

1、实验进行中仍有不会的代码,靠自己还是有点吃力,与他人交流吸取同学的长处,才能把实验做到更好。

2、理解每个文件之间的联系,代码之是如何间运行的。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值