Java implementation linked list

Source: Internet
Author: User

Today, when discussing with classmates, he said that Java does not have pointers on how to implement linked lists. Indeed, there are no pointers in Java. However, references in Java are very different from references in C + + and have a certain pointer function (summarized in two days). So, the data structure of the linked list is implemented in Java at home. Now I think of two ways to implement a linked list in Java:

1. Java allows the class to appear in the definition of the class object, so you can implement the linked list by reference to the class object.

2, you can define a base class, through the sub-class of the upward transformation. A linked list is implemented by simulating pointer types in C + + through parent-class pointers.

Nonsense not much to say, on the code:

Method 1 Node.java:

Package Com.pasilo;public class Node {private int data;private node Next;public node () {}public node (int data) {This.data = Data;this.next = new Node ();} public int GetData () {return this.data;} public void SetData (int value) {this.data = value;} Public Node GetNext () {return this.next;} public void Setnewnext (int value) {Node NewNode = new Node (value); this.next = NewNode;} public void Setnext (node node) {this.next = node;}}

Method 1 List.java:

package com.pasilo;public class list {private node head;private node  Current;public list (int data) {this.head = new node (data); current = head;} Public void append (int value) {current.setnewnext (value); Current = current.getnext ();} Public void remove (int destation) {node pointer = this.head;for (int i=0;i <destation-1;i++) {pointer = pointer.getnext ();} Node temp = pointer.getnext (). GetNext ();p ointer.setnext (temp);} Public void insert (int destation,int value) {node pointer = this.head;for ( int i=0;i<destation-1;i++) {pointer = pointer.getnext ();} Node temp = pointer.getnext (); Node newnode = new node (value);p ointer.setnext (NewNode); Newnode.setnext (temp);} Public int at (Int index) {node pointer = this.head;for (int i=0;i<index;i++) {pointer = pointer.getnext ();} Return pointer.getdata ();} Public void display () {node pointer = this.head;while (current != pointer) { System.out.println ("Data is:" +pointer.getdata ());p ointer = pointer.getnext (); System.out.println ("Data is:" +pointer.getdata ()); System.out.println ("This is the end."); Public int size () {int size = 1; Node pointer = this.head;while (Current != pointer) {pointer =  Pointer.getnext (); size++;} Return size;} Public static void main (String[] args)  {// todo auto-generated method  stublist list = new list (4); List.append (4); list.append (1); List.append (2); List.insert (2, 10); List.display (); List.remove (2); List.display (); System.out.println (list.at (2)); System.out.println ("Size of the list:" +list.size ());}}

To be continued .....

Java implementation linked list

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: info-contact@alibabacloud.com 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.