单链表的尾插法(C语言实现)
#include<stdio.h> #include<stdlib.h> #include<malloc.h> typedef struct LNode{ int data; struct LNode* next; }LNode,*LinkList; LinkList List_TailInsert(LinkList &L,int n) { int x,i; L = (LinkList)malloc(sizeof(LNode)); LNode *s , *r = L; for(i=0;i<n;i++) { s = (LNode*)malloc(sizeof(LNode)); printf("请输入第%d个数字:",i+1); scanf("%d",&x); s->data = x; r->next = s; r = s; } r->next = NULL; return L; } int main() { LinkList L; LNode *node; int n; printf("请输入链表节点数n:"); scanf("%d",&n); List_TailInsert(L,n); node = L->next; printf("按照顺序输出链表的元素:\n"); while(node != NULL) { printf("%d ",node->data); node = node->next; } printf("\n"); return 0; }