最基本的单向链表操作 C语言

本文介绍了一个简单的链表实现,包括创建、插入、打印、倒置、查找、删除等基本操作,并通过一个完整的C语言示例展示了如何使用这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <stdlib.h>

#ifndef NULL
#define NULL ((void *)0)
#endif

typedef struct node
{
        int data;
        struct node *p_next;
}node;

//新建链表 
node *create()
{
     node * head;
     head = (node *)malloc(sizeof(node));
     if(!head) return NULL;
     head->p_next = NULL;
     return head;
}

//插入数据 链表内数据自动排序 
node * insert(node *p_head,int data)
{
     node * nd ;
     node * p1 ;
     if(!p_head) return NULL ;
     nd = (node *)malloc(sizeof(node)) ;
     if(!nd) return NULL ;
     p1 = p_head;
     while(p1 && p1->p_next && p1->p_next -> data > data )
              p1 = p1 ->p_next;
     nd->data = data;
     nd ->p_next = p1->p_next ;
     p1 ->p_next = nd ;
     return p_head;
}

//打印节点信息 
void print_node(node *p_head)
{
     printf("0x%x -> {%d,0x%x}\n",p_head,p_head->data,p_head->p_next);  
}

//打印链表 
void print_list(node *p_head)
{
     if(!p_head)
     {
                printf("List is empty .\n",p_head,length(p_head));
                return ;
     }
     printf("List at : 0x%x,length : %d\n",p_head,length(p_head));
     
     p_head = p_head ->p_next ;
     while(p_head)
     {
                print_node(p_head);
                p_head = p_head ->p_next;
     }
}

//倒置链表 
node *reverse(node *p_head)
{
     node *p1,*p2,*p3;
     if(!p_head ||!(p_head->p_next))
                return p_head;
     p1 = p_head ;
     p2 = p_head->p_next;
     while(p2)
     {
              p3 = p2->p_next;
              p2->p_next = p1;
              p1 = p2;
              p2 = p3 ;
     }
     p_head->p_next = NULL ;
     p_head = p1;
     return p_head;
}

//查找节点 
node *find_node(node *p_head,int data)
{
     if(!p_head)
                return NULL;
     while(p_head ->p_next && p_head ->p_next ->data != data)
                p_head = p_head -> p_next ;
     return p_head ;
}

//删除链表 
void del_list(node *p_head)
{
     node *p_next;
     if(!p_head)
                return ;
     do
     {
                p_next = p_head ->p_next;
                p_head ->p_next =  NULL ;
                free(p_head);
                p_head = p_next;
     }while(p_head);
}

//删除节点 
node *del_node(node *p_head,int data)
{
     node *pn = find_node(p_head,data);
     if(!pn || !(pn->p_next))
            return NULL;
     pn->p_next = pn->p_next->p_next;
     free(pn->p_next);
     return p_head ;
}

//测试长度 
int length(node *p_head)
{
    int k = 0;
    while(p_head)
    {
                 p_head = p_head ->p_next;
                 k++;
    }
    return k>0?(k-1) : 0 ;
}

//获取中间节点 
node * mid(node *p_head)
{
    node *p1,*p2;
    p2 = p1 = p_head ;
    while(p2)
    {
             p1 = (p1->p_next) ? (p1->p_next) : p1 ;
             p2 = (p2->p_next) ? (p2->p_next ->p_next) : NULL ;  
    }
    return p1;
}

int main()
{
    node *pn;
    node * list = create();
    if(!list)
             return 1;
    printf("Insert several values into the list.\n");
    insert(list,20);
    insert(list,8);
    insert(list,223);
    insert(list,21);
    print_list(list);
    printf("Reverse the list.\n");
    list = reverse(list);
    print_list(list);
    printf("Reverse the list again ^_^ .\n");
    list = reverse(list);
    print_list(list);
    printf("Get the middle node ^_^ .\n");
    pn = mid(list);
    if(pn)
          print_node(pn);
    printf("Delete node with value 20 .\n");   
    del_node(list,20);
    print_list(list);
    printf("Empty the list.\n");
    del_list(list);
    print_list(list);
    
    getc(stdin);
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sanzhong104204

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值