卡提諾王國's Archiver

leonkk 發表於 2009-11-16 19:39

(C/C++)資料結構問題

[i=s] 本帖最後由 風箏惡魔 於 2009-11-18 11:02 編輯 [/i]

A) 下列為linked list Chain的class definition,試寫出其中 Print( ) operation的程式碼部分,並略說明你的程式做法;Print的作用是將整個linked list中所有資料依序列印在銀幕上。     
template <class T>
class ChainNode {
   friend Chain<T>;
   private :
     T data;
     ChainNode<T> *link;
};
template <class T>
class Chain {
   public :
   Chain() {first = 0;}
   ~Chain();
   bool IsEmpty() const;
   int Length() const ;
   bool Find(int k, T& x) const ;
   int Search(const T& x) const ;
   Chain<T>& Delete(int k, T& x);
   Chain<T>& Insert(int k,const T& x);
   void Print( ) const ;
   private :
   // pointer to first node
   ChainNode<T> *first;
};};


B) Queue的Abstract Data Type(ADT)定義如下:
AbstractDataType Queue {
instances
ordered list of elements; one end is called the front; the other is the rear;
operations:
Create ( ): Create an empty queue;
IsEmpty ( ): Return true if queue is empty, return false otherwise;
IsFull ( ): Return true if queue is full, return false otherwise;
First ( ): Return first element of queue;
Last ( ): Return last element of queue;
Add (x): Add element x to the queue;
Delete (x): Delete front element from queue and put it in x;
}
   
試利用此ADT設計一合理的C++ Queue class. (只需寫出class的definition部分,class中各function的implementation部分可不寫。)  


麻煩高手們給個指點!!
感謝了!!

[quote]
[color=Blue]風箏惡魔:
這位大大...
我想
您在PO出這個問題時
應該也想過該怎麼解決了吧
麻煩您把您的想法PO上來給大家看看
讓大家瞭解您的想法
這樣才可以互相討論
而不是PO上來要答案的喔...
況且這邊的朋友也沒有義務一定要回答您喔...
程式的功力是靠著苦練跟討論才會慢慢增長的...[/color]
[/quote]

leonkk 發表於 2009-11-18 19:53

[b] [url=http://ck101.com/redirect.php?goto=findpost&pid=71802737&ptid=1532127]1#[/url] [i]leonkk[/i] [/b]

A)
void Chain::Print ()
{
        ChainNode *cur;
//宣告一個節點來存放串列的頭,讓程式能知道要從哪開始顯示
        for(cur = first ; cur ; cur = cur->link)
//顯示出cur->data的值,並且指向下一個值
        {
                cout << cur->data << "  ";
        }
}

B)
Template <class T>
class Queue{
public:
        Queue(int MaxQueueSize = 10 ); //創造一個Queue
        ~LinkedQueue() {delete [] queue;} //刪除整個Queue
        bool IsEmpty () const {return fornt == rear;} //檢查Queue是否還有空位
        bool IsFull() const {return ((rear+1) % MaxSize == front) ?1:0}; //檢查Queue是
  否為滿的
T  First()  const ; //存放先放入的元素
        T  Last()  const ; //存放後放入的元素
        Queue<T>& Add(const T& x) ; //從後面加入一個新的元素
        Queue<T>& Delete(T& x);  //取出並刪除先放入的元素
private :
        int front; //Queue的頭
        int rear; //Queue的尾
        int MaxSize; //Queue的空位最大值
T *queue;
}

頁: [1]

Powered by Discuz! Archiver 7.0.0  © 2001-2009 Comsenz Inc.