zoukankan      html  css  js  c++  java
  • C#之链表使用

    <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:	est1.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();
            }
        }
    }


    
       
    
    
  • 相关阅读:
    查询死锁和处理死锁(SqlServer)
    日期函数(SqlServer)
    [Shell] echo/输出 中引用命令
    Github 团队协作基本流程与命令操作 图解git工作流程
    HTML 引入 CSS、JS 的三种方式
    JavaScript 字符串匹配 | JS 的正则用法 | 从后边匹配
    Sublime + Chrome 本地调试 CSS 选择器
    常用 CSS 选择器
    使用 Sublime 或其他编辑器调试 Tampermonkey 油猴脚本
    使用 chrome 扩展 Vimium 实现快捷键关闭其他标签页
  • 原文地址:https://www.cnblogs.com/zztong/p/6695191.html
Copyright © 2011-2022 走看看