C 语言中的简单链表程序
实现
该算法的实现如下 −
#include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; //显示列表 void printList() { struct node *ptr = head; printf(" [head] =>"); //从头开始 while(ptr != NULL) { printf(" %d =>",ptr->data); ptr = ptr->next; } printf(" [null] "); } //在第一个位置插入链接 void insert(int data) { //创建链接 struct node *link = (struct node*) malloc(sizeof(struct node)); //link->key = key; link->data = data; //将其指向旧的第一个节点 link->next = head; //将 first 指向新的第一个节点 head = link; } int main() { insert(10); insert(20); insert(30); insert(1); insert(40); insert(56); printList(); return 0; }
输出
程序的输出应为 −
[head] => 56 => 40 => 1 => 30 => 20 => 10 => [null]
linked_list_programs_in_c.html