第三章 线性表的链式存储

3.2 单链表

#include<iostream>
#include<vector>
#include<string>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include <stack>

using namespace std;

struct link_node
{
    int data;
    struct link_node* next;
};

void init(link_node* node, int x)
{
    node->data = x;
    node->next = nullptr;
}

void display(link_node* head)
{
    link_node* p = head;
    while (p != nullptr)
    {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("\n");
}

void insert(link_node* head, int position, int x)
{
    link_node* new_node=(link_node *)malloc(sizeof(link_node)*1);
    init(new_node, x);
    if (position == 0)
    {
        new_node->next = head;
        head = new_node;
    }
    else
    {
        int cnt = 1;
        link_node* p = head;
        while (cnt < position&&p->next!=nullptr)
        {
            p = p->next;
            cnt++;
        }
        new_node->next = p->next;
        p->next = new_node;
    }
}

int get(link_node* head, int position)
{
    link_node* p = head;
    while (position && p->next != nullptr)
    {
        position--;
        p = p->next;
    }
    return p->data;
}

void delete_node(link_node* head, int position)
{
    link_node* p = head;
    if (position == 0)
    {
        p = p->next;
        free(head);
        head = p;
    }
    else
    {
        while (position - 1 && p->next != nullptr)
        {
            position--;
            p = p->next;
        }
        link_node* t_p = p->next;
        p = p->next->next;
        free(t_p);
    }
}

void destroy(link_node* head)
{
    link_node* p = head, *t_p=nullptr;
    while (p != nullptr)
    {
        t_p = p;
        p = p->next;
        free(t_p);
    }
}

int main()
{  
    return 0;
}

3.4 循环单链表

3.5 双链表

struct dlink_node
{
    int data;
    struct dlink_node *l, *r;
};

3.6 链式栈

用单链表实现,链表头是栈顶

3.7 链式队列

双指针单链表

文章目录