@@ -16,13 +16,11 @@ class StackQueue<Item>
1616 Stack < Item > Hr ;
1717
1818 bool isRecopying ;
19- int lendiff ;
2019 int nowcopying ;
2120
2221 public StackQueue ( )
2322 {
2423 this . isRecopying = false ;
25- this . lendiff = 0 ;
2624 this . nowcopying = 0 ;
2725
2826 this . H = new Stack < Item > ( ) ;
@@ -47,35 +45,37 @@ public Item Peek()
4745
4846 public void Enqueue ( Item item )
4947 {
50- if ( ! this . isRecopying && this . lendiff > 0 )
48+ if ( ! this . isRecopying && Lendiff ( ) > 0 )
5149 {
52- this . lendiff -- ;
5350 this . nowcopying = 0 ;
5451 this . T . Push ( item ) ;
5552 }
56- else if ( ! this . isRecopying && this . lendiff == 0 )
53+ else if ( ! this . isRecopying && Lendiff ( ) == 0 )
5754 {
5855 this . T . Push ( item ) ;
5956 this . isRecopying = true ;
60- this . HH = this . H . Copy ( ) ;
57+ this . h = this . H . Copy ( ) ;
6158 OneStep ( OneStep ( this ) ) ;
6259 }
6360 else if ( this . isRecopying )
6461 {
65- this . lendiff -- ;
6662 this . TT . Push ( item ) ;
6763 OneStep ( OneStep ( this ) ) ;
6864 }
6965 }
7066
67+ public int Lendiff ( )
68+ {
69+ return this . H . Size ( ) - this . T . Size ( ) ;
70+ }
71+
7172 public Item Dequeue ( )
7273 {
73- if ( ! this . isRecopying && this . lendiff > 0 )
74+ if ( ! this . isRecopying && Lendiff ( ) > 0 )
7475 {
75- this . lendiff -- ;
7676 return this . H . Pop ( ) ;
7777 }
78- else if ( ! this . isRecopying && this . lendiff == 0 )
78+ else if ( ! this . isRecopying && Lendiff ( ) == 0 )
7979 {
8080 Item temp = this . H . Pop ( ) ;
8181 this . HH = this . H ;
@@ -86,7 +86,7 @@ public Item Dequeue()
8686 else
8787 {
8888 Item temp = this . h . Pop ( ) ;
89- this . lendiff -- ;
89+ this . nowcopying -- ;
9090 OneStep ( OneStep ( this ) ) ;
9191 return temp ;
9292 }
@@ -96,31 +96,28 @@ private static StackQueue<Item> OneStep(StackQueue<Item> q)
9696 {
9797 if ( q . isRecopying && ! q . H . IsEmpty ( ) && ! q . T . IsEmpty ( ) )
9898 {
99- q . lendiff ++ ;
10099 q . nowcopying ++ ;
101100 q . HH . Push ( q . T . Pop ( ) ) ;
102101 q . Hr . Push ( q . H . Pop ( ) ) ;
103102 }
104103 else if ( q . isRecopying && q . H . IsEmpty ( ) && ! q . T . IsEmpty ( ) )
105104 {
106105 q . isRecopying = true ;
107- q . lendiff ++ ;
108106 q . HH . Push ( q . T . Pop ( ) ) ;
109107 }
110108 else if ( q . isRecopying && q . H . IsEmpty ( ) && q . T . IsEmpty ( ) && q . nowcopying > 1 )
111109 {
112110 q . isRecopying = true ;
113- q . lendiff ++ ;
114111 q . nowcopying -- ;
115112 q . HH . Push ( q . Hr . Pop ( ) ) ;
116113 }
117114 else if ( q . isRecopying && q . H . IsEmpty ( ) && q . T . IsEmpty ( ) && q . nowcopying == 1 )
118115 {
119116 q . isRecopying = false ;
120- q . lendiff ++ ;
121117 q . nowcopying -- ;
122118 q . HH . Push ( q . Hr . Pop ( ) ) ;
123119 q . H = q . HH ;
120+ q . T = q . TT ;
124121 q . HH = new Stack < Item > ( ) ;
125122 q . TT = new Stack < Item > ( ) ;
126123 q . Hr = new Stack < Item > ( ) ;
@@ -129,7 +126,6 @@ private static StackQueue<Item> OneStep(StackQueue<Item> q)
129126 else if ( q . isRecopying && q . H . IsEmpty ( ) && q . T . IsEmpty ( ) && q . nowcopying == 0 )
130127 {
131128 q . isRecopying = false ;
132- q . lendiff ++ ;
133129 q . H = q . HH ;
134130 q . T = q . TT ;
135131 q . HH = new Stack < Item > ( ) ;
0 commit comments