简体   繁体   中英

How can I fix this linked list output?

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

struct node{

    int sayi;
    struct node* sonraki;



};

struct node* baslangic=NULL;


struct node* olustur(int sayi){

    struct node* yenidugum=(struct node*)malloc(sizeof(struct node));
    yenidugum->sayi=sayi;
    yenidugum->sonraki=NULL;

    return yenidugum;



}

void sonaekle(int sayi){

    struct node* sonaeklenecek=olustur(sayi);

    if(baslangic==NULL){

        baslangic=sonaekle;
    }
    else
    {

        struct node* temp=baslangic;

        while(temp->sonraki!=NULL){

            temp=temp->sonraki;
            temp->sonraki=sonaekle;
        }


    }

}

void yazdir(){


    struct node* temp=baslangic;

    while(temp!=NULL){


        printf("%d =>",temp->sayi);
        temp=temp->sonraki;
    }

}

int main()
{



    int secim, sayi;

    while(1){
        printf("1-Sona eleman ekle.....\n");
        printf("Yapmak istediginiz secimi yapin...\n");
        scanf("%d",&secim);


        switch(secim){

        case 1:
            printf("Hangi elemani ekleyeceksiniz..\n");
            scanf("%d",&sayi);
            sonaekle(sayi);
            yazdir();
            break;

        }

    }

    return 0;
}

I am trying to do make linked list but when I run this code it gives output like this: -443987883 => . What is my mistake. I can't find it. Thank you for your answers and giving your time.

There are typos.

Instead of

baslangic=sonaekle;

where you are assigning the function pointer of the type void ( * )( int ) to the pointer of the type struct node * you need to write

baslangic=sonaeklenecek;

Instead of this while loop

    while(temp->sonraki!=NULL){

        temp=temp->sonraki;
        temp->sonraki=sonaekle;
    }

you need to write

    while(temp->sonraki!=NULL){

        temp=temp->sonraki;
    }

    temp->sonraki = sonaeklenecek;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM