Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// Fig. 21.4: LinkedListLibrary.cs 1 // ListNode, list and EmptyListException class declarations. 2 using System: 3 4 namespace LinkedListLibrary 6 // class to represent one

image text in transcribed

image text in transcribed

image text in transcribed

// Fig. 21.4: LinkedListLibrary.cs 1 // ListNode, list and EmptyListException class declarations. 2 using System: 3 4 namespace LinkedListLibrary 6 // class to represent one node in a list 7 class ListNode 9 // automatic read-only property Data 10 public object Data {get; private set; } 11 12 // automatic property Next 13 public ListNode Next get; set; } 14 15 // constructor to create ListNode that refers to datavalve 16 // and is last node in list 17 public ListNode(object datavalue) 18 this( dataValue, null ) 19 ( 20 } // end default constructor 21 22 // constructor to create ListNode that refers to dataValue 23 // and refers to next ListNode in List 24 public ListNode( object datavalue, ListNode nextNode) 25 ( 26 Data = dataValue; 27 Next = nextNode) 28 } // ond constructor 29 } // end class ListNode 30 31 // class List declaration 32 public class List 33 { 34 private ListNode firstNode; 35 private ListNode lastNode; 36 private string name; // string like "list" to display 37 38 // construct empty List with specified name 39 public List( string listName ) 40 { 41 name = listName: 42 firstNode = lastNode = null; 43 } // end constructor 44 45 // construct empty List with "list" as its name 46 public List() 47 : this( "list" ) 48 { 49 } // end default constructor 50 51 // Insert object at front of List. If List is empty, 52 // firstNode and lastNode will refer to same object. 53 // Otherwise, firstNode refers to new node. 54 public void InsertAtFront( object insertItem> 55 { 56 if (IsEmpty()) 57 firstNode = lastNode = new ListNode.insertItem ); 58 else 59 firstNode = new ListNode_insertIten, firstNode ); 60 } // end method Insertat Front 61 62 // Insert object at end of List. If List is empty, 63 11 firstNode and lastNode will refer to same object. 64 // Otherwise, lastNode's Next property refers to new node. 65 public void InsertAtBack(object insertItem ) 66 { 67 if IsEmpty()) 68 firstNode = lastNode = new ListNode(insertItem); 69 else 70 lastNode = lastNode. Next = new ListNode(insertItem); 71 } // end method InsertAtrack 72 73 1/ remove first node from List 74 public object RemoveFromFront) 75 ( 76 if IsEmpty()) 77 throw new EmptyListException( name ); 78 79 object remove Iten - firstNode.Data; // retrieve data 80 // reset firstNode and lastNode references 81 if (firstNode == lastNode) 82 firstNode = lastNode = null; 83 else 84 firstNode = firstNode. Next; 85 86 return removeIten; // return removed data 87 } // end method RemoveFromFront ListNode current = firstNode; 133 134 135 136 137 // output current node data while not at end of list while (current != null ) { Console.Write(current Data + " ); current = current.Next; } // end while 138 88 89 // retove last node from List 90 public object RemoveFromBack) 91 { 92 if (Is Empty()) 93 throw new EmptyListException name ); 94 95 object removeItem lastNode.Data; // retrieve data 96 97 // reset firstNode and lastNode references 98 if (firstNode == lastNode ) 99 firstNode = lastNode = null; 100 else 101 { 102 ListNode current - firstNode: 103 104 // loop while current node is not lastNode 105 while (current.Next != lastNode) 106 current = current.Next; // move to next node Console.WriteLine(" "); } // end else } // end method Display } // end class List // class EmptyListException declaration public class EmptyListException: Exception { // parameterless constructor public EmptyListException() : base(_"The list is empty") { // empty constructor ) // end EmptyListException constructor 107 // current is now lastNode lastNode = current; current.Next = null; } // end else 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 return removeItem; // return removed data ) // end method RemoveFromback 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 // one-parameter constructor public EmptyListException string name) : base( "The " + name + " is ompty" ) { // empty constructor } // end EmptyListException constructor // return true if List is ompty public bool IsEmpty() { return firstNode == null; } // end method IsEmpty // two-parameter constructor public EmptyListException(string exception, Exception inner) : basel exception, inner) { // empty constructor } // end EmptyListException constructor } // and class EmptyListException } // end namespace LinkedListLibrary // output List contents public void Display() { if (IsEmpty()) ( { Console.WriteLine("Empty" + name ); } // end if else { Console.Write("The " + name + " is: "); // Fig. 21.4: LinkedListLibrary.cs 1 // ListNode, list and EmptyListException class declarations. 2 using System: 3 4 namespace LinkedListLibrary 6 // class to represent one node in a list 7 class ListNode 9 // automatic read-only property Data 10 public object Data {get; private set; } 11 12 // automatic property Next 13 public ListNode Next get; set; } 14 15 // constructor to create ListNode that refers to datavalve 16 // and is last node in list 17 public ListNode(object datavalue) 18 this( dataValue, null ) 19 ( 20 } // end default constructor 21 22 // constructor to create ListNode that refers to dataValue 23 // and refers to next ListNode in List 24 public ListNode( object datavalue, ListNode nextNode) 25 ( 26 Data = dataValue; 27 Next = nextNode) 28 } // ond constructor 29 } // end class ListNode 30 31 // class List declaration 32 public class List 33 { 34 private ListNode firstNode; 35 private ListNode lastNode; 36 private string name; // string like "list" to display 37 38 // construct empty List with specified name 39 public List( string listName ) 40 { 41 name = listName: 42 firstNode = lastNode = null; 43 } // end constructor 44 45 // construct empty List with "list" as its name 46 public List() 47 : this( "list" ) 48 { 49 } // end default constructor 50 51 // Insert object at front of List. If List is empty, 52 // firstNode and lastNode will refer to same object. 53 // Otherwise, firstNode refers to new node. 54 public void InsertAtFront( object insertItem> 55 { 56 if (IsEmpty()) 57 firstNode = lastNode = new ListNode.insertItem ); 58 else 59 firstNode = new ListNode_insertIten, firstNode ); 60 } // end method Insertat Front 61 62 // Insert object at end of List. If List is empty, 63 11 firstNode and lastNode will refer to same object. 64 // Otherwise, lastNode's Next property refers to new node. 65 public void InsertAtBack(object insertItem ) 66 { 67 if IsEmpty()) 68 firstNode = lastNode = new ListNode(insertItem); 69 else 70 lastNode = lastNode. Next = new ListNode(insertItem); 71 } // end method InsertAtrack 72 73 1/ remove first node from List 74 public object RemoveFromFront) 75 ( 76 if IsEmpty()) 77 throw new EmptyListException( name ); 78 79 object remove Iten - firstNode.Data; // retrieve data 80 // reset firstNode and lastNode references 81 if (firstNode == lastNode) 82 firstNode = lastNode = null; 83 else 84 firstNode = firstNode. Next; 85 86 return removeIten; // return removed data 87 } // end method RemoveFromFront ListNode current = firstNode; 133 134 135 136 137 // output current node data while not at end of list while (current != null ) { Console.Write(current Data + " ); current = current.Next; } // end while 138 88 89 // retove last node from List 90 public object RemoveFromBack) 91 { 92 if (Is Empty()) 93 throw new EmptyListException name ); 94 95 object removeItem lastNode.Data; // retrieve data 96 97 // reset firstNode and lastNode references 98 if (firstNode == lastNode ) 99 firstNode = lastNode = null; 100 else 101 { 102 ListNode current - firstNode: 103 104 // loop while current node is not lastNode 105 while (current.Next != lastNode) 106 current = current.Next; // move to next node Console.WriteLine(" "); } // end else } // end method Display } // end class List // class EmptyListException declaration public class EmptyListException: Exception { // parameterless constructor public EmptyListException() : base(_"The list is empty") { // empty constructor ) // end EmptyListException constructor 107 // current is now lastNode lastNode = current; current.Next = null; } // end else 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 return removeItem; // return removed data ) // end method RemoveFromback 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 // one-parameter constructor public EmptyListException string name) : base( "The " + name + " is ompty" ) { // empty constructor } // end EmptyListException constructor // return true if List is ompty public bool IsEmpty() { return firstNode == null; } // end method IsEmpty // two-parameter constructor public EmptyListException(string exception, Exception inner) : basel exception, inner) { // empty constructor } // end EmptyListException constructor } // and class EmptyListException } // end namespace LinkedListLibrary // output List contents public void Display() { if (IsEmpty()) ( { Console.WriteLine("Empty" + name ); } // end if else { Console.Write("The " + name + " is: ")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learn To Program Databases With Visual Basic 6

Authors: John Smiley

1st Edition

1902745035, 978-1902745039

More Books

Students also viewed these Databases questions

Question

What is the role of promotion in IMC?

Answered: 1 week ago

Question

LO5.2 Discuss government failure and explain why it happens.

Answered: 1 week ago