C语言链表基础操作示例

include

include

struct Node {

int data;

struct Node* next;

};

void insertAtBeginning(struct Node head_ref, int new_data) {

struct Node new_node = (struct Node)malloc(sizeof(struct Node));

new_node->data = new_data;

new_node->next = (head_ref);

(head_ref) = new_node;

}

void display(struct Node head) {

struct Node temp = head;

while (temp != NULL) {

printf("%d ", temp->data);

temp = temp->next;

}

}

int main() {

struct Node* head = NULL;

insertAtBeginning(&head, 1);

insertAtBeginning(&head, 2);

insertAtBeginning(&head, 3);

printf("Linked list: ");

display(head);

return 0;

}

zip 文件大小:3.23KB