数据结构 - 单链表的节点操作

单向链表节点的删除
有三种不同的情况
1、删除链表的第一个节点
只要把链表头指针指向第二个节点即可。

if (first.data == delNode.data)
    first = first.next;

2、删除链表内的中间节点,只要将删除节点的前一个节点的指针,指向想删除节点的下一个节点即可

newNode = null; //Point 1
temp = first;   //Point 2
while(newNode.data != delNode.data){
    temp = newNode;
    newNode = newNode.next;
}
temp.next = delNode.next;

3、删除链表最后一个节点:只要将指向最后一个节点的指针,直接指向null即可

if(last.data == delNode.data){
    newNode = first;
    while(newNode.next!=last) newNode = newNode.next;
    newNode.next = last.next;
    last = newNode;
}

*单链表插入新节点
有三种情况:
1、加到第一个节点之前

public void append_Head(Node ptr)
        {
            if (this.isEmpty())
            {
                first = ptr;
                last = ptr;
            }
            else
            {
                ptr.next = first;
                first = ptr;
            }
        }

2、加到最后一个节点之后

public void append(Node ptr)
        {
            if (this.isEmpty())
            {
                first = ptr;
                last = ptr;
            }
            else
            {
                last.next = ptr;
                last = ptr;
            }
        }

3、加到链表中间任意位置
就是改变指针位置即可

public bool InsertAt(Node ptr,int data)
        {
            Node temp;
            Node current;
            if (this.isEmpty())
            {
                Console.WriteLine("This list is Empty!");
                return false;
            }
            else
            {
                if(first.data == data)
                {
                    this.append_Head(ptr);
                    return true;
                }
                else if(last.data == data)
                {
                    this.append(ptr);
                    return true;
                }
                else
                {
                    current = first;
                    temp = first;
                    while (current.data != data)
                    {
                        temp = current;
                        current = current.next;
                    }
                    temp.next = ptr;
                    ptr.next = current;
                    return true;
                }
                
            }
        }

综合例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataStruct_Pro
{
    class Node
    {
        public int data;
        public Node next;
        public Node(int data)
        {
            this.data = data;
            this.next = null;
        }
    }
    class LinkedList
    {
        public Node first;
        public Node last;
        public bool isEmpty()
        {
            return first == null;
        }
        public void print()
        {
            var current = first;
            while(current != null)
            {
                Console.Write("[" + current.data + "]");
                current = current.next;
            }
            Console.WriteLine();
        }
        public LinkedList Concatenate(LinkedList head1,LinkedList head2)
        {
            LinkedList ptr;
            ptr = head1;
            while(ptr.last.next != null)
            {
                ptr.last = ptr.last.next;
            }
            ptr.last.next = head2.first;
            return head1;
        }
        public void Insert(Node ptr)
        {
            Node temp;
            Node newNode;
            if (this.isEmpty())
            {
                first = ptr;
                last = ptr;
            }
            else
            {
                if(ptr.next == first)
                {
                    ptr.next = first;
                    first = ptr;
                }
                else
                {
                    if(ptr.next == null)
                    {
                        last.next = ptr;
                        last = ptr;
                    }
                    else
                    {
                        newNode = first;
                        temp = first;
                        while(ptr.next != newNode.next)
                        {
                            temp = newNode;
                            newNode = newNode.next;
                        }
                        temp.next = ptr;
                        ptr.next = newNode;
                    }
                }
            }
        }
        public void Delete(Node Delnode)
        {
            Node newNode;
            Node temp;
            if (first.data == Delnode.data)
            {
                first = first.next;
            }
            else if (last.data == Delnode.data)
            {
                newNode = first;
                while (newNode.next != last) newNode = newNode.next;
                newNode.next = null;
                last = newNode;
            }
            else
            {
                newNode = first;
                temp = null;
                while (newNode.data != Delnode.data)
                {
                    temp = newNode;
                    newNode = newNode.next;
                }
                temp.next = newNode.next;
            }
        }
        public void append(Node ptr)
        {
            if (this.isEmpty())
            {
                first = ptr;
                last = ptr;
            }
            else
            {
                last.next = ptr;
                last = ptr;
            }
        }
        public void append_Head(Node ptr)
        {
            if (this.isEmpty())
            {
                first = ptr;
                last = ptr;
            }
            else
            {
                ptr.next = first;
                first = ptr;
            }
        }
        public bool InsertAt(Node ptr,int data)
        {
            Node temp;
            Node current;
            if (this.isEmpty())
            {
                Console.WriteLine("This list is Empty!");
                return false;
            }
            else
            {
                if(first.data == data)
                {
                    this.append_Head(ptr);
                    return true;
                }
                else if(last.data == data)
                {
                    this.append(ptr);
                    return true;
                }
                else
                {
                    current = first;
                    temp = first;
                    while (current.data != data)
                    {
                        temp = current;
                        current = current.next;
                    }
                    temp.next = ptr;
                    ptr.next = current;
                    return true;
                }
                
            }
        }
    }
    class Class3
    {
        public static void Main(string[] args)
        {
            //LinkedList list = new LinkedList();
            //LinkedList list_bak = new LinkedList();
            //Node node1 = new Node(5);
            //Node node2 = new Node(6);
            //list.Insert(node1);
            //list.Insert(node2);
            //Node node3 = new Node(7);
            //Node node4 = new Node(8);
            //list_bak.Insert(node3);
            //list_bak.Insert(node4);
            //list.Concatenate(list, list_bak);
            //list.print();
            //Console.ReadLine(); 
            LinkedList list = new LinkedList();
            Node[] nodes = new Node[10];
            for(int i = 1; i <= 9; i++)
            {
                nodes[i] = new Node(i);
            }
            list.append(nodes[3]);
            list.append(nodes[6]);
            list.append(nodes[8]);
            list.append_Head(nodes[2]);
            list.InsertAt(new Node(99),2);
            list.append_Head(nodes[9]);
            
            list.print();
            Console.ReadLine();
            
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43635647/article/details/104104274