单链表介绍

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

struct Node{
   int data;//数据域
   struct Node* next;//指针域
};

/*创建链表*/
struct Node *createlist()
{
    struct Node* headNode=(struct Node*)malloc(sizeof(struct Node));
    //headNode成为了结构体变量
    //变量使用前必须被初始化
    //headNode->data=1;
    headNode->next=NULL;
    return headNode;
}
/*创建节点*/
struct Node *createNode(int data)
{
    struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
    newNode->data=data;
    newNode->next=NULL;
    return newNode;
}
/*打印节点*/
void printList(struct Node *headNode)
{
    struct Node* pMOve=headNode->next;
    while(pMOve)
    {
        printf("%d",pMOve->data);
        pMOve=pMOve->next;
    }
}
/*插入节点,参数:插入哪个链表 插入节点的数据是多少*/
void insertNodeByHead(struct Node* headNode,int data)
{
    //创建插入的节点
    struct Node* newNode=createNode(data);
    newNode->next=headNode->next;
    headNode->next=newNode;
}

void deleteNodeByAppoint(struct Node* headNode,int posData)
{
    struct Node* posNode=headNode->next;//posNode只能从表头的下一个节点开始寻找
    struct Node* posNodeFront=headNode;//posNodeFront在posNode前面
    if(posNode==NULL) printf("无法删除链表为空\n");
    else 
    { 
        while(posNode->data!=posData)
        {
            posNodeFront=posNode;
            posNode=posNodeFront->next;
            if(posNode==NULL)
            {
                printf("没有找到相关信息,无法删除\n");
                 break;
            }
        }
       posNodeFront->next=posNode->next;
       free(posNode);
    }
}
int main(void)
{
    struct Node* list=createlist();
    insertNodeByHead(list,1);
    insertNodeByHead(list,2);
    insertNodeByHead(list,3);
    printList(list);
    deleteNodeByAppoint(list,2);
    printList(list);
    return 0;  
} 

什么是链表;

链表是结构体变量与结构体变量连接在一起。

动态创建一个链表:动态内存申请+模块化设计

1.创建链表(创建一个表头表示整个链表)

2.创建节点

3.插入节点(本程序采用的是表头法插入即永远在排头后面插入数据,不可能插入到headNode前面去)

4.删除节点(本程序采用的是指定位置删除法)

5.打印遍历链表(主要用来测试,头节点不打印)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Poetry _Distance

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

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

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

打赏作者

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

抵扣说明:

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

余额充值