Single-chain table
Using system;
Namespace CS
{
/// <Summary>
/// Node class
/// For convenience, the node data type is represented by int
/// </Summary>
Public class listnode
{
Public int data; // elemtype
Public listnode ()
{
}
Public listnode next;
}
/// <Summary>
/// Linked list
/// </Summary>
Public class linklist
{
Private listnode first; // The first node
Public linklist ()
{
First = NULL;
}
Public bool isempty ()
{
Return first = NULL;
}
Public int length ()
{
Listnode current = first;
Int length = 0;
While (current! = NULL)
{
Length ++;
Current = current. Next;
}
Return length;
}
/// <Summary>
/// Returns the k-th element to X.
/// </Summary>
/// <Param name = "K"> </param>
/// <Param name = "X"> </param>
/// <Returns> If the k-th element does not exist, false is returned; otherwise, true is returned. </returns>
Public bool find (int K, ref int X)
{
If (k <1)
Return false;
Listnode current = first;
Int Index = 1;
While (index <K & current! = NULL)
{
Current = current. Next;
Index ++;
}
If (current! = NULL)
{
X = current. Data;
Return true;
}
Return false;
}
/// <Summary>
/// Return the position of X
/// </Summary>
/// <Param name = "X"> </param>
/// <Returns> If X is not in the table, 0 is returned. </returns>
Public int search (int x)
{
Listnode current = first;
Int Index = 1;
While (current! = NULL & Current. Data! = X)
{
Current = current. Next;
Index ++;
}
If (current! = NULL)
Return Index;
Return 0;
}
/// <Summary>
/// Delete the k-th element and return its value with x
/// </Summary>
/// <Param name = "K"> </param>
/// <Param name = "X"> </param>
/// <Returns> </returns>
Public linklist Delete (int K, ref int X)
{
// If the k-th element does not exist, an exception is thrown.
If (k <1 | First = NULL)
Throw (New outofboundsexception ());
Listnode pnode = first; // pnode will eventually point to the K Node
// Move the pnode to the k element and delete the element from the linked list.
If (k = 1) // pnode already points to the K element
First = first. Next; // delete it
Else
{
// Use qnode to point to the K-1 Element
Listnode qnode = first;
For (INT Index = 1; index <K-1 & qnode! = NULL; index ++)
Qnode = qnode. Next;
If (qnode = NULL | qnode. Next = NULL)
Throw (New outofboundsexception (); // The K element does not exist.
Pnode = qnode. Next; // pnode points to the K element
Qnode. Next = pnode. Next; // Delete the K element from the linked list.
X = pnode. Data;
}
Return this;
}
/// <Summary>
/// Insert X after the K element
/// </Summary>
/// <Param name = "K"> </param>
/// <Param name = "X"> </param>
/// <Returns> </returns>
Public linklist insert (int K, int X)
{
// If the K element does not exist, an exception outofboundsexception is thrown.
If (k <0)
Throw (New outofboundsexception ());
Listnode pnode = first; // pnode will eventually point to the K Node
For (INT Index = 1; index <K & pnode! = NULL; index ++)
Pnode = pnode. Next;
If (k> 0 & pnode = NULL)
Throw (New outofboundsexception (); // The K element does not exist.
Listnode xnode = new listnode ();
Xnode. Data = X;
If (k> 0)
{
// Insert after pnode
Xnode. Next = pnode. Next;
Pnode. Next = xnode;
}
Else
{
// Insert as the first element
Xnode. Next = first;
First = xnode;
}
Return this;
}
Public void clear ()
{
First = NULL;
}
Public void output ()
{
Listnode current;
For (current = first; current! = NULL; current = current. Next)
{
Console. Write ("{0}", current. Data. tostring ());
}
Console. writeline ();
}
}
}
Columns and stacks
C # queue and stack implementation 20:49:46
Large, medium and small
Queue
/*
* Created by sharpdevelop.
* User: Administrator
* Date: 2007-2-27
* Time:
*
* To change this template use tools | options | coding | edit standard headers.
*/
Using system;
Namespace queueexample
{
Public class queue <t>
{
T [] data;
Int head;
Int rear;
Int count;
Public Queue (INT length)
{
Data = new T [length];
Head = rear = 0;
Count = 0;
}
Public void enqueue (T item)
{
If (! Isfull ())
{
Data [rear] = item;
Rear = (Rear + 1) % data. length;
Count ++;
}
Else
{
Throw new applicationexception ("????! ");
}
}
Public t dequeue ()
{
If (! Isempty ())
{
T item = data [head];
Head ++;
Count --;
Return item;
}
Else
{
Throw new applicationexception ("????! ");
}
}
Public bool isfull ()
{
Return COUNT = data. length;
}
Public bool isempty ()
{
Return COUNT = 0;
}
}
}
//////////
Using system;
Using system. Collections. Generic;
Namespace queueexample
{
Class mainclass
{
Public static void main (string [] ARGs)
{
Queue <string> q = new queue <string> (4 );
Q. enqueue ("11 ");
Q. enqueue ("22 ");
Q. enqueue ("33 ");
Q. enqueue ("44 ");
If (Q. isfull ())
{
Console. writeline ("full ");
}
Else
{
Console. writeline ("not full ");
}
While (! Q. isempty ())
{
Console. writeline (Q. dequeue ());
}
Console. readkey ();
}
}
}
Stack
/*
* Created by sharpdevelop.
* User: Administrator
* Date: 2007-2-27
* Time: 16: 18
*
* To change this template use tools | options | coding | edit standard headers.
*/
Using system;
Using system. Collections. Generic;
Namespace stacktest
{
Class mainclass
{
Public static void main (string [] ARGs)
{
Stack <int> STK = new stack <int> (5 );
// STK. Pop ();
Try {
STK. Push (1 );
STK. Push (2 );
STK. Push (21 );
STK. Push (22 );
Console. writeline (STK. Pop ());
Console. writeline (STK. Pop ());
Console. writeline (STK. Pop ());
Console. writeline (STK. Pop ());
} Catch (exception E)
{
Console. writeline (E. Message );
}
Console. readkey ();
}
}
Public class Stack <t> {
T [] data;
Int top; // battle Top pointer
// Int bottom; // battlefield pointer
Int count; // number of elements
// Int I = 0; // element position
Public stack (INT length ){
Data = new T [length];
}
// Retrieve the site top Element
Public t POP (){
If (isempty () = true ){
Throw new applicationexception ("Stack is empty! ");
}
Else {
T toptemp = data [Top-1];
Top --;
Return toptemp;
// Return data [Top];
}
}
// Pressure station
Public void push (T item ){
If (isfull () = true ){
Throw new applicationexception ("the stack is full! ");
}
Else {
Data [Top] = item;
Count ++;
Top ++;
}
}
// Obtain the number of elements
Public int getcount (){
Return data. length;
}
// Judge whether it is null
Public bool isempty (){
Return COUNT = 0;
}
// Determine whether the resource is full
Public bool isfull (){
Return data. Length = count;
}
}
}