posts - 297,  comments - 15,  trackbacks - 0
#include <iostream>

using namespace std;

//single link
struct slink_t{
    struct slink_t* next;
    int data;
    slink_t(): next(0), data(0){
    }
    slink_t(slink_t* p, int d): next(p), data(d){
    }
};

/*output data of single link */
ostream& operator<< (ostream& os, const struct slink_t* p)
{
    cout << "Output : ";
    while (p && cout << p->data << ' ')
        p = p->next;
    return os;
}

/* reverse the single link */
struct slink_t* slink_reverse(struct slink_t* p)
{
    struct slink_t* t = NULL, *tp = NULL;
    while (p)
    {
        tp = t;
        t = p;
        p = p->next;
        t->next = tp;
    }
    return t;
}

//double link
struct dlink_t{
    struct dlink_t* next;
    struct dlink_t* prev;
    int data;
    dlink_t(): next(0), prev(0), data(0){
    }
    dlink_t(dlink_t* n, dlink_t* p, int d): next(n), prev(p), data(d){
    }
};

/* output data of double link */
ostream& operator<< (ostream& os, const struct dlink_t* p)
{
    cout << "Output : ";
    while (p && cout << p->data << ' ')
        p = p->next;
    return os;
}

/* reverse the double link */
struct dlink_t* dlink_reverse(struct dlink_t* p)
{
    struct dlink_t* t = NULL;
    while (p)
    {
        t = p;
        p = p->next;
        t->next = t->prev;
        t->prev = p;
    }
    return t;
}

#define TEST__

#ifdef TEST__
/* test */
int main()
{
    struct slink_t* sl = new slink_t(new slink_t(new slink_t(new slink_t(new slink_t(new slink_t(NULL, 6), 5), 4), 3), 2), 1);
    cout << sl << endl;
    sl = slink_reverse(sl);
    cout << sl << endl;
    struct dlink_t* dl = NULL;
    struct dlink_t* h = new dlink_t(NULL, dl, 1);
    dl = h;
    for (int i = 2; i < 10; i++)
    {
        struct dlink_t* t = new dlink_t(NULL, dl, i);
        dl->next = t;
        dl = t;
//        dl->prev->next = dl;
    }
    cout << h << endl;
    h = dlink_reverse(h);
    cout << h << endl;
    cout << "Hello world" << endl;
    return 0;
}
#endif
转自:
http://blog.chinaunix.net/u3/101003/showart.php?id=2095827
posted on 2009-11-21 18:41 chatler 阅读(328) 评论(0)  编辑 收藏 引用 所属分类: Algorithm

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2008年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

常用链接

留言簿(10)

随笔分类(307)

随笔档案(297)

algorithm

Books_Free_Online

C++

database

Linux

Linux shell

linux socket

misce

  • cloudward
  • 感觉这个博客还是不错,虽然做的东西和我不大相关,觉得看看还是有好处的

network

OSS

  • Google Android
  • Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
  • os161 file list

overall

搜索

  •  

最新评论

阅读排行榜

评论排行榜