自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C++數(shù)據(jù)結構之單鏈表

開發(fā) 后端
線性表包含數(shù)據(jù)域和指針域。單鏈表用一組地址任意的存儲單元存放線性表中的數(shù)據(jù)元素。

  線性表包含 數(shù)據(jù)域和指針域 其中,data存儲數(shù)據(jù)本身的值,next存儲后繼元素的地址 下面的圖表示的是一個數(shù)據(jù)節(jié)點

單鏈表的結構示意圖(包括空的單鏈表):

單鏈表的節(jié)點類:

  1.  template<classT>  
  2.   classNode  
  3.   {  
  4.   public:  
  5.   T data;//數(shù)據(jù)  
  6.   Node<T> *next;//next指針  
  7.   Node()  
  8.   {  
  9.   this->next=NULL;//構造空的節(jié)點  
  10.   }  
  11.   Node(T data,Node<T> *next=NULL)//構造一個節(jié)點  
  12.   {  
  13.   this->data=data;  
  14.   this->next=next;  
  15.   }  
  16.   }; 

 

  單鏈表類聲明如下:

 

  1.   #include<iostream>  
  2.   #include "Node.h"//單鏈表節(jié)點類  
  3.   template<classT>  
  4.   classSinglyLinkedList //單鏈表類  
  5.   {  
  6.   public:  
  7.   Node<T> *head;//單鏈表的頭指針。  
  8.   SinglyLinkedList();//構造空的單鏈表。  
  9.   SinglyLinkedList(T value[], intn);//構造由指定的數(shù)組提供的單鏈表  
  10.   ~SinglyLinkedList();//析構  
  11.   boolisEmpty();//判斷是否為空。  
  12.   intlength();//獲取長度  
  13.   Node<T>* getNode(inti);//返回第i(i>=0)個節(jié)點指針  
  14.   T get(inti);//返回第i個元素  
  15.   boolset(inti,T x);//設置第i個元素為x  
  16.   template<classT> friend std::ostream& operator<<(std::ostream& out,SinglyLinkedList<T> &list);  
  17.   Node<T>* insert(inti,T x);//插入第I個節(jié)點并返回第i個節(jié)點的指針  
  18.   boolremove(inti,T& old);//刪除第i個元素,將刪除的元素存放到old  
  19.   voidclear();//清空單鏈表  
  20.   voidconcat(SinglyLinkedList<T> &list);//將List鏈接在當前單鏈表之后  
  21.   }; 

 

  單鏈表部分如構造空的鏈表對象,析構,判斷為空的實現(xiàn),沒有要講的算法,實現(xiàn)如下:

 

  1.   template<classT>  
  2.   SinglyLinkedList<T>::SinglyLinkedList()//構造空的單鏈表  
  3.   {  
  4.   this->head=NULL;  
  5.  }  
  6.   template<classT>  
  7.   SinglyLinkedList<T>::~SinglyLinkedList()//析構  
  8.   {  
  9.   clear();  
  10.   }  
  11.   template<classT>  
  12.   boolSinglyLinkedList<T>::isEmpty()//判斷鏈表是否為空  
  13.   {  
  14.   returnthis->head==NULL;  
  15.   } 

 

  單鏈表的遍歷操作,遍歷單鏈表是指從第一個節(jié)點開始訪問,沿著節(jié)點的Next可依次訪問單鏈表中的各個節(jié)點,并且各個節(jié)點只被訪問一次。實現(xiàn)的單鏈表遍歷的基本算法如下:

 

  1.   intj=0;  
  2.   Node<T> *p=head;  
  3.   while(p!=NULL&&j<i)  
  4.   {  
  5.   j++;  
  6.   p=p->next;  
  7.   } 

 

  單鏈表的length(),get(),set(),clear()和輸出等操作都基于以上算法。

  1.   template<classT>  
  2.   intSinglyLinkedList<T>::length()  
  3.   {  
  4.  inti=0;  
  5.   Node<T> *p=head;//創(chuàng)建一個用于遍的變量  
  6.   while(p!=NULL)  
  7.   {  
  8.   i++;  
  9.   std::cout<<p->data;  
  10.   p=p->next;  
  11.   }  
  12.  returni;  
  13.   }  
  14.   template<classT>  
  15.   Node<T>* SinglyLinkedList<T>::getNode(inti)  
  16.   {  
  17.   if(i<0)  
  18.   returnNULL;  
  19.   intj=0;  
  20.   Node<T> *p=head;  
  21.   while(p!=NULL&&j<i)  
  22.   {  
  23.   j++;  
  24.   p=p->next;  
  25.   }  
  26.   returnp;  
  27.   }  
  28.   template<classT>  
  29.   T SinglyLinkedList<T>::get(inti)  
  30.   {  
  31.   Node<T> *p=getNode(i);  
  32.   if(p!=NULL)  
  33.   returnp->data;  
  34.   T d;  
  35.   returnd;  
  36.   //throw "單鏈表為空或者參數(shù)指定的元素不存在";  
  37.   }  
  38.   template<classT>  
  39.   boolSinglyLinkedList<T>::set(inti,T x)  
  40.   {  
  41.   Node<T> *p=getNode(i);  
  42.   if(p!=NULL)  
  43.   {  
  44.   p->data=x;  
  45.   returntrue;  
  46.   }  
  47.   returnfalse;  
  48.   }  
  49.   template<classT>  
  50.   std::ostream& operator<<(std::ostream& out,SinglyLinkedList<T> &list)  
  51.   {  
  52.   Node<T> *p=list.head;  
  53.   out<<"(";  
  54.   while(p!=NULL)  
  55.   {  
  56.   out<<p->data;  
  57.   p=p->next;  
  58.   if(p!=NULL)  
  59.   out<<",";  
  60.   }  
  61.   out<<") ";  
  62.   returnout;  
  63.   }  
  64.   template<classT>  
  65.   voidSinglyLinkedList<T>::clear()  
  66.   {  
  67.   Node<T> *p=head;  
  68.   while(p!=NULL)  
  69.   {  
  70.   Node<T> *q=p;  
  71.   p=p->next;  
  72.   delete q;  
  73.   }  
  74.   head=NULL;  
  75.  } 

   單鏈表的插入操作,單鏈表不像順序表,對與表的插入和刪除很簡單:

  空表插入/頭插入

  1.   Node<T> *q=NULL;  
  2.   if(head==NULL||i<0)//頭插入(單鏈表為空或者)  
  3.   {  
  4.   q=newNode<T>(x,head);  
  5.   head=q;  
  6.   }  
  7.   中間插入/尾插入  
  8.   p->next=newNode<T>(x,p->next);  
  9.   單鏈表insert()以及參數(shù)構造函數(shù):  
  10.  template<classT>  
  11.   Node<T>* SinglyLinkedList<T>::insert(inti,T x)  
  12.   {  
  13.   Node<T> *q=NULL;  
  14.   if(head==NULL||i<0)//頭插入(單鏈表為空或者)  
  15.   {  
  16.   q=newNode<T>(x,head);  
  17.   head=q;  
  18.   }  
  19.   else 
  20.   {  
  21.   intj=0;  
  22.   Node<T> *p=head;  
  23.   while(p->next!=NULL&&j<i-1)  
  24.   {  
  25.   j++;  
  26.   p=p->next;  
  27.   }  
  28.   q=newNode<T>(x,p->next);  
  29.  p->next=q;  
  30.   }  
  31.   returnq;  
  32.   }  
  33.  template<classT>  
  34.   SinglyLinkedList<T>::SinglyLinkedList(T table[],intn)  
  35.   {  
  36.   head=NULL;  
  37.   if(n>0)  
  38.   {  
  39.   head=newNode<T>(table[0]);//創(chuàng)建節(jié)點  
  40.  Node<T> *rear=head;//創(chuàng)建一個指向頭節(jié)點的指針  
  41.   inti=1;  
  42.  while(i<n)  
  43.   {  
  44.   rear->next=newNode<T>(table[i++]);  
  45.   rear=rear->next;  
  46.   }  
  47.   }  
  48.   } 

  單鏈表的刪除操作也分兩類:

  頭刪除

  1.   Node<T> *q=head;  
  2.   head=head->next;  
  3.   delete q; 

  中間/尾刪除

  1.   Node<T> *q=p->next;  
  2.   if(q!=NULL)//判斷刪除節(jié)點  
  3.   {  
  4.   p->next=q->next;//讓刪除節(jié)點的前驅(qū)Next指針下一節(jié)點  
  5.   delete q;//刪除該節(jié)點  
  6.   } 

   單鏈表的刪除函數(shù)remove()實現(xiàn):

  1.   template<classT>  
  2.   boolSinglyLinkedList<T>::remove(inti,T &old)  
  3.   {  
  4.   if(i<0||head==NULL)  
  5.   {  
  6.   Node<T> *q=head;  
  7.   old=q->data;  
  8.   head=head->next;  
  9.   delete q;  
  10.  }  
  11.   else 
  12.   {  
  13.   Node<T> *p=getNode(i-1);//獲取刪除節(jié)點的前驅(qū)  
  14.  if(p!=NULL&&p->next!=NULL)//判斷刪除節(jié)點和刪除節(jié)點是否為空  
  15.   {  
  16.   Node<T> *q=p->next;//新建一個節(jié)點指針,將刪除接點復制過去  
  17.   old=q->data;  
  18.   p->next=q->next;//讓刪除節(jié)點的前驅(qū)Next指針下一節(jié)點  
  19.   delete q;//刪除該節(jié)點  
  20.  returntrue;  
  21.   }  
  22.  }  
  23.   returnfalse;  
  24.   }  
  25.   單鏈表的鏈接函數(shù):concat()  
  26.   template<classT>  
  27.   voidSinglyLinkedList<T>::concat(SinglyLinkedList<T> &list)  
  28.   {  
  29.   if(this->head==NULL)  
  30.   {  
  31.   this->head=list->head;  
  32.   }  
  33.   else 
  34.   {  
  35.   Node<T> *p=head;  
  36.   while(p->next!=NULL)  
  37.   {  
  38.   p=p->next;  
  39.   }  
  40.   p=list->head;  
  41.   }  
  42.   list->head=NULL;//設置單鏈表為空,否則運行出錯  
  43.   } 

   以上對C++單鏈表的分析 添加一個學生結構和一個測試函數(shù):

  1.   Student.h  
  2.   structStudent  
  3.   {  
  4.   charnumber[10]; //學號  
  5.   charname[20]; //姓名  
  6.   doublescore; //得分  
  7.   friend std::ostream& operator<<(std::ostream& out,Student &stu)  
  8.   {  
  9.   out<<"學號:"<<stu.number<<"姓名:"<<stu.name<<"得分:"<<stu.score;  
  10.   returnout;  
  11.   }  
  12.   };  
  13.   主函數(shù):  
  14.   #include<iostream>  
  15.   #include "SinglyLinkedList.h" 
  16.   #include "Student.h" 
  17.   void_TestToSinglyLinkedList()  
  18.   {  
  19.   Student data[]={{"090313018","Silvester",45.4},{"090313018","捐贈",45.4},{"090313018","版主",45.6}};  
  20.   SinglyLinkedList<Student> m(data,3);  
  21.   Student t;  
  22.   std::cout<<(m.isEmpty()?"不為空!":"該鏈表為空!")<<std::endl;  
  23.   std::cout<<"長度:"<<m.length()<<std::endl;  
  24.   std::cout<<"移除2個學生"<<m.remove(1,t)<<std::endl;  
  25.   std::cout<<"t:"<<t<<std::endl;  
  26.   std::cout<<"2個學生信息"<<m.getNode(1)<<std::endl;  
  27.   Student s={"415646","fdsfs",453.1};  
  28.   std::cout<<m.get(1)<<m.set(1,s)<<m.insert(5,s)<<std::endl;  
  29.   }  
  30.   voidmain()  
  31.  {  
  32.   _TestToSinglyLinkedList();  
  33.   system("pause");  
  34.   } 

 

  提供源代碼下載地址:http://39327.42la.com.cn/DataFile/Code/C++/SinglyLinkedList.zip

原文地址:http://www.cnblogs.com/Arrays/archive/2012/02/01/2335164.html

【編輯推薦】

  1. 陳皓:Why C++? 王者歸來
  2. 2011年12月編程語言排行榜:C++11它就像一個新語言
  3. Dart之于JavaScript正如C#之于C++
  4. 詳解C++11中值得關注的幾大變化
  5. C++程序員必讀:讓你的代碼更強大
責任編輯:彭凡 來源: 博客園
相關推薦

2021-07-13 07:52:03

Python數(shù)據(jù)結構

2021-07-15 06:43:12

Python數(shù)據(jù)結構

2017-03-01 13:58:46

Python數(shù)據(jù)結構鏈表

2011-04-11 17:09:37

稀疏矩陣矩陣C++

2021-03-10 08:42:19

Java數(shù)據(jù)結構算法

2011-04-11 12:22:11

數(shù)據(jù)結構C++

2011-04-11 12:48:36

隊列數(shù)據(jù)結構C++

2011-04-11 11:23:17

隊列數(shù)據(jù)結構

2021-08-03 10:24:59

數(shù)據(jù)跳躍鏈表結構

2021-05-12 14:09:35

鏈表數(shù)據(jù)結構線性結構

2010-01-27 15:58:35

C++數(shù)據(jù)結構

2021-04-12 15:47:00

數(shù)據(jù)結構算法鏈表

2021-06-08 06:01:00

C++數(shù)據(jù)結構向量和數(shù)組

2021-12-21 08:19:29

數(shù)據(jù)結構算法鏈表相交

2020-10-28 10:10:03

Java單鏈表數(shù)據(jù)結構

2009-08-12 18:35:17

C#數(shù)據(jù)結構

2021-10-29 11:27:52

鏈表數(shù)據(jù)結構算法

2021-01-06 08:03:00

JavaScript數(shù)據(jù)結構

2024-01-15 06:01:36

C++數(shù)組

2009-08-11 14:43:42

C#數(shù)據(jù)結構與算法
點贊
收藏

51CTO技術棧公眾號