犯错误的地方:
1、双链表的尾插发自己还想着跟在中间插入节点一样的写法,犯了很严重的错误,只需要这些代码就能解决问题
pRear->pNext = s;
s->pPrior = pRear;
如果不是动手做自己并没有意识到这样。
2、结构体中前一项人家给的是prior,自己习惯用的是pre,还是按照书上的说法来
3、在删除和插入节点的时候都要对是否是在尾节点进行操作进行特殊处理,在做删除操作的时候,是在写完函数后才想到这一点,在追求正确率的路上,这是不应该的。
void DoubleList::coutMSG(DLNode* pHead)
{
cout<<"输出双链表信息:"<<endl;
while (pHead->pNext!=NULL)
{
cout<<pHead->pNext->n<<endl;
pHead = pHead->pNext;
}
cout<<"输出结束"<<endl;
}
void DoubleList::InitDLNode(DLNode *&pNode)
{
pNode = (DLNode*)malloc(sizeof(DLNode));
pNode->pNext = NULL;
pNode->pPrior = NULL;
}
void DoubleList::CreateeDBList(DLNode *&pHead,int num[],int n)
{
DLNode* s;
DLNode* pRear = pHead;
for (int i=0; i<n; i++)
{
InitDLNode(s);
s->n = num[i];
pRear->pNext = s;
s->pPrior = pRear;
// s->pNext = pRear->pNext;这儿记录了尾插法自己在这儿添加如下代码是多么的愚蠢,不是自己动手做,可能都没意识到自己这儿会出这样的问题
// s->pPrior = pRear;
// if (pRear->pNext != NULL)
// {
// pRear->pNext->pPrior = s;
// }
// pRear->pNext = s;
pRear = pRear->pNext;
}
}
// 要删除的节点所存储的数据为n
void DoubleList::DeleteDLNode(DLNode *&pHead,int n)
{
DLNode* p = pHead->pNext;
while (p!=NULL)
{
if (p->n == n)
{
if (p->pNext != NULL) //第一时间没有想到删除节点的时候,需要加这个判断语句,不能删除尾节点的时候可能会报错
{
p->pNext->pPrior = p->pPrior;
}
p->pPrior->pNext = p->pNext;
free(p);
break;
}
p = p->pNext;
}
}
// 在数据为n的节点后面插入节点pInsert
void DoubleList::InsertDLNode(DLNode *&pHead,int n,DLNode*& pInsert)
{
DLNode* p = pHead->pNext;
while (p!=NULL)
{
if (p->n == n)
{
if (p->pNext!=NULL)
{
pInsert->pNext = p->pNext;
pInsert->pPrior = p;
p->pNext->pPrior = pInsert;
p->pNext = pInsert;
}
else
{
pInsert->pPrior = p;
p->pNext = pInsert;
}
break;
}
p= p->pNext;
}
}
网友评论