C # implement common data structures (1): Linked List
Node definition and its linked list implementation. The Code is as follows:
// Class listnode and class list definitions.
Using system;
Namespace upload listlibrary
{
/// <Summary>
/// Class to represent one node in a list
/// </Summary>
Class listnode
{
Private object data;
Private listnode next;
// Constructer to creat listnode that refers to datavalue
// And in last node in list
Public listnode (Object datavalue): This (datavalue, null)
{
}
// Constructer to creat listnode that refers to datavalue
// And refer to next listnode in list
Public listnode (Object datavalue, listnode nextnode)
{
Data = datavalue;
Next = nextnode;
}
// Property next
Public listnode next
{
Get
{
Return next;
}
Set
{
Next = value;
}
}
// Property Data
Public Object Data
{
Get {
Return data;
}
}
} // End class listnode
// Class list definition
Public class list
{
Private listnode firstnode;
Private listnode lastnode;
Private string name; // string like "list" to display
// Construct empty list with specified name
Public list (string listname ){
Name = listname;
Firstnode = lastnode = NULL;
}
// Construct empty list with "list" as its name
Public list (): This ("list "){
}
// Insert object at front of list. If list is empty,
// Firstnode and lastnode will refer to same object.
// Otherwise, firstnode refer to new node.
Public void insertatfront (Object insertitem ){
Lock (this ){
If (isempty ()){
Firstnode = lastnode = new listnode (insertitem );
}
Else {
Firstnode = new listnode (insertitem, firstnode );
}
}
}
// Return true if list is empty
Public bool isempty (){
Lock (this ){
Return firstnode = NULL;
}
}
// Insert object at the end of list. If list is empty,
// Firstnode and lastnode will refer to same object.
// Otherwise, lastnode's next property refers to new node
Public void insertatback (Object insertitem ){
Lock (this ){
If (isempty ()){
Firstnode = lastnode = new listnode (insertitem );
}
Else {
Lastnode = lastnode. Next = new listnode (insertitem );
}
}
}
// Remove first node from List
Public object removefromfront (){
Lock (this ){
If (isempty ()){
Throw new emptylistexception (name );
}
Object removeitem = firstnode. Data; // retrieve data
// Reset firstnode and lastnode refereances
If (firstnode = lastnode)
Firstnode = lastnode = NULL;
Else
Firstnode = firstnode. Next;
Return removeitem; // return removed data
}
}
// Remove last node from List
Public object removefromback ()
{
Lock (this ){
If (isempty ()){
Throw new emptylistexception (name );
}
Object removeitem = lastnode. Data; // retrieve data
// Reset firstnode and lastnode refereances
If (firstnode = lastnode)
Firstnode = lastnode = NULL;
Else {
Listnode current = firstnode;
// Loop while current node is not lastnode
While (current. Next! = Lastnode ){
Current = current. Next; // move to next node
}
// Current is new lastnode
Lastnode = current;
Current. Next = NULL;
}
Return removeitem; // return removed data
}
}
// Output list Contents
Virtual public void print (){
Lock (this ){
If (isempty ()){
Console. writeline ("empty" + name );
Return;
}
Console. Write ("the" + name + "is :");
Listnode current = firstnode;
// Output current node data while not at end of list
While (current! = NULL ){
Console. Write (current. Data + "");
Current = current. Next;
}
Console. writeline ("/N ");
}
}
} // End class list
// Class emptylistexception Definition
Public class emptylistexception: applicationexception
{
Public emptylistexception (string name): Base ("the" + name + "is empty "){
}
} // End class emptylistexception
} // End namespace into listlibrary
The code for using the linked list is as follows:
Using system;
Using upload listlibrary;
Namespace listtest
{
/// <Summary>
/// Summary of listtest.
/// </Summary>
Public class listtest
{
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main (string [] ARGs)
{
//
// Todo: Add code here to start the application
//
List list = new list ();
// Create data to store in list
Bool sort lean = true;
Char acharactor = '$ ';
Int aninteger = 34567;
String astring = "hello ";
// Use list insert Methods
List. insertatfront (lean );
List. Print ();
List. insertatfront (acharactor );
List. Print ();
List. insertatback (aninteger );
List. Print ();
List. insertatback (astring );
List. Print ();
// Use list remove Methods
Object removedobject;
// Remove data from list and print after each removal
Try
{
Removedobject = List. removefromfront ();
Console. writeline (removedobject + "removed ");
List. Print ();
Removedobject = List. removefromfront ();
Console. writeline (removedobject + "removed ");
List. Print ();
Removedobject = List. removefromback ();
Console. writeline (removedobject + "removed ");
List. Print ();
Removedobject = List. removefromback ();
Console. writeline (removedobject + "removed ");
List. Print ();
}
// Process exception if list empty when attempt is made to remove item
Catch (emptylistexception)
{
Console. Error. writeline ("/N" + emptylistexception );
}
} // End method main
} // End class listtest
}
The running result is as follows: