• 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();
            }
        }
    }


    
       
    
    
  • 相关阅读:
    Oracle基础知识整理
    linux下yum安装redis以及使用
    mybatis 学习四 源码分析 mybatis如何执行的一条sql
    mybatis 学习三 mapper xml 配置信息
    mybatis 学习二 conf xml 配置信息
    mybatis 学习一 总体概述
    oracle sql 语句 示例
    jdbc 新认识
    eclipse tomcat 无法加载导入的web项目,There are no resources that can be added or removed from the server. .
    一些常用算法(持续更新)
  • 原文地址:https://www.cnblogs.com/zztong/p/6695191.html
Copyright © 2020-2023  润新知