첫 번째 문제: 앞장서지 않는 결점을 정하는 단일 체인 테이블, 체인 테이블을 거꾸로 놓는 방법을 적는다.

1840 단어

 
#include
using namespace std;
template
class LinkNode{
public:
    T data;
    LinkNode *pNext;
    LinkNode(T data = 0){
        this->data = data;
        this->pNext = NULL;
    }
};
template
class Link{
private:
    LinkNode *pHead;
    LinkNode *pTail;
    int size;
public:
    Link(int size = 0){
        this->pHead = NULL;
        this->pTail = NULL;
        this->size = 0;
    }
    void push(T data){
        LinkNode *x = new LinkNode(data);
        if(pTail == NULL)
            pHead = pTail = x;
        else
        {
            pTail->pNext = x;
            pTail = x;
        }
        size += 1;
    }
    void show(){
        LinkNode *p = this->pHead;
        for(;p!=pTail;p = p->pNext){
            cout << p->data << "    ";
        }
        cout << pTail->data;
        cout << endl;
    }
    void invert();
};

template
void Link :: invert(){
    LinkNode *pNewHead = pTail;
    LinkNode *p = pHead;
    while(pTail != pHead)
    {
        for(p = pHead;p->pNext != pTail;p = p->pNext);
        pTail->pNext = p;
        pTail=p;
        cout << pTail ->data ;
    }
    pHead = pNewHead;
}
int main(){
    LinkNode *p = new LinkNode('p');
    Link *li = new Link;
    cout << " ( 0 ):";
    char c;
    cin >> c;
    while(c != '0'){
        li-> push(c);
        cin >> c;
    }
    cout << " :" << endl;
    li->show();
    li->invert();
    cout << " :" << endl;
    li->show();
return 0;
}

 

좋은 웹페이지 즐겨찾기