c#实现单链表

  单链表

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;
 
 }
 }
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值