C# 链表

PHPz
发布: 2024-09-03 15:28:12
原创
929 人浏览过

以非连续方式存储元素的线性数据结构称为 LinkedList,其中指针用于将链表中的元素相互链接,System.Collections.Generic 命名空间由 LinkedList C# 中的类,可以以非常快速的方式从中删除或插入元素,实现经典链表,并且每个对象的分配在链表中是单独的,并且不需要复制整个集合来执行某些操作在链接列表上。

语法:

C#中LinkedList类的语法如下:

LinkedList<Type> linkedlist_name = new LinkedList <Type>();
登录后复制

其中Type代表链表的类型。

C# 中 LinkedList 类的工作

  • 链表中存在节点,每个节点由两部分组成,即数据字段和指向链表中下一个节点的链接。
  • 链表中每个节点的类型都是LinkedListNode;类型。
  • 节点可以从链表中删除,也可以插入回同一个链表,也可以插入到另一个链表,因此堆上没有额外的分配。
  • 向链表中插入元素、从链表中删除元素以及获取点赞列表维护的内部属性count属性都是O(1)操作。
  • 链表类支持枚举器,因为它是通用链表。
  • 链表不支持任何使链表不一致的东西。
  • 如果链表是双向链表,那么每个节点都有两个指针,一个指向链表中的前一个节点,另一个指向链表中的下一个节点。

LinkedList 类的构造函数

C#中的LinkedList类中有几个构造函数。他们是:

  • LinkedList(): 链表类的新实例被初始化,该实例为空。
  • LinkedList(IEnumerable):初始化链表类的新实例,该实例取自 IEnumerable 的指定实现,其容量足以累积所有复制的元素。
  • LinkedList(SerializationInfo, StreamingContext): 链表类的新实例被初始化,可以使用指定为参数的serializationInfo和StreamingContext进行序列化。

C#中LinkedList类的方法

C#中的LinkedList类中有几个方法。他们是:

  • AddAfter: A value or new node is added after an already present node in the linked list using the AddAfter method.
  • AddFirst: A value or new node is added at the beginning of the linked list using the AddFirst method.
  • AddBefore: A value or new node is added before an already present node in the linked list using the AddBefore method.
  • AddLast: A value or new node is added at the end of the linked list using the AddLast method.
  • Remove(LinkedListNode): A node specified as a parameter will be removed from the linked list using Remove(LinkedListNode) method.
  • RemoveFirst(): A node at the beginning of the linked list will be removed from the linked list using RemoveFirst() method.
  • Remove(T): The first occurrence of the value specified as a parameter in the linked list will be removed from the linked list using the Remove(T) method.
  • RemoveLast(): A node at the end of the linked list will be removed from the linked list using the RemoveLast() method.
  • Clear(): All the nodes from the linked list will be removed using the Clear() method.
  • Find(T): The value specified as the parameter present in the very first node will be identified by using the Find(T) method.
  • Contains(T): We can use the Contains(T) method to find out if a value is present in the linked list or not.
  • ToString(): A string representing the current object is returned by using the ToString() method.
  • CopyTo(T[], Int32): The whole linked list is copied to an array which is one dimensional and is compatible with the linked list and the linked list begins at the index specified in the array to be copied to using CopyTo(T[], Int32) method.
  • OnDeserialization(Object): After the completion of deserialization, an event of deserialization is raised and the ISerializable interface is implemented using OnDeserialization(Object) method.
  • Equals(Object): If the object specified as the parameter is equal to the current object or not is identified using Equals(Object) method.
  • FindLast(T): The value specified as the parameter present in the last node will be identified by using FindLast(T) method.
  • MemberwiseClone(): A shallow copy of the current object is created using MemeberwiseClone() method.
  • GetEnumerator(): An enumerator is returned using GetEnumerator() method and the returned enumerator loops through the linked list.
  • GetType(): The type of the current instance is returned using GetType() method.
  • GetHashCode(): The GetHashCode() method is the hash function by default.
  • GetObjectData(SerializationInfo, StreamingContext): The data which is necessary to make the linked list serializable is returned by using GetObjectData(SerializationInfo, StreamingContext) method along with implementing the ISerializable interface.

Example of LinkedList Class in C#

C# program to demonstrate AddLast() method, Remove(LinkedListNode) method, Remove(T) method, RemoveFirst() method, RemoveLast() method and Clear() method in Linked List class:

Code:

using System;
using System.Collections.Generic;
//a class called program is defined
public class program
{
// Main Method is called
static public void Main()
{
//a new linked list is created
LinkedList<String> list = new LinkedList<String>();
//AddLast() method is used to add the elements to the newly created linked list
list.AddLast("Karnataka");
list.AddLast("Mumbai");
list.AddLast("Pune");
list.AddLast("Hyderabad");
list.AddLast("Chennai");
list.AddLast("Delhi");
Console.WriteLine("The states in India are:");
//Using foreach loop to display the elements of the newly created linked list
foreach(string places in list)
{
Console.WriteLine(places);
}
Console.WriteLine("The places after using Remove(LinkedListNode) method are:");
//using Remove(LinkedListNode) method to remove a node from the linked list
list.Remove(list.First);
foreach(string place in list)
{
Console.WriteLine(place);
}
Console.WriteLine("The places after using Remove(T) method are:");
//using Remove(T) method to remove a node from the linked list
list.Remove("Chennai");
foreach(string plac in list)
{
Console.WriteLine(plac);
}
Console.WriteLine("The places after using RemoveFirst() method are:");
//using RemoveFirst() method to remove the first node from the linked list
list.RemoveFirst();
foreach(string pla in list)
{
Console.WriteLine(pla);
}
Console.WriteLine("The places after using RemoveLast() method are:");
//using RemoveLast() method to remove the last node from the linked list
list.RemoveLast();
foreach(string pl in list)
{
Console.WriteLine(pl);
}
//using Clear() method to remove all the nodes from the linked list
list.Clear();
Console.WriteLine("The count of places after using Clear() method is: {0}",
list.Count);
}
}
登录后复制

The output of the above program is as shown in the snapshot below:

C# 链表

In the above program, a class called program is defined. Then the main method is called. Then a new linked list is created. Then AddLast() method is used to add the elements to the newly created linked list. Then foreach loop is used to display the elements of the newly created linked list. Then Remove(LinkedListNode) method is used to remove a node from the linked list. Then Remove(T) method is used to remove a node from the linked list. Then RemoveFirst() method is used to remove the first node from the linked list. Then RemoveLast() method is used to remove the last node from the linked list. Then Clear() method is used to remove all the nodes from the linked list. The output of the program is shown in the snapshot above.

以上是C# 链表的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!