文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>c++双向链表

c++双向链表

时间:2010-04-04  来源:@sky

#include <iostream>

#include <wchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <time.h>

using namespace std;

class SinoSession {
        public:
                SinoSession();
                ~SinoSession();

                SinoSession *prev;
                SinoSession *next;

                int get_value() { return this->value; }

        private:
                int value;
};

SinoSession::SinoSession()
{
        prev = next = NULL;
        value = 1024;
}

SinoSession::~SinoSession()
{
}

class SinoSessionHead {
        public:
                SinoSessionHead();
                ~SinoSessionHead();

                int session_queue_tail(SinoSession *session);
                void __session_queue_before(SinoSessionHead *h, SinoSession *next, SinoSession *news);
                void __session_insert(SinoSession *news, SinoSession *prev,
                                        SinoSession *next, SinoSessionHead *h);
                SinoSession * session_dequeue(SinoSessionHead *h);
                SinoSession * session_peek(SinoSessionHead *h);
                void __session_unlink(SinoSession *s, SinoSessionHead *h);

                SinoSession *prev;
                SinoSession *next;
};

SinoSessionHead::SinoSessionHead()
{
        prev = (SinoSession *)this;
        next = (SinoSession *)this;
}

SinoSessionHead::~SinoSessionHead()
{
}

int SinoSessionHead::session_queue_tail(SinoSession *session)
{
        __session_queue_before(this, (SinoSession *)this, session);
}

void SinoSessionHead::__session_queue_before(SinoSessionHead *h, SinoSession *next, SinoSession *news)
{
        __session_insert(news, next->prev, next, h);
}

void SinoSessionHead::__session_insert(SinoSession *news,
                                SinoSession *prev, SinoSession *next,
                                SinoSessionHead *h)
{
        news->next = next;
        news->prev = prev;
        next->prev = prev->next = news;
}

SinoSession * SinoSessionHead::session_dequeue(SinoSessionHead *h)
{
        SinoSession *s = session_peek(h);
        if (s)
                __session_unlink(s, h);

        return s;
}

SinoSession * SinoSessionHead::session_peek(SinoSessionHead *list_)
{
        SinoSession *list = ((SinoSession *)list_)->next;
        if (list == (SinoSession *)list_)
                list = NULL;
        return list;

}

void SinoSessionHead::__session_unlink(SinoSession *skb, SinoSessionHead *list)
{
        SinoSession *next, *prev;

        next = skb->next;
        prev = skb->prev;
        skb->next = skb->prev = NULL;
        next->prev = prev;
        prev->next = next;

}

int main()
{
        SinoSessionHead head;

        SinoSession *session = new SinoSession;

        head.session_queue_tail(session);

        SinoSession *s = head.session_dequeue(&head);

        cout << s->get_value() << endl;
        delete s;

        return 0;
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载