单链表 · 头插法 / 尾插法 逐行动态演示

示例:依次插入 3 个结点,值为 1、2、3
void LinkList::CreateList_h(int n){ LNode *s; for(int i=0; i<n; i++){ s = new LNode; // 建立新结点 cin >> s->data; // 读入数据元素 s->next = head->next; // 新结点插入头结点之后 head->next = s; } }
void LinkList::CreateList_t(int n){ LNode *rear = head; LNode *s; for(int i=0; i<n; i++){ s = new LNode; // 建立新结点 cin >> s->data; // 读入数据元素 rear->next = s; // 新结点插在表尾 rear = s; // 尾指针指向新结点 } s->next = NULL; // 置最后结点的指针域为NULL }