C语言 为什么我的链表中包含垃圾值



在程序中,第一个节点的元素总是垃圾,我一直在尝试寻找解决方案,但找不到任何解决方案。我一直在尝试检查它很多次,但它就是不起作用。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct nd{
    int elem;
    struct nd * next;
} nodeType;
typedef struct listT{
    nodeType *head;
    int elemCtr;
} listType;
void input(nodeType **head);
void segregate(nodeType *head, listType *odd, listType *even);
void display(nodeType *head, listType odd, listType even);
int main(){
    listType odd, even;
    nodeType *head;
    odd.head=NULL;
    even.head=NULL;
    head = NULL;
    input(&head);
    segregate(head, &odd, &even);
    display(head, odd, even);
}
void input(nodeType **head){
    printf("nPlease input numbers input 0 if you wish to end the inputting process.:n");
    int x;
    scanf("%d", &x);
    for(;x!=0;){    
    nodeType *newnode;
    newnode = (nodeType*)malloc(sizeof(struct nd));
    newnode->elem=x;
    newnode->next=*head;    
    *head=newnode;
    scanf("%d", &x);
    }
}
void segregate(nodeType *head, listType *odd, listType *even){
    nodeType **trav;
    for(*trav=head;*trav!=NULL;trav=&(*trav)->next){
        if((*trav)->elem%2==0){
                nodeType *newnode;
                newnode = (nodeType*)malloc(sizeof(struct nd));
                newnode->elem = (*trav)->elem;
                newnode->next = even->head;
                even->head = newnode;
            }
        else{
                nodeType *newnode;
                newnode = (nodeType*)malloc(sizeof(struct nd));
                newnode->elem = (*trav)->elem;
                newnode->next = odd->head;
                odd->head = newnode;
        }
    }
}
void display(nodeType *head, listType odd, listType even){
    printf("nAll numbers :");
    nodeType **trav;
    for(trav=&head;*trav!=NULL;trav=&(*trav)->next){
        printf("n%d", (*trav)->elem);
    }
    printf("nEven numbers :");
    for(trav=&even.head;*trav!=NULL;trav=&(*trav)->next){
        printf("n%d", (*trav)->elem);
    }
    printf("nOdd numbers :");
    for(trav=&odd.head;*trav!=NULL;trav=&(*trav)->next){
        printf("n%d", (*trav)->elem);
    }
}

我也尝试在没有隔离和显示功能的情况下运行代码,我意识到它不会将第一个节点的繁琐更改为垃圾。我不知道可能是什么问题。

变量trav在函数隔离中未初始化,导致未定义的行为:

nodeType **trav;
for(*trav=head ...

我没有遍历代码,但你的意思是:

nodeType** trav = &head;

我猜你的错误是你把 trav 变量声明为指向节点类型的指针,它应该只是一个指向节点类型的指针 我修改了您的代码,它在这里工作正常。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct nd{
    int elem;
    struct nd * next;
} nodeType;
typedef struct listT{
    nodeType *head;
    int elemCtr;
} listType;
void input(nodeType **head);
void segregate(nodeType *head, listType *odd, listType *even);
void display(nodeType *head, listType odd, listType even);
int main(){
    listType odd, even;
    nodeType *head;
    odd.head=NULL;
    even.head=NULL;
    head = NULL;
    input(&head);
    print(head);
    segregate(head, &odd, &even);
    display(head, odd, even);
}
void input(nodeType **head){
    printf("nPlease input numbers input 0 if you wish to end the inputting process.:n");
    int x;
    scanf("%d", &x);
    for(;x!=0;){
    nodeType *newnode;
    newnode = (nodeType*)malloc(sizeof(struct nd));
    newnode->elem=x;
    newnode->next=*head;
    *head=newnode;
    scanf("%d", &x);
    }
}
void segregate(nodeType *head, listType *odd, listType *even){
    nodeType *trav;
    for(trav=head;trav!=NULL;trav=trav->next){
        if(trav->elem%2==0){
                nodeType *newnode;
                newnode = (nodeType*)malloc(sizeof(struct nd));
                newnode->elem = trav->elem;
                newnode->next = even->head;
                even->head = newnode;
            }
        else{
                nodeType *newnode;
                newnode = (nodeType*)malloc(sizeof(struct nd));
                newnode->elem = trav->elem;
                newnode->next = odd->head;
                odd->head = newnode;
        }
    }
}
void display(nodeType *head, listType odd, listType even){
    printf("nAll numbers :");
    nodeType *trav;
    for(trav=head;trav!=NULL;trav=trav->next){
        printf("n%d", trav->elem);
    }
    printf("nEven numbers :");
    for(trav=even.head;trav!=NULL;trav=trav->next){
        printf("n%d", trav->elem);
    }
    printf("nOdd numbers :");
    for(trav=odd.head;trav!=NULL;trav=trav->next){
        printf("n%d", trav->elem);
    }
}

相关内容

  • 没有找到相关文章

最新更新