线性表的链式存储结构 单链表(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
相关文章
|
21天前
|
C语言 C++
C/C++ 自定义头文件,及头文件结构详解
还是从"stdio.h"说起,这是C语言中内置的标准库,也就是说,头文件很多时候其实就是一个“库”,类似于代码的仓库,也就是说将某些具有特定功能的常量、宏、函数等归为一个大类,然后放进这个“仓库”,就像stdio.h就是一个标准输入/输出的头文件
29 1
|
8天前
|
存储 搜索推荐 C++
【C++高阶(二)】熟悉STL中的map和set --了解KV模型和pair结构
【C++高阶(二)】熟悉STL中的map和set --了解KV模型和pair结构
|
8天前
|
存储 缓存 编译器
【C++进阶(五)】STL大法--list模拟实现以及list和vector的对比
【C++进阶(五)】STL大法--list模拟实现以及list和vector的对比
|
8天前
|
算法 C++ 容器
【C++进阶(四)】STL大法--list深度剖析&list迭代器问题探讨
【C++进阶(四)】STL大法--list深度剖析&list迭代器问题探讨
|
9天前
|
C++
c++的学习之路:16、list(3)
c++的学习之路:16、list(3)
12 0
|
9天前
|
C++
c++的学习之路:15、list(2)
c++的学习之路:15、list(2)
14 0
|
9天前
|
存储 C++ 容器
c++的学习之路:14、list(1)
c++的学习之路:14、list(1)
18 0
|
19天前
|
编译器 C++ 容器
【C++初阶】STL详解(八)List的模拟实现
【C++初阶】STL详解(八)List的模拟实现
39 0
|
19天前
|
存储 C++ 容器
【C++初阶】STL详解(五)List的介绍与使用
【C++初阶】STL详解(五)List的介绍与使用
32 0
|
22天前
|
存储 编译器 程序员
【C++】类和对象①(什么是面向对象 | 类的定义 | 类的访问限定符及封装 | 类的作用域和实例化 | 类对象的存储方式 | this指针)
【C++】类和对象①(什么是面向对象 | 类的定义 | 类的访问限定符及封装 | 类的作用域和实例化 | 类对象的存储方式 | this指针)