用C#基本语句实现的链表算法

链表基本操作实现
本文介绍了一个简单的链表实现,包括插入、删除、查找等基本操作,并提供了完整的C#代码示例。
ContractedBlock.gifExpandedBlockStart.gifCode
using System;

namespace AlgorithmsLinkList
{
    
class LinkList //结点类
    {
       
public int data;//数据域(可为任何类型,可以有多个)
       public LinkList link;//指针域(引用)

    }
//end LinkList
    class Chain //操作类
    {
      
private LinkList first;//头指针
      
      
public Chain()
      {
         first
=null;
      }
//构造函数
      public bool IsEmpty()//判断链表是否为空
      {
        
return first==null;
      }
//end IsEmpty()
      public int Length()//链表的长度
      {
        LinkList current
=first;
        
int len=0;
        
while(current!=null)
        {
          len
++;
          current
=current.link;//指向下一个结点
        }
        
return len;
      }
//end Length()
      public bool Find(int k,int x)//查找第K个元素
      {
          
if(k<1)
          {
              
return false;
          }
//end if
          LinkList current=first;
          
int index=1;//current的索引
          while(index<&& current!=null)
          {
              current
=current.link;//指向下一个结点
              index++;
          }
//end while
          if(current!=null)
          {
            x
=current.data;
            
return true;
          }
//end if
          return false;
          
      }
//end Find()
      public int Search(int x)//查找元素在那个位置
      {
        LinkList current
=first;
        
int index=1;
        
while(current!=null && current.data!=x)
        {
          current
=current.link;
          index
++;
        }
//end while
        if(current!=null)
        {
          
return index;
        }
//end if
        return 0;
      }
//end Search()
      public void Delete(int k,int x)//删除算法
      {
        LinkList p
=first;
        
if(k==1//如果删除的表头
        {
         first
=first.link;
        }
//end if
        else //如果删除的是表的其他位置
        {
          LinkList q
=first;
          
for(int index=1;index<k-1&&p!=null;index++)
          {
            q
=q.link;
          }
//end for
          p=q.link;
          q.link
=p.link;
        }
//end else
        x=p.data;
        
      }
//end Delete()
      public void Insert(int k,int x)//插入算法
      {
         LinkList p
=first;
         
for(int index=1;index<&& p!=null;index++//查找插入位置
         {
           p
=p.link;
         }
//end for
         LinkList y=new LinkList(); //创造新结点
         y.data=x;
         
if(k!=0//如果在P后插入
         {
            y.link
=p.link;
            p.link
=y;
         }
//end if
         else //作为第一个元素插入
         {
           y.link
=first;
           first
=y;
         }
//end else
      }//end Insert()
      public void Output()//输出链表信息
      {
        LinkList current;
        
for(current=first;current!=null;current=current.link)
            Console.Write(
"{0}->",current.data);
        Console.WriteLine();
      }
//end Output()
          
    }
//end class Chain 

转载于:https://www.cnblogs.com/batv2009/archive/2009/08/02/1536758.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值