单向链表的创建、访问、插入、删除——C语言基础

单向链表的创建、访问、插入和删除操作

前言:本文是前几篇文章的综合,只有代码没有解析,如有需要请根据文章尾部的链接进行查看详细模块

代码部分:

(1)main.c

#include "creat.h"
#include "output.h"
#include "insert.h"
#include "delete.h"

int main(){
    struct stu *head;
    head = creat();
    list(head);

    int m;
    printf("please input the number witch you want to delete:\n");
    scanf("%d",&m);
    list(del(head,m));
    
    struct stu *newstu;
    newstu = (struct stu*) malloc(LEN);

    printf("please input new note's number:\n");
    scanf("%d",&newstu->num);
    printf("please input new note's score:\n");
    scanf("%f",&newstu->score);
    head = insert(head, newstu);
    list(head);

    return 0;
}

(2)structure.h

// Structure
// Created by Lanyan on 20/07/2021.
// JiaoZuo

#ifndef _structure_h
#define _structure_h
#include "stdio.h"

struct stu{          //定义一个包含学号和成绩的结构体
    int num;         //数据域
    float score;     //数据域
    struct stu *next;//指针域
};

#endif

(3)creat.h

// Structure constructor
// Created by Lanyan on 20/07/2021.
// JiaoZuo

#ifndef _creat_h
#define _creat_h
#include "structure.h"
#include <malloc.h>
#define LEN sizeof(struct stu)        /*LEN为结构体stu的长度*/

struct stu *creat(){
    struct stu *head, *tail;        //链表的头指针和尾指针
    struct stu *p;      //代表新产生的节点
    int x;      //控制输入
    head = tail  =NULL;     //初始化

    printf("Please input the student number:\n");
    scanf("%d",&x);
    while(x!=0){
        p = (struct stu*) malloc(LEN);      //开辟一个空间
        p->num = x;
        printf("Please input the student score:\n");
        scanf("%f",&p->score);

        if(head == NULL)        //解决了情况1:构建的节点是表头
            head = p;
        if(tail != NULL)        //解决了情况2:构建的节点不是表头
            tail->next = p;
        tail = p;       //始终让tail指向最后一个节点

        printf("Please input the student number:\n");
        scanf("%d",&x);
    }
    if(tail !=NULL)
        tail->next = NULL;
    return head;
}
#endif

(4)output.h

// sequential output function
// Created by Lanyan on 20/07/2021.
// JiaoZuo
#ifndef _output_h
#define _output_h
#include "structure.h"

void list(struct stu *head){
    struct stu *p;      //构建一个遍历工作指针p
    p = head;       //初始化
    if(head != NULL){
        printf("\nthe list records are:\n");
        do{
            printf("%d\t%5.1f\n", p->num, p->score);
            p = p->next;
        }while(p != NULL);
    } else{
        printf("\nThe list is null\n");
    }
}

#endif

(5)insert.h

// insert a note into a singly linked list
// Created by Lanyan on 20/07/2021.
// JiaoZuo

#ifndef _insert_h
#define _insert_h
#include "structure.h"


struct stu *insert(struct stu *head, struct stu *stud){
    //head: the linked list
    //stud: the new note
    struct stu *p0;     //point to the new note
    struct stu *p1;     //point to the first note gather than to the student number of the new note
    struct stu *p2;     //point to the previous note of p1

    p0 = stud;      //initialization
    p1 = head;

    if(head == NULL){       //attention_1, head or p1?
        //solve situation 1
        head = p0;      //attention_2, head or p1?
        p0->next = NULL;
    }else{
        while((p1 != NULL) && (p0->num >= p1->num)){        //attention_3 p1 or head? sequence?
            //search
            //the positions of the two conditions can not be reversed
            p2 = p1;
            p1 = p1->next;
        }
        if(p1 != NULL){     //attention_4,it can be replace with p0->num < p0->num
            if(head == p1) head = p0;       //solve situation 2
            else p2->next = p0;     //solve situation 3
            p0->next = p1;
        }else{      //solve situation 4
            p2->next = p0;
            p0->next = NULL;
        }
    }
    return head;
}
#endif _insert_h

(6)delete.h

// delete a designated note
// Created by Lanyan on 22/07/2021.
// JiaoZuo

#ifndef _DELETE_H
#define _DELETE_H

#include "structure.h"
struct stu *del(struct stu *head, int m){
    struct stu *p1;     //point to the note to delete
    struct stu *p2;     //point to the precursor note to delete
    if(head == NULL)
        printf("the list is NULL\n");     //list is NULL
    else {
        p1 = head;
        while((p1 != NULL) && (p1 ->num != m)){     //search
            p2 = p1;
            p1 = p1->next;
        }
        if(p1 != NULL){
            if(p1 == head){
                head = p1->next;
            } else
                p2->next = p1->next;
            printf("delete successfully!\n");
            free(p1);
        } else
            printf("there is no note to delete\n");
    }
    return head;
}

#endif //_DELETE_H
模块详解链接:

编译预处理——文件包含
C语言基础——单向链表的创建
顺序访问链表中的节点——C语言基础
在单向链表中插入节点——C语言基础
在单向链表中删除节点——C语言基础

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值