Single linked list
Using System;
Namespace CS
{
<summary>
Node class
For convenience, node data types are represented by int
</summary>
public class ListNode
{
public int data; Elemtype
Public ListNode ()
{
}
Public ListNode Next;
}
<summary>
Linked List class
</summary>
public class Linklist
{
Private ListNode; The first node.
Public linklist ()
{
i = null;
}
public bool IsEmpty ()
{
Return of the ' a ' = null;
}
public int Length ()
{
listnode current = i
int length = 0;
while (current!= null)
{
length++;
current = Current.next;
}
return length;
}
///<summary>
///returns the K element to x
///</summary>
///<param name= "K" ></PARAM>
///<param name= "x" ></PARAM>
///<returns> returns False if the K element is not present, otherwise returns TRUE</RETURNS>
public bool Find (int k, ref int X
{
if (k<1)
return false;
listnode current = i;
int index = 1;
while (index<k && current!= null)
{
current = Current.next;
index++;
}
if (current!= null)
{
x = current.data;
return true;
}
return false;
}
<summary>
Returns the location of X
</summary>
<param name= "x" ></param>
<returns> return 0</returns> If X is not in the table
public int Search (int x)
{
ListNode current = i;
int index = 1;
while (current!= null && current.data!=x)
{
current = Current.next;
index++;
}
if (current!= null)
return index;
return 0;
}
<summary>
Deletes the k element and returns its value with X
</summary>
<param name= "K" ></param>
<param name= "x" ></param>
<returns></returns>
public linklist Delete (int k, ref int x)
{
Throws an exception if the K element is not present
if (k<1 | | | = NULL)
Throw (new OutOfBoundsException ());
ListNode pnode = A; Pnode will eventually point to K nodes.
Move Pnode to the K element and remove the element from the list
if (k = = 1)//pnode has pointed to the K element
i = First.next; Delete the
Else
{
Point k-1 element with Qnode
ListNode Qnode = A;
for (int index=1; index< k-1 && qnode!= null; index++)
Qnode = Qnode.next;
if (Qnode = null | | qnode.next = NULL)
Throw (new OutOfBoundsException ());//no k element exists
Pnode = Qnode.next; Pnode Point K Element
Qnode.next = Pnode.next; Remove the k element from the 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 is not present, an exception is thrown outofboundsexception
if (k<0)
Throw (new OutOfBoundsException ());
ListNode pnode = A; Pnode will eventually point to K nodes.
for (int index = 1; index<k && pnode!= null; index++)
Pnode = Pnode.next;
if (k>0 && pnode = null)
Throw (new OutOfBoundsException ());//no k element exists
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 = A;
i = XNode;
}
return this;
}
public void Clear ()
{
i = null;
}
public void OutPut ()
{
ListNode current;
for (current = the!= null; current = Current.next)
{
Console.Write ("{0}", current.data.ToString ());
}
Console.WriteLine ();
}
}
}
On columns and stacks
C # implementation queues and stacks 2007-02-28 20:49:46
Big Middle
Queue
/*
* Created by SharpDevelop.
* User:administrator
* date:2007-2-27
* time:15:07
*
* To change the 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 the 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;//element number
int i=0;//element position
public Stack (int length) {
Data =new T[length];
}
Take the top element of a station
Public T Pop () {
if (IsEmpty () ==true) {
throw new ApplicationException ("Stack is empty.") ");
}
else{
T Toptemp=data[top-1];
top--;
return toptemp;
return data[top];
}
}
Press Station
public void Push (T item) {
if (Isfull () ==true) {
throw new ApplicationException ("Stack is full.") ");
}
else{
Data[top]=item;
count++;
top++;
}
}
Get the number of elements
public int GetCount () {
return data. Length;
}
To determine if it is empty
public bool IsEmpty () {
return count==0;
}
Judge whether full
public bool Isfull () {
return data. Length==count;
}
}
}