In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail how to learn the linkedList algorithm, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Brief introduction
LinkedList should be a very, very simple data structure. When the nodes are connected one by one, they become linkedList. Today we use animation to see how linkedList is inserted and deleted.
Construction of linkedList
LinkedList is composed of one node after another. Each node only needs to store the data to be saved and the reference of the next node.
LinkedList itself requires a head node, so our linkedList can be built like this:
Public class LinkedList {Node head; / / head node / / Node represents a node in Linked list and contains the operation of a data data and a reference to class Node {int data; Node next; / / Node constructor Node (int d) {data = d;} linkedList of the next node.
Let's take a look at how linkedList inserts data. There are three ways to insert data: head insert, tail insert, and middle insert.
Head insertion
Let's take a look at an example of head insertion:
What is the logic of head insertion?
The newly inserted node acts as the head node, and then points the original head node to the next reference of the current head node.
/ insert into the header of linkedList public void push (int newData) {/ / build the node to be inserted Node newNode = newNode (newData); / / the next of the new node points to the current head node newNode.next = head; / / the existing head node points to the new node head = newNode;}
Tail insertion
Take another look at the example of tail insertion:
What is the logic of insertion?
Find the last node and point the next of the last node to the newly inserted node.
/ / insert the new node into the back of the list public void append (int newData) {/ / create a new node Node newNode = newNode (newData); / / if the list is empty, the new node is if as the head node (head = = null) {head = newNode; return;} newNode.next = null / / find the last node Node last = head; while (last.next! = null) {last = last.next;} / / insert last.next = newNode; return;}
Intermediate insertion
Take another look at the example of intermediate insertion:
In this example, we insert a 93 at the location of the third node.
The insertion logic is to find the second node first, point the next of the second node to the new node, and then point the next of the new node to the original third node.
Take a look at how the java code implements:
/ insert after several elements public void insertAfter (int index, int newData) {Node prevNode = head; for (int I = 1; I < index; iTunes +) {if (prevNode = = null) {System.out.println ("incorrect index entered, please re-enter"); return;} prevNode = prevNode.next } / / create a new node Node newNode = newNode (newData); / / the next of the new node points to the next node of the prevNode newNode.next = prevNode.next; / / insert the new node after the prevNode prevNode.next = newNode;}
Delete nod
Take another look at how to delete a node in a location:
In the above example, we want to delete the fifth node.
The logic of deletion is to find the fourth node and the sixth node. Then point the next of the fourth node to the sixth node.
Look at the corresponding java code as follows:
/ / delete the node void deleteNode (int index) in a specific location {/ / if it is empty, directly return if (head = = null) return; / / head node Node temp = head; / / if you delete the head node if (index = = 1) {head = temp.next; return } / / find the previous node for (int item1; temptation deleted null & & inext is the node to be deleted. Delete node Node next = temp.next.next; temp.next = next;} so much about how to learn the linkedList algorithm. I hope the above can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.