【資料結構】回顧表、棧、佇列

nomasp發表於2015-05-09

1.如何通過調整鏈而不是資料來交換兩個相鄰的元素?

    // 單向連結串列
    Node *p,*afterp;
    p=beforep->next;
    afterp=p->next;

    p->next=afterp->next;
    beforep->next=afterp;
    afterp->next=p;

    // 雙向連結串列
    Node *beforep,*afterp;
    beforep=p->prev;
    afterp=p->next;

    p->next=afterp->next;
    beforep->next=afterp;
    afterp->next=p;
    p->next->prev=p;
    p->prev=afterp;
    afterp->prev=beforep;

2.如何求出兩個已排序的表L1和L2的交集和並集。

// 交集
template <typename Object>
list<Object> intersection( const list<Object> & L1,
const list<Object> & L2)
{
    list<Object> intersect;
    typename list<Object>:: const_iterator iterL1 = L1.begin();
    typename list<Object>:: const_iterator iterL2 = L2.begin();
    while(iterL1 != L1.end() && iterL2 != L2.end())
    {
        if (*iterL1 == *iterL2)
        {
            intersect.push_back(*iterL1);
            iterL1++;
            iterL2++;
        }
        else if (*iterL1 < *iterL2)
            iterL1++;
        else
            iterL2++;
    }
    return intersect;
}
// 並集
// Assumes both input lists are sorted
template <typename Object>
list<Object> listUnion( const list<Object> & L1,
const list<Object> & L2)
{
    list<Object> result;
    typename list<Object>:: const_iterator iterL1 = L1.begin();
    typename list<Object>:: const_iterator iterL2= L2.begin();
    while(iterL1 != L1.end() && iterL2 != L2.end())
    {
        if (*iterL1 == *iterL2)
        {
            result.push_back(*iterL1);
            iterL1++;
            iterL2++;
        }
        else if (*iterL1 < *iterL2)
        {
            result.push_back(*iterL1);
            iterL1++;
        }
        else
        {
            result.push_back(*iterL2);
            iterL2++;
        }
    }
    return result;
}

3.一個有表頭結點,沒有尾結點,還有一個指向表頭結點的指標的單向連結串列,寫一個類包括以下函式:
返回連結串列的大小,
列印連結串列,
檢測值x是否在連結串列中,
如果x不在連結串列中則加入連結串列,
如果x在連結串列中則刪除它。

template <typename Object>
struct Node
{
    Object data;
    Node *next;
    Node (const Object & d = Object(),Node *n=NULL)
        :data(d),next(n){}
};

template <typename Object>
class singleList
{
public:
    singleList(){init();}
    ~singleList()
    {
        eraseList(head);
    }

    singleList(const singleList & rhs)
    {
        eraseList(head);
        init();
        *this=rhs;
    }

    bool add(Object x)
    {
        if(contains(x))
        {
            return false;
        }
        else
        {
            Node<Object> *ptr =new Node<Object>(x);
            ptr->next=head->next;
            head->next=ptr;
            theSize++;
        }
        return true;
    }

    bool remove(Object x)
    {
        if(!contains(x))
            return false;
        else 
        {
            Node<Object>*ptr=head->next;
            Node<Object>*trailer;
            while(ptr->data!=x)
            {
                trailer=ptr;
                ptr=ptr->next;
            }
            trailer->next=ptr->next;
            delete ptr;
            theSize--;
        }
        return true;
    }

    int size()
    {
        return theSize;
    }

    void print()
    {
        Node<Object> *ptr=head->next;
        while(ptr!=NULL)
        {
            count<<ptr->data<<" ";
            ptr=ptr->next;
        }
        count<<endl;
    }

    bool contains(const Object & x)
    {
        Node<Object> * ptr=head->next;
        while(ptr!=NULL)
        {
            if(x==ptr->data)
                return true;
            else 
                ptr=ptr->next;
        }
        return false;
    }

    void init()
    {
        theSize=0;
        head=new Node<Object>;
        head->next=NULL;
    }

    void eraseList(Node<Object> * h)
    {
        Node<Object> *ptr=h;
        Node<Object> *nextPtr;
        while(ptr!=NULL)
        {
            nextPtr=ptr->next;
            delete ptr;
            ptr=nextPtr;
        }
    }

private:
    Node<Object> *head;
    int theSize;
};

4.如何給List迭代器類新增對operator–的支援。

const_iterator & operator-- ()
{
    current=current->prev;
    return *this;
}

const_iterator operator-- (int)
{
    const_iterator old=*this;
    --(*this);
    return old;
}

5.前面談到過的雙端佇列(deque),給它新增以下功能:

函式 備註
push(x) 將項x插入到雙端佇列的前段
pop() 從雙端佇列刪除前段項並返回
inject(x) 將項x插入到雙端佇列的尾端
eject() 從雙端佇列中刪除尾端項並將其返回
template <typename Object>
class deque
{
public:
    deque(){ l();}
    void push (Object obj) {l.push_front(obj);}
    Object pop () {Object obj=l.front();pop_front();return obj;}
    void inject (Object obj) {l.push_back(obj);}
    Object eject() {pop_back(obj);}
private:
    list<Object> l;
}

6.不包含表頭結點和尾結點,用單向連結串列高效地實現棧類。

template<typename Object>
struct node
{
    node() {next=NULL;}
    node(Object obj):data(obj){}
    node(Object obj,node * ptr):data(obj),next(ptr){}
    Object data;
    node *next;
};

template<typename Object>
class stack
{
public:
    stack(){head=NULL;}
    ~stack(){while(head) pop();}
    void push(Object obj)
    {
        node<object> *ptr=new node<Object>(obj,head);
        head=ptr;
    }
    Object top()
    {
        return (head->data);
    }
    void pop()
    {
        node<Object> *ptr=head->next;
        delete head;
        head=ptr;
    }
private:
    node<Object> *head;
}

7.不包含表頭結點和尾結點,用單向連結串列高效地實現佇列類。

template<typename Object>
class queue
{
public:
    queue(){front=NULL;rear=NULL;}
    ~queue(){while(front)deque();}
    void deque(Object obj)
    {
        node<Object> *ptr=new node<Object>(obj,NULL);
        if(rear)
            rear=rear->next=ptr;
        else
            front=rear=ptr;
    }
    Object deque()
    {
        Object temp=front->data;
        node<Object> *ptr=front;
        if(front->next==NULL)
            front=rear=NULL;
        else
            front=front->next;
        delete ptr;
        return temp;
    }
private:
    node<Object> *front;
    node<Object> *rear;
}

8.使用由vector作為基本的資料結構的迴圈陣列高效地實現佇列類。

template<typename Object>
class queue
{
public:
    queue(int s):maxSize(s),front(0),rear(0){elements.resize(maxSize);}
    queue(){maxSize=100;front=0;rear=0;elements.resize(maxSize);}
    ~queue(){while(front!=rear) deque();}
    void enque(Object obj)
    {
        if(!full())
        {
            elements[rear]=obj;
            rear=(rear+1)%maxSize;
        }
    }
    Object deque()
    {
        Object temp;
        if(!empty())
        {
            temp=elements[front];
            front=(front+1)%maxSize;
            return temp;
        }
    }
    bool empty(){return front==rear;}
    bool full(){return (rear+1)%maxSize==front;}
private:
    int front,rear;
    int maxSize;
    vector<Object> elements;
}



感謝您的訪問,希望對您有所幫助。

歡迎大家關注或收藏、評論或點贊。


為使本文得到斧正和提問,轉載請註明出處:
http://blog.csdn.net/nomasp


相關文章