线性表的链式存储结构 单链表(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
相关文章
|
2天前
|
算法 C++ 容器
模拟实现c++中的list模版
模拟实现c++中的list模版
|
1月前
|
存储 算法 C++
【C++数据结构——图】图的邻接矩阵和邻接表的存储(头歌实践教学平台习题)【合集】
本任务要求编写程序实现图的邻接矩阵和邻接表的存储。需掌握带权有向图、图的邻接矩阵及邻接表的概念。邻接矩阵用于表示顶点间的连接关系,邻接表则通过链表结构存储图信息。测试输入为图的顶点数、边数及邻接矩阵,预期输出为Prim算法求解结果。通关代码提供了完整的C++实现,包括输入、构建和打印邻接矩阵与邻接表的功能。
49 10
|
2月前
|
编译器 C语言 C++
【c++丨STL】list模拟实现(附源码)
本文介绍了如何模拟实现C++中的`list`容器。`list`底层采用双向带头循环链表结构,相较于`vector`和`string`更为复杂。文章首先回顾了`list`的基本结构和常用接口,然后详细讲解了节点、迭代器及容器的实现过程。 最终,通过这些步骤,我们成功模拟实现了`list`容器的功能。文章最后提供了完整的代码实现,并简要总结了实现过程中的关键点。 如果你对双向链表或`list`的底层实现感兴趣,建议先掌握相关基础知识后再阅读本文,以便更好地理解内容。
42 1
|
2月前
|
算法 C语言 C++
【c++丨STL】list的使用
本文介绍了STL容器`list`的使用方法及其主要功能。`list`是一种双向链表结构,适用于频繁的插入和删除操作。文章详细讲解了`list`的构造函数、析构函数、赋值重载、迭代器、容量接口、元素访问接口、增删查改操作以及一些特有的操作接口如`splice`、`remove_if`、`unique`、`merge`、`sort`和`reverse`。通过示例代码,读者可以更好地理解如何使用这些接口。最后,作者总结了`list`的特点和适用场景,并预告了后续关于`list`模拟实现的文章。
69 7
|
2月前
|
存储 编译器 C++
C++ initializer_list&&类型推导
在 C++ 中,`initializer_list` 提供了一种方便的方式来初始化容器和传递参数,而右值引用则是实现高效资源管理和移动语义的关键特性。尽管在实际应用中 `initializer_list&&` 并不常见,但理解其类型推导和使用方式有助于深入掌握现代 C++ 的高级特性。
28 4
|
4月前
|
存储 搜索推荐 C++
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器2
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器
100 2
|
4月前
|
存储 算法 C++
【C++打怪之路Lv10】-- list
【C++打怪之路Lv10】-- list
33 1
|
4月前
|
存储 编译器 C++
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
103 2
|
4月前
|
存储 缓存 C++
C++番外篇——list与vector的比较
C++番外篇——list与vector的比较
37 0
|
4月前
|
C++
C++番外篇——list的实现
C++番外篇——list的实现
29 0