Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
83 views

MyDoublyLinkedList Java

This Java code defines a doubly linked list data structure with Node objects that contain data and pointers to the previous and next Nodes. The MyDoublyLinkedList class contains methods to add Nodes to the list by passing in data, and to traverse the list and print out the data values of each Node. The main method demonstrates adding several Nodes to the list and then calling the method to traverse and print the values.

Uploaded by

Kevin King Yabut
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

MyDoublyLinkedList Java

This Java code defines a doubly linked list data structure with Node objects that contain data and pointers to the previous and next Nodes. The MyDoublyLinkedList class contains methods to add Nodes to the list by passing in data, and to traverse the list and print out the data values of each Node. The main method demonstrates adding several Nodes to the list and then calling the method to traverse and print the values.

Uploaded by

Kevin King Yabut
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

public class MyDoublyLinkedList {

class Node {
int data;
Node getPrevious;
Node getNext;

public Node(int data){

this.data = data;
}
}
Node head, tail = null;

public void getData(int data){


Node newNode = new Node(data);

if(head == null) {
head = tail = newNode;
head.getPrevious = null;
tail.getNext = null;
}
else {
tail.getNext = newNode;
newNode.getPrevious = tail;
tail = newNode;
tail.getNext = null;
}
}
public void setNext() {
Node node = head;
if (head == null) {
System.out.println("List is Empty");
return;
}
System.out.println("Doubly linked list");
while (node != null){
System.out.println(node.data + " ");
node = node.getNext;

}
System.out.println();
}

public static void main(String[] args) {


MyDoublyLinkedList add = new MyDoublyLinkedList();

add.getData(1);
add.getData(2);
add.getData(3);
add.getData(4);

add.setNext();
}
}

This study source was downloaded by 100000851302356 from CourseHero.com on 08-30-2022 04:32:00 GMT -05:00

https://www.coursehero.com/file/106719579/MyDoublyLinkedListjava/
Powered by TCPDF (www.tcpdf.org)

You might also like