欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

C++如何實(shí)現(xiàn)線(xiàn)性表鏈?zhǔn)酱鎯?chǔ)

這篇文章主要講解了C++如何實(shí)現(xiàn)線(xiàn)性表鏈?zhǔn)酱鎯?chǔ),內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設(shè),市北企業(yè)網(wǎng)站建設(shè),市北品牌網(wǎng)站建設(shè),網(wǎng)站定制,市北網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷(xiāo),網(wǎng)絡(luò)優(yōu)化,市北網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿(mǎn)足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專(zhuān)業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶(hù)成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

實(shí)現(xiàn)的功能:

1、定義了三中傳入不同參數(shù)的構(gòu)造函數(shù),用于初始化創(chuàng)建不同的鏈表;
2、能實(shí)現(xiàn)增、刪、查等基本功能;

存在的問(wèn)題:

當(dāng)創(chuàng)建一個(gè)已知大小的空鏈表后,鏈表中的數(shù)據(jù)并不為空,見(jiàn)下圖:

C++如何實(shí)現(xiàn)線(xiàn)性表鏈?zhǔn)酱鎯?chǔ)

下面是代碼及測(cè)試結(jié)果:

singlelinklist.h

#pragma once

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

using namespace std;

//結(jié)點(diǎn)類(lèi)
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() {};
};

//定義鏈表類(lèi)
template<class T>
class SLinkList
{
public:
 Node<T> node; //結(jié)點(diǎn)
 Node<T>* head; //頭指針

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

 int LengthSLinkList();  //表長(zhǎng)
 void InsertNode(int i, T elem);  //插入結(jié)點(diǎn)
  void InsertNode(T elem);
 void DeleteNode(int i); //刪除結(jié)點(diǎn)
 void DeleteAllNode(); //刪除表
 T GetElem(int i);  //按序號(hào)查找
 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參數(shù)必須大于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)); //這里犯了一個(gè)錯(cuò),就是把elem[i]直接丟進(jìn)去,()里面放的是T類(lèi)型初始值,實(shí)參傳進(jìn)來(lái)的是T* elem[],無(wú)法用下標(biāo)進(jìn)行訪(fǎng)問(wèn)
  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("輸入初始化參數(shù)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);
 }
}

//調(diào)用函數(shù)析構(gòu)
/*
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("輸入的參數(shù)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("輸入的參數(shù)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("輸入?yún)?shù)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:測(cè)試初始化構(gòu)造函數(shù)
 //operator:測(cè)試增、刪、查
 //標(biāo)注行代碼為異常測(cè)試,主要包括length_error和out_of_range
 //最后輸出的是析構(gòu)函數(shù),下面代碼行中未給出,見(jiàn).h文件中的析構(gòu)函數(shù)定義
 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;
}

看完上述內(nèi)容,是不是對(duì)C++如何實(shí)現(xiàn)線(xiàn)性表鏈?zhǔn)酱鎯?chǔ)有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

本文題目:C++如何實(shí)現(xiàn)線(xiàn)性表鏈?zhǔn)酱鎯?chǔ)
文章轉(zhuǎn)載:http://chinadenli.net/article4/jpsgie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)App開(kāi)發(fā)做網(wǎng)站虛擬主機(jī)面包屑導(dǎo)航微信公眾號(hào)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

小程序開(kāi)發(fā)