Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the concept of linked list of Java data structure and how to implement it

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "what is the concept of Java data structure linked list and how to achieve it", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what is the concept of Java data structure linked list and how to achieve it" can help you solve your doubts.

What is the concept of linked list

The linked list is a discontinuous storage structure on the physical storage structure, and the logical order of data elements is realized through the reference link order in the linked list.

Structure of linked list

There are 8 types of linked list structures:

Here we only talk about the bottom two, because in the work, business, examination questions, brushed to the chain list questions, interview questions are used in these two linked lists.

How linked lists store data

A linked list is made up of one node. (here we take the single linked list as an example)

What is a node?

The node is divided into two domains, assuming that one is called the vale domain and the other is called the next domain.

Val: data domain

Next: the address of the next node

The implementation of the linked list / / ListNode represents a node class ListNode {public int val; public ListNode next; / / constructor public ListNode (int val) {this.val = val;}} / / MyLinkedList represents a linked list public class MyLinkedList {public ListNode head / / the header reference of the linked list, so it is defined in the linked list. Head is the header of the linked list, not the head of the node. The node has only two attributes, one is valal and the other is next.So it cannot be defined in the ListNode class ListNode listNode = new ListNode (2); / / Node instantiation, val field assignment 2} exhaustive method to create a linked list / MyLinkedList represents a linked list public class MyLinkedList {public ListNode head / the header reference of the linked list, so it is defined in the linked list public void createList () {ListNode listNode0 = new ListNode (11); ListNode listNode1 = new ListNode (26); ListNode listNode2 = new ListNode (23); ListNode listNode3 = new ListNode (45); ListNode listNode4 = new ListNode (56); listNode0.next = listNode1; listNode1.next = listNode2; listNode2.next = listNode3; listNode3.next = listNode4 This.head = listNode0;} print linked list / / print linked list public void display () {ListNode cur = this.head; while (cur! = null) {System.out.print (cur.val+ ""); cur = cur.next;} System.out.println ();}

Print the results:

Find whether the keyword key is in the single linked list / / find whether the keyword key is in the single linked list public boolean contains (int key) {ListNode cur = this.head; while (cur! = null) {if (cur.val = = key) {return true;} cur = cur.next;} return false }

Print the results:

Get the length of single linked list: / / get the length of single linked list public int size () {ListNode cur = this.head; int count = 0; while (cur! = null) {count++; cur = cur.next;} return count;}

Print the results:

Public void addFirst (int data) {ListNode node = new ListNode (data); if (this.head = = null) {this.head = node;} else {node.next = this.head; head = node;}}

Print the results:

Public void addLast (int data) {ListNode node = new ListNode (data); ListNode cur = this.head; if (this.head = = null) {this.head = node;} else {while (cur.next! = null) {cur = cur.next;} cur.next = node;}}

Print the results:

Insert anywhere, the first data node is 0 subscript public ListNode findIndex (int index) {ListNode cur = this.head; while (index- 1! = 0) {cur = cur.next; index--;} return cur } / / insert anywhere, the first data node is 0 subscript public void addIndex (int index,int data) {if (index)

< 0 || index >

Size () {System.out.println ("invalid position"); return;} if (index = = 0) {addFirst (data); return;} if (index = = size ()) {addLast (data); return } ListNode cur = findIndex (index); ListNode node = new ListNode (data); node.next = cur.next; cur.next = node;}

Print the results:

Delete the node whose keyword is key for the first time / / delete the node whose keyword is key for the first time public void remove (int key) {if (this.head = = null) {System.out.println ("no section you want to delete"); return;} if (this.head.val = = key) {this.head = this.head.next; return } ListNode cur = this.head; while (cur.next! = null) {if (cur.next.val = = key) {cur.next = cur.next.next; return;} cur = cur.next;} if (cur.next = = null) {System.out.println ("no node"); return }}

Print the results:

Delete all nodes with a value of key / / delete all nodes with a value of key public ListNode removeAllKey (int key) {if (this.head = = null) return null; ListNode prev = this.head; ListNode cur = this.head; while (cur! = null) {if (cur.val = = key) {prev.next = cur.next; cur = cur.next } else {prev = cur; cur = cur.next;}} if (this.head.val = = key) {this.head = this.head.next;} return this.head;}

Print the results:

Read here, this "what is the concept of Java data structure linked list and how to achieve" article has been introduced, want to master the knowledge of this article also need to practice and use in order to understand, if you want to know more related articles, welcome to pay attention to the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report