| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 424 人关注过本帖
标题:求助!!! 链表问题
只看楼主 加入收藏
sun004715
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2010-10-30
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:2 
求助!!! 链表问题
自己写的一个程序 无法正确插入节点  帮忙看看什么问题呢

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NUM 4

typedef struct student
{
    char name[10];
    int  num;
    struct student *prev;
    struct student *next;
}NODE, *PNODE;

struct student *creatInode(struct student *pHead);
void removeInode(struct student *pHead);
void InsertInode(PNODE pHead, NODE *Inode);
void traverse_list(PNODE pHead);
struct student *pHead = NULL;

int main(void)
{

    //PNODE insert = NULL;
    int i = NUM-1;
    NODE a[NUM] = {{"zhang", 1}, {"li", 3}, {"wu", 2}, {"xu", 4}};
     PNODE studentInfo =(PNODE)malloc(sizeof(NODE));

    pHead = creatInode(studentInfo);
    for (; i>=0; i--)
    {
        InsertInode(pHead, &a[i]);
    }
    traverse_list(pHead);
    removeInode(pHead);
    traverse_list(pHead);
    return 0;
}

PNODE creatInode(PNODE pHead)
{
    PNODE pTail;
    pHead = (PNODE)malloc(sizeof(NODE));
    if (pHead == NULL)
    {
        printf("动态内存分配失败,程序中止!\n");
        exit(-1);
    }
    pTail = pHead;
    pTail->next = NULL;
    return pHead;
}

void InsertInode(PNODE pHead, PNODE Inode)
{
    if (!(Inode = (PNODE)malloc(sizeof(NODE))))
    {
        printf("malloc error");
        exit -1;
    }
    if(pHead->next != NULL)
    {
        pHead->next->prev = Inode;
        Inode->next = pHead->next;
        pHead->next = Inode;
        Inode->prev = pHead;
    }
    else
    {
                Inode->next = pHead->next;
                pHead->next = Inode;
                Inode->prev = pHead;
    }
}

void removeInode(NODE *pHead)
{
    int i = 0;
    int j = i+1;
    pHead = (PNODE)malloc(sizeof(NODE));
    for(; i<NUM; i++)
    {
        for(; j<NUM; j++)
        {
            if (pHead[i].num > pHead[j].num)
            {
                pHead = &pHead[i];
                pHead[i].next = pHead[j].next;
                pHead[i].prev = &pHead[j];
                pHead[j].prev = pHead->prev;
                pHead[j].next = &pHead[i];
            }
        }
    }
}

void traverse_list(PNODE pHead)
{
    PNODE p;
    p = pHead->next;
    if (NULL == p)
    {
       printf("链表为空!\n");
    }
    while(NULL != p)
    {
        printf("name :%s  num :%d \n",p->name,p->num);
        p = p->next;
    }
}
搜索更多相关主题的帖子: 链表 
2010-10-30 10:25
m21wo
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:4
帖 子:440
专家分:1905
注 册:2010-9-23
收藏
得分:10 
程序代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NUM 4

typedef struct student
{
    char name[10];
    int  num;
    struct student *prev;
    struct student *next;
}NODE, *PNODE;

struct student *creatInode(struct student *pHead);
void removeInode(struct student *pHead);
void InsertInode(PNODE pHead, NODE *Inode);
void traverse_list(PNODE pHead);
struct student *pHead = NULL;

int main(void)
{

    //PNODE insert = NULL;
    int i = NUM-1;
    NODE a[NUM] = {{"zhang", 1}, {"li", 3}, {"wu", 2}, {"xu", 4}};
     PNODE studentInfo =(PNODE)malloc(sizeof(NODE));

    pHead = creatInode(studentInfo);
    for (; i>=0; i--)
    {
        InsertInode(pHead, &a[i]);
    }
    traverse_list(pHead);
    printf("\n\n");
    removeInode(pHead);
    traverse_list(pHead);
    return 0;
}

PNODE creatInode(PNODE pHead)
{
    //PNODE pTail;
    pHead = (PNODE)malloc(sizeof(NODE));
    if (pHead == NULL)
    {
        printf("动态内存分配失败,程序中止!\n");
        exit(-1);
    }
   // pTail = pHead;
   // pTail->next = NULL;
    pHead->next=NULL;
    return pHead;
}

void InsertInode(PNODE pHead, PNODE Inode)
{
    if (Inode==NULL)  // 有内存不需要申请了
    {
        printf("malloc error");
        exit(-1);   //exit函数带的参数要用括号括起来
    }
    if(pHead->next != NULL)
    {
        //pHead->next= Inode;
        Inode->next = pHead->next;
        pHead->next = Inode;
        //Inode->prev = pHead;
    }
    else
    {
                Inode->next = pHead->next;
                pHead->next = Inode;
               // Inode->prev = pHead;
    }
}

void removeInode(NODE *pHead)
{
    int i = 0;
    int j = i+1;
    pHead = (PNODE)malloc(sizeof(NODE));
    for(; i<NUM; i++)
    {
        for(; j<NUM; j++)
        {
            if (pHead[i].num > pHead[j].num)
            {
                pHead = &pHead[i];
                pHead[i].next = pHead[j].next;
                pHead[i].prev = &pHead[j];
                pHead[j].prev = pHead->prev;
                pHead[j].next = &pHead[i];
            }
        }
    }
}

void traverse_list(PNODE pHead)
{
    PNODE p;
    p = pHead->next;
    if (NULL == p)
    {
       printf("链表为空!\n");
    }
    while(NULL != p)
    {
        printf("name :%s  num :%d \n",p->name,p->num);
        p = p->next;
    }
}





小改了一下

If You Want Something, Go Get It, Period.
2010-10-30 12:47
sun004715
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2010-10-30
收藏
得分:0 
回复 2楼 m21wo
谢谢 原来是多分配了 一段内存
2010-10-30 17:33
快速回复:求助!!! 链表问题
数据加载中...
 
   



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

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