【C#的文件和流】链表算法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace mylist
{
    ///<summary>
    /// 结点类
    ///</summary>
    public class ListNode
    {
       public ListNode()
       {
       }
        public string data;
        public ListNode next;
    }
    /// <summary>
    /// 链表类
    /// </summary>
     public class LinkList
   {
     private ListNode head;
     private ListNode current;
        public LinkList()
    {
    head=null;
    current = null;
   }
   /// <summary>
   /// 计算链表长度
   /// <summary>
   public int Length()
   {
    current=head;
    int length=0;
    while(current!= null)
    {
    current =current.next;
    length++;
     }
     return length;
   }
   /// <summary>
   /// 返回第k个元素至x中,如果不存在第k个元素则返回false,否则返回true
   /// </summary>
   public bool Find ( int k, string x)
   {
        if( k<1 || k> Length ())
             throw( new OutOfMemoryException ());
        current=head;
        int index=1;
        while(index<k && current!=null)
        {
          current=current.next;
          index++;
        }       
        if( current !=null)
         {
           x=current.data;
           return true;
        }
       else return false;
    }
    /// <summary>
    ///查找值为x的结点,返回x所在的位置,如果x不在表中则返回0
    /// </summary>
    public int Search (string x)
    {
        ListNode current = head;
        int index=1;
        while( current != null && current.data !=x)
        {
          current = current.next;
          index++;
        }
        if(current !=null)
           return index;
        return 0;
    }
    /// <summary>
    ///删除第k个元素,并用x返回其值
    /// </summary>
    public LinkList Delete ( int k, string x)
    {
        if( k<1 || head == null )
         throw( new OutOfMemoryException ());
        current = head;
         for( int index=0; index< k && current != null; index++)
           current=current.next;
          if( current == null )
           throw( new OutOfMemoryException());
        ListNode pNode=current;
        current =current.next;
        pNode.next = current.next;
        x = current.data;
        current=pNode.next;
        return this;
    }
    /// <summary>
    /// 在第k个元素之后插入x
    /// </summary>
    public LinkList Insert ( int k, int x)
    {
       if(k<0)
         throw( new OutOfMemoryException () );
       current = head;
       for( int index = 0; index<k && current != null; index++)
         current = current.next;
       if( k>0 && current == null)
         throw( new OutOfMemoryException());
       ListNode xNode = new ListNode ( );
       xNode.data =x; 
       if( k>1 )
       {
          //在xNode之后插入
          xNode.next = current.next;
          current.next= xNode;
       }
       else 
       {
          //作为第一个元素插入
          xNode.next=head;
          head=xNode;
       }
        return this;
    }
    ///<summary>
    ///将链表中的数据存入文件
    ///<summary>
    public void SaveToFile()
    {
        Console.WriteLine("Please input the name of the file");
        string filename = Console.ReadLine ();
        FileStream fs = File.Create(filename);
        StreamWriter sw = new StreamWriter ((System.IO.Stream) fs);
        if (head==null)
          Console.WriteLine("the List is empty, nothing write!");
        else
        {
            current=head.next;
            while (current != null)
            {
               sw.WriteLine(current.data);
               current = current.next;
            }
        }
        sw.Close();
    }
    /// <summary>
    ///从文件中读取数据并放入链表
    /// </summary>
    public LinkList ReadFromFile()
    {
        Console.WriteLine("Please input the name of the file");
        String filename = Console.ReadLine();
             FileStream fs = File.Open(filename,FileMode.Open);
        StreamWriter sr = new StreamWriter((System.IO.Stream) fs);
        ListNode current = new ListNode ();
        head.next = current;
        string filedata;
        do
        {
            filedata = sr.ToString();
            current.data=filedata;
            ListNode lcurrent = new ListNode ( );
            current.next=lcurrent;
            current = lcurrent;
            filedata =sr.ToString();
        }
        while (filedata.Length!=-1);
        sr.Close ();
        return  this;
       }
     }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vatebur

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值