#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;
}
|