顯示廣告
隱藏 ✕
看板 TL
作者 TL (踢欸樓)
標題 [筆記] Double-Linked List
時間 2013年01月27日 Sun. PM 06:28:15


看板 C_and_CPP
作者 jesscy (小羊)
標題 請問...
時間 Sun May 16 02:48:57 1999


  下面是一個在 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

> -------------------------------------------------------------------------- <

看板 C_and_CPP
作者 DickG (龍龍)
標題 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 掉就行了

想想為什麼要這樣處理

龍龍


--
Shaken, Not Stirred.
--
※ 作者: TL 時間: 2013-01-27 18:28:15
※ 看板: TL 文章推薦值: 0 目前人氣: 0 累積人氣: 27 
分享網址: 複製 已複製
guest
x)推文 r)回覆 e)編輯 d)刪除 M)收藏 ^x)轉錄 同主題: =)首篇 [)上篇 ])下篇