鱼C论坛

 找回密码
 立即注册
查看: 1658|回复: 6

[已解决]这个为什么、 一输入0就错误?scanf不是可以接受0,吗?

[复制链接]
发表于 2022-12-15 19:16:52 | 显示全部楼层 |阅读模式
20鱼币
203304wopiawi11hxa88wi.png.thumb.png
#include <stdio.h>
#include <stdlib.h>

struct Node{
        int num;
        struct Node *next;
};

void function(void);
void ds_init(struct Node **head);
int ds_insert(struct Node **head, int i);
void ds_delete(struct Node **head, int i);
int ds_search(struct Node *head, int i);
void print(struct Node *head);
int ClerrList(struct Node **head);

void function(void){
        printf("1.初始化链表\n");
        printf("2.插入结点\n");
        printf("3.删除结点\n");
        printf("4.查找结点\n");
        printf("5.遍历链表\n");
        printf("0.退出\n");
}

void ds_init(struct Node **head){
        int i;
        struct Node *node;
        struct Node *temp;

        printf("输入结点的值,输入-1完成初始化\n");

        while(1){
                scanf("%d", &i);
                if (i == -1){
                        return ;
                }

                if (!(*head)){//        !(*head) --> *head == NULL
                        *head = (struct Node *)malloc(sizeof(struct Node));
                        if(!(*head)){
                                exit(1);
                        }
                        (*head)->num = i;
                        (*head)->next = *head;
                }
                else{
                        for (node = (*head); node->next != (*head); node = node->next)
                                ;
                        temp = (struct Node *)malloc(sizeof(struct Node));
                        if(!(*head)){;
                                exit(1);
                        }

                        temp->num = i;
                        temp->next = *head;
                        node->next = temp;
                }
        }
}

int ds_insert(struct Node **head, int i){
        int j, k = 1;
        struct Node *node;
        struct Node *now;
        struct Node *temp;

        printf("请输入要插入的结点的值");
        scanf("%d", &j);

        if (i == 1){//新插入的结点作为第一个结点
                now = (struct Node *)malloc(sizeof(struct Node));
                if(!now){
                        exit(1);
                }
                now->num = j;
                for (node = *head; node->next != *head; node = node->next)//找到最后一个结点
                        ;

                now->next = *head;
                node->next = now;//temp放到head前面
                *head = now; //head要指向第一个结点
        }
        else{
                node = *head;

                while ( ++k < i){
                        node = node->next;
                }

                now = (struct Node *)malloc(sizeof(struct Node));
                if(!now){
                        exit(1);
                }
                now->num = j;

                temp = node->next;
                node->next = now;
                now->next = temp;
        }
        return j;
}

void ds_delete(struct Node **head, int i){
        int j = 1;
        struct Node *node;
        struct Node *temp;

        if (i == 1){//删除的第一个结点
                for (node = *head; node->next != (*head); node = node->next)//找到最后一个结点
                        ;

                temp = *head;
                *head = (*head)->next;
                node->next = *head;
                free(temp);
        }
        else{
                node = *head;

                while ( ++j < i){
                        node = node->next;
                }

                temp = node->next;
                node->next = temp->next;
                free(temp);
        }
}

int ds_search(struct Node *head, int i){
        int j = 1;
        struct Node *node;

        for (node = head; node->next != head && node->num != i; j++){//找到最后一个结点
                node = node->next;
        }

        if (node->next == head)
                return 0;
        else
                return j;
}

void print(struct Node *head){//遍历
        struct Node *node;
        int i = 0;

        node = head;

        do{
                printf("%d ", node->num);
                node = node->next;
        }while (node != head);
        putchar('\n');
}

int ClerrList(struct Node **head){//将单链表清空
        struct Node *node, *temp;

        node = *head;
        while (node){
                temp = node;
                node = node->next;
                free(temp);
                /*temp = node->next;
                free(node);
                node = temp;*/
        }
        if (node)
                return 0;
        else
                (*head)->next = NULL;

        return 1;
}

int main(){
        char i;
        int j;
        struct Node *head = NULL;

        function();
        while (1){
                printf("请输入指令:");
                scanf("%c", &i);
                printf("__________");
                switch (i){
                        case '1': ds_init(&head);
                                print(head);break;
                        case '2': printf("请输入要插入的位置:");
                                scanf("%d", &j);
                                printf("再位置%d插入%d后:", j, ds_insert(&head, j));
                                print(head);break;
                        case '3': printf("请输入要删除的位置:");
                                scanf("%d", &j);
                                ds_delete(&head, j);
                                printf("删除第%d个结点后:", j);
                                print(head);break;
                        case '4': printf("请输入查找的值:");
                                scanf("%d", &j);
                                printf("元素%d所在的位置:%d\n", j, ds_search(head, j));break;
                        case '5': print(head);break;
                        case '0': ClerrList(&head);exit(1);
                }
        }

        return 0;
}
最佳答案
2022-12-15 19:16:53
int ClerrList(struct Node **head){//将单链表清空
        struct Node *node, *temp;

        node = *head;
        while (node){//你怎么改成node->next
                temp = node;
                node = node->next;
                free(temp);
                /*temp = node->next;
                free(node);
                node = temp;*/
        }
        if (node)
                (*head)->next = NULL;
        else
                return 0;
        return 1;
}

最佳答案

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-12-15 19:16:53 | 显示全部楼层    本楼为最佳答案   
int ClerrList(struct Node **head){//将单链表清空
        struct Node *node, *temp;

        node = *head;
        while (node){//你怎么改成node->next
                temp = node;
                node = node->next;
                free(temp);
                /*temp = node->next;
                free(node);
                node = temp;*/
        }
        if (node)
                (*head)->next = NULL;
        else
                return 0;
        return 1;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-12-15 19:40:29 | 显示全部楼层
问题不在scanf那里,在ClerrList这个函数里,那个函数写的有问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-12-15 20:14:57 | 显示全部楼层
lvk 发表于 2022-12-15 19:40
问题不在scanf那里,在ClerrList这个函数里,那个函数写的有问题

可是那个printf都没有执行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-12-15 20:15:27 | 显示全部楼层
if (node)
               (*head)->next = NULL; 
        else 
               return 0;
               
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-12-15 20:21:09 | 显示全部楼层

KO`LAB6}O]MVC11~UZP~KL7.png 还是不行
#include <stdio.h>
#include <stdlib.h>

struct Node{
        int num;
        struct Node *next;
};

void function(void);
void ds_init(struct Node **head);
int ds_insert(struct Node **head, int i);
void ds_delete(struct Node **head, int i);
int ds_search(struct Node *head, int i);
void print(struct Node *head);
int ClerrList(struct Node **head);

void function(void){
        printf("1.初始化链表\n");
        printf("2.插入结点\n");
        printf("3.删除结点\n");
        printf("4.查找结点\n");
        printf("5.遍历链表\n");
        printf("0.退出\n");
}

void ds_init(struct Node **head){
        int i;
        struct Node *node;
        struct Node *temp;

        printf("输入结点的值,输入-1完成初始化\n");

        while(1){
                scanf("%d", &i);
                if (i == -1){
                        return ;
                }

                if (!(*head)){//        !(*head) --> *head == NULL
                        *head = (struct Node *)malloc(sizeof(struct Node));
                        if(!(*head)){
                                exit(1);
                        }
                        (*head)->num = i;
                        (*head)->next = *head;
                }
                else{
                        for (node = (*head); node->next != (*head); node = node->next)
                                ;
                        temp = (struct Node *)malloc(sizeof(struct Node));
                        if(!(*head)){;
                                exit(1);
                        }

                        temp->num = i;
                        temp->next = *head;
                        node->next = temp;
                }
        }
}

int ds_insert(struct Node **head, int i){
        int j, k = 1;
        struct Node *node;
        struct Node *now;
        struct Node *temp;

        printf("请输入要插入的结点的值");
        scanf("%d", &j);

        if (i == 1){//新插入的结点作为第一个结点
                now = (struct Node *)malloc(sizeof(struct Node));
                if(!now){
                        exit(1);
                }
                now->num = j;
                for (node = *head; node->next != *head; node = node->next)//找到最后一个结点
                        ;

                now->next = *head;
                node->next = now;//temp放到head前面
                *head = now; //head要指向第一个结点
        }
        else{
                node = *head;

                while ( ++k < i){
                        node = node->next;
                }

                now = (struct Node *)malloc(sizeof(struct Node));
                if(!now){
                        exit(1);
                }
                now->num = j;

                temp = node->next;
                node->next = now;
                now->next = temp;
        }
        return j;
}

void ds_delete(struct Node **head, int i){
        int j = 1;
        struct Node *node;
        struct Node *temp;

        if (i == 1){//删除的第一个结点
                for (node = *head; node->next != (*head); node = node->next)//找到最后一个结点
                        ;

                temp = *head;
                *head = (*head)->next;
                node->next = *head;
                free(temp);
        }
        else{
                node = *head;

                while ( ++j < i){
                        node = node->next;
                }

                temp = node->next;
                node->next = temp->next;
                free(temp);
        }
}

int ds_search(struct Node *head, int i){
        int j = 1;
        struct Node *node;

        for (node = head; node->next != head && node->num != i; j++){//找到最后一个结点
                node = node->next;
        }

        if (node->next == head)
                return 0;
        else
                return j;
}

void print(struct Node *head){//遍历
        struct Node *node;
        int i = 0;

        node = head;

        do{
                printf("%d ", node->num);
                node = node->next;
        }while (node != head);
        putchar('\n');
}

int ClerrList(struct Node **head){//将单链表清空
        struct Node *node, *temp;

        node = *head;
        while (node->next){
                temp = node;
                node = node->next;
                free(temp);
                /*temp = node->next;
                free(node);
                node = temp;*/
        }
        if (node)
                (*head)->next = NULL;
        else 
                return 0;
        return 1;
}

int main(){
        char i;
        int j;
        struct Node *head = NULL;

        function();
        while (1){
                printf("请输入指令:");
                scanf("%c", &i);
                switch (i){
                        case '1': ds_init(&head);
                                print(head);break;
                        case '2': printf("请输入要插入的位置:");
                                scanf("%d", &j);
                                printf("再位置%d插入%d后:", j, ds_insert(&head, j));
                                print(head);break;
                        case '3': printf("请输入要删除的位置:");
                                scanf("%d", &j);
                                ds_delete(&head, j);
                                printf("删除第%d个结点后:", j);
                                print(head);break;
                        case '4': printf("请输入查找的值:");
                                scanf("%d", &j);
                                printf("元素%d所在的位置:%d\n", j, ds_search(head, j));break;
                        case '5': print(head);break;
                        case '0': ClerrList(&head);exit(1);
                }
        }

        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-12-15 20:22:51 | 显示全部楼层
御坂19090 发表于 2022-12-15 20:14
可是那个printf都没有执行

错误到底是不是在ClerrList这个函数里,你调试一下,一步步执行不就知道了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-7 21:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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