This article describes the C # implementation of a single list (linear table) method. Share to everyone for your reference, specific as follows:
Sequential tables are composed of contiguous memory, and the linked list is different. The advantage of the sequential table is to find, the advantage of the list is to insert elements and other operations. Example of a sequential table: http://www.jb51.net/article/87605.htm
Note that a single linked list of the Add () method is best not to call frequently, especially when the length of the chain is longer, because each add, will be from the beginning node to the tail node traversal, the disadvantage of the optimization method is to add nodes to the head, but the order is reversed.
So, in the following example, when executing purge (cleaning a repeating element), instead of adding the element using the Add () method, you define a node so that it always points to the last node in the target list, so that you don't have to iterate through it every time.
In addition, the linked list can also be made into a circular list, that is, the last node of the next property is equal to head, the main operation is similar to the single linked list, the last node to determine, not equal to null, but equal to head
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
namespace Linearlist {//define the behavior of the linear table, the Order table class and the single linked list class inherit the public interface ilistds<t> {int getlength ();
void Insert (T item, int i);
void Add (T item);
BOOL IsEmpty ();
T getelement (int i);
void Delete (int i);
void Clear ();
int locateelement (T item);
void Reverse ();
}//List node class Node<t> {private T tdata;
Private node<t> NNext;
Public T Data {get {this.tdata;}
set {This.tdata = value;}
Node<t> Next {get {this.nnext}
set {This.nnext = value;}
Public Node () {this.tdata = default (T);
This.nnext = null;
Public Node (T t) {this.tdata = t;
This.nnext = null;
Public Node (t t,node<t> node) {this.tdata = t;
This.nnext = node; }//This enumeration represents the position of a single linked list add element, divided into two types of enum ADDP, head and tailosition {head,tail}; Single-linked list class Linkedlist<t>:ilistds<t> {private node<t> thead;//single linked table header public node<t>
Head {get {this.thead;}
set {This.thead = value;}
Public LinkedList () {this.thead = null;
Public LinkedList (node<t> node) {this.thead = node; public void Add (T item,addposition p)//Select Add location {if (p = = addposition.tail) {this. Add (item)//default added at end} else//the cost of finding from the head, the time complexity of O (1) does not have to loop to the tail every time, which is exactly the advantage of the sequential table {node<t> Node = This.
Head;
node<t> nodetmp = new node<t> (item); if (node = null) {this.
head = nodetmp;
else {nodetmp.next = node;
This.thead = nodetmp; #region ilistds<t> member public int getlength () {node<t> Node = new node<t&
gt; ();
int count = 0; Node = This.thead;
while (node!= null) {count++; node = node.
Next;
return count; public void Insert (T item, int i)//i minimum starting from 1 {node<t> insertnode = new node<t> (item, NULL);//Instance
To add node if (This.thead = = NULL && i = 1) {this.thead = Insertnode;
Return } if (I < 1 | | i > this. GetLength () | |
(This.thead = = NULL && i!= 1))
{Console.WriteLine ("There are no elements in this linked list!");
Return
int j = 1;
node<t> node = this.thead;
Node<t> nodetmp; while (node!= null && J < i)//loop ends, the node is guaranteed to be the first node {node = node.
Next;
j + +; } nodetmp = node. next;//The first i+1 node node of the original single linked list.
Next = insertnode;//node After section I is modified to node Insertnode.next = nodetmp;//to be inserted after node insertion, its successor node is the first i+1 node of the original list The public void Add (T Item)//is added to the tail, with a time complexity of O (n), if addedTo the head, it saves the cost of the loop {node<t> lastnode = new node<t> (item, NULL);//Instantiate Node if to add (This.thead = = nul
L) {this.thead = Lastnode;
else {node<t> Node = This.thead; while (node. Next!= null) {node = node.
Next; node.
Next = Lastnode;
} public bool IsEmpty () {return this.thead = = NULL; The public T getelement (int i)//set I minimum starting from 1 {if (I < 1 | | i > this.
GetLength ()) {Console.WriteLine ("The location is not right!");
return default (T);
else {if (i = = 1) {return this.tHead.Data;
else {node<t> Node = This.thead;
int j = 1; while (J < i) {node = node.
Next;
j + +; Return node.
Data;
}} public void Delete (int i)/set I minimum starting from 1 { if (I < 1 | | | i > this.
GetLength ()) {Console.WriteLine ("The location is not right!");
else {if (i = = 1) {node<t> Node = This.thead; This.thead = node.
Next;
else {node<t> Node = This.thead;
int j = 1; while (J < i-1) {node = node.
Next;
j + +; node. Next = node.
Next.next; }} public void Clear () {This.thead = null;//tHead is set to NULL, all subsequent nodes are waiting for GC to automatically recycle} pub because of loss of reference LIC int locateelement (T Item)//return value minimum starting from 1 {if (This.thead = null) {Console.WriteLine ("There are
No elements in this linked list! ");
return-1;
} node<t> Node = This.thead;
int i = 0;
while (node!= null) {i++; if (node.
Data.equals (item))//If data is a custom type, its Equals function must override {return i; node = node.
Next;
} Console.WriteLine ("No found!");
return-1; public void Reverse () {if (This.thead = = null) {Console.WriteLine ("There are no elements I
n this linked list! ");}
else {node<t> Node = This.thead; if (node. Next = = null)//If there is only a header node, do not make any changes {} else {node<t> Node1 = node.
Next;
Node<t> Node2; while (Node1!= null) {Node2 = node.
Next.next; Node. Next = node2;//can find that node is always unchanged, always the same head node Node1.
Next = This.thead;
This.thead = Node1;
Node1 = Node2; #endregion} class Program {static void Main (string[] args) {/* clear of test single linked list
Empty llist.clear ();
node<int> n = new node<int> ();
n = llist.head; while (n!= null) {Console.WriteLine (n).Data);
n = n.next;
} console.readline ();
* * * Test single linked list returns the number of elements linkedlist<int> llist = new linkedlist<int> ();
Llist.add (3);
Console.WriteLine (Llist.getlength ());
Console.ReadLine ();
* * * Test single linked list insert linkedlist<int> llist = new linkedlist<int> ();
Llist.insert (0,1);
Llist.add (1);
Llist.add (2);
Llist.add (3);
Llist.add (4);
Llist.insert (99,3);
node<int> n = new node<int> ();
n = llist.head;
while (n!= null) {Console.WriteLine (n.data);
n = n.next;
} console.readline ();
* * * Test single linked list to obtain a value of a location linkedlist<int> llist = new linkedlist<int> ();
Llist.add (1);
Llist.add (2);
Llist.add (3);
Llist.add (4);
Console.WriteLine (Llist.getelement (1));
Console.ReadLine (); * * * Test single linked list deletes a value of a location linkedlist<int> llist = new Linkedlist<int> ();
Llist.add (1);
Llist.add (2);
Llist.add (3);
Llist.add (4);
node<int> n = new node<int> ();
n = llist.head;
while (n!= null) {Console.WriteLine (n.data);
n = n.next;
} console.readline ();
Llist.delete (2);
node<int> m = new node<int> ();
m = Llist.head;
while (M!= null) {Console.WriteLine (m.data);
m = M.next;
} console.readline ();
Llist.add (1);
Llist.add (2);
Llist.add (3);
Llist.add (4);
Console.WriteLine (Llist.locateelement (3));
Console.ReadLine ();
* * * Test single linked list reverse operation (reverse order) linkedlist<int> llist = new linkedlist<int> ();
Llist.add (1);
Llist.add (2);
Llist.add (3);
Llist.add (4);
Llist.add (5);
node<int> m = new node<int> (); m = Llist.head;
while (M!= null) {Console.WriteLine (m.data);
m = M.next;
} console.readline ();
Llist.reverse ();
node<int> n = new node<int> ();
n = llist.head;
while (n!= null) {Console.WriteLine (n.data);
n = n.next;
} console.readline ();
Llist.add (1,addposition.head);
Llist.add (2, Addposition.head);
Llist.add (3, Addposition.head);
Llist.add (4, Addposition.head);
Llist.add (5, Addposition.head);
node<int> m = new node<int> ();
m = Llist.head;
while (M!= null) {Console.WriteLine (m.data);
m = M.next;
} console.readline (); *//////* Test to clear the duplicate element operation (return another list) on a single linked list. This example avoids the use of the Add () method because each add () is traversed from beginning to end, and the Add () method does not apply to the need to save the last element of the target list in real time. Another way to avoid this is to add from the head, but the end result is reverse linkedlist<int> lList = new linkedlist<int> ();//original list linkedlist<int> lList2 = new linkedlist<int> ()//save result of the linked list LL Ist.
ADD (1);
Llist.add (2);
Llist.add (1);
Llist.add (3);
Llist.add (3);
Llist.add (4);
Llist.add (5);
node<int> m = new node<int> ();
m = Llist.head;
while (M!= null) {Console.WriteLine (m.data);
m = M.next;
} console.readline (); Node<int> Node1 = llist.head;//Identifies the elements of the original list that are currently participating in the comparison size, that is, the element that may be put into the list 2 node<int> node2;//identifies the last element of the result list, avoiding the use of the add The function causes multiple traversal node<int> node3;//to temporarily save the successor of Node1, and eventually pay node1 Node3 = Node1.
Next; Llist2.head = node1;//Chain Table 1 The head node must be added to the list 2 Node2 = Llist2.head;//node2 represents the last element of the list 2, at which point the last element is the header node Node2. Next = null;//Because the Node1 is assigned to the head node of the list 2, the subsequent nodes must be set to null or they will be brought over together Node1 = node3;//If there is no node3 temporary Node1 successor, the successor to the Llist2.head is assigned null will also modify the successor of the Node1, because both point to the same memory while (Node1!= null) {//Compare size Node in IList2<int> tmp = Llist2.head; if (Node1. Data.equals (TMP. Data)) {Node1 = Node1.
Next;
If the continue;//is equal, the Node1 is moved back one bit to recalculate} else {node<int> TMP2 = tmp; TMP = tmp. Next;//tmp identifies the nodes used for loops in List 2, and the node compares if (TMP = NULL)//when the existing element in the list 2 is not equal to Node1 {node3 = Node1.
Next; Node2.
Next = Node1;
Node2 = Node1; Node2.
Next = null;
Node1 = Node3;
Continue while (TMP!= null)//TMP is not NULL, it loops until it is null {if (NODE1). Data.equals (TMP. Data)) {Node1 = Node1.
Next;
else {TMP2 = tmp; TMP = tmp.
Next; if (TMP = = null) {Node3 = Node1.
Next; Node2.
Next = Node1;
Node2 = Node1; Node2.
Next = null;
Node1 = Node3; The output clears the duplicated array node<int> n = new Node<int>
;();
n = llist2.head;
while (n!= null) {Console.WriteLine (n.data);
n = n.next;
} console.readline ();
*/
}
}
}
For more information on C # related content readers can view the site topics: "C # Data structure and algorithm tutorial", "C # traversal algorithm and Skills summary", "C # Design Thread Usage Tips summary", "C # Operation Excel Skills Summary", "C # XML file Operation skills Summary", "C # Common control usage tutorials, summary of WinForm control usage, summary of C # array operations tips, and the introductory course on C # object-oriented programming
I hope this article will help you with C # programming.