数据结构中的链表

链表

链表中由数据域指针域

Snipaste_2022-05-29_18-15-37

  • 头结点的指针域为null就说明链表为空
  • 头指针会指向头结点或首元结点。
Tyepdef struct student{
    int score;//数据域
    struct student *next;//指针域
}LinkList;


LinkList *a;  //声明
#include<stdio.h>
#include<stdlib.h>

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

int main(){
	struct Node Node1 = {1, NULL};
    struct Node Node2 = {2, NULL};
    struct Node Node3 = {3, NULL};
    
    Node1.next = &Node2;
    Node2.next = &Node3;
    
    return 0;
}
  • 初始了三个空链表

Snipaste_2022-05-29_20-06-45

  • 创建一个静态链表

Snipaste_2022-05-29_20-07-35

动态创建一个链表

动态内存申请+模块化设计

  1. 创建链表(创建一个表头表示整个链表)
  2. 创建结点
  3. 插入结点
  4. 删除结点
  5. 打印遍历链表

Snipaste_2022-05-29_20-15-53

  • 打印结点(遍历)

Snipaste_2022-05-29_20-28-03

  • 头插法

    • 插入前

      Snipaste_2022-05-29_20-34-58

    • 插入时

      Snipaste_2022-05-29_20-39-47

  • 删除(指定位置删除)

    Snipaste_2022-05-29_20-52-21

#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;
    }
    printf("\n");
}
//头插入(参数:插入哪个链表,插入结点的数据是多少)
void insertNodeByHead(struct Node* headNode,int data){
    //1.创建插入的结点
    struct Node* newNode = createNode(data)
    newNode->next = headNode->next;
    headNode->next = newNode;
}

//尾插法
void insertNodeInEnd(struct Node *EndNode, int data) {
    //没有第一个元素 就直接把list的next指向新节点
    if (list->next == NULL) {
        list->next = creatNode(data);
        return;
    }
    //每次操作必须使用代理指针!!
    struct Node *p = list->next;
    while (p->next) {//不能判断p 因为要知道上一个有效节点
        p = p->next;
    }
    p->next = creatNode(data);//不应该是p=creatNode() 要知道上一个节点 并指向其
}

//删除(参数:删除哪个结点headNode ,指定位置(数据))
void deleteNodeByAppion(struct Node* headNode,int posData){
	struct Node* posNode = headNode->next;  //(只能从表头的下一个查找)
    struct Node* posNodeFront = headNode;
    if(posNode==NULL){
        printf('无法删除,链表为null')
    }else{
         while(posNode->data != posData){
        	posNodeFront = posNode;
            posNode = posNodeFront->next;
            if(posNode == NULL){
                ptintf("没有找到相关信息,无法删除");
                return;
			}
    	}
        posNodeFront->next=posNode->next;
        free(posNode);   //删除结点
    }
}

int main(){
    system("pause");
    return 0;
}
增加

Snipaste_2022-05-29_18-20-52

删除

Snipaste_2022-05-29_18-23-54

链表增删元素更方便

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

抗争的小青年

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

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

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

打赏作者

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

抵扣说明:

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

余额充值