0
点赞
收藏
分享

微信扫一扫

django MTV 静态文件js的添加方式,以及怎么优化js的加载

转角一扇门 03-05 07:00 阅读 3

这段代码是一个简单的链表操作程序,包括创建节点、在链表末尾添加节点、在指定位置插入节点和删除指定位置的节点。以下是详细的注释:

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

// 定义链表节点结构体
struct listnode{
    int i; // 节点存储的整数值
    struct listnode *next; // 指向下一个节点的指针
}Node;

// 在链表末尾添加节点的函数声明
struct listnode *list_append(struct listnode *node,int num);

// 在指定位置插入节点的函数声明
struct listnode *list_insert(struct listnode *node,int position,int num);

// 删除指定位置节点的函数声明
struct listnode *list_delete(struct listnode *node,int position);

// 创建节点的函数声明
struct listnode* createNode(int data);

// 创建节点的函数实现
struct listnode* createNode(int data) {
    struct listnode *newNode = (struct listnode *)malloc(sizeof(struct listnode));
    newNode->i = data;
    newNode->next = NULL;
    return newNode;
}

void main(){
    // 创建一个头节点并初始化为0
    struct listnode *head = createNode(0);

    // 创建一个临时头节点用于遍历
    struct listnode *head_t;

    // 分配内存给临时头节点
    head_t = (struct listnode*)malloc(sizeof(struct listnode));

    // 打印头节点的值
    printf("%d",head->i);

    // 将临时头节点指向头节点
    head_t = head;

    // 循环创建5个节点并添加到链表中
    for(int j = 0;j<5;j++){
        struct listnode* node = (struct listnode*)malloc(sizeof(struct listnode));
        node->i = j+1;
        node->next = NULL;
        head->next = node;
        head = node;
    }

    // 打印最后一个节点的值
    printf("%d",head->i);

    // 遍历链表并打印每个节点的值
    struct listnode* curr = head_t;
    while(curr){
        printf("%d",curr->i);
        curr=curr->next;
    }

    // 在链表末尾添加一个值为7的节点,并遍历打印链表
    struct listnode* curr2 = list_append(head_t,7);
    while(curr2){
        printf("%d",curr2->i);
        curr2=curr2->next;
    }

    // 在链表的第2个位置插入一个值为2的节点,并遍历打印链表
    struct listnode* curr3 = list_insert(head_t,2,2);
    while(curr3){
        printf("%d",curr3->i);
        curr3=curr3->next;
    }

    // 删除链表的第6个位置的节点,并遍历打印链表
    struct listnode* curr4 = list_delete(head_t,6);
    while(curr4){
        printf("%d",curr4->i);
        curr4=curr4->next;
    }
}

// 在链表末尾添加节点的函数实现
struct listnode *list_append(struct listnode *node,int num){
    struct listnode *node_head;
    struct listnode *node_head2;
    node_head = (struct listnode *)malloc(sizeof(struct listnode));
    node_head2 = (struct listnode *)malloc(sizeof(struct listnode));
    node_head = node;
    node_head2 = node;
    while(node_head->next){
        node_head = node_head->next;
    }
    struct listnode *node_end;
    node_end = (struct listnode *)malloc(sizeof(struct listnode));
    node_end->i = num;
    node_end->next = NULL;
    node_head->next = node_end;
    return node;
}

// 在指定位置插入节点的函数实现
struct listnode *list_insert(struct listnode *node,int position,int num){
    struct listnode *node_head;
    node_head = (struct listnode *)malloc(sizeof(struct listnode));
    int pos_1 = 0;
    node_head = node;
    while(node_head->next){
        node_head = node_head->next;
        pos_1++;
        if(pos_1==position-1){
            break;
        }
    }
    struct listnode *newnode;
    newnode = (struct listnode *)malloc(sizeof(struct listnode));
    newnode->i=num;
    newnode->next=node_head->next;
    node_head->next = newnode;
    return node;
}

// 删除指定位置节点的函数实现
struct listnode *list_delete(struct listnode *node,int position){
    struct listnode *node_head;
    node_head = (struct listnode *)malloc(sizeof(struct listnode));
    int pos_1 = 0;
    node_head = node;
    while(node_head->next){
        node_head = node_head->next;
        pos_1++;
        if(pos_1==position){
            break;
        }
    }
    if(node->next->next)
    node_head->next = node_head->next->next;
    else{
        node_head->next = NULL;
    }
    return node;
}
举报

相关推荐

0 条评论