| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4273 人关注过本帖
标题:双链表的尾部添加节点问题
取消只看楼主 加入收藏
lanke711
Rank: 9Rank: 9Rank: 9
来 自:流浪在天国之路
等 级:蜘蛛侠
威 望:7
帖 子:317
专家分:1437
注 册:2015-7-16
结帖率:100%
收藏
已结贴  问题点数:100 回复次数:1 
双链表的尾部添加节点问题
程序代码:
#include <stdio.h>
#include <stdlib.h>

typedef struct list
{
    int data;
    struct list* prior;//前驱节点
    struct list* pnext;//后继节点
}node,*ptrl;

int listcount;//全局变量,链表节点

//建立链表
ptrl createlist()
{
    node* lhead,*lnew,*lend;
    int createnum;
    listcount=0;        //存放数据
    lhead=(node*)malloc(sizeof(node));
    lend=lhead;
    lend->pnext=NULL;
    printf("输入数据:\n");
    while((scanf("%d",&createnum))!=0)
    {
        
        if(createnum==0)
        {
            break;
        }
        listcount++;
        lnew=(node*)malloc(sizeof(node));
        lnew->data=createnum;        //数据存入链表
        if(lhead==NULL)
        {
            lhead=lnew;
            lend=lhead;
        }
        else
        {
            lnew->pnext=lend;
            lnew->prior=lend;
            lend->pnext=lnew;
            lend=lnew;
            
        }
        lend->pnext=NULL;
    }
    return lhead;
}


//在指定节点添加节点数据
ptrl addnum(int num,node* lhead,int listdata)
{
    node *lnew,*temp;    //temp用来遍历到指定节点的LIST指针,lnew用于保存新节点数据
    int addcount=0;
    lnew=(node*)malloc(sizeof(node));
    temp=lhead;
    lnew->data=listdata;
    //在表头插入数据
    if(temp->pnext==NULL)
    {
        
        lnew->pnext=lhead;    
        temp->pnext=lnew->pnext;
        temp->prior=lnew;
        listcount++;
    }
    //如果在表中插入节点数据
    if(temp->pnext!=NULL&&listcount!=num-1)
    {
        while(addcount<num)
        {
            temp=temp->pnext;
            addcount++;
        }
        lnew->pnext=temp;
        lnew->prior=temp->prior;
        temp->prior->pnext=lnew;
        temp->prior=lnew;
        listcount++;
        
    }
    else
    {    //在表尾插入节点数据                            
        for(addcount=0;addcount<num-1;++addcount)
        {
            temp=temp->pnext;
            
        }
        
        listcount++;//链表节点递增一次
        lnew->pnext=temp;    //接入链表
        lnew->prior=temp->prior;
        temp->prior->pnext=lnew;
        temp->pnext=NULL;    
        
    }
    return  lhead;

}
void prit(node* lhead)
{
    node* ltemp;
    ltemp=lhead->pnext;
    if(lhead==NULL)
    {
        printf("空链表");
        exit(-1);
    }
    while(ltemp!=NULL)
    {
        printf("%d\n",ltemp->data);
        ltemp=ltemp->pnext;
    }
}
int main()
{
    node* temp;
    int listnum;//节点
    int listdata;//数据
    temp=(node*)malloc(sizeof(node));
    temp=createlist();
    prit(temp);
    printf("输入要添加的节点:");
    scanf("%d",&listnum);
    printf("输入要添加的数据:");
    scanf("%d",&listdata);
    temp=addnum(listnum,temp,listdata);
    prit(temp);
    return 0;
}


图片附件: 游客没有浏览图片的权限,请 登录注册

调试结果:
图片附件: 游客没有浏览图片的权限,请 登录注册



程序代码:
//如果在表中插入节点数据
    if(temp->pnext!=NULL&&listcount!=num-1)
    {
        while(addcount<num)
        {
            temp=temp->pnext;
            addcount++;
        }
        lnew->pnext=temp;
        lnew->prior=temp;
        temp->prior->pnext=lnew;
        temp->prior=lnew;
        listcount++;
        
    }
    else
    {    //在表尾插入节点数据                            
        for(addcount=0;addcount<num-1;++addcount)
        {
            temp=temp->pnext;
            
        }
        
        listcount++;//链表节点递增一次
        lnew->pnext=temp;    //接入链表
        lnew->prior=temp;
        temp->prior->pnext=lnew;
        temp->pnext=NULL;    
        
    }

想问一下这是哪里出了问题?在表尾添加节点数据,实际运行时却在表尾的前一个节点添加了。。
2016-09-13 20:25
lanke711
Rank: 9Rank: 9Rank: 9
来 自:流浪在天国之路
等 级:蜘蛛侠
威 望:7
帖 子:317
专家分:1437
注 册:2015-7-16
收藏
得分:0 
谢谢各位版主,看完回复我哭了。重新整理思路,重新画流程 重新写过。。。。

普通人之所以普通,是因为他们普遍有一个通病,那就是认为自己永远普通。
千夫所指,我亦坚持。就算被所有人误解,我也照样守护这一切。
我们总是觉得,这些灵魂的表情,傲慢自大,目中无人,其实,真正目中无人的是我们。它们傲慢的不过是表情,而我们傲慢的却是行为!
记得,是为了忘记!
只要想着有那么一天,我就能忍受现在的每一天!
灾难并不可怕,可怕的是心中没有了希望。
你以为我在天堂,其实我正在路上。
当你觉得自己走不到终点的时候,请不要放弃。或许你的对手也是这种感觉。
2016-09-13 22:59
快速回复:双链表的尾部添加节点问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016703 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved