C # data structure and algorithm uncovered three-link list _c# tutorial

Source: Internet
Author: User

We've discussed the simplest linear structure--the sequential table, which we'll discuss in another linear structure--a linked list.

What is linked list, does not require logically adjacent data elements in the physical storage location is also adjacent to store the linear structure called the linked list. As a practical example, if a company held a video conference, can be seen in Beijing office of people in Shanghai, they are like logically adjacent data elements, and physically disconnected. This is like a linked list. The linked list is divided into ① single linked list, ② one-way cyclic chain list, ③ two-way linked list, ④ bidirectional circular chain list.

Before introducing a variety of linked lists, we need to understand such a concept. What is a node. When storing a data element, in addition to storing information about the data element itself, store the address information of the data element that is adjacent to it. These two pieces of information form the storage image (image) of the data element, known as the Node (node). In the C language, these process-oriented languages implement nodes in the form of pointers. NET, is through the simulation pointer-class object nesting form.

Then, first, introduce the single linked list. If the reference domain of a node stores only the storage address of the node's immediate successor node, the linked list is called a single list (singly linked list). Call the Reference field next. The structure of a single linked table node is shown in the figure, and data in the graph indicates the node's field

In reality, it's like a team of blind people crossing the road. As shown in the figure

The Single link table node is regarded as a class, and the class name is node<t>. The implementation of the single linked table node class is shown below.

Copy Code code as follows:

public class Node<t>
{
A group of blind people cross the road and store the names of the blind.
Private T data; An object that stores data in a data field
A group of blind people crossing the road pointed to the next blind object.
Private node<t> Next; The reference field points to the next object

Construction device
Public Node (T Val, node<t> p)
{
data = Val;
Next = p;
}

Construction device
Public Node (node<t> p)
{
Next = p;
}

Construction device
Public Node (T val)
{
data = Val;
next = null;
}

Construction device
Public Node ()
{
Data = default (T);
next = null;
}

Data Field Properties
Public T Data
{
Get
{
return data;
}
Set
{
data = value;
}
}

Reference Field Properties
Public node<t> Next
{
Get
{
return to Next;
}
Set
{
Next = value;
}
}
}


The following figure is a schematic diagram of a chain-type storage structure corresponding to a linear table (A1,A2,A3,A4,A5,A6).

The implementation of the linklist<t> source code for the single linked list class is shown below. First of all, he inherits the interface with Ilistds.

This is a simulation of a blind man crossing the road.

public class Linklist<t>: ilistds<t> {

The blind in the first place

Private node<t> head; Header reference for a single linked list

Header Reference Property
Public node<t> Head
{
Get
{
return head;
}

Set
{
head = value;
}
}

Construction device

At first there was no blind person, and the head node points to an empty position. A blind man without a queue
Public linklist ()
{
head = NULL;
}

Here we seek the length of the blind, from the first blind number, then the second, the third. And so on ... The length of the Blind man's team will come out.

To find the length of a single linked list
public int GetLength ()
{
Node<t> p = head;

int len = 0;
while (P!= null)
{
++len;
p = p.next;
}
return Len;
}

To keep the blind in line is to make the head of this team non-existent.

Empty single linked list
public void Clear ()
{
head = NULL;
}

Judged by a blind man's queue was not as empty as to see that his head was not someone

Determine if a single linked list is empty
public bool IsEmpty ()
{
if (head = = null)
{
return true;
}
Else
{
return false;
}
}

Add a new element at the end of a single linked list
public void Append (T item)
{
node<t> q = new node<t> (item);
node<t> p = new node<t> ();
If there's no blind man in line, it's in the head of the queue.


if (head = = null)
{
head = q;
Return
}
Don't understand, everything in the legend

If someone is in line, go through it from the ground up and let him have no place to join the queue and point the pointer at the back.
p = head;
while (P.next!= null)
{
p = p.next;
}

P.next = q;

Do not understand everything in the legend

The algorithm complexity of this method is O (n)
}

is to increase the number of people who queue up in the first team.

Inserts a node that has a value of item before the position of the first node of the single linked list
public void Insert (T item, int i)
{
if (IsEmpty () | | I < 1)
{
Console.WriteLine ("The List is empty or Position is error!");
Return
}
Is the position of the head knot, put his head in power to point with him, and put the other pointer with his

if (i = = 1)
{
node<t> q = new node<t> (item);
Q.next = head;
head = q;
Return
}
Do not understand, as shown in the picture:

And this is looping it to the appropriate position in the queue, and inserting it from scratch into this position

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);
Q.next = p;
R.next = q;
}

It's all in the legend.

}

The algorithm complexity of this method O (n)

Delete the first node of a single linked list
Public T Delete (int i)
{

Is it that the blind line or the queue position is not correct this returns an error message
if (IsEmpty () | | I < 0)
{
Console.WriteLine ("Link is empty or Position is
Error! ");
return default (T);
}
It's the head node. Returns the position of the second node to the top of the first node

node<t> q = new node<t> ();

if (i = = 1)
{
Q = head;
Head = head. Next;
return q.data;
}
This step is O (1)

Not the head position of the bar, just look for the corresponding location of the node in the delete. The man in front of the line pointed to the man behind him. This is the new team. If not found, return the error.
Node<t> p = head;
int j = 1;

while (P.next!= null&& J < i)
{
++j;
Q = p;
p = p.next;
}

if (j = = i)
{
Q.next = P.next;
return p.data;
}
Else
{
Console.WriteLine ("The ith node is not exist!");
return default (T);
}

Do not understand as shown in the picture:

The run-time complexity of this method is O (n)
}

Get the first data element of a single linked list

Know the team I'm going to query out the ranks of the nth person who is that,
Public T Getelem (int i)
{

If it's empty, return to the wrong result.
if (IsEmpty ())
{
Console.WriteLine ("List is empty!");
return default (T);
}

The number of points from the graph point N of the result
node<t> p = new node<t> ();
p = head;
int j = 1;

while (P.next!= null&& J < i)
{

++j;
p = p.next;
}
It returns without returning an error.

if (j = = i)
{
return p.data;
}
Else
{
Console.WriteLine ("The ith node is not exist!");
return default (T);
}
}

Do not understand, everything in the legend.

The complexity of the time of this method is O (n)

I want to find out where Zhang Shan Gujiao is in the ranks.

Find a node in a single list that values are value
public int Locate (T value)
{

Null returned as False
if (IsEmpty ())
{
Console.WriteLine ("List is empty!");
return-1;
}

node<t> p = new node<t> ();
p = head;
int i = 1;

To return to the corresponding index by traversing the comparer equally

while (!p.data.equals (value) && p.next!= null)
{
P = P.next;
++i;
}
Do not understand, as shown in the picture:


return i;

The complexity of this algorithm is O (n2)
}

}

In this section we discuss the basic operation of the linked list, and draw to prove that in the next session we will discuss two-way linked lists, circular list application examples.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.