單鏈表
using System;
namespace CS
{
/// <summary>
/// 結點類
/// 為方便起見,結點資料類型用int表示
/// </summary>
public class ListNode
{
public int data; //ElemType
public ListNode()
{
}
public ListNode next;
}
/// <summary>
/// 鏈表類
/// </summary>
public class LinkList
{
private ListNode first; //第一個結點
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>
/// 返回第k個元素至x中
/// </summary>
/// <param name="k"></param>
/// <param name="x"></param>
/// <returns>如果不存在第k個元素則返回false,否則返回true</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>
/// 返回x所在的位置
/// </summary>
/// <param name="x"></param>
/// <returns>如果x不在表中則返回0</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>
/// 刪除第k個元素,並用x返回其值
/// </summary>
/// <param name="k"></param>
/// <param name="x"></param>
/// <returns></returns>
public LinkList Delete( int k, ref int x )
{
//如果不存在第k個元素則引發異常
if( k<1 || first == null )
throw( new OutOfBoundsException() );
ListNode pNode = first; //pNode將最終指向第k個結點
//將pNode移動至第k個元素,並從鏈表中刪除該元素
if( k == 1 ) //pNode已經指向第k個元素
first = first.next; //刪除之
else
{
//用qNode指向第k-1個元素
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() );//不存在第k個元素
pNode = qNode.next; //pNode指向第k個元素
qNode.next = pNode.next; //從鏈表中刪除第k個元素
x = pNode.data;
}
return this;
}
/// <summary>
/// 在第k個元素之後插入x
/// </summary>
/// <param name="k"></param>
/// <param name="x"></param>
/// <returns></returns>
public LinkList Insert( int k, int x )
{
//如果不存在第k個元素,則引發異常OutOfBoundsException
if( k<0 )
throw( new OutOfBoundsException() );
ListNode pNode = first; //pNode將最終指向第k個結點
for( int index = 1; index<k && pNode != null; index++ )
pNode = pNode.next;
if( k>0 && pNode == null )
throw( new OutOfBoundsException() );//不存在第k個元素
ListNode xNode = new ListNode();
xNode.data = x;
if( k>0 )
{
//在pNode之後插入
xNode.next = pNode.next;
pNode.next = xNode;
}
else
{
//作為第一個元素插入
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();
}
}
}
對列和堆棧
c#實現隊列和棧 2007-02-28 20:49:46
大 中 小
隊列
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2007-2-27
* Time: 15:07
*
* 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("已滿");
}
else
{
Console.WriteLine("未滿");
}
while(!q.IsEmpty())
{
Console.WriteLine(q.DeQueue());
}
Console.ReadKey();
}
}
}
棧
/*
* 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;//戰頂指標
//int bottom;//戰地指標
int count;//元素個數
//int i=0;//元素位置
public Stack(int length){
data =new T[length];
}
//取站頂元素
public T Pop(){
if(isEmpty()==true){
throw new ApplicationException("棧為空白!");
}
else{
T topTemp=data[top-1];
top--;
return topTemp;
// return data[top];
}
}
//壓站
public void Push(T item){
if(isFull()==true){
throw new ApplicationException("棧已滿!");
}
else{
data[top]=item;
count++;
top++;
}
}
//擷取元素個數
public int getCount(){
return data.Length;
}
//判斷是否為空白
public bool isEmpty(){
return count==0;
}
//判斷是否滿
public bool isFull(){
return data.Length==count;
}
}
}