[cpp][data_structure]: queue -- user defined( bug )
一、示意
二、源代码中存在的问题
1、 有问题;在pop()中front指针设置有问题。
三、源码
1 #include <iostream>
2 #include <string>
3
4
5 struct unit
6 {
7 // functions
8 unit(std::string s) { data = s; }
9
10 // data
11 unit* front = NULL;
12 unit* back = NULL;
13 std::string data = "";
14 };
15
16
17 class queue
18 {
19 public:
20
21 queue()
22 {
23 top = new unit( sinit ); // first object of unit, named "first_object", it has a vaule of "sinit"
24 init = top ;
25 bottom = top ;
26 size++ ;
27 }
28
29 ~queue()
30 {
31 std::cout << "\n[queue_class]#\t~queue()" << std::endl;
32 }
33
34 void push(std::string data)
35 {
36 unit* keep = bottom;
37
38 bottom = new unit( data );
39 bottom->front = keep;
40 keep->back = bottom;
41
42 size++ ;
43 //std::cout << "[queue:c_push:f]#\t" << data << std::endl;
44 }
45
46 void pop()
47 {
48 unit* keep = top;
49
50 if ( size == -1 )
51 {
52 this -> ~queue(); // destroy the current object by ' this '; very funny;
53 std::cout << "\nERROR[queue:c_pop:f]#\tqueue out of range. \n" << std::endl ;
54
55 exit(1);
56 }
57 else
58 {
59 if ( keep->data == sinit )
60 {
61 top = top->back ;
62 unit* keep2 = top;
63 size-- ;
64 delete keep;
65
66 keep = keep2 ;
67 }
68
69 std::cout << "[queue:c_pop:f]#\t" << keep->data << std::endl;
70
71 top = top->back ;
72 size-- ;
73 delete keep;
74 }
75 }
76
77 int getsize()
78 {
79 return size;
80 }
81
82 void out()
83 {
84 int i = 0;
85 for( unit* pt = top; i <= size; pt = pt->back )
86 {
87 if ( pt->data == sinit )
88 {
89 i++ ;
90 continue;
91 }
92 std::cout << "[queue:c_out:f]#\tdata(" << i++ << "/" << size << ")\t:=\t" << pt->data << std::endl;
93 }
94 }
95
96 private:
97
98 // data
99 const std::string sinit = "initial queue"; // initialize first object of unit
100 int size = -1;
101 unit* init = NULL;
102 unit* top = NULL;
103 unit* bottom = NULL;
104 };
105
106
107 // test part
108 int main()
109 {
110 queue q;
111 q.push("tangxuanzang");
112 q.push("sunwukong");
113 q.push("zhuwuneng");
114 q.push("shawujing");
115
116 q.out();
117
118 q.pop(); // pop "tangxuanzang" ;
119 //q.out();
120
121 q.pop(); // pop "sunwukong" ;
122 //q.out();
123
124 q.pop(); // pop "zhuwuneng" ;
125 q.out();
126
127 q.pop(); // pop "shawujing" ;
128 //q.out();
129
130 q.pop(); // out of range of queue ;
131 //q.out();
132
133 return 0;
134 }
四、运行结果
1 g++ -std=c++20 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
2
3
4 [queue:c_out:f]# data(1/4) := tangxuanzang
5 [queue:c_out:f]# data(2/4) := sunwukong
6 [queue:c_out:f]# data(3/4) := zhuwuneng
7 [queue:c_out:f]# data(4/4) := shawujing
8 [queue:c_pop:f]# tangxuanzang
9 [queue:c_pop:f]# sunwukong
10 [queue:c_pop:f]# zhuwuneng
11 [queue:c_out:f]# data(0/0) := shawujing
12 [queue:c_pop:f]# shawujing
13
14 [queue_class]# ~queue()
15
16 ERROR[queue:c_pop:f]# queue out of range.
五、参考资料
1、 数据结构与算法 -- https://www.runoob.com/data-structures/data-structures-tutorial.html
2、 cpp在线工具 -- https://coliru.stacked-crooked.com/
本文由 lnlidawei 原创、整理、转载,本文来自于【博客园】; 整理和转载的文章的版权归属于【原创作者】; 转载或引用时请【保留文章的来源信息】:https://www.cnblogs.com/lnlidawei/p/17979014