数据结构-单链表

c 复制代码
#include<stdio.h>
#include<stdlib.h>

typedef struct Node
{
    int data;
    struct Node* next;
}Node;

//创建一个头结点,数据域保存链表节点数
Node* init_single_list()
{
    Node* node = (Node*)malloc(sizeof(Node));
    node->next = NULL;
    node->data = 0; //初始化时,链表节点数为0
    return node;
}

//头插法
static int insert_from_head(Node* head,int data)
{
    if(!head)
        return -1;
    Node* tmp_node = (Node*)malloc(sizeof(Node));
    tmp_node->data = data;
    tmp_node->next = head->next;
    head->next = tmp_node;
    head->data++;
    printf("data of list is: %d\n",head->data);
    return 0;
}
//尾插法
static int insert_from_tail(Node* head,int data)
{
    if(!head)
        return -1;
    
    Node* node = (Node*)malloc(sizeof(Node));
    Node* tmp_node = head;

    node->data = data;
    node->next = NULL;
    if (NULL == tmp_node->next)//链表初始化后首次插入
    {
        tmp_node->next = node;
        printf("in hear\n");
        head->data++;
        printf("head -> data in 1 = %d\n",head -> data);
        return 0;
    }
       
    tmp_node = tmp_node->next;
    while(tmp_node->next)
    {
        tmp_node = tmp_node->next;
    }
    tmp_node->next = node;
    head->data++;
    printf("head -> data in 2 = %d\n",head -> data);
    return 0;

}

/*
另一种尾插法的写法
void tailInsert(Node* L, int data) 
{
    Node* node = L;
    Node* node1 = node;

    for(int i = 0; i < L -> data; i++) {
        node = node->next;
    }
    Node* n = (Node*)malloc(sizeof(Node));
    n -> data = data;
    n -> next = NULL;
    node -> next = n;
    L -> data ++;
    printf("L -> data = %d\n",L -> data);
    printf("node1 -> data = %d\n",node1 -> data);
    printf("node -> data = %d\n",node -> data );
}
*/

//打印链表
static int print_single_list(Node* head)
{
    if((!head->next) && (!head))
        return -1;

    Node* tmp_node = head->next;
    while(tmp_node)
    {
        printf("%d ",tmp_node->data);
        tmp_node = tmp_node->next;
    }
    putchar('\n');
    return 0;
}

//删除链表中的某个节点
static int delete_node(Node* head,int data)
{
    if((!head->next) && (!head))
        return -1;

    Node* node = head;
    Node* tmp_node = node->next;
    while(tmp_node)
    {
        if (tmp_node->data == data)
        {
            node->next = tmp_node->next;
            free(tmp_node);
            printf("head->data start is %d\n",head->data);
            head->data--;
            printf("head->data end-- is %d\n",head->data);
            return 0;
        }
        node = tmp_node;
        tmp_node = tmp_node->next;
    }
    return -1;
    
}

/*
有问题的写法(free有问题)
static int delete_node_1(Node* head,int data)
{
    if((!head->next) && (!head))
        return -1;

    Node* tmp_node = head;
    Node* node = tmp_node;
    //Node* tmp_node = node->next;
    while(node->next)
    {
        if (node->next->data == data)
        {
            tmp_node->next = node->next->next;
            //free(tmp_node->next);   //直接free会发生错误
            printf("head->data is %d\n",head->data);
            head->data--;
            printf("head->data is %d\n",head->data);
            return 0;
        }
        tmp_node = node;
        node->next = node->next->next;
    }
    return -1;    
}*/

//指定在某个节点后面加入新节点
static int insert_from_node_tail(Node* head,int num,int data)
{
    if((!head->next) && (!head))
        return -1;

    Node* tmp_node = head;
    Node* node = (Node*)malloc(sizeof(Node));
    node->data = data;
    while (tmp_node->next)
    {
        if (tmp_node->next->data == num)
        {
            node->next = tmp_node->next->next;
            tmp_node->next->next = node;
            head->data++;
            printf("insert_from_node_tail ok\n");
            return 0;
        }
        tmp_node = tmp_node->next;
    }
    return -1;
}
//指定在某个节点前面加入新节点
static int insert_from_node_head(Node* head,int num,int data)
{
    if((!head->next) && (!head))
        return -1;

    Node* tmp_node = head;
    Node* node = (Node*)malloc(sizeof(Node));
    node->data = data;
    while (tmp_node->next)
    {
        if (tmp_node->next->data == num)
        {
            node->next = tmp_node->next;
            tmp_node->next = node;
            head->data++;
            printf("insert_from_node_head ok\n");
            return 0;
        }
        tmp_node = tmp_node->next;
    }
    return -1;
}
int main()
{
    Node* head = init_single_list();
    //Node* head_1 = init_single_list(head,12);
    int list_d;
    int insert_value;
    //insert_from_head(head,12);
    printf("please input data to insert the list and how many you want to insert\n");
    scanf("%d",&list_d);
    printf("list_d is %d\n",list_d);

    for (int i = 1; i <= list_d ; i++)
    {
        printf("please input insert_value %d:\n",i);
        scanf("%d",&insert_value);
        //insert_from_head(head,insert_value);
        insert_from_tail(head,insert_value);
        //tailInsert(head,insert_value);
        printf("insert_value is %d\n",insert_value);
    }
    
    //insert_from_head(head,12);
    print_single_list(head);
    printf("in main_1 head->data is %d\n",head->data);
    insert_from_node_tail(head,12,55555);
    print_single_list(head);
    printf("in main_2 head->data is %d\n",head->data);

    insert_from_node_head(head,55555,6666);
    print_single_list(head);
    printf("in main_3 head->data is %d\n",head->data);

    int ret = delete_node(head,12);
    if(-1 == ret)
        printf("the data is not in the list\n");
    else
        print_single_list(head);
    
    return 0;
}
相关推荐
8Qi815 小时前
LeetCode 23. 合并 K 个升序链表 —— 小顶堆(PriorityQueue)
数据结构·算法·leetcode·链表·
QiLinkOS15 小时前
《打破“用爱发电”:一种基于 Gitee 与时间戳的开源权益分配机制探索》
c语言·数据结构·c++·科技·算法·gitee·开源
Boom_Shu17 小时前
长方形的关系
数据结构·c++·算法
Lsk_Smion18 小时前
力扣实训 _ [543].二叉树的直径 _ [23].合并K个升序列表
数据结构·算法·leetcode
ID_1800790547321 小时前
淘宝商品详情数据接口深度解析:架构、鉴权、数据结构与实战
数据结构·架构
散峰而望21 小时前
【算法练习】算法练习精选:陶陶摘苹果(基础+升级)、Music Notes、字串变换,你能AC几道?
数据结构·c++·算法·leetcode·贪心算法·github·动态规划
凤凰院凶涛QAQ1 天前
《Java版数据结构 & 集合类剖析》集合框架的封装设计与顺序表:“从 Iterable 到 ArrayList:集合框架的‘职业树“
java·开发语言·数据结构
8Qi81 天前
LeetCode 148. 排序链表 —— 解法一:自顶向下递归(分治 + 归并)
数据结构·算法·leetcode·链表·递归·分治·归并
8Qi81 天前
LeetCode 148. 排序链表 —— 解法二:自底向上归并(迭代,O(1) 空间)
数据结构·算法·leetcode·链表·归并·迭代
嘿黑嘿呦1 天前
数据结构-图论-最小生成树
数据结构·算法·图论