中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C++如何實現線性表鏈式存儲

發布時間:2020-07-22 10:56:26 來源:億速云 閱讀:106 作者:小豬 欄目:編程語言

這篇文章主要講解了C++如何實現線性表鏈式存儲,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。

實現的功能:

1、定義了三中傳入不同參數的構造函數,用于初始化創建不同的鏈表;
2、能實現增、刪、查等基本功能;

存在的問題:

當創建一個已知大小的空鏈表后,鏈表中的數據并不為空,見下圖:

C++如何實現線性表鏈式存儲

下面是代碼及測試結果:

singlelinklist.h

#pragma once

#include "iostream"
#include "exception"
#include "stdlib.h"
#include "malloc.h"

using namespace std;

//結點類
template<class T>
class Node
{
public:
 T data;
 Node<T> *next;
 Node()
 {
 this->next = NULL;
 }
 Node(T data, Node<T>* next = NULL)
 {
 this->data = data;
 }
 ~Node() {};
};

//定義鏈表類
template<class T>
class SLinkList
{
public:
 Node<T> node; //結點
 Node<T>* head; //頭指針

 SLinkList();   //創建鏈表
 SLinkList(int num, T elem[]);
 SLinkList(int num);
 ~SLinkList();

 int LengthSLinkList();  //表長
 void InsertNode(int i, T elem);  //插入結點
  void InsertNode(T elem);
 void DeleteNode(int i); //刪除結點
 void DeleteAllNode(); //刪除表
 T GetElem(int i);  //按序號查找
 int* GetNum(T elem);  //按元素查找
 void OutputList();    //輸出
};

template<class T>
SLinkList<T>::SLinkList()
{
 Node<T>* p = new Node<T>();
 this->head = p;
 cout << "finish<SLinkList()>!" << endl;
}

template<class T>
SLinkList<T>::SLinkList(int num, T elem[])
{
 try
 {
 if(num < 1)
  throw length_error("輸入初始化num參數必須大于1!");
 else
 {
  Node<T>* p = new Node<T>();
  this->head = p;
  Node<T>* temp = this->head;
  for (int i = 0; i < num; i++)
  {
  temp->next = new Node<T>(*(elem + i)); //這里犯了一個錯,就是把elem[i]直接丟進去,()里面放的是T類型初始值,實參傳進來的是T* elem[],無法用下標進行訪問
  temp = temp->next;
  }
  temp->next = NULL;
  cout << "finish<SL]inkList(int num, T elem[])>!" << endl;
 }
 }
 catch(length_error e)
 {
 cout << "info:" << e.what() << endl;
 exit(1);
 }
}

template<class T>
SLinkList<T>::SLinkList(int num)
{
 try
 {
 if (num < 1)
  throw length_error("輸入初始化參數num必須大于1!");
 else
 {
  Node<T>* p = new Node<T>();
  this->head = p;
  Node<T>* temp = this->head;
  for(int i = 1; i <= num; i++)
  {
  temp->next = new Node<T>();
  temp = temp->next;
  }
  temp->next = NULL;
  cout << "finish<SLinkList(int num)>!" << endl;
 }
 }
 catch(length_error e)
 {
 cout << "info:" << e.what() << endl;
 exit(0);
 }
}

//調用函數析構
/*
template<class T>
SLinkList<T>::~SLinkList()
{
 this->DeleteAllNode();
}
*/


template<class T>
SLinkList<T>::~SLinkList()
{
 Node<T>* p = this->head;
 Node<T>* temp = p->next;
 Node<T>* q = NULL;
 while(temp)
 {
 q = temp;
 temp = temp->next;
 delete q;
 }
 delete p;
 delete temp;
 cout << "finish ~SLinkList()" << endl;
}


template<class T>
int SLinkList<T>::LengthSLinkList()
{
 int count = 0;
 Node<T>* p = this->head;
 Node<T>* temp = p->next;
 while (temp)
 {
 count++;
 temp = temp->next;
 }
 return count;
}

template<class T>
void SLinkList<T>::InsertNode(int i, T elem)
{
 try
 {
 if(i<1 || i>LengthSLinkList() + 1)
  throw out_of_range("輸入的參數i值超出了鏈表的范圍!");
 else
 {
  if (i != LengthSLinkList()+1) //1-len之間插入
  {
  Node<T>* Elem = new Node<T>(elem);
  Node<T>* temp = this->head;
  for (int j = 1; j < i; j++)
   temp = temp->next;
  Node<T>* p = temp->next;
  temp->next = Elem;
  Elem->next = p;
  }
  else
  InsertNode(elem); //在末尾插入
  cout << "finish<Insert(int i, T elem)>!" << endl;
 }
  }
 catch(out_of_range e)
 {
 cout << "info:" << e.what() << endl;
 exit(0);
 }
}

template<class T>
void SLinkList<T>::InsertNode(T elem)  //末尾插入
{
 Node<T>* Elem = new Node<T>(elem);
 Node<T>* temp = this->head;
 Node<T>* p = NULL;
 while (temp)
 {
 p = temp;
 temp = temp->next;
 }
 p->next = Elem;
 Elem->next = NULL;
 cout << "finnish<InsertNode(T elem)>!" << endl;
}

template<class T>
void SLinkList<T>::DeleteNode(int i)  //返回的是被刪除的值
{
 try
 {
 if (i<1 || i > LengthSLinkList())
  throw out_of_range("輸入的參數i值超出了鏈表的范圍!");
 else
 {
  Node<T>* temp = this->head;
  for (int j = 1; j < i; j++)
  temp = temp->next;
  Node<T> *p = temp->next;
  temp->next = p->next;
  cout << "finish<DeleteNode(int i)>!" << endl;
 }
 }
 catch(out_of_range e)
 {
 cout << "info:" << e.what() << endl;
 exit(1);
 }
}

template<class T>
void SLinkList<T>::DeleteAllNode()
{
 Node<T>* temp = this->head;
 int count = this->LengthSLinkList();
 while(count)
 {
 DeleteNode(count);
 count--;
 }
 delete temp;
 cout << "finish<DeleteAllNode()>!" << endl;
}

template<class T>
T SLinkList<T>::GetElem(int i)
{
 try
 {
 if(i<1 || i > LengthSLinkList())
  throw out_of_range("輸入參數i值超出了鏈表的范圍!");
 else
 {
  Node<T>* temp = this->head;
  for(int j = 1; j <=i; j++)
  temp = temp->next;
  cout << "finish!" << endl;
  cout << "finish<GetElem(int i)>! " << endl;
  return temp->data;
 }
 }
 catch(out_of_range e)
 {
 cout << "info:" << e.what() << endl;
 exit(1);
 }
}

template<class T>
int* SLinkList<T>::GetNum(T elem)
{
 Node<T>* p = this->head;
 Node<T>* temp = p->next;
 int* count = new int[LengthSLinkList()];
 int j = 0;
 for(int i = 1; i <= LengthSLinkList(); i++)  
 {
 if(temp->data == elem)
 {
  count[j] = i;
  j++;
 }
 else
  temp = temp->next;
 }
 cout << "finish<GetNum(T elem)>!" << endl;
 return count;
}

template<class T>
void SLinkList<T>::OutputList()
{
 Node<T>* p = this->head;
 Node<T>* temp = p->next;
 cout << "output list:" ;

 for(int i = 1; i <= LengthSLinkList(); i++)
 {
 cout << temp->data << "   " ;
 temp = temp->next;
 }
 cout << endl;
}

main.cpp

#include "iostream"
#include "singlelinklist.h"

using namespace std;

int main()
{
 //Init:測試初始化構造函數
 //operator:測試增、刪、查
 //標注行代碼為異常測試,主要包括length_error和out_of_range
 //最后輸出的是析構函數,下面代碼行中未給出,見.h文件中的析構函數定義
 cout << "**************************************************" << endl;
 cout << "test Innit<SLinkList()>" << endl;  
 SLinkList<int> sqlinklist1;
 cout << "**************************************************" << endl;
 cout << "test Init<SLinkList(int num, T elem[])>" << endl;
 int num = 5;
 int a[] = {1, 2, 3, 4, 5};
 cout << "input list:" ;
 for(int i = 0; i < num; i++)
 cout << a[i] << "   ";
 cout << endl;
 SLinkList<int> sqlinklist2(num, a);
 cout << "length:" << sqlinklist2.LengthSLinkList() << endl;
 sqlinklist2.OutputList();
 cout << endl;
 //cout << "test error" << endl;
 //SLinkList<int> error1(0, a);
 cout << "**************************************************" << endl;
 cout << "test Init<SLinkList(int num)>" << endl;
 int num1 = 5;
 SLinkList<int> slistlink3(num1);
 cout << "length:" << slistlink3.LengthSLinkList() << endl;
 slistlink3.OutputList();
 cout << endl;
 //cout << "test error" << endl;
 //SLinkList<int> errror(0);
 cout << "**************************************************" << endl;
 cout << "test operation<InsertNode(int i, T elem)>" << endl;
 sqlinklist2.InsertNode(2, 1996);
 sqlinklist2.OutputList();
 cout << endl;
 //cout << "test error" << endl;
 //error.InsertNode(0, 0);
 //error.InsertNode(7, 0);
 cout << "test operation<InsertNode(T elem)>" << endl;
 sqlinklist2.InsertNode(256);
 sqlinklist2.OutputList();
 cout << endl;
 //cout << "test error" << endl;
 //error.InsertNode(0);
 //error.InsertNode(7);
 cout << "**************************************************" << endl;
 cout << "test operation<DeleteNode(int i)>" << endl;
 sqlinklist2.DeleteNode(1);
 sqlinklist2.OutputList();
 cout << endl;
 //cout << "test error" << endl;
 //error.DeleteNode(18);
 //cout << "test operation<DeleteAllNode()>" << endl;
 //sqlinklist2.DeleteAllNode();
 cout << "**************************************************" << endl;
 cout << "test operation<GetElem(int i)>" << endl;
 cout << "num '2' is:" << sqlinklist2.GetElem(2) << endl;
 //cout << "test error" << endl;
 //error.DeleteNode(18);
 cout << "test operation<GetNum(T elem)>" << endl;
 cout << "elem '2' is :" << *(sqlinklist2.GetNum(2)) << endl;
 cout << endl;
 return 0;
}

看完上述內容,是不是對C++如何實現線性表鏈式存儲有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

香河县| 渑池县| 赤峰市| 湾仔区| 隆尧县| 门源| 宁安市| 盘锦市| 武夷山市| 邵武市| 绩溪县| 渭源县| 宜昌市| 民勤县| 肇庆市| 霍林郭勒市| 大新县| 客服| 龙口市| 三河市| 涪陵区| 汽车| 彭泽县| 深圳市| 曲松县| 酉阳| 鄯善县| 富平县| 乐都县| 稻城县| 云林县| 喜德县| 庆元县| 阿鲁科尔沁旗| 敦化市| 沭阳县| 沙湾县| 连州市| 额济纳旗| 疏勒县| 阿拉善右旗|