线性表的链式存储结构 单链表(Singly Linked List) C++

简介: 线性表的链式存储结构 单链表(Singly Linked List) C++

Node.h

#ifndef __NODE_H__
#define __NODE_H__
template <class T>
struct Node
{
  T data;
  Node<T> * next;
};
#endif


LinkedList.h

#ifndef __LINKEDLIST_H__
#define __LINKEDLIST_H__
#include "Node.h"
template <class T>
class LinkedList
{
public:
  LinkedList();
  LinkedList(T *_nodes, int _length); 
  virtual ~LinkedList();
public:
  void Insert(int _pos, T _node);
  T Delete(int _pos);
  void setNode(int _pos, T _node);
  int getLength();
  T getNode(int _pos);
  int LocateNode(T _node);
  void Traversal();
private:
  Node<T> *firstNode;
};
#endif


LinkedList.cpp

#include "LinkedList.h"
template <class T>
LinkedList<T>::LinkedList()
{
  firstNode = new Node();
  firstNode->next = nullptr;
}
template <class T>
LinkedList<T>::LinkedList(T *_nodes, int _length)
{
  firstNode = new Node<T>;
  Node<T> *tail = firstNode;
  for (int i = 0; i < _length; i++)
  {
  Node<T> *tempNode = new Node<T>;
  tempNode->data = *(_nodes + i);
  tail->next = tempNode;
  tail = tail->next;
  }
  tail->next = nullptr;
}
template <class T>
LinkedList<T>::~LinkedList()
{
  while (firstNode != nullptr)
  {
  Node<T> *deleteNode = firstNode;
  firstNode = firstNode->next;
  delete deleteNode;
  }
}
template <class T>
void LinkedList<T>::Insert(int _pos, T _node)
{
  Node<T> *tempNode = firstNode;
  Node<T> *insertNode = new Node<T>;
  for (int i = 1; i <= _pos - 1; i++)
  tempNode = tempNode->next;
  insertNode->data = _node;
  insertNode->next = tempNode->next;
  tempNode->next = insertNode;
}
template <class T>
T LinkedList<T>::Delete(int _pos)
{
  T tempdata;
  Node<T> *delNode = new Node<T>;
  Node<T> *tempNode = firstNode;
  for (int i = 1; i <= _pos - 1; i++)
  {
  tempNode = tempNode->next;
  delNode = tempNode->next;
  }
  tempNode->next = tempNode->next->next;
  tempdata = delNode->data;
  delete delNode;
  return tempdata;
}
template <class T>
void LinkedList<T>::setNode(int _pos, T _node)
{
  Node<T> *setNode = firstNode;
  for (int i = 1; i <= _pos; i++)
  setNode = setNode->next;
  setNode->data = _node;
}
template <class T>
int LinkedList<T>::getLength()
{
  int count = 0;
  Node<T> *countNode = firstNode;
  countNode = countNode->next;
  while (countNode != nullptr)
  {
  countNode = countNode->next;
  count += 1;
  }
  return count;
}
template <class T>
T LinkedList<T>::getNode(int _pos)
{
  T getData;
  Node<T> *getNode = firstNode;
  for (int i = 1; i <= _pos; i++)
  getNode = getNode->next;
  getData = getNode->data;
  return getData;
} 
template <class T>
int LinkedList<T>::LocateNode(T _Node)
{
  int locate = 0;
  Node<T> *locNode = firstNode;
  for (int i = 1; i <= this->getLength(); i++)
  {
  locate += 1;
  locNode = locNode->next;
  if (locNode->data == _Node)
    break;
  }
  return locate;
}
template <class T>
void LinkedList<T>::Traversal()
{
  Node<T> * traNode = firstNode->next;
  while (traNode != nullptr)
  {
  cout << traNode->data << " ";
  traNode = traNode->next;
  }
  cout << endl;
}


SinglyLinkedList.cpp


#include <iostream>
#include "Node.h"
#include "LinkedList.h"
#include "LinkedList.cpp"
using namespace std;
int main(void)
{
  int nodes[4] = {11, 23, 76, 45};
  LinkedList<int> *list = new LinkedList<int>(nodes, 4);
  list->Traversal();
  list->Insert(4, 0);
  list->Traversal();
  cout << list->Delete(4) << endl;
  list->Traversal();
  list->s
相关文章
|
C++
C++选择结构
C++选择结构
241 0
|
存储 编译器 程序员
c++存储类
c++存储类
130 3
|
10月前
|
存储 算法 C++
【C++数据结构——图】图的邻接矩阵和邻接表的存储(头歌实践教学平台习题)【合集】
本任务要求编写程序实现图的邻接矩阵和邻接表的存储。需掌握带权有向图、图的邻接矩阵及邻接表的概念。邻接矩阵用于表示顶点间的连接关系,邻接表则通过链表结构存储图信息。测试输入为图的顶点数、边数及邻接矩阵,预期输出为Prim算法求解结果。通关代码提供了完整的C++实现,包括输入、构建和打印邻接矩阵与邻接表的功能。
399 10
|
算法 测试技术 C++
【C++】map&set的底层结构 -- AVL树(高度平衡二叉搜索树)(下)
【C++】map&set的底层结构 -- AVL树(高度平衡二叉搜索树)(下)
|
C++ 容器
【C++】map&set的底层结构 -- AVL树(高度平衡二叉搜索树)(上)
【C++】map&set的底层结构 -- AVL树(高度平衡二叉搜索树)(上)
|
C++
【C++基础】程序流程结构详解
这篇文章详细介绍了C++中程序流程的三种基本结构:顺序结构、选择结构和循环结构,包括if语句、三目运算符、switch语句、while循环、do…while循环、for循环以及跳转语句break、continue和goto的使用和示例。
313 2
|
C++ 容器
【C++航海王:追寻罗杰的编程之路】关联式容器的底层结构——AVL树
【C++航海王:追寻罗杰的编程之路】关联式容器的底层结构——AVL树
147 5
|
存储 Prometheus Cloud Native
SLS Prometheus存储问题之为什么SLS时序引擎最终选择了使用C++实现PromQL的部分算子
SLS Prometheus存储问题之为什么SLS时序引擎最终选择了使用C++实现PromQL的部分算子
|
C++
c++学习笔记03 程序流程结构
C++学习笔记,主要介绍了程序流程结构,包括顺序结构、选择结构和循环结构。选择结构中详细解释了if语句、三目运算符和switch语句的用法和注意事项。循环结构部分则涵盖了while循环、do-while循环和for循环的语法和使用技巧。此外,还介绍了跳转语句,包括break、continue和goto语句的用途和用法。
147 0
|
关系型数据库 C++ 容器
【C++航海王:追寻罗杰的编程之路】关联式容器的底层结构——红黑树
【C++航海王:追寻罗杰的编程之路】关联式容器的底层结构——红黑树
132 0
下一篇
oss云网关配置