Data structure Review--java realization of single-linked list basic operation

Source: Internet
Author: User

The basic operations of a single-linked list include the creation of single-linked lists, lookup operations (search by order and lookup by value), insert operations (pre-and post-interpolation), and delete operations. The following is a specific Java implementation program:

Package com.zpp.test;//first creates a node class public class Node {private node Next;//pointer field private int data;//data field Publi     c Node (int data) {this. data = data;     }}package Com.zpp.test;public class Linklist {public node first;//define a head node private int pos = 0;//node Location     Public linklist () {this. first = null;          }//Insert a head node public void Addfirstnode (int data) {node node = new node (data); Node.           Next = first;     First = node;           }//delete the head node and return the head node to public node Deletefirstnode () {node Tempnode = first; First = Tempnode.           Next     return tempnode;          }//Insert node after index at specified position public void InsertAfter (int index, int data) {node node = new node (data);           Node current = first;               while (pos! = index) {current = current. Next;          pos++; } node.          Next =; Current.           Next = node;  pos = 0;   }//Insert node in front of the specified position index public void insertbefore (int index, int data) {node node = new node (data);          Node current = first;           Node previous = first;              while (pos! = index) {previous = current; Current = current.               Next          pos++; } node.          Next = current; Previous.           Next = node;     pos = 0;          }//Delete node at the specified location public void deletebypos (int index) {node current = first;           Node previous = first;              while (pos! = index) {pos++;              previous = current; Current = current.          Next          } if (current = = first) {first = first. Next;              } else {pos = 0; Previous. Next = current.          Next          }}//delete the node based on the data of the node (delete only the first) public void deletebydata (int data) {node-current = primary; Node previous = first;           Remember the previous nodewhile (current data! = data) {if (current. Next = null) {System.out.println ("No corresponding node found, cannot be deleted In addition to Operation!              ");              } previous = current; Current = current.          Next          } if (current = = first) {first = first. Next;          } else {Previous. Next = current. Next;           }}//Based on location find node information public node Findbypos (int index) {node current = first;               if (pos! = index) {current = current. Next;          pos++;     } return current;           }//Find node information based on data public nodes findbydata (int data) {node current = first;              while (current data! = data) {if (current. Next = null) return null; Current = current.          Next     } return current;           }//Displays all node information public void Displayallnodes () {node current = first; while (current! = nulL) {System.out.print ( ""); Current = current.          Next     } System.out.println (); }}package com.zpp.test;//test class public class Testlinklist {public static void main (string[] args) {linklist lin          Klist = new linklist ();          Linklist.addfirstnode (20);          Linklist.addfirstnode (21);           Linklist.addfirstnode (19); 19,21,20 Linklist.insertbefore (1, 22); 19,22,21,20 Linklist.insertbefore (2, 23); 19,22,23,21,20 Linklist.insertafter (3, 99); 19,22,23,21,99,20 linklist.displayallnodes ();//Node node = Linklist.deletefirstnode ();//System.        Out.println ("node:" +;//Linklist.displayallnodes ();//node = Linklist.deletebypos (2);//          System.out.println ("node:" +;//Linklist.displayallnodes ();//Linklist.deletefirstnode (); Linklist.deletebydata (+);//Node node = LinkliSt.deletebypos (0); System. Out.println ("Node:" + node.)          data);          Linklist.displayallnodes ();          Node Node1 = linklist.findbypos (0); System. Out.println ("Node1:" + node1.          data);          Node Node2 = Linklist.findbydata (22); System. Out.println ("Node2:" + node2.     data); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data structure Review--java realization of single-linked list basic operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.