Я сделал упрощение моего двойного связанного списка.Мой двойной связанный список - это структура, в которой узлами являются голова и хвост.
Существует функция для создания списка и его возврата.В той же функции я делаю связь между хвостом и головными узлами.Проблема в том, что когда я возвращаю список (так что выхожу за пределы функции), все ссылки исчезают или они просто указывают на узлы списка, который был временно создан в функции.Правильно ли мое предположение?Если так, как я собираюсь обойти эту проблему?
Вот код:
#include <stdio.h>
typedef struct node{ /*a node of a list*/
int number;
struct node *next;
struct node *prev;
} node;
typedef struct list{ /*the list structure that holds only the head and tail*/
node head;
node tail;
} list;
list createList(){
list newList;
newList.head.prev=NULL;
newList.head.next=&newList.tail; /*first node points to the second*/
newList.tail.prev=&newList.head; /*second node points to the first*/
newList.tail.next=NULL;
puts("--CREATE LIST FUNC--");
printf("Head element address: %p\n", &newList.head);
printf("Tail element address: %p\n", &newList.tail);
printf("Head element points here: %p\n\n\n", newList.head.next);
return newList;
}
int main(){
list numbers=createList();
puts("--MAIN FUNC--");
printf("Head element address: %p\n", &numbers.head);
printf("Tail element address: %p\n", &numbers.tail);
printf("Head element points here: %p\n", numbers.head.next);
return 0;
}