Create a one-way linked list and perform basic operations on the one-way linked list through three classes: Create, add (before the specified node, after the specified node), delete, judge whether it is null ....
The following three classes are implemented and tested respectively.Code
Linkedlistnode: node class of the linked list
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
NamespaceCadatastructuretest. shortlist
{
Public ClassLinkedlistnode
{
Public ObjectData {Get;Private Set;}
PublicLinkedlistnode next {Get;Set;}
PublicLinkedlistnode (ObjectDatavalue)
:This(Datavalue,Null)
{
}
PublicLinkedlistnode (ObjectDatavalue, linkedlistnode nextnode)
{
Data = datavalue;
Next = nextnode;
}
}
}
Linked List: Linked List
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
NamespaceCadatastructuretest. shortlist
{
Public ClassShortlist
{
PrivateOptional listnode firstnode;
PrivateLinkedlistnode lastnode;
Private StringName;
Public Revoke list ( String Listname)
{
Name = listname;
Firstnode = lastnode = Null ;
}
Public Partition list ()
: This ( " List " )
{
}
Public Void Insertatfront ( Object Insertitem)
{
If (Isempty ())
Firstnode = lastnode = New Jsonlistnode (insertitem );
Else
Lastnode = lastnode. Next = New Jsonlistnode (insertitem );
}
Public Void Insertatback ( Object Insertitem)
{
If (Isempty ())
Firstnode = lastnode = New Jsonlistnode (insertitem );
Else
Firstnode = New Inclulistnode (insertitem, firstnode );
}
Public Object removefromfront ()
{< br> If (isempty ()
throw New emptylistexception (name );
Object removeitem = firstnode. data;
If (firstnode = lastnode)
firstnode = lastnode = null ;
else
firstnode = firstnode. next;
return removeitem;
}
Public Object removefromback ()
{< br> If (isempty ()
throw New emptylistexception (name);
Object removeitem = lastnode. data;
If (firstnode = lastnode)
firstnode = lastnode = null ;
else
{< br> Publish listnode current = firstnode;
While(Current. Next! = Lastnode)
Current = current. Next;
Lastnode = current;
Current. Next =Null;
}
ReturnRemoveitem;
}
Public BoolIsempty ()
{
ReturnFirstnode =Null;
}
Public Void Display ()
{
If (Isempty ())
{
Console. writeline ( " Empty " + Name );
}
Else
{
Console. Write ( " The " + Name + " Is: " );
Optional listnode current = firstnode;
While (Current! =Null )
{
Console. Write (current. Data + " " );
Current = current. Next;
}
Console. writeline ( " \ N " );
}
}
}
}
Emptylistexception: handling of linked list operation exceptions
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
NamespaceCadatastructuretest. shortlist
{
Public ClassEmptylistexception: exception
{
PublicEmptylistexception ()
:Base("The list is empty")
{
}
Public emptylistexception ( string name)
: base (" the " + name + " is empty " )
{< BR >}
PublicEmptylistexception (StringException, exception inner)
:Base(Exception, inner)
{
}
}
}
The test code is as follows:
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using Cadatastructuretest. shortlist;
Namespace Cadatastructuretest
{
Class Program
{
Static Void Main ( String [] ARGs)
{
Cadatastructuretest. shortlist. shortlist list = New Cadatastructuretest. shortlist. shortlist ();
Bool Lean = True ;
Char Acharacter = ' # ' ;
Int Aninteger = 9258 ;
String Astring = " Datastructure " ;
List. insertatfront (lean );
List. Display ();
List. insertatfront (acharacter );
List. Display ();
List. insertatback (aninteger );
List. Display ();
List. insertatback (astring );
List. Display ();
ObjectRemovedobject;
try
{< br> removedobject = List. removefromfront ();
console. writeline (removedobject + " removed " );
list. display ();
removedobject = List. removefromfront ();
console. writeline (removedobject + " removed " );
list. display ();
Removedobject = List. removefromback ();
Console. writeline (removedobject +"Removed");
List. Display ();
Removedobject = List. removefromback ();
Console. writeline (removedobject + " Removed " );
List. Display ();
}
Catch (Cadatastructuretest. Listing list. emptylistexception)
{
Console. Error. writeline ( " \ N " + Emptylistexception );
}
Console. Readline ();
}
}
}
The effect is as follows: