C#之链表使用
2016-10-05 21:28
78 查看
<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(); } } }
相关文章推荐
- C#使用链表结构(intptr)与c语言 dll配合使用。
- c#中内置链表的基本使用
- c# 链表(LinkedList)使用
- C#中链表的用使用LinkedList
- 使用C#循环链表解决约瑟夫环的问题
- c# 链表(LinkedList)使用中的问题(.net framework 2.0)
- 使用C#链表简单实现的约瑟夫环
- C#基础之温习--使用列表和链表实现优一个先级队列
- C# 简单链表的实现下(使用泛型)
- c#中内置链表的基本使用
- 在WinForm中使用Web Services 来实现 软件 自动升级( Auto Update ) (C#) skyover [原作]
- 源码推荐:一个使用C#绘制图形引擎的Framework
- 你还在为使用P/Invoke时,写不出win32 api对应的C#声明而犯愁吗?
- C#中使用Property Grid 控件 学习笔记(一)
- C# Builder 使用心得
- 磁盘配额的WMI版本(还是使用C#)
- 使用C#+ASP.NET来进行RSA(非对称)加密.
- 使用C#开发一个简单的P2P应用
- C#中事件的使用
- 如何使用C#创建一个三层的数据库应用程序