| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 518 人关注过本帖
标题:双链表里面的,希望大神指点!删除第九个时,为什么就不可以了?
只看楼主 加入收藏
xiaosalin
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-11-23
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
双链表里面的,希望大神指点!删除第九个时,为什么就不可以了?
# include <stdio.h>
# include <malloc.h>
# include <iostream>
# include <conio.h>
using namespace std;

# define INIT_LENGTH 10
# define OK 1
# define ERROR 0

typedef struct DNode
{
    int data;
    struct DNode * pPrior;
    struct DNode * pNext;

}DNODE, *PDNODE;


PDNODE ListCreate_DL(void)//'n' is node quantities
{
    PDNODE pDHead = (PDNODE)malloc(sizeof(DNODE));
    if (pDHead == NULL)
    {
        cout<<"The internal memnory allocting is failed,the program exit!";
        exit(-1);
    }
    pDHead->pNext = pDHead->pPrior = NULL;

    PDNODE p = pDHead;
    p->pNext = p->pPrior = NULL;
    cout<<"List has ten elements!"<<endl;
    for (int i=0; i<INIT_LENGTH; i++)
    {
        PDNODE pNew = (PDNODE)malloc(sizeof(DNODE));
        cout<<"Please input value of pNew->data "<<i+1<<": ";
        cin>>pNew->data; cout<<endl;
        p->pNext = pNew;
        pNew->pPrior = p;
        pNew->pNext = NULL;
        p = pNew;
    }
    return pDHead;
}
int ListDelete_DL(PDNODE &pDHead, int pos, int &e)
 {
     PDNODE p  = pDHead;
     int i = 0;
     if (pos<1 || pos>INIT_LENGTH)
     {
         cout<<"Error! out of lacotion";
         return ERROR;
     }
     while (i<pos-1&& p!=NULL)
     {
         p = p->pNext;
         i++;
     }
     if (i>pos-1 || p==NULL)
     {
         cout<<"The NO."<<pos<<"is not exist!";
         return ERROR;
     }
     if (pos == 10)
     {
         e = p->pNext->data;
         free(p->pNext);
         p->pNext = NULL;
     }
     else
     {
         e = p->pNext->data;
         PDNODE q = p->pNext;
         p->pNext = p->pNext->pNext;
         p->pNext->pNext->pPrior = p;
         free(q);
     }
     return OK;
 }

int main(void)
{

        int i, j, e;
    PDNODE pDHead = NULL;
    pDHead = ListCreate_DL();
    PDNODE p = pDHead;
    cout<<"The old double directions list is:";
    for (i=0; i<INIT_LENGTH; i++)
    {
        p = p->pNext;
        cout<<p->data<<" ";
    }
    cout<<endl;

    cout<<"Please input the location to delete(1-11):";
    cin>>i;
    if (ListDelete_DL(pDHead, i, e))
    {
        cout<<endl<<"The new double directions list is :";
        p = pDHead;
        for (j=0; j<INIT_LENGTH-1; j++)
        {
            p = p->pNext;
            cout<<p->data<<" ";
        }
    }
    cout<<endl<<endl<<"...OK...";
    getch();

}
搜索更多相关主题的帖子: include failed 
2013-11-23 21:27
zhaogay
Rank: 7Rank: 7Rank: 7
来 自:宫
等 级:黑侠
帖 子:151
专家分:586
注 册:2013-10-10
收藏
得分:20 
int ListDelete_DL(PDNODE &pDHead, int pos, int &e)
 {
      PDNODE p  = pDHead;
      int i = 0;
      if (pos<1 || pos>INIT_LENGTH)
      {
          cout<<"Error! out of lacotion";
          return ERROR;
      }
      while (i<pos-1&& p!=NULL)
      {
          p = p->pNext;
          i++;
      }
      if (i>pos-1 || p==NULL)
      {
          cout<<"The NO."<<pos<<"is not exist!";
          return ERROR;
      }
      if (pos == 10)
      {
          e = p->pNext->data;
          free(p->pNext);
          p->pNext = NULL;
      }
      else
      {
          e = p->pNext->data;
          PDNODE q = p->pNext;
          p->pNext = p->pNext->pNext;
          p->pNext->pNext->pPrior = p;//应该是这里越界了
          free(q);
      }
      return OK;
 }

好好学习,天天想上
2013-11-24 00:11
xiaosalin
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-11-23
收藏
得分:0 
那要怎么改呢?谢谢……
2013-11-25 21:27
zhaogay
Rank: 7Rank: 7Rank: 7
来 自:宫
等 级:黑侠
帖 子:151
专家分:586
注 册:2013-10-10
收藏
得分:0 
           e = p->pNext->data;
           PDNODE q = p->pNext;
           p->pNext = p->pNext->pNext;
           if (p->pNext->pNext!=NULL)
               p->pNext->pNext->pPrior = p;
           free(q);

好好学习,天天想上
2013-11-26 13:24
快速回复:双链表里面的,希望大神指点!删除第九个时,为什么就不可以了?
数据加载中...
 
   



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

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