1. The general structure of the program as shown below:
2. The following codes are listed in sequence each class
①ILISTDs.cs This class is an interface, a single list listed methods
the System the using; the using the System.Collections.Generic; the using the System.Linq; the using the System.Text; the using System.Threading.Tasks; namespace single list { public interface IListDs <T> { int a GetLength (); // find the length of void Clear ( ); // erase bool IsEmpty (); // determines whether linear table is empty void append (T item); // additional operations void insert (T item, int i ); // insert T Delete (int i) ; // delete T getElem (int i); // get bucket int locate (T value); // Find by value } }
②LinkList.cs singly linked list implementation class
the System the using; the using the System.Collections.Generic; the using the System.Linq; the using the System.Text; the using System.Threading.Tasks; namespace single list { public class LinkList <T>: IListDs <T> { Private the Node <T> head; // single head of the list references // head referenced attribute public the Node <T> head { GET { return head; } SET { head = value; } } // constructor public LinkList () { head = null; } { // find a single list length int a GetLength public () { the Node <T> P = head; int len = 0; the while (P = null!) { P = p.Next; len ++; } return len; } // empty single chain public void Clear () { head = null; } // determines whether an empty public BOOL the IsEmpty () { return head == null;
} // add a new element to the end of a single list public void the Append (Item T) the Node <T> Q = the Node new new <T> (item); The Node <T> P = the Node new new <T> (); IF (head == null) { head = Q; return; } P = head; the while (p.Next = null!) { P = p.Next; } Q = p.Next; } // item is inserted in front of a single linked list node i th position public void the insert (T item, int i) { IF (the IsEmpty () || i <. 1) { Console.WriteLine ( "list is empty or the position error"); return; } IF (I ==. 1) { Node<T> q = new Node<T>(item); q.Next = head; head = q; return; } Node<T> p = head; Node<T> r = new Node<T>(); int j = 1; while (p.Next != null && j < i) { r = p; p = p.Next; j++; } if (j == i) { Node<T> q = new Node<T>(item); Node<T> m = r.Next; r.Next = q; q.Next = m; } } // item is inserted into a single linked list node after the position of the i-th public void InsertPost (T item, int i) { IF (the IsEmpty () || i <. 1) { Console.WriteLine ( "list is empty or the position error "); return; } IF (I ==. 1) { the Node <T> Q = the Node new new <T> (Item); q.Next = head.Next; head.Next = Q; return; } the Node <T > P = head; the Node <T> = R & lt new new the Node <T> (); int J =. 1; the while (! = null && p.Next J <= I) { P = R & lt; P = p.Next; J ++; } IF (I + J ==. 1) { the Node <T> Q = the Node new new <T> (Item); the Node <T> m = r.Next; R & lt. Q = the Next; q.Next = m; } the else { Console.WriteLine ( "insertion position is too large, error"); } } public T the Delete (int I) { IF (the IsEmpty () || I <. 1) { Console .WriteLine ( "list is empty or the position error"); return default (T); } Node<T> q = new Node<T>(); if (i == 1) { q = head; head = head.Next; return q.Data; } Node<T> p = head; int j = 1; while (p.Next != null && j < i) { q = p; p = p.Next; j++; } if (j == i) { q.Next = p.Next; return p.Data; } else { Console.WriteLine ( "incorrect position"); return default (T); } } // get the i-th element of a single linked list public T getElem (int i) { IF (the IsEmpty ()) { Console.WriteLine ( "list is empty list "); return default (T); } the Node <T> P = new new the Node <T> (); P = head; int J =. 1; ! the while (p.Next = null && J <I) { P = P .NEXT; J ++; } IF (J == I) { return p.Data; } the else { Console.WriteLine ( "position is not correct!"); } return default (T); } // find the value of the value of a single linked list node public int the Locate (T value) { IF ( the IsEmpty ()) { Console.WriteLine ( "list is empty list!"); return -1; } the Node <T> P = the Node new new <T> (); P = head; int I =. 1; the while ((( ! p.Next = null) &&))) (p.Data.Equals (value!) { P = p.Next; ++ I; } IF (P == null) { Console.WriteLine ( "this node does not exist."); return -1; } the else { return I; } } } }
③ Node.cs Node Class
the System the using; the using the System.Collections.Generic; the using the System.Linq; the using the System.Text; the using System.Threading.Tasks; namespace single list { public class the Node <T> { Private Data T; // data fields private Node <T > next; // reference field // constructor public the Node (T Val, the Node <T> P) { Data = Val; Next = P; } // constructor public the Node (the Node <T> P) { Next = P ; } // constructor public the Node (T Val) { Data = Val; } // constructor Public the Node () { Data = default (T); Next = null; } // attribute data fields public the Data T { GET { return Data; } SET { Data = value; } } // reference field attribute public Node <T > the Next { GET { return Next; } SET { Next = value; } } } }
④Program.cs main program
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 单链表 { class Program { static void Main(string[] args) { LinkList<string> link = new LinkList<string>(); link.Append("123"); link.Append("567"); link.Append("jqk"); link.Insert("abc",2); link.InsertPost("def",2); int length = link.GetLength(); int k=link.Locate("567"); string m=link.GetElem(3); Console.WriteLine("Position 567 is "+ k); Console.WriteLine ( "position 3 value of" m +); Console.WriteLine ( "length of list" + length); the Node <String> = n-link.Head; the while (! = N-null) { Console. the WriteLine (n.Data); n-n.Next =; } } } }
FIG ⑤ results are as follows, and estimation results consistent