SoFunction
Updated on 2025-03-08

C# defines and implements single linked list instance analysis

This article describes in detail the process and principles of C# defining and implementing single linked lists using examples. Generally speaking, C# defines and implements single linked lists, and the code includesConstituting the node definition of the linked list, using variables to implement the table header, clear the entire linked list, and reset the linked list, so that the first node becomes the current node, determine whether the linked list is empty, determine whether the current node is the last node, return the value of the next node of the current node, and make it the current node, move the current node out of the linked list, and the next node becomes the current nodeetc.

The specific implementation code is as follows:

using System;
using ;
// The node definition that forms the linked listpublic class Node 
{
 public Object data;
 public Node next;
 public Node( Object d ) 
 {
 data = d;
 next = null;
 }
}
public class List 
{
 // Use variables to implement the table header private Node Head = null;
 private Node Tail = null;
 private Node Pointer = null;
 private int Length = 0;
 //Clear the entire link list public void deleteAll( ) 
 {
 Head = null;
 Tail = null;
 Pointer = null;
 Length = 0;
 }
 //Reset the linked list to make the first node the current node public void reset( ) 
 {
 Pointer = null;
 }
 //Judge whether the linked list is empty public bool isEmpty( ) 
 {
 return (Length == 0);
 }
 //Judge whether the current node is the last node public bool isEnd( ) 
 {
 if (Length == 0)
  throw new ( );
 else if (Length == 1)
  return true;
 else
  return (cursor( ) == Tail);
 }
 //Return the value of the next node of the current node and make it the current node public Object nextNode( ) 
 {
 if (Length == 1)
  throw new ( );
 else if (Length == 0)
  throw new ( );
 else 
 {
  Node temp = cursor();
  Pointer = temp;
  if (temp != Tail)
  return ();
  else
  throw new ( );
 }
 }
 //Return the value of the current node public Object currentNode( ) 
 {
 Node temp = cursor( );
 return ;
 }
 //Insert a node in front of the current node and make it the current node public void insert( Object d ) 
 {
 Node e = new Node( d );
 if (Length == 0) 
 {
  Tail = e;
  Head = e;
 } 
 else 
 {
  Node temp = cursor( );
   = temp;
  if (Pointer == null)
  Head = e;
  else
   = e;
 }
 Length++;
 }
 //Return the size of the linked list public int size( ) 
 {
 return Length;
 }
 //Move the current node out of the linked list, and the next node becomes the current node //If the moved out node is the last node, the first node becomes the current node public Object remove( ) 
 {
 Object temp;
 if (Length == 0)
  throw new ( );
 else if (Length == 1) 
 {
  temp = ;
  deleteAll( );
 } 
 else 
 {
  Node cur = cursor( );
  temp = ;
  if (cur == Head)
  Head = ;
  else if (cur == Tail) 
  {
   = null;
  Tail = Pointer;
  reset( );
  } 
  else
   = ;
  Length--;
 }
 return temp;
 }
 //Return the pointer to the current node private Node cursor( ) 
 {
 if (Head == null)
  throw new ( );
 else if (Pointer == null)
  return Head;
 else
  return ;
 }
 //Simple application examples of linked list public static void Main( ) 
 {
 List a = new List();
 for (int i = 1; i <= 10; i++)
  ( new IntPtr(i));
 (( ));
 while (!( ))
  (( ));
 ();
 while (!( )) 
 {
  ( );
 }
 ( );
 ( );
 if (( ))
  ("There is no Node in List!");
 ("You can press return to quit!");
 try 
 {
  // Make sure the user sees the program running results clearly  ( );
 } 
 catch (IOException e) 
 {
 }
 }
}