Chinaunix首页 | 论坛 | 博客
  • 博客访问: 175547
  • 博文数量: 89
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 828
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-08 10:44
文章分类
文章存档

2014年(9)

2013年(80)

我的朋友

分类: C/C++

2013-12-03 17:45:44

  1. /* 
  2.   循环链表 
  3.   实现功能,创建循环链表,插入指定数据 
  4.   到指定位置,删除,查找数据位置,插入 
  5.   到指定位置 
  6. */  
  7. #include  
  8. using namespace std;  
  9.   
  10. template<class T>  
  11. struct Node  
  12. {  
  13.    T data;  
  14.    struct Node *next;/*指针域,在这里可省略*/  
  15. };  
  16.   
  17. template <class T>  
  18. class ClinkList  
  19. {  
  20.  public:  
  21.   ClinkList()  
  22.   {  
  23.    head=new Node ;  
  24.    head->next=head;  
  25.   }        /*无参构造函数*/  
  26.   ClinkList(T a[], int n);  /*有参构造函数,使用含有n个元素的数组a初始化单链表(头插法和尾插法)*/  
  27.   ~ClinkList();      /*析构函数*/  
  28.   int GetLength();    /*获取线性表的长度*/  
  29.   Node * Get(int i);   /*获取线性表第i个位置上的元素*/  
  30.   int Locate(T x);    /*查找线性表中值为x的元素,找到后返回其位置*/  
  31.   void Insert(int i, T x);        /*在线性表的第i个位置上插入值为x的新元素*/  
  32.   int Delete(int i);    /*删除线性表第i个元素,并将该元素返回*/  
  33.   void PrintList();    /*按次序遍历线性表中的各个数据元素*/  
  34.  private:  
  35.   Node  *head;     /*尾指针*/  
  36. };  
  37.   
  38.   
  39. template<class T>  
  40. ClinkList::ClinkList(T a[],int n)     //头插法建立循环列表  
  41. {  
  42.  head = new Node;  
  43.     head->next = head;  
  44.  head->data = n;  
  45.  for(int i=0;i<=n-1;i++)  
  46.  {  
  47.   Node *s=new Node;   //修改新节点的指针域  
  48.   s->data=a[i];     //修改头结点的指针域,将新节点加入到链表中  
  49.   s->next=head->next;  
  50.   head->next=s;  
  51.  }  
  52. }  
  53.   
  54.    
  55.   
  56. template <class T>  
  57. int ClinkList::GetLength()  
  58. {  
  59.  int i=0;  
  60.  Node*p=head;  
  61.  while(p->next!=head)  
  62.  {  
  63.   i++;  
  64.   p=p->next;  
  65.  }  
  66.  return i;  
  67. }  
  68.   
  69. template <class T>  
  70. Node* ClinkList::Get(int i)  
  71. {  
  72.  if(i>head->data) cout<<"超出范围";  
  73.  Node *p= head;  
  74.  int k = 0;  
  75.  while(k!=i)  
  76.  {  
  77.   k++;  
  78.   p=p->next;  
  79.  }  
  80.  return p;  
  81. }  
  82. /*查找x这个数据在循环链表中的位置*/  
  83. template <class T>  
  84. int ClinkList:: Locate(T x)  
  85. {   int i = 0;  
  86.  Node *p = head;  
  87.  while(p->data!=x)  
  88.  {  
  89.         p=p->next;  
  90.         i++;  
  91.     }  
  92.  if(i>head->data)  
  93.   throw"该链表中无此数";  
  94.  else  
  95.         return i;  
  96. }  
  97.   
  98. /*插入到第i个数据后面*/  
  99. template<class T>  
  100. void ClinkList:: Insert(int i, T x)  
  101. {  
  102.  if(i>head->data) throw"插入位置错误";  
  103.  Node *p = head;  
  104.  int k = 0;  
  105.  while(k!=i)  
  106.  {  
  107.   k++;  
  108.   p=p->next;  
  109.  }  
  110.     Node *s=new Node;  
  111.     s->data=x;  
  112.     s->next=p->next;  
  113.     p->next=s;  
  114.     head->data++;  
  115. }  
  116.   
  117. template<class T>  
  118. int ClinkList:: Delete(int i)  
  119. {  
  120.  if(i>head->data) throw"删除位置错误";  
  121.  Node *p=head->next;  
  122.  Node *q=head;  
  123.  int k = 1;  
  124.  while(k!=i)  
  125.  {   k++;  
  126.         q = p;  
  127.   p = p->next;  
  128.  }  
  129.  int x = p->data;  
  130.  q->next = p->next;  
  131.  delete p;  
  132.  p = NULL;  
  133.  head->data--;  
  134.  return x;  
  135. }  
  136.   
  137. template <class T>  
  138. void ClinkList:: PrintList()  
  139. {  
  140.  Node *p=head->next;  
  141.  //int i=GetLength();  
  142.  int i = head->data;  
  143.  cout<<"length = "<
  144.  while(p!=head)  
  145.  {  
  146.   cout<data<<"   ";  
  147.   p = p->next;  
  148.     }  
  149.     cout<
  150. }  
  151.   
  152. template <class T>  
  153. ClinkList::~ClinkList()  
  154. {  
  155.     while(head->next!=head)  
  156.     {  
  157.        Node *p = head->next;  
  158.        head->next = p->next;  
  159.        delete p;  
  160.        p = NULL;  
  161.     }  
  162.     delete head;  
  163.     head = NULL;  
  164. }  
  165.   
  166. int main()  
  167. {  
  168.  int b[]={0,1,2,3,4,5,6,7,8,9};  
  169.  ClinkList<int>  circleLinkList(b,10);  
  170.  circleLinkList.PrintList();  
  171.   
  172.     circleLinkList.Delete(5);  
  173.     cout<"after circleLinkList.Delete(5)"<
  174.  circleLinkList.PrintList();  
  175.   
  176.  Node<int> *result = circleLinkList.Get(5);  
  177.  cout<"circleLinkList.Get(5) is "<data<
  178.   
  179.  circleLinkList.Insert(4,5);  
  180.  circleLinkList.PrintList();  
  181.   
  182.  cout<"circleLinkList.Locate(7) is at  "<
  183.   
  184.  return 0;  
  185. }  
  186.   
  187.    
  188.   
  189.    
  190.   
  191.    
阅读(872) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~