下面是一個在 double linked list 插入及刪除節點的程式
程式有點長,而且是我第一次寫 C++ ,寫的並不好,希望您能耐心看完!
我反反覆覆看了無數次,都不知道到底錯在哪裡,
煩請諸位高手指點迷津 !!
我得到的訊息是 0 error, 0 warning
但 execute 後, 出現 Access Violated 的訊息
/ the header file : DuLList.h
#ifndef DULLLIST_H
#define DULLLIST_H
template <class type> class DuLList;
template <class type>
class DuLListNode
// define a node in the list
{
friend class DuLList<type>;
private:
type data;
DuLListNode *llink, *rlink;
public:
DuLListNode(type dt):data(dt){};
void Initialize(DuLListNode<type> *p);
};
template <class type>
class DuLList
// define a double linked list
{
private:
DuLListNode<type> *first;
public:
void Create(DuLListNode<type> *fr);
void Insert(DuLListNode<type> *il, DuLListNode<type> *i);
void Delete(DuLListNode<type> *d);
};
#endif
include <iostream.h>
#include "DuLList.h"
template <class type>
void DuLListNode<type>::Initialize(DuLListNode<type> *p)
// let llink and rlink ofthe node point to itself
{
p->llink = p;
p->rlink = p;
}
template <class type>
void DuLList<type>::Create(DuLListNode<type> *fr)
// start a list pointed by first
{
first->llink = fr;
first->rlink = fr;
fr->llink = first;
fr->rlink = first;
}
template <class type>
void DuLList<type>::Insert(DuLListNode<type> *il, DuLListNode<type> *i)
// insert node i to the right of node il
{
i->llink = il;
i->rlink = il->rlink;
il->rlink->llink = i;
il->rlink = i;
}
template <class type>
void DuLList<type>::Delete(DuLListNode<type> *d)
// delete node d from the list
{
if(first==d)
cerr<<"Deletion not permitted."<<endl;
else
{
d->llink->rlink = d->rlink;
d->rlink->llink = d->llink;
delete d;
}
}
void main()
{
DuLListNode<int> *a = new DuLListNode<int>(10);
a->Initialize(a);
DuLList<int> test_list;
DuLListNode<int> *b = new DuLListNode<int>(20);
b->Initialize(b);
DuLListNode<int> *c = new DuLListNode<int>(30);
c->Initialize(c);
DuLListNode<int> *d = new DuLListNode<int>(40);
d->Initialize(d);
DuLListNode<int> *e = new DuLListNode<int>(25);
e->Initialize(e);
test_list.Create(a);
test_list.Insert(a,b);
test_list.Insert(b,c);
test_list.Insert(c,d);
test_list.Insert(b,e);
test_list.Delete(b);
}
感謝您耐心的看完與指導!!
--
人生是一塊沒有洗過的調色盤
重疊記載著喜怒哀愁
--
※ 發信站: 批踢踢實業坊(ptt.twbbs.org)
◆ From: t200-115.dialup
> -------------------------------------------------------------------------- <
作者: DickG (龍龍) 看板: C_and_CPP
標題: Re: 請問...
時間: Tue May 18 07:49:44 1999
※ 引述《jesscy (小羊)》之銘言:
: template <class type>
: void DuLList<type>::Create(DuLListNode<type> *fr)
: // start a list pointed by first
: {
: first->llink = fr;
: first->rlink = fr;
: fr->llink = first;
: fr->rlink = first;
: }
將上述改為
first = fr;
其餘的 comment 掉就行了
想想為什麼要這樣處理
龍龍