双链表里面的,希望大神指点!删除第九个时,为什么就不可以了?
# 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();
}