C # Data Structure chapter (one chain List Class) Killertang (original)

Source: Internet
Author: User
Tags bool exit empty header insert
C # Data Structure chapter (a) Linear table



Author: Cold Feather Wolf (Dark_slaer_tang)







Recently, the girl ran away, you said that women are always prone to turn. , it seems to be a programmer must "alone, the shadow phase hanging" miserable fate. Or honest programming it, I found that with C # Some data structure of the class also deceive good, so want to put the data structure of the algorithm, with C # rewrite, pass the boring time, the following is the data structure of the realization of the linked list.



First defines the node type, defines the previous pointer field, and the next pointer field, as follows:







Using System;



Namespace List
{
<summary>
Summary description for ListNode.
</summary>



Node class




public class ListNode
{
public ListNode (int newvalue)
{
Value=newvalue;
}



<summary>
Previous one
</summary>




Public ListNode Previous;




<summary>
After a
</summary>




Public ListNode Next;




<summary>
Value
</summary>




public int Value;
}
}



Using System;

Namespace List
{


<summary>
Linked List class
</summary>

After defining the node, the operation of the class linear table is programmed. In the list class, using the, head, Tail, current, three pointers, using Append, Movefrist,moveprevious,movenext,movelast, Delete,insertascending,insertunascending, clear realizes move, add, delete, ascending insert, descending insert, empty list operation, GetCurrentValue () method gets the current value.


public class Clist
{
Public Clist ()

{

Constructors

Class


listcountvalue=0;

Head=null;

Tail=null;


}


<summary>
Head pointer
</summary>


Private ListNode head;


<summary>
Tail pointer
</summary>

Private ListNode Tail;

<summary>
Current pointer
</summary>

Private ListNode current;

<summary>
Number of linked list data
</summary>

private int listcountvalue;

<summary>
Tail Add data
</summary>

public void Append (int datavalue)
{
ListNode newnode=new ListNode (datavalue);

if (IsNull ())

If the header pointer is empty

{
Head=newnode;

Tail=newnode;

}
Else
{
Tail.next =newnode;

Newnode.previous =tail;

Tail=newnode;

}

Current=newnode;

The number of linked list data plus a

Listcountvalue+=1;

}

<summary>
Delete the current data
</summary>


public void Delete ()
{
If the list is empty

if (! IsNull ())
{
If you delete a header

if (IsBof ())
{
Head=current.next;

Current=head;

Listcountvalue-=1;

Return
}

If you delete the tail

if (IsEof ())
{
tail=current.previous;

Current=tail;

Listcountvalue-=1;

Return
}

If you delete intermediate data

Current.Previous.Next =current.next;

current=current.previous;

Listcountvalue-=1;

Return
}


}


<summary>
Move one data back
</summary>


public void MoveNext ()
{
if (! IsEof ()) Current=current.next;
}
<summary>
Move one data forward
</summary>

public void MovePrevious ()
{
if (! IsBof ()) current=current.previous;
}

<summary>
Move to the first data
</summary>

public void Movefrist ()
{
Current=head;
}

<summary>
Move to the last data
</summary>


public void MoveLast ()
{
Current=tail;
}

<summary>
Determine if an empty list
</summary>


public bool IsNull ()
{
if (listcountvalue==0)
return true;

return false;
}

<summary>
Determine whether to reach the tail
</summary>

public bool IsEof ()
{
if (current ==tail)
return true;

return false;
}

<summary>
To determine whether to reach the head
</summary>


public bool IsBof ()
{
if (current ==head)
return true;

return false;

}

public int GetCurrentValue ()
{

return current.value;

}

<summary>
To get the number of data in a linked list
</summary>

public int ListCount
{
Get
{
return listcountvalue;
}
}

<summary>
Clear the linked list
</summary>

public void Clear ()
{
Movefrist ();
while (! IsNull ())
{
If not an empty list, delete from the tail

Delete ();

}
}

<summary>
Insert data before current position
</summary>

public void Insert (int datavalue)

{
ListNode newnode=new ListNode (datavalue);
if (IsNull ())
{
is an empty table, add the

Append (DataValue);

Return

}

if (IsBof ())
{
Insert for Head

Newnode.next =head;

Head.previous =newnode;

Head=newnode;

Current=head;

Listcountvalue+=1;

Return
}

Middle Insert


Newnode.next =current;

Newnode.previous =current.previous;

Current.Previous.Next =newnode;

Current.previous =newnode;

Current=newnode;

Listcountvalue+=1;

}

<summary>
Insert in ascending order
</summary>

public void insertascending (int insertvalue)
{
Parameters: Insertvalue Inserted data


is an empty linked list

if (IsNull ())
{
Add to

Append (Insertvalue);

Return

}

Move Head

Movefrist ();

if ((Insertvalue<getcurrentvalue ()))
{
If the condition is met, insert, exit

Insert (Insertvalue);

Return

}

while (true)

{

if (Insertvalue<getcurrentvalue ())
{

Manchu condition, then insert, exit

Insert (Insertvalue);

Break

}

if (IsEof ())
{
Tail add

Append (Insertvalue);

Break

}

Move to next pointer

MoveNext ();

}
}


<summary>
Insert in descending order
</summary>


public void insertunascending (int insertvalue)
{
Parameters: Insertvalue Inserted data


is an empty linked list

if (IsNull ())
{
Add to

Append (Insertvalue);

Return

}

Move Head

Movefrist ();

if (Insertvalue>getcurrentvalue ())
{
If the condition is met, insert, exit

Insert (Insertvalue);

Return

}

while (true)

{

if (Insertvalue>getcurrentvalue ())
{

Manchu condition, then insert, exit

Insert (Insertvalue);

Break

}

if (IsEof ())
{
Tail add

Append (Insertvalue);

Break

}

Move to next pointer

MoveNext ();

}
}
}
}






OK, a simple list of the class implementation, of course, there are many functions, can be added according to their own needs. To be CONTI


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.