using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 单链表
{
class Program
{
static void Main(string[] args)
{
LinkList<int> myLinkedList = new LinkList<int>();
myLinkedList.Append(0);
myLinkedList.Append(1);
myLinkedList.Append(2);
//链表插入 //value post
myLinkedList.Insert(200, 0);
myLinkedList.Insert(300, 1);
myLinkedList.Insert(400, 2);
//在任意位置插入
myLinkedList.Insert(500, 2);
//链表删除
myLinkedList.Delete(2);
//链表查找
myLinkedList.GetElem(2);
}
}
public interface IListDS<T>
{
int GetLength(); //求长度
void Clear(); //清空操作
bool IsEmpty(); //判断线性表是否为空
bool Append(T item); //附加操作
bool Insert(T item, int i); //插入操作
T Delete(int i); //删除操作
T GetElem(int i); //取表元
int Locate(T value); //按值查找
}
public class Node<T>
{
private T data; //数据域
private Node<T> next; //引用域
/// <summary>
/// 构造器,要求输入数据值
/// </summary>
/// <param name="val">数据值</param>
public Node(T val)
{
data = val;
next = null;
}
/// <summary>
/// 构造器, 数据值为系统默认值
/// </summary>
public Node()
{
data = default(T);
next = null;
}
/// <summary>
/// 数据域属性
/// </summary>
public T Data
{
get
{
return data;
}
set
{
data = value;
}
}
/// <summary>
/// 引用域属性
/// </summary>
public Node<T> Next
{
get
{
return next;
}
set
{
next = value;
}
}
}
public class LinkList<T> : IListDS<T>
{
/// <summary>
/// 单链表的头节点,即第一个节点
/// </summary>
private Node<T> head;
/// <summary>
/// 单链表的头节点
/// </summary>
public Node<T> Head
{
get
{
return head;
}
set
{
head = value;
}
}
/// <summary>
///构造器,构造具有空引用的头节点
/// </summary>
public LinkList()
{
head = null;
}
/// <summary>
/// 求单链表的长度
/// </summary>
/// <returns></returns>
public int GetLength()
{
Node<T> curNode = head;
int len = 0;
while (curNode != null)
{
++len;
curNode = curNode.Next;
}
return len;
}
/// <summary>
/// 清空单链表
/// </summary>
public void Clear()
{
head = null;
}
/// <summary>
/// 判断单链表是否为空
/// </summary>
/// <returns></returns>
public bool IsEmpty()
{
if (head == null)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 在单链表的末尾添加新元素
/// </summary>
/// <param name="item">新数据</param>
public bool Append(T item)
{
Node<T> newNode = new Node<T>(item);
Node<T> curNode = null;
if (head == null)
{
head = newNode;
return false;
}
curNode = head;
while (curNode.Next != null)
{
curNode = curNode.Next;
}
curNode.Next = newNode;
return true;
}
/// <summary>
/// 在单链表的第pos个结点的位置前插入一个值为item的结点
/// </summary>
/// <param name="item">新数据</param>
/// <param name="pos">将要插入的位置,有效值为大于等于0的整数</param>
/// <returns></returns>
public bool Insert(T item, int pos)
{
if (pos < 0)//IsEmpty() ||
{
Console.WriteLine("List is empty or Position is error!");
return false;
}
Node<T> newNode = new Node<T>(item);
if (pos == 0)
{
newNode.Next = head;
head = newNode;
return true;
}
Node<T> curNode = head.Next;
Node<T> frontNode = head;
int index = 1;
while (curNode.Next != null && index < pos)
{
frontNode = curNode;
curNode = curNode.Next;
++index;
}
if (index == pos)
{
newNode.Next = curNode;
frontNode.Next = newNode;
return true;
}
else
{
Console.WriteLine(" Position is error!");
return false;
}
}
/// <summary>
/// 删除单链表的第pos个结点
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
public T Delete(int pos)
{
if (IsEmpty() || pos < 0)
{
Console.WriteLine("Link is empty or Position is error!");
return default(T);
}
T data = default(T);
if (pos == 0)
{
data = head.Data;
head = head.Next;
return data;
}
Node<T> frontNode = this.head;
Node<T> curNode = this.head.Next;
int index = 1;
while (curNode.Next != null && index < pos)
{
++index;
frontNode = curNode;
curNode = curNode.Next;
}
if (index == pos)
{
frontNode.Next = curNode.Next;
return curNode.Data;
}
else
{
Console.WriteLine("Position is out of range!");
return default(T);
}
}
/// <summary>
/// 获得单链表的第pos个数据元素
/// </summary>
/// <param name="pos">要查找的位置</param>
/// <returns></returns>
public T GetElem(int pos)
{
if (IsEmpty())
{
Console.WriteLine("List is empty!");
return default(T);
}
Node<T> curNode = head;
int index = 0;
while (curNode.Next != null && index < pos)
{
++index;
curNode = curNode.Next;
}
if (index == pos)
{
return curNode.Data;
}
else
{
Console.WriteLine("Position is out of range!");
return default(T);
}
}
/// <summary>
/// 在单链表中查找值为value的结点
/// </summary>
/// <param name="value">要查找的数据</param>
/// <returns>该数据在链表中的位置(从0开始),-1表示没有找到</returns>
public int Locate(T value)
{
if (IsEmpty())
{
Console.WriteLine("List is Empty!");
return -1;
}
Node<T> curNode = new Node<T>();
curNode = head;
int pos = 0;
while (curNode != null && !curNode.Data.Equals(value))
{
curNode = curNode.Next;
++pos;
}
if (curNode == null)
{
return -1;
}
return pos;
}
public static LinkList<T> operator +(LinkList<T> a, LinkList<T> b)
{
return null;
}
}
}
单链表
最新推荐文章于 2020-09-08 14:13:12 发布