首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

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

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

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



/*
 ============================================================================
 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;
}
链表?C语言?尾插法
[解决办法]
你的问题在于你没有指针永远指向你的链表的尾节点。每次你插入节点都是在头结点的下一个节点插入。建议,插入前首先寻找到尾节点。
[解决办法]
你的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函数做这项处理。

[解决办法]
    “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;
 }



程序可以正常输出。

热点排行