自变量是链表的函数(赋值操作)

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

// 定义链表节点结构体及类型别名
typedef struct node {
    int value;
    struct node *next;  // 指向下一个节点的指针
} Node;

// 定义链表控制结构体
typedef struct {
    Node *head;  // 链表头指针
    Node *tail;  // 链表尾指针
} List;

// 函数原型声明
void add(List *list, int number);

int main() {
    int number;
    List list;  // 链表控制结构体
    list.head = NULL;
    list.tail = NULL;  // 初始化尾指针为 NULL

    // 读取用户输入并构建链表
    while (1) {
        // 读取用户输入并验证有效性
        if (scanf("%d", &number) != 1) {
            fprintf(stderr, "输入无效,请输入整数!\n");
            // 清空输入缓冲区防止死循环
            while (getchar() != '\n');
            continue;
        }

        // 终止条件:输入 -1
        if (number == -1) {
            break;
        }

        // 调用 add 函数将数字添加到链表尾部
        add(&list, number);
    }

    // 遍历链表并输出结果
    Node *current = list.head;
    while (current != NULL) {
        printf("%d->", current->value);
        current = current->next;
    }
    printf("NULL\n");  // 明确标识链表结尾

    // 释放链表内存
    Node *temp;
    while (list.head != NULL) {
        temp = list.head;
        list.head = list.head->next;
        free(temp);
    }

    return 0;
}

// 向链表尾部添加一个节点
void add(List *list, int number) {
    // 分配新节点内存
    Node *new_node = (Node*)malloc(sizeof(Node));
    if (new_node == NULL) {
        fprintf(stderr, "内存分配失败!\n");
        exit(EXIT_FAILURE);
    }
    new_node->value = number;
    new_node->next = NULL;

    // 插入节点到链表尾部
    if (list->head == NULL) {
        // 第一个节点成为头节点和尾节点
        list->head = new_node;
        list->tail = new_node;
    } else {
        // 将新节点链接到尾节点后,更新尾指针
        list->tail->next = new_node;
        list->tail = new_node;
    }
}

自己重写1遍

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

// 定义链表节点结构体及类型别名
typedef struct node {
    int value;
    struct node *next;  // 指向下一个节点的指针
} Node;
typedef struct 
{
	Node *head;
	Node *tail;
}List;
void add(List *new_node, int number);
int main() {
    int number=0;
     List list;  // 链表头指针初始化
       // 新增尾指针优化插入效率
    list.head=NULL;
    list.tail=NULL;
    
    while(1)
    {
         //重复scanf("%d",&number); 
    	if(number==-1){break;}
    	 // 读取用户输入并验证有效性
        if (scanf("%d", &number) != 1) {
            fprintf(stderr, "输入无效,请输入整数!\n");
            // 清空输入缓冲区防止死循环
            while (getchar() != '\n');
            continue;
        }
    	add(&list,number);
	}
    // 遍历链表并输出结果
    Node *current = list.head;
    while (current != NULL) {
        printf("%d->", current->value);
        current = current->next;
    }
    printf("NULL\n");  // 明确标识链表结尾

    // 释放链表内存
    Node *temp;
    while (list.head != NULL) {
        temp = list.head;
        list.head = list.head->next;
        free(temp);
    }

    return 0;
}
void add(List *list, int number)
{
        
        // 分配新节点内存
        Node *new_node = (Node*)malloc(sizeof(Node));
        if (new_node == NULL) {
            fprintf(stderr, "内存分配失败!\n");
            exit(EXIT_FAILURE);
        }
        new_node->value = number;
        new_node->next = NULL;

        // 插入节点到链表尾部
        if (list->head == NULL) {
            // 第一个节点成为头节点和尾节点
            list->head = new_node;
            list->tail = new_node;
        } else {
            // 将新节点链接到尾节点后,更新尾指针
            list->tail->next = new_node;
            list->tail = new_node;
        }
    
}

again please

void add(List *list,int number)
{
	Node *newnode=(Node *)malloc(sizeof(Node));
	newnode->value=number;
	newnode->next=NULL;
	
	if(list->head==NULL)
	{
		list->head=newnode;
		list->tail=newnode;
	}
	else
	{
		list->tail->next=newnode;
		list->tail=newnode;
	}
 } 

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务