Data Structure and algorithm-C # implement a shortlist instance

Source: Internet
Author: User

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:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.