#include <stdio.h>
#include <stdlib.h>
typedef struct LNode
{
int data;
struct LNode *next;
}LNode,*LinkList;
// }LNode;
//int tail_insert(LinkList *L, int n)
//int tail_insert(LinkList &L, int n)
//static LNode *L;
int TailInsert(LinkList *L, int n)
{
int i;
//LinkList *s;
LNode *s;
LNode *r;
// LNode *L=*M;
//建立头节点
// *T=(LNode *)malloc(sizeof(LNode));
//(*T)->next=NULL;
*L=(LinkList)malloc(sizeof(LNode));
(*L)->next=NULL;
//插入数据
//for(i = n ;i > 0; i--)
r=*L;
for(i = 0 ;i < n; ++i)
{
//s = (LNode *)malloc(sizeof(LNode));//生成新数据
s = (LinkList)malloc(sizeof(LNode));//生成新数据
//scanf("s->data = ",&n);
printf("please input data[%d]\n",i);
//scanf(&s->data);//输入元素值
scanf("%d",&s->data);
printf("just input:%d\n",s->data);
// s->next = (*T)->next;//插入到表头
// (*T)->next = s;
//s->next = (*L)->next;
//(*L)->next = s;
r->next=s;
r=s;
}
r->next=NULL;
// printf("the new link list is:\n");
// for(p = L->next; p->next != NULL ; p=p->next)
// printf("%d->",p->data);
// printf("%d\n",p->data);
return 0;
}
int main()
{
// LNode *L;
LinkList l;
LNode *p;
//int tail_insert(&L,5);
TailInsert(&l,5);
printf("the new link list is:\n");
for(p = l->next; p->next != NULL ; p=p->next)
printf("%d->",p->data);
printf("%d\n",p->data);
return 0;
}尾插法建立链表
最新推荐文章于 2024-02-28 16:26:57 发布
本文介绍了一种在链表中使用尾插法插入元素的方法,并提供了完整的C语言实现代码。通过该方法,可以有效地在链表末尾添加新元素。
451

被折叠的 条评论
为什么被折叠?



