4.单链表操作

2020-02-28  本文已影响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);
}
上一篇 下一篇

猜你喜欢

热点阅读