asp.net C # data structure single linked list linklist

Source: Internet
Author: User

Continue to send data structure series ~ Today is a single linked list.

Class Diagram:

The code for the interface does not duplicate

The code is as follows Copy Code

public class Node<t>
{
Private T _data;
Private node<t> _next;

Public T Data
{
get {return _data;}
set {_data = value;}
}

Public node<t> Next
{
get {return _next;}
set {_next = value;}
}

Public Node (T Val, node<t> p)
{
_data = val;
_next = p;
}

Public Node (node<t> p)
{
_next = p;
}

Public Node (T val)
{
_data = val;
_next = null;
}

Public Node ()
{
_data = Default (T);
_next = null;
}
}

Single-linked list class Linklist<t>

public class Linklist<t>: ilistds<t>
{
Private node<t> _head;

Public node<t> Head
{
Get
{
return _head;
}
Set
{
_head = value;
}
}

    public int Length
    {
        get
        {
             node<t> p = _head;
            int len = 0;
            while (P!= null)
             {
                 ++len;
                p = p.next;
           }
            return len;
       }
   }

    public bool IsEmpty
    {
        get
        {
             if (_head = null)
            {
                 return true;
           }
            Else
             {
                 return false;
           }
       }
   }

Public linklist ()
{
_head = null;
}

public void Clear ()
{
_head = null;
}

    public void Append (T item)
    {
         node<t> q = new node<t> (item);
        node<t> p = new node<t> ();
        if (_head = null)
        {
            _head = q;
            return;
       }
        p = _head;
        while (p.next!= null)
         {
            p = p.next
        }
        p.next = q;
   }

public void Insert (T item, int i)
{
if (IsEmpty | | i < 1)
{
The List is empty or Position is 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);
Q.next = p;
R.next = q;
}
}

public void Insertpost (T item, int i)
{
if (IsEmpty | | i < 1)
{
The List is empty or Position is error!
Return
}
if (i = = 1)
{
node<t> q = new node<t> (item);
Q.next = _head.next;
_head.next = q;
Return
}
node<t> p = _head;
int j = 1;
while (P!= null && J < i)
{
p = p.next;
++j;
}
if (j = = i)
{
node<t> q = new node<t> (item);
Q.next = P.next;
P.next = q;
}
}

Public T Delete (int i)
{
if (IsEmpty | | I < 0)
{
Link is empty or Position is 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)
{
++j;
Q = p;
p = p.next;
}
if (j = = i)
{
Q.next = P.next;
return p.data;
}
Else
{
The ith node is not exist!
return default (T);
}
}

Public T getelement (int i)
{
Convert to physical order bit
I.;

if (IsEmpty)
{
List is empty!
return default (T);
}
node<t> p = new node<t> ();
p = _head;
int j = 0;
while (P.next!= null && J < i)
{
++j;
p = p.next;
}
if (j = = i)
{
return p.data;
}
Else
{
The ith node is not exist
return default (T);
}
}

public int Locate (T value)
{
if (IsEmpty)
{
List is empty!
return-1;
}
node<t> p = new node<t> ();
p = _head;
int i = 1;
while (!p.data.equals (value) && p.next!= null)
{
p = p.next;
++i;
}
return i;
}
}

private static void Linklisttest ()
{
Console.WriteLine ("Linklist created:n---------------------------------");
linklist<string> lklist = Createllisthead (new string[] {"A", "B", "C", "D"});
Printlinklist (lklist);
Console.WriteLine ("N");

Console.WriteLine ("Post Insert" World "into index 2:n---------------------------------");
Lklist.insertpost ("World", 2);
Printlinklist (lklist);
Console.WriteLine ("N");

Console.WriteLine ("Insert" Hello "into index 2:n---------------------------------");
Lklist.insertpost ("Hello", 2);
Printlinklist (lklist);
Console.WriteLine ("N");

Console.WriteLine ("Append" E ": N---------------------------------");
Lklist.append ("E");
Printlinklist (lklist);
Console.WriteLine ("N");

Console.WriteLine ("Delete item on index 5:n---------------------------------");
Lklist.delete (5);
Printlinklist (lklist);
Console.WriteLine ("N");

Console.WriteLine ("Locate Item" Hello ": N---------------------------------");
Console.WriteLine ("#{0}", Lklist.locate ("Hello"));
Console.WriteLine ();

Console.WriteLine ("Clear Linklist:n---------------------------------");
Lklist.clear ();
Printlinklist (lklist);
Console.WriteLine ("N");

Console.readkey ();
}

public static linklist<string> Createllisthead (string[] strs)
{
linklist<string> L = new linklist<string> ();
foreach (var item in STRs)
{
node<string> p = new node<string> (item);
P.next = L.head;
L.head = p;
}
return L;
}

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.