数据结构(如双链表,树和图等)需要实现引用类型的节点。通常用类和对象来实现
是否有一种方法可以在实现那些值时使用诸如struct之类的值类型?
使用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;
}