C # implementation-single-chain table (chain)

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace singlelinkedlist
{
Class Program
{
Static void main (string [] ARGs)
{

// Instance call

}
}

// Define the node of a single-chain table
// The address (reference) of the node that stores data and the next node. A class is used here.
Public class node <t>
{
Private t data; // data
Private node <t> next; // reference

// Constructor
Public node (T Val, node <t> P)
{
Data = val;
Next = P;
}

// Constructor 2
Public node (node <t> P)
{
Next = P;
}

// Constructor 3
Public node (T Val)
{
Data = val;
Next = NULL;
}

// Constructor 4
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 ;}
}
}


// Single-chain table class-some methods for defining operation nodes (such as deletion and insertion ).
// Ilist <t> is an API provided by. net. This API is implemented here.
Public class linklist <t>: ilist <t>
{
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
Public linklist ()
{
Head = NULL;
}

/// <Summary>
/// Calculate the length of a single-chain table
/// You need to start from the header and traverse one node at a node until the end of the table.
/// </Summary>

Public int getlength ()
{
Node <t> P = head;

Int Len = 0;
While (P! = NULL)
{
++ Len;
P = P. Next;
}

Return Len;
}

/// <Summary>
/// Clear a single-chain table
/// Head = NULL.
/// After a single-chain table is cleared, the space occupied by the original node is not retained, but is recycled by the garbage collector.
/// </Summary>
Public void clear ()
{
Head = NULL;
}

/// <Summary>
/// Determine whether a single-chain table is empty
/// Head = NULL, that is, null
/// </Summary>
Public bool isempty ()
{
If (Head = NULL)
{
Return true;
}
Else
{
Return false;
}
}

/// <Summary>
/// Additional operations
/// Add new elements to the end of a single-chain table
/// </Summary>
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;
}

// Insert a node with the item value before the I node position 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;
}

// Just a head element. (just insert it before the head)
If (I = 1)
{
Node <t> q = new node <t> (item );
Q. Next = head;
Return;
}

// Non-head (insert P before a certain element in the middle)
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;
}

}

// Insert a node with the item value after the position of node I of the single-chain table.
Public void insertpost (T item, int I)
{
If (isempty () | I <1)
{
Console. writeline ("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;
}
}

// Delete the I node of a single-chain table
Public t Delete (int I)
{
If (isempty () | I <0)
{
Console. writeline ("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;

// Locate I from the beginning
// The condition is: (1). The single-link table is not at the end, (2). It is not at the position where I is located (j <I ).
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 item node is not exist! ");
Return default (t );
}
}

// Obtain the I-th data element of a single-chain table
Public t getelem (int I)
{
If (isempty ())
{
Console. writeline ("list is empty! ");
Return default (t );
}

Node <t> P = new node <t> ();
P = head;
Int J = 1;

While (P. Next! = NULL & J <I)
{
++ J;
P = P. Next;
}

If (j = I)
{
Return P. Data; // found.
}
Else
{
Console. writeline ("the item node is not exist! ");
Return default (t );
}
}

/// <Summary>
/// Find the node with the value in the single-chain table
/// </Summary>
Public int locate (T value)
{
If (isempty ())
{
Console. writeline ("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;
}

 

}


}

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.