C # three linked lists of data structures and algorithms

Source: Internet
Author: User

We have discussed a simple linear structure-sequence table. In this section, we will discuss another linear structure-linked list.

What is a linked list? logically adjacent data elements are not required to be stored in a linear structure adjacent to the physical storage location. For example, if a company holds a video conference, the Beijing company can see the people in the Shanghai branch. They are like logically adjacent data elements, physically not connected. This is like a linked list. The linked list is divided into 1 single-chain table, 2 one-way cyclic linked list, 3 two-way linked list, and 4 two-way cyclic linked list.

Before introducing various linked lists, we need to understand this concept. What is a node. When storing data elements, in addition to storing the information of the data elements, it also stores the storage address information of adjacent data elements. These two pieces of information constitute the storage Image of the data element, which is called a Node ). In C language-oriented process languages, the implementation node is in the form of pointers. Net is nested by simulating Pointers-class objects.

First, we will introduce the single-chain table. If the reference domain of a node only stores the storage address of the direct successor node of the node, the Linked List is called a Singly Linked List ). Call the reference domain "next. The structure of a Single-linked table node. data in the figure indicates the data domain of the node.

In reality, it is like a team of blind people crossing the road.

Consider a single-linked table Node as a class named Node <T>. The implementation of the Single-linked table node class is as follows.

Copy codeThe Code is as follows: public class Node <T>
{
// A team of blind people stored the names of blind people when crossing the road
Private T data; // data object stored in the data domain
// A team of blind people crossing the road points to the next blind object.
Private Node <T> next; // The reference domain points to the next object.

// Constructor
Public Node (T val, Node <T> p)
{
Data = val;
Next = p;
}

// Constructor
Public Node (Node <T> p)
{
Next = p;
}

// Constructor
Public Node (T val)
{
Data = val;
Next = null;
}

// Constructor
Public Node ()
{
Data = default (T );
Next = null;
}

// Data domain attributes
Public T Data
{
Get
{
Return data;
}
Set
{
Data = value;
}
}

// Reference domain attributes
Public Node <T> Next
{
Get
{
Return next;
}
Set
{
Next = value;
}
}
}

It is a chain storage structure corresponding to a linear table (a1, a2, a3, a4, a5, a6.

The implementation of the source code of the LinkList <T> Single-chain table class is described as follows. First, he inherits the IListDS interface.

// This is a simulation of a class for the blind to cross the road

Public class LinkList <T>: IListDS <T> {

// The blind person in the first position

Private Node <T> head; // header reference of a single-chain table

// Header reference attribute
Public Node <T> Head
{
Get
{
Return head;
}

Set
{
Head = value;
}
}

// Constructor

// At the beginning, no blind person exists, and the header node points to an empty position. Blind people with no Headers
Public LinkList ()
{
Head = null;
}

// Here we seek the length of the blind team, starting from the first blind, then the second, and the third. And so on... In this way, the length of the blind team can be obtained.

// Calculate the length of a single-chain table
Public int GetLength ()
{
Node <T> p = head;

Int len = 0;
While (p! = Null)
{
++ Len;
P = p. Next;
}
Return len;
}

// Do not let the blind man line up, that is, make the team's head not exist

// Clear a single-chain table
Public void Clear ()
{
Head = null;
}

// Judge whether a blind queue is empty and check whether there is someone in its header.

// Determine whether a single-chain table is empty
Public bool IsEmpty ()
{
If (head = null)
{
Return true;
}
Else
{
Return false;
}
}

// Add new elements to the end of a single-chain table
Public void Append (T item)
{
Node <T> q = new Node <T> (item );
Node <T> p = new Node <T> ();
// If there is no blind person in the queue, it will be in the queue's header.

If (head = null)
{
Head = q;
Return;
}
// Do not understand, everything is in the legend

// If someone is waiting in a queue, traverse from the ground up and let him join the queue from nobody's place and point the pointer to the back of the queue.
P = head;
While (p. Next! = Null)
{
P = p. Next;
}

P. Next = q;

Everything you don't know is in the legend.

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

// Adds the staff to the queue.

// Insert a node with the item value before the position of node I of the single-chain table
Public void Insert (T item, int I)
{
If (IsEmpty () | I <1)
{
Console. WriteLine ("List is empty or Position is error! ");
Return;
}
// It is the position of the head node, and points his head to Him, and points another pointer to him

If (I = 1)
{
Node <T> q = new Node <T> (item );
Q. Next = head;
Head = q;
Return;
}
// Do not understand ,:

// This is to loop it to the corresponding position of the queue, insert it from the beginning to 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;
}

Everything is in the legend

}

Algorithm complexity O (n) of this method)

// Delete the I node of a single-chain table
Public T Delete (int I)
{

// Is it because the blind queue or the queue location is incorrect? An error message is returned.
If (IsEmpty () | I <0)
{
Console. WriteLine ("Link is empty or Position is
Error! ");
Return default (T );
}
// If it is a header node, return the position from the second node to 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)

// If it is not the header position, find the corresponding node and delete it. The person in front of the queue points to the person in the back. If the new team is not found, an error is returned.
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:

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

// Obtain the I-th data element of a single-chain table

// Know the team. I want to query the n person in the team,
Public T GetElem (int I)
{

// If it is null, the returned result is an error.
If (IsEmpty ())
{
Console. WriteLine ("List is empty! ");
Return default (T );
}

// The result of number n in the graph
Node <T> p = new Node <T> ();
P = head;
Int j = 1;

While (p. Next! = Null & j <I)
{

++ J;
P = p. Next;
}
// If yes, an error is returned If no result is returned.

If (j = I)
{
Return p. Data;
}
Else
{
Console. WriteLine ("The ith node is not exist! ");
Return default (T );
}
}

If you don't understand it, everything is in the legend.

The time complexity of this method is O (n)

// I want to query the number of positions in the team of zhangshan

// Find the node with the value in the single-chain table
Public int Locate (T value)
{

// Null returns false
If (IsEmpty ())
{
Console. WriteLine ("List is Empty! ");
Return-1;
}

Node <T> p = new Node <T> ();
P = head;
Int I = 1;

// Returns the corresponding index if the comparator is equal from the ground up.

While (! P. Data. Equals (value) & p. Next! = Null)
{
P = p. Next;
++ I;
}
Do not understand ,:

Return I;

The complexity of this algorithm is O (n2)
}

}

In this section, we will discuss the basic operations of the linked list and draw a picture to prove that we will discuss two-way linked list and the application example of the circular linked list in the next session.

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.