C语言链表尾插法出现问题,插入最后的元素总是覆盖了之前插入的。

孤独的存在 2013-03-30 08:56:47


/*
============================================================================
Name : 30.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct node {
int data;
struct node *next;
} Lnode;
//初始化链表
Lnode* init_create(int data) {
Lnode *head,*p,*p1;//p用来接受系统为链表分配的内存,p1->next用来连接下一个节点地址
//当创建第一个节点的时候 为该节点分配内存
head=p1=p=(Lnode*)malloc(sizeof(Lnode));

//为节点的data取赋值 此时的p1要指向下一个节点 如果没有则为NULL
if(p){
p->data=data;//向data区添加数据,并设置下一个节点为NULL,此时已经到达了为节点,到了链表的尾部;
p->next=NULL;


}else{
printf("节点内存申请失败");
}
return head;
}
/*void Insert_Lnode(Lnode* head,int data){
//向链表中插入数据
Lnode *p;
p=(Lnode*)malloc(sizeof(Lnode));//接受系统为链表开辟的内存
if(p){
p->data=data;
p->next=head->next;
head->next=p;

}else{
printf("开辟内存失败");
}

}*/
void Insert_Lnode(Lnode* head,int data){
//为插入的数据开辟内存
Lnode *p=(Lnode*)malloc(sizeof(Lnode));
//判断p是否为空
if(p){
p->data=data;
head->next=p;
head=p;
p->next=NULL;

}else{
printf("内存申请失败");
}

}
void Printf_Lnode(Lnode* head){
Lnode *p;
p=head->next;
while(p!=NULL){
printf("%d ",p->data);
p=p->next;

}

}
int main(void) {
Lnode *lnode=init_create(6);
Insert_Lnode(lnode,4);
Insert_Lnode(lnode,8);
Insert_Lnode(lnode,9);
Printf_Lnode(lnode);
return EXIT_SUCCESS;
}
...全文
728 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
孤独的存在 2013-04-01
  • 打赏
  • 举报
回复
谢谢楼上 问题已经解决
孤独的存在 2013-03-31
  • 打赏
  • 举报
回复
这个原因 我也刚刚找到,但是没有解决啊
cmail2005 2013-03-31
  • 打赏
  • 举报
回复
“Insert_Lnode”函数中“head->next=p”,使“lnode->next”始终指向新插入的节点,新节点的“next”指向“NULL”。 语句“head = p;”并没有改变main()函数中头指针“Lnode”的值,打印时只会打印最后插入的节点。 应该建立一个头结点,程序如下:
#include <stdio.h>
#include <stdlib.h>
 
typedef struct node 
{
	int data;
	struct node * next;
 } Lnode;
 

//初始化链表
 Lnode* init_create(int data) 
 {
	Lnode *head,*p;//p用来接受系统为链表分配的内存,p1->next用来连接下一个节点地址
	//当创建第一个节点的时候 为该节点分配内存
	p=(Lnode*)malloc(sizeof(Lnode));

	head=(Lnode*)malloc(sizeof(Lnode)); //建立头结点

	//为节点的data取赋值 此时的p1要指向下一个节点 如果没有则为NULL
	if(p!=0 && head!=0)
	{
		head->next = p;		

		p->data=data;//向data区添加数据,并设置下一个节点为NULL,此时已经到达了为节点,到了链表的尾部;
		p->next=NULL;
	}

	else
	{
		printf("节点内存申请失败");
	}

	return head;
 }

 void Insert_Lnode(Lnode* head,int data)
 {
	//为插入的数据开辟内存
	 Lnode *p=(Lnode*)malloc(sizeof(Lnode));
	//判断p是否为空
	if(p)
	{
		p->data=data;
		p->next=head->next;		
		head->next=p;
	}
	else
	{
		printf("内存申请失败");
	}
 
}

 void Printf_Lnode(Lnode* head)
 {
	Lnode *p;
	p=head->next;
	while(p!=NULL)
	{
		printf("%d ",p->data);
		p=p->next;
	}
 }

 int main(void) 
 {
	Lnode * head = init_create(6);

	Insert_Lnode(head,4);

	Insert_Lnode(head,8);

	Insert_Lnode(head,9);

	Printf_Lnode(head);

	return EXIT_SUCCESS;
 }
程序可以正常输出。
luzhengyue07 2013-03-31
  • 打赏
  • 举报
回复
你的main函数可以这样写: int main(void) { Lnode *head, *lnode=init_create(6); head = lnode; Insert_Lnode(lnode,4); lnode = lnode->next; Insert_Lnode(lnode,8); lnode = lnode->next; Insert_Lnode(lnode,9); Printf_Lnode(head); return EXIT_SUCCESS; } 或者另编一个函数在调用Insert_Lnode前将指针移到链表尾,或者改进Insert_Lnode函数做这项处理。
ithaibo 2013-03-30
  • 打赏
  • 举报
回复
你的问题在于你没有指针永远指向你的链表的尾节点。每次你插入节点都是在头结点的下一个节点插入。建议,插入前首先寻找到尾节点。

70,011

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧