c#单链表基础操作

最近开始刷leetcode,感觉自己的基础太差,基本的链表部分的问题都得想很久,今天花了一个晚上的时间将链表的基础操作都实现了一遍,记录一下自己的收获

最大的是收获的一句话:链表的操作使用替身

(替身其实就是类似指针的东西,但是我之前一直没有想到这一点,导致思路堵塞)

举个例子

 //我要操作first,如遍历等
 ListNode temp = first;//先创建一个first的替身
 temp = XXX;//直接修改temp并不会改变first的值
 //有两种操作会改变first中的值
 temp.val = XXX;//直接改变first中对应节点的值,比如temp指向first[0],这一步下去first[0]的值就相应的改变
 temp.next = XXX;//改变这个节点,链接到其他节点,同样first对应节点也被改变了

 temp = temp.next;//我们经常会看到这个操作,意思是temp指向下一节点,也不会改变first的中任何值,相当于1=1
下面给我练习写的链表类,其中的方法没有经过详细测试,只是能跑而已

using System;

namespace LeetCode
{
    /// <summary>
    /// 单链表训练
    /// </summary>
    class ListNodeTrain
    {
        static void Main(string[] args)
        {
            int[] arr = { 5,4,3,2,1 };
            LinkList ll = new LinkList(arr);
            //ll.Insert(2, 6);
            ll.Reversal();
            ll.Print();
            //Console.WriteLine(ll.Get(2));
        }
        /// <summary>
        /// 链表操作类
        /// </summary>
        public class LinkList {
            private ListNode first;
            /// <summary>
            /// 建立只有头指针的单链表
            /// </summary>
            public LinkList() {
                first = null;
            }
            /// <summary>
            /// 顺序打印链表
            /// </summary>
            public void Print()
            {
                if (first == null)
                    return;
                ListNode t = first;
                while (t != null) {
                    Console.Write(t.val);
                    t = t.next;
                }
            }
            /// <summary>
            /// 将数组转换成单链表
            /// </summary>
            /// <param name="a"></param>
            public LinkList(int[] a) {
                if (a == null || a.Length <= 0)
                    return;
                first = new ListNode(a[0]);
                ListNode temp = first;
                for (int i = 1; i < a.Length; i++) {
                    temp.next = new ListNode(a[i]);
                    temp = temp.next;
                }
            }
            /// <summary>
            /// 求单链表的长度
            /// </summary>
            public int Length {
                get {
                    int len = 0;
                    ListNode t = first;
                    while (t != null) {
                        len++;
                        t = t.next;
                    }
                    return len;
                }
            }
            /// <summary>
            /// 获取链表中指定下标的值
            /// </summary>
            /// <param name="index"></param>
            /// <returns></returns>
            public int Get(int index){
                if (Length < index)
                    return -1;
                ListNode t = first;
                int count = 0;
                while (t != null) {
                    if (count == index)
                        return t.val;
                    t = t.next;
                    count++;
                }
                return -1;
            }
            /// <summary>
            /// 求值为val的节点的下标,返回第一个
            /// </summary>
            /// <param name="val"></param>
            /// <returns></returns>
            public int Locate(int val) {
                ListNode t = first;
                int count = 0;
                while (t != null) {
                    if (t.val == val)
                        return count;
                    t = t.next;
                    count++;
                }
                return -1;
            }
            /// <summary>
            /// 在链表的指定位置插入值为val的节点
            /// </summary>
            /// <param name="index"></param>
            /// <param name="val"></param>
            public bool Insert(int index,int val) {
                ListNode t = first;
                int count = 0;
                while (t != null) {
                    if (count == index) {
                        ListNode n = new ListNode(val);
                        n.next = t.next;
                        t.next = n;
                        return true;
                    }
                    t = t.next;
                    count++;
                }
                return false;
            }
            /// <summary>
            /// 删除下标为index的节点
            /// </summary>
            /// <param name="index"></param>
            /// <returns></returns>
            public bool Delete(int index) {
                int count = 1;
                ListNode t = first;
                while (t != null) {
                    if (count == index) {
                        ListNode curr = t;
                        t = t.next;
                        curr.next = t.next;
                        return true;
                    }
                    t = t.next;
                    count++;
                }
                return false;
            }
            /// <summary>
            /// 判断是否为空
            /// </summary>
            /// <returns></returns>
            public bool IsEmpty() {
                if (first == null)
                    return true;
                return false;
            }
            /// <summary>
            /// 将链表按照从小到大排序,值交换(冒泡排序)
            /// </summary>
            public void SortValue() {
                if (first == null || first.next == null)
                    return;
                ListNode t1 = first;//t1是first的替身
                while (t1 != null) {
                    ListNode t2 = t1.next;
                    while (t2 != null) {
                        if (t2.val < t1.val) {
                            int t = t1.val;
                            t1.val = t2.val;
                            t2.val = t;
                        }
                        t2 = t2.next;
                    }
                    t1 = t1.next;
                }
            }
            /// <summary>
            /// 将链表进行翻转,递归实现,节点翻转
            /// </summary>
            public void Reversal() {
                if (first == null || first.next == null)
                    return;
                first = reversal(first);
            }
            // 先回溯到最终值,反过来进行节点的翻转
            ListNode reversal(ListNode head) {
                if (head == null || head.next == null) {
                    return head;
                }
                ListNode reHead = reversal(head.next);//rehead始终为最后的节点
                head.next.next = head;
                head.next = null;
                return reHead;
            }
        }
        public class ListNode
        {
            public int val;
            public ListNode next;
            public ListNode(int x) { val = x; }
        }
    }
}



  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是单链表的基本运算的C#实现: ```csharp using System; namespace LinkedListDemo { class Program { static void Main(string[] args) { LinkedList list = new LinkedList(); list.InsertAtEnd(10); list.InsertAtEnd(20); list.InsertAtEnd(30); list.InsertAtEnd(40); Console.WriteLine("Linked List:"); list.Display(); Console.WriteLine("Length of Linked List: {0}", list.Length()); Console.WriteLine("Deleting element 30 from Linked List"); list.Delete(30); Console.WriteLine("Linked List after deletion:"); list.Display(); Console.ReadKey(); } } class Node { public int data; public Node next; public Node(int value) { data = value; next = null; } } class LinkedList { private Node head; public LinkedList() { head = null; } public void InsertAtEnd(int value) { Node newNode = new Node(value); if (head == null) { head = newNode; } else { Node current = head; while (current.next != null) { current = current.next; } current.next = newNode; } } public void Delete(int value) { if (head == null) { return; } if (head.data == value) { head = head.next; return; } Node current = head; while (current.next != null) { if (current.next.data == value) { current.next = current.next.next; return; } current = current.next; } } public int Length() { int count = 0; Node current = head; while (current != null) { count++; current = current.next; } return count; } public void Display() { Node current = head; while (current != null) { Console.Write(current.data + " "); current = current.next; } Console.WriteLine(); } } } ``` 在主函数中,我们创建一个新的链表对象并插入四个节点。然后,我们打印链表并检查其长度。接下来,我们删除值为30的节点,并再次打印链表以确认节点是否被删除。 链表的节点由Node类表示,它包含一个整型数据和一个指向下一个节点的指针。LinkedList类包含链表的头节点,以及插入,删除,长度和显示链表的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值