C#单向链表的实现

简介:

using System ; public class LinkedList { //嵌套类表示单个节点; private class Node { public Node (object values) { item=values ; } public object item; //数据域; public LinkedList.Node next;//指针域; public override string ToString() { return item.ToString (); } } private int count;//记录元素个数; public int Count { get {return this.count ;} } private Node head;//头指针; public object this[int index]//索引器; { get {return GetByIndex (index).item;} set{GetByIndex (index).item=value ;} } //①添加元素; public void Add(object values) { Node newNode=new Node (values); if(head ==null ) //如果头指针为空; { head =newNode ; } else { GetByIndex(count-1).next=newNode; //插到链表结尾; } count ++; //链表长度+1; } //②在指定索引处插入元素; public void Insert(int index,object values) { Node tempNode; if(index ==0) { if(head ==null ) { tempNode =new Node (values ); tempNode.next =head ; head =tempNode ; } } else { Node preNode=GetByIndex(index-1); //找插入节点的前驱; Node nextNode=preNode.next ; //找插入节点的后继结点; tempNode =new Node (values); preNode.next =tempNode; tempNode.next =nextNode ; } count ++; } //③删除指定索引元素; public void RemoveAt(int index) { if (index ==0) //删除节点为头指针; { head =head.next ; } else { Node preNode=GetByIndex(index-1); if(preNode.next ==null) { throw new ArgumentOutOfRangeException("index","索引超出范围!"); } preNode.next =preNode.next.next ; } count --; } public override string ToString() { string s=""; for(Node temp=head; temp!=null; temp=temp.next) { s+=temp.ToString ()+" "; } return s; } private Node GetByIndex(int index) { if((index <0)||(index >= this.count )) { throw new ArgumentOutOfRangeException("index","索引超出范围!"); } Node tempNode=this.head ; for(int i=0;i<index ;i++) { tempNode=tempNode.next ; } return tempNode ; } } class App { static void Main() { LinkedList lst=new LinkedList (); Console .WriteLine("①添加元素:"); lst .Add (0); lst .Add (1); lst .Add (2); lst .Add (3); Console .WriteLine(lst.ToString()); Console .WriteLine("②在2号位置,添加元素50:"); lst .Insert (2,50); Console .WriteLine(lst.ToString()); Console .WriteLine("③移除1号元素:"); lst.RemoveAt(1); Console .WriteLine(lst.ToString()); Console .WriteLine("④把2号元素赋值为9:"); lst [2]=9; Console .WriteLine(lst.ToString()); } }

目录
相关文章
|
2月前
【数据结构】单链表之--无头单向非循环链表
【数据结构】单链表之--无头单向非循环链表
|
4天前
|
存储
数据结构第二课 -----线性表之单向链表
数据结构第二课 -----线性表之单向链表
|
7天前
|
算法 数据可视化 Java
数据结构与算法-单向链表的实现以及相关面试题
数据结构与算法-单向链表的实现以及相关面试题
7 0
|
3月前
|
存储
数据结构 模拟实现LinkedList单向不循环链表
数据结构 模拟实现LinkedList单向不循环链表
34 0
|
3月前
|
存储 Python
如何在Python中实现单向链表和双向链表?
如何在Python中实现单向链表和双向链表?
|
4月前
|
缓存 算法 Java
6.单向链表正确实现方式
6.单向链表正确实现方式
41 1
|
4月前
|
存储 程序员 C语言
链表篇---单向链表的C语言实现
链表篇---单向链表的C语言实现
|
4月前
|
存储 缓存 算法
Algorithms_基础数据结构(02)_线性表之链表_单向链表
Algorithms_基础数据结构(02)_线性表之链表_单向链表
39 0
|
5月前
|
存储 C语言
链接未来:深入理解链表数据结构(一.c语言实现无头单向非循环链表)
在上一篇文章中,我们探索了顺序表这一基础的数据结构,它提供了一种有序存储数据的方法,使得数据的访 问和操作变得更加高效。想要进一步了解,大家可以移步于上一篇文章:探索顺序表:数据结构中的秩序之美 今天,我们将进一步深入,探讨另一个重要的数据结构——链表 链表和顺序表一样,都属于线性表,也用于存储数据,但其内部结构和操作方式有着明显的不同。通过C语言的具体实现,我们将会更加直观地理解它
109 1
链接未来:深入理解链表数据结构(一.c语言实现无头单向非循环链表)
|
5月前
|
存储
队列的学习(一)用数组和链表实现单向队列
队列的学习(一)用数组和链表实现单向队列 队列(Queue)是一种先进先出的数据结构,类似于现实生活中排队的场景。它有两个基本操作:入队(enqueue)和出队(dequeue)。在本文中,我们将介绍如何使用数组和链表来实现单向队列。