Linked list-I wrote it myself in C++

Linked list

Linked list is a kind of data structure and an extremely powerful array. Linked lists can easily add, delete, and insert nodes.


table of Contents

Linked list

Singly linked list

1. Create a linked list

1.1 Create Node

1.2 Initialization

2. Link list length

3. Linked list printing

4. Find nodes

5. Delete node

6. Add nodes

7. Modify the node

Complete code



Singly linked list

Beginners generally start with a singly linked list. The operations of a singly linked list generally include: create, modify, insert, output, sort, reverse order, empty, and find length.

 


1. Create a linked list

Use the structure to define the node, and use the pointer to realize the linked list

 

1.1 Create Node

Use structure to create linked list nodes

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

When you define the nodes of the linked list below, you can directly use NodeList *node to define

 

1.2 Initialization

Create a singly linked list of n nodes

 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. Link list length

Returns the length of the linked list, namely length or 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. Linked list printing

Print the nodes of the linked list through loops

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

 

4. Find nodes

Find whether it exists by traversing (return the position of the first occurrence of the node)

 
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. Delete node

Find the nth node of the linked list, by modifying the pointer, make the previous node of n nodes point to the next node of n, and release the nth node

 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. Add nodes

Add nodes in the middle by modifying the pointer

 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. Modify the node

Find the node first, modify the value of the node

 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;
 }

Complete code

/**
 * @分函数编写单链表
 * 
 * 
 * @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;
}

 

 

Guess you like

Origin blog.csdn.net/qq_34811382/article/details/112474431