鱼C论坛

 找回密码
 立即注册
查看: 6383|回复: 12

[知识点备忘] S1E47:单链表3

[复制链接]
发表于 2020-1-31 22:01:18 | 显示全部楼层
零基础入门学习C语言封面
《零基础入门学习C语言》
小甲鱼 著
立即购买
本帖最后由 cslu 于 2020-2-1 12:01 编辑

#include <stdio.h>
#include <stdlib.h>
struct Node
{
        int value;
        struct Node *next;
};


void insertNode(struct Node **head,int value)
{
        struct Node *previous,current,new;
      
               
                current = *head;
        previous = NULL;
      
        
        while(current!= NULL && current->value < value)
        {
                previous=current;
                current=current->next;
        }
        
        new=(struct Node *)malloc(sizeof(struct Node));
        if(new==NULL)
        {
                printf("内存分配失败!");
                exit(1);
        }
        
      
        new->value=value;
        new->next=current;
        
         if(previous==NULL)
        {
                *head=new;
        }
        else
        {
                previous->next=new;
        }
}

void printNode(struct Node *head)
{
        struct Node *current;
       
        current=head;
        while(current!=NULL)
        {
                printf("%d",current->value);
                current=current->next;
        }
        putchar('\n');
}


int main(void)
{
        struct Node *head=NULL;
        int input;
       
        while(1)
        {
                printf("请输入1个整数(输入-1表示结束):");
                scanf("%d",&input);
                if(input==-1)
                {
                        break;
                }
                insertNode(&head,input);
                printNode(head);
        }
       
        return 0;
}
俺不知道哪里还有问题,编译不通过
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2020-7-25 20:20:14 | 显示全部楼层
阎建伟 发表于 2020-4-17 10:39
这句:struct Node *previous,current,new;
应该改为: struct Node *previous,*current,*new;
(你少打 ...

晓得了,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-4-2 00:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表