<pre name="code" class="csharp">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace test
{
    class Node     //结点
    {
        public string data;
        public Node next;
    }
    class Program
    {
        public Node head=new Node();    //头结点
        public bool CreateLink()   //创建链表
        {
            head.next = null;
            //Node temp=new Node()       //位置放错
            string path = @"F:\test1.txt";
            try
            {
                StreamReader sr = new StreamReader(path, Encoding.Default);
                string st;
                while ((st = sr.ReadLine()) != null)
                {  //将文件中的数据读入到链表中
                    string[] ss = st.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < ss.Length; i++)
                    {
                        Node temp = new Node();  //创建临时节点  重要位置
                        temp.data = ss[i];
                        temp.next = head.next;
                        head.next = temp;
                    }
                }
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Data);
                return false;
            }
            return true;
        }
        public int GetLength()    //获取链表head的长度
        {
            int length;     //长度
            length = 0;
            Node temp=new Node();
            temp=head;
            while (temp.next != null)
            {
                length++;
                temp = temp.next;
            }
            return length;
        }
        public bool InsertNode(int position,string s)  //在位置position插入数据为s的节点
        {
            if (position <= 0 || position > GetLength() + 1)  //越界
            {
                Console.WriteLine("插入位置越界");
                return false;
            }
            else
            {
                Node newNode = new Node();
                newNode.data = s;
                Node temp = head;
                for (int i = 0; i < position; i++)
                {
                    if (i == position - 1)
                    {
                        newNode.next = temp.next;
                        temp.next = newNode;
                    }
                    temp = temp.next;
                }
            }
            return true;
        }
        public bool GetItem(int position, ref string s)  //获取位置position的元素s
        {
            if (position < 1 || position > GetLength())   //越界
            {
                Console.WriteLine("不存在此位置");
                return false;
            }
            else
            {
                Node temp = new Node();
                temp = head;
                for (int i = 0; i < position; i++)
                {
                    if (i == position - 1)
                    {
                        s = temp.next.data;
                    }
                    temp = temp.next;
                }
            }
            return true;
        }
        public bool deleteItem(int position)     //删除position位置的元素
        {
            if (position < 1 || position > GetLength())
            {
                Console.WriteLine("不存在此位置");
            }
            Node temp = new Node();
            temp = head;
            for (int i = 0; i < position; i++)
            {
                if (i == position - 1)
                {
                    temp.next = temp.next.next;
                }
                temp = temp.next;
            }
            return true;
        }
        public void OutLink()   //输出链表
        {
            Node temp=new Node();
            temp=head;
            while(temp.next!=null)
            {
                Console.WriteLine(temp.data);
                temp = temp.next;
            }
        }
        static void Main()
        {
            Program p = new Program();
            if (p.CreateLink())
            {
                Console.WriteLine("创建成功");
            }
            Console.WriteLine("链长为{0}",p.GetLength());
            p.OutLink();
            p.InsertNode(8, "seweo");
            p.OutLink();
            string s="";
            p.GetItem(0, ref s);
            Console.WriteLine("第三个元素为{0}", s);
            p.deleteItem(4);
            p.OutLink();
        }
    }
}



            

本文转载:CSDN博客