Как создать структуры данных, используя типы значений? - PullRequest
0 голосов
/ 02 октября 2019

Структуры данных, такие как двойной связанный список, деревья и Graph .. и т. Д., Требуют реализации узлов ссылочного типа. обычно реализуется с классами и объектами

. Существует ли способ использовать типы значений, такие как структуры, для реализации этих типов?

1 Ответ

0 голосов
/ 02 октября 2019

Реализация списка избранного с использованием struct:

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

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

// Program to create a simple linked 
// list with 3 nodes 
int main() 
{ 
    struct Node* head = NULL; 
    struct Node* second = NULL; 
    struct Node* third = NULL; 

    // allocate 3 nodes in the heap 
    head = (struct Node*)malloc(sizeof(struct Node)); 
    second = (struct Node*)malloc(sizeof(struct Node)); 
    third = (struct Node*)malloc(sizeof(struct Node)); 

    head->data = 1; // assign data in first node 
    head->next = second; // Link first node with 

    // the second node 
    // assign data to second node 
    second->data = 2; 

    // Link second node with the third node 
    second->next = third; 

    return 0; 
} 
...