数据结构链表的常用操作

链表常用操作

using System;

namespace 链表
{

//一般链表都是有头部节点的 简称为头结点 头结点不参与运算
public class LinkList
{
    private Node _head;
    private int _count;
    public LinkList()
    {
        _head = new Node();
        _count = 0;
    }
    //添加
    public void AddItem(Node newNode)
    {
        //找到头结点
        Node tmpNode = _head;
       
        while (tmpNode.Next != null)
        {
            tmpNode = tmpNode.Next;
        }
        tmpNode.Next = newNode;
     
        _count++;
    }
    //插入
    public void InsertItem(int index, Node newnode)
    {
        if (index<0|index>_count )
        {
            Console.WriteLine("错误");
        }
        Node tempNode = _head;
        for (int i=0;i <index;i++)
        {
           tempNode = tempNode.Next;
        }

        newnode.Next = tempNode.Next;
        tempNode.Next = newnode;
        _count++;
    }
    //删除某个
    public int  RemoveAt(int index)
    {
        int returnValue = default(int);
        if (index<0|index>_count-1 )
        {
            Console.WriteLine("错误");
            goto returnTip;
        }
        Node tempNode = _head;
      
        for (int i = 0; i < index; i++)
        {
            tempNode = tempNode.Next;
        }
        Node deleteNode = tempNode.Next;
        tempNode.Next = tempNode.Next.Next;
        deleteNode.Next = null;

        _count--;
        returnTip:
        return returnValue;
    }
    //清空
  
    public void Clear()
    {
        _head.Next = null;
        _count = 0;
    }
    //遍历
    // int  --index    int --value
    public void ShowItem(Action <int ,int >ac)
    {
        if (_count == 0)
        {
            Console.WriteLine("空");
            return;
        }
        Node tempNode = _head.Next;
        for (int i=0;i <_count;i++ )
        {
            ac(i,tempNode .Data );
            tempNode = tempNode.Next;
        }
       
    }
    public void daoxu()
    {
        Node tempnode = _head;
        Node shang = null;
        Node xia;
        tempnode = tempnode.Next;
        while (tempnode != null)
        {
            xia = tempnode.Next;
            tempnode.Next = shang;
            shang = tempnode;
            tempnode = xia;
        }
        _head.Next = shang;
    }
    public int GetLength()
        {
            return _count;
        }
  

}

}

using System;

namespace 链表
{
internal class Program
{
public static void Show(int index,int value)
{
Console.WriteLine(“第{0}个元素是{1}”,index+1,value );
}
public static void Main(string[] args)
{
LinkList linkList = new LinkList();
linkList.AddItem( new Node (1));
linkList.AddItem(new Node(2));
linkList.AddItem(new Node(3));
linkList.AddItem(new Node(4));

        linkList.InsertItem(1,new Node (100));
       
      linkList.RemoveAt(3);
        linkList.daoxu();
        linkList.ShowItem(Show);
       // Console.WriteLine(linkList.GetLength());
        Console.ReadKey();
    }
}

}
namespace 链表
{
public class Node
{
public int Data;
//这个就是地址
public Node Next;

    /// <summary>
    /// 构造函数目的就是初始化
    /// </summary>
    public Node()
    {
        Data = default(int);
        Next = null;
    }

    public Node(int value)
    {
        Data = value;
        Next = null;
    }
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值