Data structure (C #): Single linked list

Source: Internet
Author: User
Tags count

Compared with the sequential table, the linked list has its own characteristics: INSERT, delete operation without moving elements, efficient implementation of dynamic memory allocation, but not by node index fast to the node, because the need to record pointer fields, the system overhead.

This article describes the implementation of a single linked list, using the interface defined in the previous article.

Code:

/*
* File:SingleLinkedList.cs
* Author:zhenxing Zhou
* date:2008-12-06
* blog:http://www.xianfen.net/
*/
Using System;
Using System.Collections;
Using System.Collections.Generic;

Namespace Xianfen.Net.DataStructure
{
Defining nodes
Internal class singlelinkedlistnode<t>
{
Private T m_value;
Private singlelinkedlistnode<t> M_next;

Public T Value
{
get {return m_value;}
set {m_value = Value;}
}

Public singlelinkedlistnode<t> Next
{
get {return m_next;}
set {M_next = value;}
}

Public Singlelinkedlistnode ()
{
M_next = null;
}

Public Singlelinkedlistnode (t)
: This ()
{
Value = t;
}
}

Realize single linked list
public class Singlelinkedlist<t>: ilinearlist<t>
{
private int m_count;
Private singlelinkedlistnode<t> M_head;

Public Singlelinkedlist ()
{
M_count = 0;
M_head = new singlelinkedlistnode<t> ();
}

Public Singlelinkedlist (t)
: This ()
{
M_count = 1;
M_head.next = new singlelinkedlistnode<t> (T);
}

public bool IsEmpty
{
get {return m_count = = 0;}
}

public int Count
{
get {return m_count;}
}

public void Add (t)
{
InsertAt (t, this. Count);
}

public void AddHead (T-t)
{
InsertAt (t, 0);
}

public void AddTail (T-t)
{
ADD (t);
}

public void InsertAt (T t, int POS)
{
if (pos > this. Count | | POS < 0)
{
throw New IndexOutOfRangeException ("POS");
}

if (m_count = Int. MaxValue)
{
throw new ArithmeticException ();
}

singlelinkedlistnode<t> CurrentNode = M_head;

while (pos--> 0)
{
CurrentNode = Currentnode.next;
}

singlelinkedlistnode<t> Tempnode = new singlelinkedlistnode<t> (T);

Tempnode.next = Currentnode.next;
Currentnode.next = Tempnode;
m_count++;
}

public void RemoveTail ()
{
RemoveAt (this. COUNT-1);
}

public void RemoveHead ()
{
RemoveAt (0);
}

public void RemoveAt (int pos)
{
if (pos >= Count | | pos < 0)
{
throw New IndexOutOfRangeException ("POS");
}

singlelinkedlistnode<t> CurrentNode = M_head;

while (pos--> 0)
{
CurrentNode = Currentnode.next;
}

Currentnode.next = CurrentNode.Next.Next;
m_count--;
}

public void RemoveAll ()
{
M_head.next = null;
M_count = 0;
}

public void Clear ()
{
RemoveAll ();
}

Public T GetHead ()
{
Return GetAt (0);
}

Public T GetTail ()
{
Return GetAt (this. COUNT-1);
}

Public T GetAt (int pos)
{
Return GetNodeAt (POS). Value;
}

Private singlelinkedlistnode<t> getnodeat (int pos)
{
if (pos >= Count | | pos < 0)
{
throw New IndexOutOfRangeException ("POS");
}

singlelinkedlistnode<t> CurrentNode = M_head;

while (pos--> 0)
{
CurrentNode = Currentnode.next;
}

return currentnode.next;
}

public void SetAt (int pos, T t)
{
GetNodeAt (POS). Value = t;
}

public int find (T t)
{
singlelinkedlistnode<t> node = m_head;
int pos = 0;

while (node = node. Next)!= null)
{
if (node. Value.equals (t))
{
return POS;
}

pos++;
}

return-1;
}

Public ienumerator<t> GetEnumerator ()
{
singlelinkedlistnode<t> node = m_head;

while (node = node. Next)!= null)
{
Yield return node. Value;
}
}

IEnumerator Ienumerable.getenumerator ()
{
return GetEnumerator ();
}
}
}

If you find a bug please leave a message stating that the next article introduces the realization of the double linked list.

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.