博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
双向循环链表
阅读量:5840 次
发布时间:2019-06-18

本文共 4479 字,大约阅读时间需要 14 分钟。

1:  /*
2:  @@
3:  Author: justinzhang
4:  Email:  uestczhangchao@gmail.com
5:  Time:   2012-9-1 20:37:37
6:  desc:   double list related code
7:  */
8:  #include 
9:  #include 
10:  #include 
11:  using namespace std;
12:   
13:  template 
14:  class doublelist
15:  {
16:  public:
17:      static const int TERMINAL = 111;
18:      doublelist
* get_next()
19:      {
20:          return next;
21:      }
22:      void set_next(doublelist
* ne)
23:      {
24:          this->next = ne;
25:      }
26:   
27:      doublelist
* get_prev()
28:      {
29:          return prev;
30:      }
31:      void set_prev(doublelist
* prev)
32:      {
33:          this->prev = prev;
34:      }
35:   
36:      type get_data()
37:      {
38:          return data;
39:      }
40:      void set_data(const type &data)
41:      {
42:          this->data = data;
43:      }
44:      doublelist
* create_doublelist();
45:      void show_doublelist(doublelist
*phead);
46:      doublelist
* create_doublelist_with_vector(vector
data);
47:  private:
48:      doublelist
* next;
49:      doublelist
* prev;
50:      type data;
51:  };
52:   
53:  template
doublelist
* doublelist
:: create_doublelist()
54:  {
55:      doublelist
*phead = new doublelist
();
56:      assert(phead);
57:      phead->next = phead;
58:      phead->prev = phead;
59:      phead->data = TERMINAL;
60:      doublelist
*preal_head = phead;
61:   
62:      type tmp_data;
63:      doublelist
*node = NULL;
64:      cout << "input list data, end with " << TERMINAL << "!" << endl;
65:      while(1)
66:      {
67:          cin>> tmp_data;
68:          if(tmp_data == TERMINAL)
69:              break;
70:          node = new doublelist
();
71:          assert(node);
72:          node->data = tmp_data;
73:          phead->next = node;
74:          node->prev = phead;
75:          node->next = NULL;
76:          phead = node;
77:      }
78:      /* the following two statements make the list become circular link list*/
79:      node->next = preal_head;
80:      preal_head->prev = node;
81:      return preal_head;
82:  }
83:   
84:  template 
void doublelist
::show_doublelist(doublelist
*phead)
85:  {
86:      doublelist
* cur = phead;
87:      /* if the cur pointer points to the head pointer, it means the end of the list;
88:         you can draw a draft graph to help you understatnd it;
89:      */
90:      while(cur->next!=phead)
91:      {
92:          cur = cur->next;
93:          cout << cur->data << endl;
94:      }
95:  }
96:   
97:  template 
doublelist
* doublelist
::create_doublelist_with_vector(vector
data)
98:  {
99:      int i = 0;
100:      doublelist
*ptmp_head = new doublelist
();
101:      assert(ptmp_head);
102:      ptmp_head->next = ptmp_head;
103:      ptmp_head->prev = ptmp_head;
104:      doublelist
*phead = ptmp_head;
105:      doublelist
*node = NULL;
106:      for(i = 0; i < data.size(); i++)
107:      {
108:          node = new doublelist
();
109:          assert(node);
110:          node->data = data[i];
111:          ptmp_head->next = node;
112:          node->prev = ptmp_head;
113:          ptmp_head = node;
114:          node->next = NULL;
115:      }
116:   
117:      /* make it become circular doble link list */
118:      phead->prev = node;
119:      node->next = phead;
120:      return phead;
121:  }
122:   
123:  int main()
124:  {
125:      doublelist
dlist;
126:      //doublelist
*dl1 = dlist.create_doublelist();
127:      //dlist.show_doublelist(dl1);
128:   
129:      /* test create_doublelist_with_vector */
130:      doublelist
int_dlist;
131:      vector
vec;
132:      for(int i=0; i<10; i++)
133:          vec.push_back(i+1);
134:      doublelist
* pint_dlist = int_dlist.create_doublelist_with_vector(vec);
135:      int_dlist.show_doublelist(pint_dlist);
136:   
137:      return 0;
138:  }

转载地址:http://mpvcx.baihongyu.com/

你可能感兴趣的文章
Ubuntu解压
查看>>
爬虫_房多多(设置随机数反爬)
查看>>
藏地密码
查看>>
爬虫去重(只是讲了去重的策略,没有具体讲实现过程,反正就是云里雾里)...
查看>>
react中将px转化为rem或者vw
查看>>
8816
查看>>
avcodec_open2()分析
查看>>
何如获取单选框中某一个选中的值
查看>>
paip.输入法编程----删除双字词简拼
查看>>
QQ悬浮返回顶部
查看>>
MySQL建表语句的一些特殊字段
查看>>
《Unix环境高级编程》读书笔记 第8章-进程控制
查看>>
腾讯前端二面题目详解
查看>>
mascara-1
查看>>
Jquery Form表单取值
查看>>
Android API level 与version对应关系
查看>>
Team Name
查看>>
String类
查看>>
西门子_TDC_数据耦合小经验
查看>>
接口测试与postman
查看>>