It's written in front.
Modular programming is the only way for most beginners, and then maybe you go to the structured programming, the list is a typical structure pattern, its appearance overcomes the array must know the size of the defect in advance, do not understand? You just have to keep in mind that the list structure is very bull-like, learning this structure has a great boost to our logical thinking.
What is a linked list structure?
A linked list is a discontinuous, non sequential storage structure on a physical storage unit. such as a->b->c, this structure, we can understand that a is connected to the B,b connection C, like this structure we are called linked list structure. By the way, the train compartment is actually the best description of the structure of the list.
Why should there be a linked list structure?
The computer has learned that the array, the array is commonly used to cut, but there are problems. First, the array must know the size of the space (int[] age = new int[100], the length must be declared, and then the insertion and deletion between elements is inefficient (how do I insert an element in the middle of an array?). )。
The emergence of the list, the perfect solution to these problems.
How to implement a linked list
First we need to declare a structure
List structure: construct node-connection node
//template
class Node {
public
int num;
Point to next element public
Node next;
List structure: construct node-connection node
//template
class Node {
public
int num;
Point to next element public
Node next;
We can think of the above structure as a gift box, which can store plastic values.
Then we create a Mr. MyList, who uses node to store plastic items and uses a linked list structure.
Class MyList
{public
Node CurrentNode;
public Node Point;
Public MyList ()
{
CurrentNode = new Node ();
}
Store items public
void Add (int value)
{
//First
if (point = = null)
{
currentnode.num = value;
Point = CurrentNode;
}
Else //2 3 4 ... times
{
Node temp = new node ();
Temp.num = value;
Point.next = temp;
Update pointer point
= temp;
}
} Class MyList
{public
Node CurrentNode;
public Node Point;
Public MyList ()
{
CurrentNode = new Node ();
}
Store items public
void Add (int value)
{
//First
if (point = = null)
{
Currentnode.num = value;< C43/>point = CurrentNode;
}
Else //2 3 4 ... times
{
Node temp = new node ();
Temp.num = value;
Point.next = temp;
Update pointer point
= temp;
}
}
Then, we can test on the client:
public static void Main (string[] args)
{
mylist<int> mlist = new mylist<int> ();
add Element
Mlist.add (1);
Mlist.add (one);
Mlist.add (a);
Mlist.add (1111);
while (Mlist.currentnode!= null)
{
Console.WriteLine (mList.currentNode.num);
Mlist.currentnode = MList.currentNode.next;
}
public static void Main (string[] args)
{
mylist<int> mlist = new mylist<int> ();
add Element
Mlist.add (1);
Mlist.add (one);
Mlist.add (a);
Mlist.add (1111);
while (Mlist.currentnode!= null)
{
Console.WriteLine (mList.currentNode.num);
Mlist.currentnode = MList.currentNode.next;
}
This is OK if we define an orthopedic set ourselves. It has two advantages: any number of elements can be stored! Easy insertion and deletion of elements.
Definition and simple operation of bidirectional linked list:
The
Bidirectional linked list is actually an improvement of a single linked list. When we operate on a single linked list, there are times when you have to work with the direct precursor of a node, and you must start looking at the table header. This is restricted by the structure of a single linked table node. Because each node of a single linked list has only one chain domain that stores the direct successor node address, can you define a chain domain with both stored direct subsequent node addresses and a two-link domain node structure that stores the address of a direct predecessor node? This is the doubly linked list. In the bidirectional linked list, the node contains the data outside the domain, there are two chain domains, one storage direct successor node address, generally called right chain domain; A storage direct predecessor node address, commonly called the left chain domain.
namespace Dounlylinkedlist {//defines a two-way linked list of nodes public class Node {public Object Element;
Public Node Flink;
Public Node BLink;
Public Node () {Element = null;
Flink = null;
BLink = null;
Public Node (Object element) {element = element;
Flink = null;
BLink = null;
}//The class public class LinkedList {public Node Header of the linked list operation;
Public LinkedList () {header = new Node ("header");
Header.flink = null;
Header.blink = null;
//Lookup Node private node find (item) {Node current = new node ();
current = Header;
while (current.element!= item) {current = Current.flink;
return to current;
}//Insert node public void Insertnode (Object item,object postionitem) {Node current = new node ();
Node NewItem = new node (item);
Current = find (Postionitem); if (current!= null) {NewItem.flink = Current.flink;
Newitem.blink = current;
Current.flink = NewItem;
}//delete node public void Remove (Object) {node P = find (item);
if (P.flink!= null) {p.blink.flink = P.flink;
P.flink.blink = P.blink;
P.blink = null;
P.flink = null;
}//Find the last node element of a two-way list private node FindLast () {Node current = new node ();
current = Header; while (!) (
Current.flink = = null) {current = Current.flink;
return to current;
//Reverse print bidirectional linked list public void PrintReverse () {Node current = new node ();
Current = FindLast (); while (!) (
Current.blink = = null)) {Console.WriteLine (current.element);
current = Current.blink;
}//Print bidirectional linked list public void print () {node current = new Node ();
current = Header; while (!) ( Current.flink = = null)) {CoNsole.
WriteLine (Current.FLink.Element);
current = Current.flink;
}
}
}
}
Linked List Application scenario
Application Scenarios: Set (dynamic array), greedy snake, map cycle generation, slot machine effect, etc., linked list can help us accomplish a lot of things.