数据结构之链表的增删查等功能实现

#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Node)

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

struct Node*createList()
{
    struct Node*headnode=(struct Node*)malloc(LEN);
    headnode->next=NULL;
    return headnode;
}

struct Node*createNode(int data)
{
    struct Node*newnode=(struct Node*)malloc(LEN);
    newnode->data=data;
    newnode->next=NULL;
    return newnode;
}

void insertnode(struct Node*headnode,int data)
{
    struct Node*node=createNode(data);
    node->next=headnode->next;
    headnode->next=node;
}

void deletenode(struct Node*headnode,int data)
{
    struct Node*prenode=headnode;
    struct Node*backnode=headnode->next;
    if(!backnode)
    {
        printf("NOT Found\n");
    }
    while(backnode->data!=data)
    {
        prenode=backnode;
        backnode=prenode->next;
    }
    prenode->next=backnode->next;
    free(backnode);
}
void printList(struct Node*headnode)
{
    struct Node*pmove=headnode->next;
    if(!pmove)
    {
        printf("链表为空,无法打印!\n");
    }
    else
    {
        while(pmove)
        {
            printf("%d\n",pmove->data);
            pmove=pmove->next;
        }
    }
}

int main()
{
    struct Node*List=createList();
    insertnode(List,1);
    insertnode(List,2);
    insertnode(List,3);
    deletenode(List,2);
    printList(List);
    return 0;
}

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值