部分链表操作总结

简介: 部分链表操作总结#include <iostream>#include <cstdlib>using namespace std;// definition of Nodestruct Node{ int val; Node *next; Node(int x) : val(x), next(NULL){}};

部分链表操作总结

#include <iostream>
#include <cstdlib>
using namespace std;

// definition of Node
struct Node
{
    int val;
    Node *next;
    Node(int x) : val(x), next(NULL){}
};

// create a linklist with n nodes
Node* create(int n)
{
    Node *head = new Node(rand() % 100);
    Node *pre = head;
    for (int i = 1; i < n; i++)
    {
        Node *newnode = new Node(rand() % 100);
        pre->next = newnode;
        pre = newnode;
    }

    return head;
}

// insert node with value num
Node* insert(Node *head, int num)
{
    Node *p = head;
    Node *q;
    while (p != NULL && p->val < num)
    {
        q = p;
        p = p->next;
    }

    Node *newnode = new Node(num);
    if (p == head)
    {
        newnode->next = head;
        head = newnode;
    }
    else if (p == NULL)
    {
        q->next = newnode;
    }
    else
    {
        newnode->next = q->next;
        q->next = newnode;
    }

    return head;
}

// delete node with value num
Node* del(Node *head, int num)
{
    Node *p = head;
    Node *q;
    while (p != NULL && p->val != num)
    {
        q = p;
        p = p->next;
    }

    if (p == head)
    {
        head = head->next;
        delete p;
    }
    else if (p == NULL)
    {
        cout<<"Node could not found!"<<endl;
    }
    else
    {
        q->next = q->next->next;
        delete p;
    }

    return head;
}

int length(Node *head)
{
    int len = 0;
    while (head != NULL)
    {
        ++len;
        head = head->next;
    }

    return len;
}

// sort the linklist
Node* sort(Node *head)
{
    int n = length(head);
    for (int i = 0; i < n - 1; i++)
    {
        Node *p = head;
        for (int j = 0; j < n - i - 1; j++)
        {
            if (p->val > p->next->val)
            {
                int temp = p->val;
                p->val = p->next->val;
                p->next->val = temp;
            }
            p = p->next;
        }
    }

    return head;
}

// reverse the linklist non-recursive
Node *reverse(Node *head)
{
    if (head == NULL)
    {
        return head;
    }

    Node *pre = head;
    Node *cur = head->next;
    while (cur != NULL)
    {
        pre->next = cur->next;
        cur->next = head;
        head = cur;
        cur = pre->next;
    }

    return head;
}

// reverse the linklist recursively
Node* reverse2(Node *head)
{
    if (head == NULL || head->next == NULL)
    {
        return head;
    }
    Node *newhead = reverse2(head->next);
    head->next->next = head;
    head->next = NULL;

    return newhead;
}

void print(Node *head)
{
    while (head != NULL)
    {
        cout<<head->val<<" ";
        head = head->next;
    }
    cout<<endl;
}

void droplist(Node *head)
{
    Node *p;
    while (head != NULL)
    {
        p = head->next;
        delete head;
        head = p;
    }
}

int main()
{
    Node *head = create(10); print(head);
    sort(head); print(head);
    head = insert(head, 101); print(head);
    head = insert(head, -1); print(head);
    head = insert(head, 50); print(head);
    head = del(head, -1); print(head);
    head = del(head, 101); print(head);
    head = del(head, 50); print(head);
    head = reverse(head); print(head);
    head = reverse2(head); print(head);
    droplist(head);
}
目录
相关文章
|
6月前
特殊链表(循环单链表,循环双链表,静态链表)
特殊链表(循环单链表,循环双链表,静态链表)
53 3
不带头非循环的单向链表的增删查改的实现(代码版)
不带头非循环的单向链表的增删查改的实现(代码版)
|
11月前
|
存储
链表操作详解
链表操作详解
|
存储
链表(一) 单链表操作详解
链表(一) 单链表操作详解
50 0
|
存储 算法 Java
Java数据结构与算法分析(三)链表(单链表、双链表、环形链表)
通过前篇文章《[数组](https://blog.csdn.net/gozhuyinglong/article/details/109702860)》了解到数组的存储结构是一块连续的内存,插入和删除元素时其每个部分都有可能整体移动。为了避免这样的线性开销,我们需要保证数据可以不连续存储。本篇介绍另一种数据结构:链表。
223 0
动态链表和静态链表的实现
动态链表和静态链表的实现
107 0
|
存储 API 索引
链表——双链表
双向链表也叫双向表,是链表的一种,它由多个结点组成,每个结点都由一个数据域和两个指针域组成,数据域用来存储数据,其中一个指针域用来指向其后继结点,另一个指针域用来指向前驱结点。链表的头结点的数据域不存储数据,指向前驱结点的指针域值为null,指向后继结点的指针域指向第一个真正存储数据的结点
153 0
链表——双链表
|
移动开发
【17. 双链表】
双链表 双链表和单链表原理是一样的,只不过双链表有俩个指针,一个指向前,一个指向后。
103 0
【17. 双链表】