链表——自己动手写了一下C++的

链表

链表是一种数据结构,是一种功能极其强大的数组。链表可以很轻松的进行增添,删除,插入节点。


目录

链表

单向链表

1.创建链表

1.1创建节点

1.2初始化

2.链表长度

3.链表打印

4.查找节点

5.删除节点

6.增加节点

7.修改节点

完整代码



单向链表

初学者一般先从单向链表开始,单向链表的操作一般包括:创建,修改,插入,输出,排序,反序,清空,求长度。


1.创建链表

利用结构体来定义节点,通过指针来实现链表

1.1创建节点

利用结构体进行创建链表的节点

 struct NodeList
 {
     int val;
     NodeList *next;
 };

下面再进行链表的节点定义时,可以直接用NodeList *node来进行定义

1.2初始化

创建n个节点的单向链表

 NodeList *CreateList(int &n)
 {
     NodeList *head,*node,*end; //创建头节点,中间节点,尾节点
     head=new NodeList;      //给头结点分配地址
     end=head;           //令尾节点等于头结点
     for(int i=0;i<n;i++)
     {
         node =new NodeList; //给每一个Node分配地址
         cin>>node.val;      //输入每一个节点的值
         end.next=node;      //让尾节点(第一次是头节点)的指针指向下一个节点
         end=node;           //让尾节点等于新添加的节点
     }
     end.next=NULL;      //给最后一个节点的下一个节点置空
     return head;        //返回头部指针
 }

2.链表长度

返回链表的长度,即length或者size

 
int length(ListNode *head) //返回链表的长度
 {
     ListNode *p = new ListNode;
     p = head;
     int len = 0;
     while (p->next != nullptr)
     {
         p = p->next;
         len++;
     }
     return len;
 }

3.链表打印

通过循环来打印链表的节点

 
void PrintList(ListNode *head) //打印链表
 {
     ListNode *p = head;
     while (p->next != nullptr)
     {
         p = p->next;
         cout << p->val << " ";
     }
 }
 ​

4.查找节点

通过遍历查找是否存在(返回节点的第一次出现的位置)

 
int FindNode(ListNode *head, const int &n) //查找链表中是否存在n,存在返回第一次位置,不存在返回-1
 {
     ListNode *p = new ListNode;
     p = head;
     int Pos = 0;
     while (p->next != nullptr)
     {
         p = p->next;
         Pos++;
         if (p->val == n)
             return Pos;
     }
     return -1;
 }
 ​

5.删除节点

找到链表的第n个节点,通过修改指针,让n个节点的上一个节点指向n的下一个节点,释放第n个节点

 bool DeleteNode(ListNode *head, const int n) //删除链表的第n个节点
 {
     if (length(head) < n)
         return false;
     ListNode *temp = new ListNode;
     for (int i = 0; i < n - 1; i++)
     {
         head = head->next;
     }
     temp = head->next;
     head->next = head->next->next;
     delete (temp);
     return true;
 }

6.增加节点

通过修改指针的指向来在中间添加节点

 bool AddNode(ListNode *head, const int Pos, const int value) //添加一个节点,在pos这个位置添加一个数值为value的节点
 {
     if (length(head) < Pos)
         return false;
     for (int i = 0; i < Pos - 1; i++)
     {
         head = head->next;
     }
     ListNode *temp = new ListNode;
     temp->val = value;
     temp->next = head->next;
     head->next = temp;
     return true;
 }

7.修改节点

先查找到节点,在修改节点的值

 bool ChangeNode(ListNode *head, const int Pos, const int Value) //修改节点 修改位置pos上的值为value
 {
     if (length(head) < Pos)
         return false;
     for (int i = 0; i < Pos; i++)
     {
         head = head->next;
     }
     head->val = Value;
     return true;
 }

完整代码

/**
 * @分函数编写单链表
 * 
 * 
 * @1.链表创建 √
 * @2.链表长度 √
 * @3.链表打印 √
 * @4.查找节点 √ 返回位置
 * @5.删除节点 √
 * @6.增加节点 √
 * @7.修改节点 √
 * 
 * 
 * @Progra:Zbooo
 **/

#include <iostream>
using namespace std;

struct ListNode
{
    int val;
    ListNode *next;
};

ListNode *CreatNode(int &n) //创建n个节点的链表
{
    ListNode *head, *node, *end;
    head = new ListNode;
    end = head;
    for (int i = 0; i < n; i++)
    {
        node = new ListNode;
        cin >> node->val;
        end->next = node;
        end = node;
    }
    end->next = nullptr;
    return head;
}

void PrintList(ListNode *head) //打印链表
{
    ListNode *p = head;
    while (p->next != nullptr)
    {
        p = p->next;
        cout << p->val << " ";
    }
}

int length(ListNode *head) //返回链表的长度
{
    ListNode *p = new ListNode;
    p = head;
    int len = 0;
    while (p->next != nullptr)
    {
        p = p->next;
        len++;
    }
    return len;
}

int FindNode(ListNode *head, const int &n) //查找链表中是否存在n,存在返回位置,不存在返回-1
{
    ListNode *p = new ListNode;
    p = head;
    int Pos = 0;
    while (p->next != nullptr)
    {
        p = p->next;
        Pos++;
        if (p->val == n)
            return Pos;
    }
    return -1;
}

bool DeleteNode(ListNode *head, const int &n) //删除链表的第n个节点
{
    if (length(head) < n)
        return false;
    ListNode *temp = new ListNode;
    for (int i = 0; i < n - 1; i++)
    {
        head = head->next;
    }
    temp = head->next;
    head->next = head->next->next;
    delete (temp);
    return true;
}

bool AddNode(ListNode *head, const int Pos, const int &value) //添加一个节点,在pos这个位置添加一个数值为value的节点
{
    if (length(head) < Pos)
        return false;
    for (int i = 0; i < Pos - 1; i++)
    {
        head = head->next;
    }
    ListNode *temp = new ListNode;
    temp->val = value;
    temp->next = head->next;
    head->next = temp;
    return true;
}

bool ChangeNode(ListNode *head, const int Pos, const int &Value) //修改节点 修改位置pos上的值为value
{
    if (length(head) < Pos)
        return false;
    for (int i = 0; i < Pos; i++)
    {
        head = head->next;
    }
    head->val = Value;
    return true;
}

int main()
{
    int n;
    cin >> n;
    ListNode *head = new ListNode;
    head = CreatNode(n);
    DeleteNode(head, 3);
    AddNode(head, 3, 10);
    ChangeNode(head, 3, 10);
    cout << FindNode(head, 3) << endl;
    PrintList(head);
    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_34811382/article/details/112474431