Как добавить элемент в конец Linked-List в C? - PullRequest
0 голосов
/ 26 мая 2020

Я работаю над программой полета, но не могу добавить в программу новые маршруты. Вот мои коды, как исправить? Я хочу, чтобы пользователи вводили такие данные, как источник, пункт назначения и номер рейса. Как я могу получить данные от пользователя и хранить эту информацию. Спасибо за помощь всем: /

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable:4996)

struct flightnode {

    char source[20];
    char destination[20];
    struct flightnode *next;
};


struct flightnode* head = NULL;
struct flightnode* temp = NULL;
struct flightnode* k = NULL;

char source[20];
char destination[20];

/*void addtoend(char x) {

    struct node* newtemp = (struct node*)malloc(sizeof(struct flightnode));
    newtemp->source = x;
    newtemp->next = NULL;



}*/

int main() {

    head = (struct flightnode*)malloc(sizeof(struct flightnode));
    temp= (struct flightnode*)malloc(sizeof(struct flightnode));

    printf("for source: \n");
    scanf(" %s", &source);
    printf("for dest: \n");
    scanf(" %s",&destination);


    strcpy(head->source,source);
    strcpy(temp->destination,destination);

    printf("source : %s\n", head->source);
    printf("dest : %s", temp->destination);

    return 0;
}

Ответы [ 2 ]

0 голосов
/ 26 мая 2020

Во-первых, непонятно, что вы хотите от newtemp->source = x;. Вы не можете назначить один символ для массива char ... Я предполагаю, что это строка ...

В любом случае - способ classi c добавить в конец связанного списка - :

void addtoend(struct flightnode **p, char* x) 
{
    // Construct the new node
    struct node* newtemp = malloc(sizeof *newtemp);
    strcpy(newtemp->source, x);
    newtemp->next = NULL;

    // Is the list empty?
    if (*p == NULL)
    {
        // yes... so the new node will be the new head
        *p = newtemp;
        return;
    }

    // Iterate to last node
    struct flightnode *tmp = *p;    
    while(tmp->next != NULL) tmp = tmp->next;

    // Add new node after last node
    tmp->next = newtemp;
}

и назовите его так:

addtoend(&head, "Hello");

И полный пример:

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

struct flightnode {

    char source[20];
    char destination[20];
    struct flightnode *next;
};



void addtoend(struct flightnode **p, char* x) 
{
    // Construct the new node
    struct flightnode* newtemp = malloc(sizeof *newtemp);
    strcpy(newtemp->source, x);
    newtemp->next = NULL;

    // Is the list empty?
    if (*p == NULL)
    {
        // yes... so the new node will be the new head
        *p = newtemp;
        return;
    }

    // Iterate to last node
    struct flightnode *tmp = *p;    
    while(tmp->next != NULL) tmp = tmp->next;

    // Add new node after last node
    tmp->next = newtemp;
}

void printList(struct flightnode *p)
{
    printf("---- LIST ----\n");
    while(p)
    {
        printf("%s\n", p->source);
        p = p->next;
    }
    printf("--------------\n");
}

int main() {

    struct flightnode* head = NULL;

    addtoend(&head, "Hello");
    printList(head);

    addtoend(&head, "world");
    printList(head);

    addtoend(&head, "have");
    printList(head);

    addtoend(&head, "a");
    printList(head);

    addtoend(&head, "nice");
    printList(head);

    addtoend(&head, "day");
    printList(head);

    return 0;
}

Вывод:

---- LIST ----
Hello
--------------
---- LIST ----
Hello
world
--------------
---- LIST ----
Hello
world
have
--------------
---- LIST ----
Hello
world
have
a
--------------
---- LIST ----
Hello
world
have
a
nice
--------------
---- LIST ----
Hello
world
have
a
nice
day
--------------
0 голосов
/ 26 мая 2020

Под файлом заголовка списка #include используйте adjList.push_back (data), где adjList - имя связанного списка

...