C++如何实现线性表链式存储

这篇文章主要讲解了C++如何实现线性表链式存储,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

创新互联建站是一家集网站建设,市北企业网站建设,市北品牌网站建设,网站定制,市北网站建设报价,网络营销,网络优化,市北网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

实现的功能:

1、定义了三中传入不同参数的构造函数,用于初始化创建不同的链表;
2、能实现增、删、查等基本功能;

存在的问题:

当创建一个已知大小的空链表后,链表中的数据并不为空,见下图:

C++如何实现线性表链式存储

下面是代码及测试结果:

singlelinklist.h

#pragma once

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

using namespace std;

//结点类
template
class Node
{
public:
 T data;
 Node *next;
 Node()
 {
 this->next = NULL;
 }
 Node(T data, Node* next = NULL)
 {
 this->data = data;
 }
 ~Node() {};
};

//定义链表类
template
class SLinkList
{
public:
 Node node; //结点
 Node* 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
SLinkList::SLinkList()
{
 Node* p = new Node();
 this->head = p;
 cout << "finish!" << endl;
}

template
SLinkList::SLinkList(int num, T elem[])
{
 try
 {
 if(num < 1)
  throw length_error("输入初始化num参数必须大于1!");
 else
 {
  Node* p = new Node();
  this->head = p;
  Node* temp = this->head;
  for (int i = 0; i < num; i++)
  {
  temp->next = new Node(*(elem + i)); //这里犯了一个错,就是把elem[i]直接丢进去,()里面放的是T类型初始值,实参传进来的是T* elem[],无法用下标进行访问
  temp = temp->next;
  }
  temp->next = NULL;
  cout << "finish!" << endl;
 }
 }
 catch(length_error e)
 {
 cout << "info:" << e.what() << endl;
 exit(1);
 }
}

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

//调用函数析构
/*
template
SLinkList::~SLinkList()
{
 this->DeleteAllNode();
}
*/


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


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

template
void SLinkList::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* Elem = new Node(elem);
  Node* temp = this->head;
  for (int j = 1; j < i; j++)
   temp = temp->next;
  Node* p = temp->next;
  temp->next = Elem;
  Elem->next = p;
  }
  else
  InsertNode(elem); //在末尾插入
  cout << "finish!" << endl;
 }
  }
 catch(out_of_range e)
 {
 cout << "info:" << e.what() << endl;
 exit(0);
 }
}

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

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

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

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

template
int* SLinkList::GetNum(T elem)
{
 Node* p = this->head;
 Node* 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!" << endl;
 return count;
}

template
void SLinkList::OutputList()
{
 Node* p = this->head;
 Node* 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" << endl;  
 SLinkList sqlinklist1;
 cout << "**************************************************" << endl;
 cout << "test Init" << 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 sqlinklist2(num, a);
 cout << "length:" << sqlinklist2.LengthSLinkList() << endl;
 sqlinklist2.OutputList();
 cout << endl;
 //cout << "test error" << endl;
 //SLinkList error1(0, a);
 cout << "**************************************************" << endl;
 cout << "test Init" << endl;
 int num1 = 5;
 SLinkList slistlink3(num1);
 cout << "length:" << slistlink3.LengthSLinkList() << endl;
 slistlink3.OutputList();
 cout << endl;
 //cout << "test error" << endl;
 //SLinkList errror(0);
 cout << "**************************************************" << endl;
 cout << "test operation" << endl;
 sqlinklist2.InsertNode(2, 1996);
 sqlinklist2.OutputList();
 cout << endl;
 //cout << "test error" << endl;
 //error.InsertNode(0, 0);
 //error.InsertNode(7, 0);
 cout << "test operation" << endl;
 sqlinklist2.InsertNode(256);
 sqlinklist2.OutputList();
 cout << endl;
 //cout << "test error" << endl;
 //error.InsertNode(0);
 //error.InsertNode(7);
 cout << "**************************************************" << endl;
 cout << "test operation" << endl;
 sqlinklist2.DeleteNode(1);
 sqlinklist2.OutputList();
 cout << endl;
 //cout << "test error" << endl;
 //error.DeleteNode(18);
 //cout << "test operation" << endl;
 //sqlinklist2.DeleteAllNode();
 cout << "**************************************************" << endl;
 cout << "test operation" << endl;
 cout << "num '2' is:" << sqlinklist2.GetElem(2) << endl;
 //cout << "test error" << endl;
 //error.DeleteNode(18);
 cout << "test operation" << endl;
 cout << "elem '2' is :" << *(sqlinklist2.GetNum(2)) << endl;
 cout << endl;
 return 0;
}

看完上述内容,是不是对C++如何实现线性表链式存储有进一步的了解,如果还想学习更多内容,欢迎关注创新互联行业资讯频道。


本文标题:C++如何实现线性表链式存储
网页地址:http://scyanting.com/article/jpsgie.html