Theoretical Basis:
A linked list uses any storage unit to store data elements in a linear table.
If the reference domain of a node only stores the storage address of the direct successor node of the node, the linked list is called a singly linked list ).
A single-chain table is uniquely identified by the header reference H. The header reference points to the first node of the single-chain table, that is, the address of the first node of the single-chain table is placed in H.
C # implementation:
1 Interface
Reference the linear Table Interface ilistds <t>
2. Implementation
First, you must define a node class for a single-chain table.
Public class node <t>
{
Private t data; // data domain
Private node <t> next; // reference the domain
Public node (T Val)
{
Data = val;
Next = NULL;
}
Public node ()
{
Data = default (t );
Next = NULL;
}
Public t data
{
Get {return data ;}
Set {DATA = value ;}
}
Public node <t> next
{
Get {return next ;}
Set {next = value ;}
}
}
Implement subject class
The append, insert, and insertback methods are essentially insert operations. You can consider using overload or override. If you are interested, try it.
Public class linklist <t>: ilistds <t>
{
Private node <t> head;
Public node <t> head
{
Get {return head ;}
Set {head = value ;}
}
Public linklist ()
{
Head = NULL;
}
/// <Summary>
/// Obtain the length
/// </Summary>
/// <Returns> </returns>
Public int getlength ()
{
Node <t> P = head;
Int Len = 0;
While (P! = NULL)
{
++ Len;
P = P. Next;
}
Return Len;
}
/// <Summary>
/// Clear operation
/// </Summary>
Public void clear ()
{
Head = NULL;
}
/// <Summary>
/// Determine whether the linear table is empty
/// </Summary>
/// <Returns> </returns>
Public bool isempty ()
{
If (Head = NULL)
{
Return true;
}
Else
{
Return false;
}
}
/// <Summary>
/// Additional operation. If the linear table is not full, add the new element with the item value to the end.
/// </Summary>
/// <Param name = "item"> </param>
Public void append (T item)
{
Node <t> newnode = new node <t> (item); // create a node based on the element
Node <t> node = new node <t> ();
If (Head = NULL)
{
Head = newnode;
Return;
}
Node = head;
While (node. Next! = NULL)
{
Node = node. Next;
}
Node. Next = newnode;
}
/// <Summary>
/// Search for nodes
/// </Summary>
/// <Param name = "I"> </param>
/// <Returns> </returns>
Public node <t> findnode (int I)
{
If (isempty ())
{
Console. Write ("list is empty ");
Return NULL;
}
If (I <1)
{
Console. Write ("index is error ");
Return NULL;
}
Node <t> current = head;
Int J = 1;
While (current. Next! = NULL & J <I)
{
++ J;
Current = current. Next;
}
Return Current;
}
/// <Summary>
/// Insert operation, insert item before node I
/// </Summary>
/// <Param name = "item"> </param>
/// <Param name = "I"> </param>
Public void insert (T item, int I)
{
Node <t> newnode = new node <t> (item );
Node <t> node = new node <t> ();
Node <t> current = findnode (I );
If (current! = NULL)
{
Node = current; // back up the target node
Newnode. Next = current;
Node. Next = newnode;
}
}
/// <Summary>
/// Insert operation, insert item after node I
/// </Summary>
/// <Param name = "item"> </param>
/// <Param name = "I"> </param>
Public void insertback (T item, int I)
{
Node <t> newnode = new node <t> (item );
Node <t> current = findnode (I );
If (current! = NULL)
{
Newnode. Next = current. Next;
Current. Next = newnode;
}
}
/// <Summary>
/// Delete operation
/// </Summary>
/// <Param name = "I"> </param>
/// <Returns> </returns>
Public t Delete (int I)
{
Node <t> current = findnode (I );
Node <t> node = new node <t> ();
If (current! = NULL)
{
Node = current; // back up the target node
Node. Next = current. Next;
Return Current. Data;
}
Else
{
Console. Write ("the node is not exist! ");
Return default (t );
}
}
/// <Summary>
/// De-table element
/// </Summary>
/// <Param name = "I"> </param>
/// <Returns> </returns>
Public t getelem (int I)
{
Node <t> current = findnode (I );
If (current! = NULL)
{
Return Current. Data;
}
Else
{
Console. Write ("the node is not exist! ");
Return default (t );
}
}
/// <Summary>
/// Search by value
/// </Summary>
/// <Param name = "value"> </param>
/// <Returns> </returns>
Public int locate (T value)
{
If (isempty ())
{
Console. writeline ("list is empty! ");
Return-1;
}
Node <t> current = new node <t> ();
Current = head;
Int I = 1;
While (current. Next! = NULL &&! Current. Data. Equals (value ))
{
Current = current. Next;
++ I;
}
Return I;
}
}