最近使用C++实现数据结构链表,记录一下方便以后复习,若有错误,敬请指正,
1、创建头结点
链表的基本节点是Node,首先手动创建一个Node
struct Node
{
int data;
Node *next;
Node(const int& da) :data(da), next(nullptr){}
};
Node的变量一共有两个,将next指针置为NULL。
2、创建链表
先给出程序,再做注释
#ifndef _LIST_H
#define _LIST_H
class ListCode
{
public:
ListCode();
~ListCode();
bool isEmpty();
void create_List();
void insert(const int &d);
void print();
void delete_List(const int &d);
void update(const int &da, const int &dl);
void insert_pos(const int &da, const int &dl);
private:
struct Node
{
int data;
Node *next;
Node(const int& da) :data(da), next(nullptr){}
};
Node *head ;//头结点
//清理链表
void clear()
{
Node *p = head;
while (p)
{
Node *q = p->next