美文网首页
4.单链表操作

4.单链表操作

作者: 陈忠俊 | 来源:发表于2020-02-28 17:14 被阅读0次

单链表的操作,包含增删改查

定义了几个函数:

Node *create()

用于创建新的链表,并返回链表头

Node *append(Node *head)

用于向已经创建的非空链表的尾部添加一个新的节点。

int getNodeLength(Node *head)

用于获取当前非空链表的长度

Node *insert(Node *head, int location)

用于向当前的非空链表插入数据,在存在的location位置。

Node *delete(Node *head, int location)

用于删除在非空链表的指定位置的节点。

void list(Node *head)

用于打印出非空链表所有节点的数据。


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

/*自定义数据类型*/
typedef struct Student{
    int num;
    float score;
    struct Student *next;
}Node;

/*用于创建新的链表*/
Node *create(){
    Node *head, *pf, *pb; /*pf, pb用于递推创建新的节点,pf是前节点,pb是后节点*/
    Node *temp; /*临时结点,用于保存pf的地址,传递给head;*/
    int number;
    pf = pb = (Node *)malloc(sizeof(Node)); /*创建首地址,pf, pb开始是同一个地址*/
    temp = pf;
    head = NULL;
    printf("Input the node number what you want to create: \n");
    scanf("%d", &number);
    if(number <= 0) {
        printf("The number you input is illegal, exit(1)");
        exit(1);
    }
    while(number > 0){
        printf("Input data: \n");
        scanf("%d, %f", &pf->num, &pf->score);
        if(number == 1) head = temp;
        else{
            pb = (Node *)malloc(sizeof(Node));  /*开始递推操作*/
            pf->next = pb;
            pb->next = NULL;
            pf = pb;
        }
        number--;
    }
    pb->next = NULL;
    return(head);
}

//链表的最后添加一个节点
Node *append(Node *head){
    Node *p, *newNode;
    p = head;
    if(head == NULL){
        printf("the node table is empty, exit(1)");
        exit(1);
    }
    while(p->next != NULL){
        p = p->next;
    }
    newNode = (Node *)malloc(sizeof(Node));
    printf("Input new node's data:\n");
    scanf("%d, %f", &newNode->num, &newNode->score);
    p->next = newNode;
    newNode->next = NULL;

    return(head);
}
//获得链表的长度
int getNodeLength(Node *head){
    Node *p;
    int length = 0;
    if(head == NULL){
        return 0;
    }
    p = head;
    while(p->next != NULL){
        length+=1;
        p = p->next;
    }
    length = length + 1;
    return(length);
}
//向链表插入数据
Node *insert(Node *head, int location){
    Node *p, *newNode;
    int count;
    p = head;
    if(head == NULL){
        printf("Node table is empty, exit(1");
        exit(1);
    }
    if(location >= getNodeLength(head)){
        printf("Can't insert new node to that place\n");
        printf("Actual Node length is: %d", getNodeLength(head));
        exit(1);
    }
    newNode = (Node *)malloc(sizeof(Node));
    printf("Input the data of new node: \n");
    scanf("%d, %f", &newNode->num, &newNode->score);
    if(location == 0){
        newNode->next = head;
        return(newNode);
    }
    for(count = 1; count < location; count++){
        p = p->next;
    }
    newNode->next = p->next;
    p->next = newNode;
    return(head);
}
//删除某个节点
Node *delete(Node *head, int location){
    Node *p, *temp;
    int count;
    if(head == NULL){
        printf("Node table is empty, exit(1)");
        exit(1);
    }
    if(location > getNodeLength(head)){
        printf("Your location to delete does not exist, exit(1)");
        printf("The length of this nodes is: %d\n", getNodeLength(head));
        exit(1);
    }
    p = head;
    if(location == 1){
        head = p->next;
        free(p);
    }
    else if(location == getNodeLength(head)){
        while(p->next != NULL){
            temp = p;
            p = p->next;
        }
        temp->next = NULL;
        free(p); 
    }
    else {
        for(count = 1; count < location - 1; count++){
            p = p->next;
        }
        temp = p->next;
        p->next = temp->next;
        free(temp);
    }
    return(head);
}
//打印节点数据
void list(Node *head){
    Node *p;
    p = head;
    if(head == NULL){
        printf("Null table, exit\n");
        exit(1);
    }
    while(p->next != NULL){
        printf("%d-%f\n", p->num, p->score);
        p = p->next;
    }
    printf("%d-%f\n", p->num, p->score);
    printf("List Done!\n");
}

void main(void){
    Node *head;
    head = create();
    printf("list all Nodes.\n");
    list(head);
    printf("delete node 2\n");
    head = delete(head, 2);
    printf("list node after deleting.\n");
    list(head);
}

相关文章

  • 数据结构-单链表学习目录

    1.单链表的基本操作 2.求单链表的长度 3.判断单链表是否为空 4.查找单链表中倒数第K个结点 5.单链表的反转...

  • 4.单链表操作

    单链表的操作,包含增删改查 定义了几个函数: 用于创建新的链表,并返回链表头 用于向已经创建的非空链表的尾部添加一...

  • 线性表的链式存储-单链表

    单链表操作 [x] 单链表的创建(尾插法、头插法) [x] 单链表的查找操作 [x] 单链表的删除操作 [x] 单...

  • 链表问题集锦

    1.单链表的初始化,输出以及插入删除的基本操作 2.在O(1)时间删除链表节点 3.反转单链表 4.求链表倒数第k...

  • 单链表的删除操作

    单链表的删除操作

  • 2018-12-01

    数据结构使用二级指针创建单链表以及单链表的各种操作 //单链表创建 #include #include #incl...

  • 单链表的操作

    单链表代码定义 单链表的操作 初始化单链表 插入结点 注: L为插入的单链表,node为将要插入的结点 前插法 尾...

  • 单链表操作

    简单的单链表操作 下面是操作类

  • 链表相关

    总结一下链表相关的操作 单链表节点的定义 实现单向链表的反向 删除单链表的所有节点

  • 数据结构课程 第三周 线性表--链式表

    逻辑次序和物理次序不一定相同 带头结点的单链表 单链表上的操作实现 初始化 判空 单链表销毁 清空单链表(头指针和...

网友评论

      本文标题:4.单链表操作

      本文链接:https://www.haomeiwen.com/subject/riwlhhtx.html