回分類題庫
d169: OOP Lab Exercise: Doubly Linked List Class
出處:

Difficulity : N/A
Accepted : 80 Times | Submit :180 Times | Clicks : 1872
Accepted : 58 Users | Submit : 63 Users | Accepted rate : 92%
Time Limit :10000 ms | Memory Limit : 64000 KBytes
題目加入時間 : 2010-05-11 14:31

Content :

Given a MyList and LinkNode class,

class LinkNode{
    public:
        char element;
        LinkNode *next;
        LinkNode *prev;
};

class MyList{
    public:
        MyList();
        ~MyList();
        void toHead();
        void toTail();
        int Size();
        void next();
        void prev();
        bool isHead()const;
        bool isTail()const;
        void Insert(LinkNode*);
        void Delete();
        char getElement();
        char setElement();
    private:
        LinkNode *current;
        LinkNode *head;
        LinkNode *tail;
};
 

please implement the following operations:

~MyList()  :  The destructor. You have to delete all the elements in MyList.

toHead() / toTail()  :  Move the current pointer to Head / Tail.

next() / prev()  :  Move current to its next / previous element.  Output a line
                         "Error: Current is NULL" if there is no target element.

Size()  :   Return the number of elements in this list.  You have to traverse the list and
               count the number.  You cannot add a data member in the class or use any global
              variable.

Insert()  :  Insert an element to current position.  (Refer to the slide today.)

Delete()  :  Delete the element pointed by current. (Refer to the slide today.)

Notice that you cannot change the member function's prototype and add any data member to MyList and LinkNode class.

To get pass, you must,

1.  Submit your program with the following code.

2.  Follow the specification in slide and above difinition.

3.  Submit with your OOP account and gain AC.

The code is here:    http://www.cs.nccu.edu.tw/~g9803/oop/DoublyLinkedList_ex.cpp

========================================================

#include <iostream>

using namespace std;

class LinkNode{
    public:
        LinkNode(char);
        char element;
        LinkNode *next;
        LinkNode *prev;
};

LinkNode::LinkNode(char c){
    element = c;
    next = NULL;
    prev = NULL;
}

class MyList{
    public:
        MyList();
        ~MyList();
        void toHead();
        void toTail();
        int Size();
        void next();
        void prev();
        bool isHead()const;
        bool isTail()const;
        void Insert(LinkNode*);
        void Delete();
        char getElement();
        void setElement(char);
    private:
        LinkNode *current;
        LinkNode *head;
        LinkNode *tail;
};

MyList::MyList(){
    current = NULL;
    head = NULL;
    tail = NULL;
}

MyList::~MyList(){
  
}

void MyList::toHead(){
   
}

void MyList::toTail(){
   
}

void MyList::next(){
   
}

void MyList::prev(){
   
}

int MyList::Size(){
   
}

bool MyList::isHead()const{
    return current == head;
}

bool MyList::isTail()const{
    return current == tail;
}

char MyList::getElement(){
    if(current != NULL){
        return current -> element;
    }
    else{
        cout << "Error: Current is NULL" << endl;
        return '\0';
    }
}

void MyList::setElement(char c){
    if(current != NULL){
        current -> element = c;
    }
    else{
        cout << "Error: Current is NULL" << endl;
    }
}

void MyList::Insert(LinkNode *l){
   
}

void MyList::Delete(){
   
}

int main(){

    string input;
    while(getline(cin, input)){
        MyList list;
        MyList list2;
        MyList list3;
        cout << "Size: " << list.Size() << endl;
        list.Delete();
        list.next();
        list.prev();
        list.getElement();
        list.setElement('#');
        for(int i = 0; i < input.length(); i++){
            LinkNode *node = new LinkNode(input.at(i));
            list.Insert(node);
            list2.Insert(new LinkNode(input.at(i)));
            list3.Insert(new LinkNode(input.at(i)));
        }
       
        list2.toHead();
        for(int i = 0; i < input.length(); i++){
            list2.Delete();
        }
        list3.toTail();
        for(int i = 0; i < input.length(); i++){
            list3.Delete();
        }

        cout << "Size equal input length? " << (list.Size() == input.length()) << endl;
        list.toHead();
        if(list.isHead()){
            cout << "true" << endl;
        }
        for(list.toHead(); !list.isTail(); list.next()){
            cout << list.getElement();
        }
        cout << list.getElement() << endl;
        list.toTail();
        if(list.isTail()){
            cout << "true" << endl;
        }
        for(list.toTail(); !list.isHead(); list.prev()){
            cout << list.getElement();
        }
        cout << list.getElement() << endl;
        list.toHead();
        list.Delete();
        for(list.toHead(); !list.isTail(); list.next()){
            cout << list.getElement();
        }
        cout << list.getElement() << endl;
        list.toHead();
        list.next();
        list.next();
        list.Insert(new LinkNode('W'));
        list.Insert(new LinkNode('G'));
        for(list.toHead(); !list.isTail(); list.next()){
            cout << list.getElement();
        }
        cout << list.getElement() << endl;
        list.toHead();
        list.next();
        list.next();
        list.Delete();
        for(list.toHead(); !list.isTail(); list.next()){
            cout << list.getElement();
        }
        cout << list.getElement() << endl;
        list.toTail();
        list.Delete();
        for(list.toHead(); !list.isTail(); list.next()){
            cout << list.getElement();
        }
        cout << list.getElement() << endl;
        list.toTail();
        list.Insert(new LinkNode('P'));
        for(list.toHead(); !list.isTail(); list.next()){
            cout << list.getElement();
        }
        cout << list.getElement() << endl;
        list.toTail();
        list.Delete();
        for(list.toHead(); !list.isTail(); list.next()){
            cout << list.getElement();
        }
        cout << list.getElement() << endl;
        list.toHead();
        list.Delete();
        for(list.toHead(); !list.isTail(); list.next()){
            cout << list.getElement();
        }
        cout << list.getElement() << endl;
        list.toHead();
        list.next();
        list.next();
        list.next();
        list.prev();
        list.setElement('@');
        list.next();
        list.setElement('_');
        list.next();
        list.setElement('@');
        for(list.toHead(); !list.isTail(); list.next()){
            cout << list.getElement();
        }
        cout << list.getElement() << endl;
    }
   
    return 0;
}
 

Input :

Don't worry this.

Output :

Don't worry this.

Sample Input :

THISISIT
MICHAELJACKSON

Sample Output :

Size: 0
Error: Current is NULL
Error: Current is NULL
Error: Current is NULL
Error: Current is NULL
Size equal input length? 1
true
TTISISIH
true
HISISITT
TISISIH
TISGWISIH
TIGWISIH
TIGWISI
TIGWISIP
TIGWISI
IGWISI
IG@_@I
Size: 0
Error: Current is NULL
Error: Current is NULL
Error: Current is NULL
Error: Current is NULL
Size equal input length? 1
true
MNOSKCAJLEAHCI
true
ICHAELJACKSONM
NOSKCAJLEAHCI
NOSGWKCAJLEAHCI
NOGWKCAJLEAHCI
NOGWKCAJLEAHC
NOGWKCAJLEAHCP
NOGWKCAJLEAHC
OGWKCAJLEAHC
OG@_@AJLEAHC

Hint :


  

Author :


  Solve it!   Status Forum (0)

C++
C
JAVA
21360. oop98703042 (298 ms , 7674KB)
21384. oop98703032 (300 ms , 652KB)
21397. oop98703032 (302 ms , 652KB)
21394. oop97306037 (302 ms , 644KB)
21530. oop97703049 (304 ms , 654KB)
沒有解題記錄 沒有解題記錄

執行時間會受很多因素影響因此僅供參考,主機等級請看這裡