



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Data structure introduction and motivation, Object references, Recursive data structure listnode, References of links, Linked list a list of listnodes, Examples operations enumerations, Examples operators insertatfront, Examples operations removefromfront, Examples operations insertatback, Insertatback with list node, Graphical representations of insertatback, Graphical representations of removefromfront, Graphical representations of insertatfront
Typology: Slides
1 / 5
This page cannot be seen from the preview
Don't miss anything!




All rights reserved.
Outline
68 // construct empty List with "list" as its name 69 public List() : this( "list" ) 70 { 71 } 72 73 // Insert object at front of List. If List is empty, 74 // firstNode and lastNode will refer to same object. 75 // Otherwise, firstNode refers to new node. 76 public void InsertAtFront( object insertItem ) 77 { 78 lock ( this ) 79 { 80 if ( IsEmpty() ) 81 firstNode = lastNode = 82 new ListNode( insertItem ); 83 else 84 firstNode = 85 new ListNode( insertItem, firstNode ); 86 } 87 } 88 89 // Insert object at end of List. If List is empty, 90 // firstNode and lastNode will refer to same object. 91 // Otherwise, lastNode's Next property refers to new node. 92 public void InsertAtBack( object insertItem ) 93 { 94 lock ( this ) 95 { 96 if ( IsEmpty() ) 97 firstNode = lastNode = 98 new ListNode( insertItem ); 99
All rights reserved.
Outline
100 else 101 lastNode = lastNode.Next = 102 new ListNode( insertItem ); 103 } 104 } 105 106 // remove first node from List 107 public object RemoveFromFront() 108 { 109 lock ( this ) 110 { 111 object removeItem = null; 112 113 if ( IsEmpty() ) 114 throw new EmptyListException( name ); 115 116 removeItem = firstNode.Data; // retrieve data 117 118 // reset firstNode and lastNode references 119 if ( firstNode == lastNode ) 120 firstNode = lastNode = null; 121 122 else 123 firstNode = firstNode.Next; 124 125 return removeItem; // return removed data 126 } 127 } 128 129 // remove last node from List 130 public object RemoveFromBack() 131 { 132 lock ( this ) 133 { 134 object removeItem = null;
All rights reserved.
Outline
136 if ( IsEmpty() ) 137 throw new EmptyListException( name ); 138 139 removeItem = lastNode.Data; // retrieve data 140 141 // reset firstNode and lastNode references 142 if ( firstNode == lastNode ) 143 firstNode = lastNode = null; 144 145 else 146 { 147 ListNode current = firstNode; 148 149 // loop while current node is not lastNode 150 while ( current.Next != lastNode ) 151 current = current.Next; // move to next node 152 153 // current is new lastNode 154 lastNode = current; 155 current.Next = null; 156 } 157 158 return removeItem; // return removed data 159 } 160 } 161 162 // return true if List is empty 163 public bool IsEmpty() 164 { 165 lock ( this ) 166 { 167 return firstNode == null; 168 } 169 }
All rights reserved.
Outline
171 // output List contents 172 virtual public void Print() 173 { 174 lock ( this ) 175 { 176 if ( IsEmpty() ) 177 { 178 Console.WriteLine( "Empty " + name ); 179 return; 180 } 181 182 Console.Write( "The " + name + " is: " ); 183 184 ListNode current = firstNode; 185 186 // output current node data while not at end of list 187 while ( current != null ) 188 { 189 Console.Write( current.Data + " " ); 190 current = current.Next; 191 } 192 193 Console.WriteLine( "\n" ); 194 } 195 } 196 197 } // end class List 198
All rights reserved.
Outline
199 // class EmptyListException definition 200 public class EmptyListException : ApplicationException 201 { 202 public EmptyListException( string name ) 203 : base( "The " + name + " is empty" ) 204 { 205 } 206 207 } // end class EmptyListException 208 209 } // end namespace LinkedListLibrary
All rights reserved.
Outline
1 // Fig 23.5: ListTest.cs 2 // Testing class List. 3 4 using System; 5 using LinkedListLibrary; 6 7 namespace ListTest 8 { 9 // class to test List class functionality 10 class ListTest 11 { 12 static void Main( string[] args ) 13 { 14 List list = new List(); // create List container 15 16 // create data to store in List 17 bool aBoolean = true; 18 char aCharacter = '$'; 19 int anInteger = 34567; 20 string aString = "hello"; 21 22 // use List insert methods 23 list.InsertAtFront( aBoolean ); 24 list.Print(); 25 list.InsertAtFront( aCharacter ); 26 list.Print(); 27 list.InsertAtBack( anInteger ); 28 list.Print(); 29 list.InsertAtBack( aString ); 30 list.Print(); 31 32 // use List remove methods 33 object removedObject; 34
All rights reserved.
Outline
35 // remove data from list and print after each removal 36 try 37 { 38 removedObject = list.RemoveFromFront(); 39 Console.WriteLine( removedObject + " removed" ); 40 list.Print(); 41 42 removedObject = list.RemoveFromFront(); 43 Console.WriteLine( removedObject + " removed" ); 44 list.Print(); 45 46 removedObject = list.RemoveFromBack(); 47 Console.WriteLine( removedObject + " removed" ); 48 list.Print(); 49 50 removedObject = list.RemoveFromBack(); 51 Console.WriteLine( removedObject + " removed" ); 52 list.Print(); 53 } 54 55 // process exception if list empty when attempt is 56 // made to remove item 57 catch ( EmptyListException emptyListException ) 58 { 59 Console.Error.WriteLine( "\n" + emptyListException ); 60 } 61 62 } // end method Main 63 64 } // end class ListTest 65 }
All rights reserved.
Outline
The list is: True
The list is: $ True
The list is: $ True 34567
The list is: $ True 34567 hello
$ removed The list is: True 34567 hello
True removed The list is: 34567 hello
hello removed The list is: 34567
34567 removed Empty list