File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -165,9 +165,7 @@ public void RemoveL()
165165 else
166166 {
167167 Cell cCell = first ;
168- while (
169- cCell . Next . Next != null
170- )
168+ while ( cCell . Next . Next != null )
171169 {
172170 cCell = cCell . Next ;
173171 }
Original file line number Diff line number Diff line change @@ -152,11 +152,10 @@ static void Main(string[] args)
152152 myList3 . RemoveI ( 0 ) ;
153153 Console . WriteLine ( "After removing element at index 0:" ) ;
154154 Console . WriteLine ( myList3 ) ;
155- myList3 . AddL ( 11.5 ) ;
156- myList3 . AddF ( - 8.15 ) ;
157- Console . WriteLine ( myList3 ) ;
158- myList3 . RemoveL ( ) ;
159- Console . WriteLine ( myList3 ) ;
160-
161- }
155+ myList3 . AddL ( 11.5 ) ;
156+ myList3 . AddF ( - 8.15 ) ;
157+ Console . WriteLine ( myList3 ) ;
158+ myList3 . RemoveL ( ) ;
159+ Console . WriteLine ( myList3 ) ;
160+ }
162161}
Original file line number Diff line number Diff line change 66
77class CStack < T >
88{
9+ private class Cell
10+ {
11+ public T Data { get ; set ; }
12+ public Cell Next { get ; set ; }
913
10- private class Cell
14+ public Cell ( T data = default ( T ) , Cell node = null )
1115 {
12- public T Data { get ; set ; }
13- public Cell Next { get ; set ; }
14- public Cell ( T data = default ( T ) , Cell node = null )
15- {
16- Data = data ;
17- Next = node ;
18- }
16+ Data = data ;
17+ Next = node ;
1918 }
19+ }
2020
21- private Cell top ;
22-
23- public CStack ( )
24- {
25- top = null ;
26- }
27-
28- public void Clear ( )
29- {
30- top = null ;
31- }
32- public bool IsEmpty ( )
33- {
34- return top == null ;
35- }
36-
37- public void Push ( T value )
38- {
39- top = new Cell ( value , top ) ;
40- }
41-
42- public T Pop ( )
43- {
44- if ( IsEmpty ( ) )
45- throw new ApplicationException ( "An empty stack cannot be popped." ) ;
46- T removedData = top . Data ;
47- top = top . Next ;
48- return removedData ;
49- }
50-
51- public T Peek ( )
52- {
53- if ( IsEmpty ( ) )
54- throw new ApplicationException ( "An empty stack cannot be peeked." ) ;
55- return top . Data ;
56- }
57- public int Count
21+ private Cell top ;
22+
23+ public CStack ( )
24+ {
25+ top = null ;
26+ }
27+
28+ public void Clear ( )
29+ {
30+ top = null ;
31+ }
32+
33+ public bool IsEmpty ( )
34+ {
35+ return top == null ;
36+ }
37+
38+ public void Push ( T value )
39+ {
40+ top = new Cell ( value , top ) ;
41+ }
42+
43+ public T Pop ( )
44+ {
45+ if ( IsEmpty ( ) )
46+ throw new ApplicationException (
47+ "An empty stack cannot be popped."
48+ ) ;
49+ T removedData = top . Data ;
50+ top = top . Next ;
51+ return removedData ;
52+ }
53+
54+ public T Peek ( )
55+ {
56+ if ( IsEmpty ( ) )
57+ throw new ApplicationException (
58+ "An empty stack cannot be peeked."
59+ ) ;
60+ return top . Data ;
61+ }
62+
63+ public int Count
64+ {
65+ get
5866 {
59- get
60- {
61- int count = 0 ;
62- Cell cCell = top ;
63- while ( cCell != null )
64- {
65- count ++ ;
66- cCell = cCell . Next ;
67- }
68- return count ;
69- }
67+ int count = 0 ;
68+ Cell cCell = top ;
69+ while ( cCell != null )
70+ {
71+ count ++ ;
72+ cCell = cCell . Next ;
73+ }
74+ return count ;
7075 }
76+ }
7177
72- public override string ToString ( )
78+ public override string ToString ( )
79+ {
80+ string returned = "" ;
81+ if ( ! IsEmpty ( ) )
7382 {
74- string returned = "" ;
75- if ( ! IsEmpty ( ) )
76- {
77- Cell cCell = top ;
78- while ( cCell != null )
79- {
80- if ( returned . Length > 0 )
81- returned += ":" ;
82- returned += cCell . Data ;
83- cCell = cCell . Next ;
84- }
85- }
86- return returned ;
83+ Cell cCell = top ;
84+ while ( cCell != null )
85+ {
86+ if ( returned . Length > 0 )
87+ returned += ":" ;
88+ returned += cCell . Data ;
89+ cCell = cCell . Next ;
90+ }
8791 }
92+ return returned ;
93+ }
8894}
Original file line number Diff line number Diff line change 22
33class Program
44{
5- static void Main ( string [ ] args )
6- {
7- /* First example. */
8- CStack < int > myStack1 = new CStack < int > ( ) ;
9- Console . WriteLine ( myStack1 ) ;
10- myStack1 . Push ( 1 ) ;
11- myStack1 . Push ( 5 ) ;
12- myStack1 . Push ( 2 ) ;
13- myStack1 . Push ( 2 ) ;
14- myStack1 . Push ( 1 ) ;
15- myStack1 . Push ( 1 ) ;
16- myStack1 . Push ( 3 ) ;
17- myStack1 . Push ( 4 ) ;
18- myStack1 . Push ( 5 ) ;
19- myStack1 . Push ( 5 ) ;
20- myStack1 . Push ( 5 ) ;
21- myStack1 . Push ( 2 ) ;
22- myStack1 . Push ( 2 ) ;
23- Console . WriteLine ( myStack1 ) ;
24-
25- /* Second example. */
26- CStack < char > myStack2 = new CStack < char > ( ) ;
27- try
28- {
29- myStack2 . Pop ( ) ;
30- }
31- catch ( Exception ex )
32- {
33- Console . WriteLine (
34- ex . Message
35- ) ;
36- }
37- try
38- {
39- myStack2 . Peek ( ) ;
40- }
41- catch ( Exception ex )
42- {
43- Console . WriteLine (
44- ex . Message
45- ) ;
46- }
5+ static void Main ( string [ ] args )
6+ {
7+ /* First example. */
8+ CStack < int > myStack1 = new CStack < int > ( ) ;
9+ Console . WriteLine ( myStack1 ) ;
10+ myStack1 . Push ( 1 ) ;
11+ myStack1 . Push ( 5 ) ;
12+ myStack1 . Push ( 2 ) ;
13+ myStack1 . Push ( 2 ) ;
14+ myStack1 . Push ( 1 ) ;
15+ myStack1 . Push ( 1 ) ;
16+ myStack1 . Push ( 3 ) ;
17+ myStack1 . Push ( 4 ) ;
18+ myStack1 . Push ( 5 ) ;
19+ myStack1 . Push ( 5 ) ;
20+ myStack1 . Push ( 5 ) ;
21+ myStack1 . Push ( 2 ) ;
22+ myStack1 . Push ( 2 ) ;
23+ Console . WriteLine ( myStack1 ) ;
4724
48- myStack2 . Push ( 'a' ) ;
49- myStack2 . Push ( 'z' ) ;
50- Console . WriteLine ( "Value at top of stack: " + myStack2 . Peek ( ) ) ;
25+ /* Second example. */
26+ CStack < char > myStack2 = new CStack < char > ( ) ;
27+ try
28+ {
29+ myStack2 . Pop ( ) ;
30+ }
31+ catch ( Exception ex )
32+ {
33+ Console . WriteLine ( ex . Message ) ;
5134 }
52- }
35+ try
36+ {
37+ myStack2 . Peek ( ) ;
38+ }
39+ catch ( Exception ex )
40+ {
41+ Console . WriteLine ( ex . Message ) ;
42+ }
43+
44+ myStack2 . Push ( 'a' ) ;
45+ myStack2 . Push ( 'z' ) ;
46+ Console . WriteLine (
47+ "Value at top of stack: " + myStack2 . Peek ( )
48+ ) ;
49+ }
50+ }
You can’t perform that action at this time.
0 commit comments