What can I do to make this code correct? public class LINKEDLIESTD { class Node{ int data; Node previous; Node next; public Node(int data) { this.data = data; } } Node head, tail = null; public void addNode(int data)
What can I do to make this code correct? public class LINKEDLIESTD { class Node{ int data; Node previous; Node next; public Node(int data) { this.data = data; } } Node head, tail = null; public void addNode(int data)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
Related questions
Question
What can I do to make this code correct?
public class LINKEDLIESTD {
class Node{
int data;
Node previous;
Node next;
public Node(int data) {
this.data = data;
}
}
Node head, tail = null;
public void addNode(int data) {
Node newNode = new Node(data);
if(head == null) {
head = tail = newNode;
head.previous = null;
tail.next = null;
}
else {
tail.next = newNode;
newNode.previous = tail;
tail = newNode;
tail.next = null;
}
}
public void display() {
Node current = head;
if(head == null) {
System.out.println("list is empty");
return;
}
System.out.println("Nodes of doubly linked list: ");
while(current != null) {
System.out.print(current.data + " ");
current = current.next;
}
}
public void addBegining(int d) {
Node n=new Node(d);
if(head==null) {
head=n;
}else {
n.next=head;
head=n;
}
}
public void addEnd(int d) {
if(head==null) {
head=new Node(d);
}else {
Node tmp=head;
while(tmp.next!=null)
tmp=tmp.next;
tmp.next=new Node(d);
}
}
public String toString() {
Node tmp=head;
String ans="";
while(tmp!=null) {
ans+=tmp.data+"-->";
tmp=tmp.next;
}
return ans;
}
public void deleteBegining() {
if (head!=null) {
head=head.next;
}
}
public void deleteEnd() {
Node tmp=head;
Node prev = null;
while (tmp.next!=null) {
prev=tmp;
tmp=tmp.next;
}
prev.next=null;
}
public boolean contains(int d) {
Node tmp=head;
while(tmp!=null) {
if (tmp.data==d) return true;
tmp=tmp.next;
}
return false;
}
public void remove(int d) {
Node tmp=head;
Node prev=null;
while(tmp!=null && tmp.data!=d) {
prev=tmp;
tmp=tmp.next;
}
if(tmp!=null) prev.next=tmp.next;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
LINKEDLIESTD L=new LINKEDLIESTD();
L.addBegining(10);
L.addBegining(20);
L.addBegining(5);
L.addEnd(50);
L.addEnd(100);
L.addBegining(1);
System.out.println(L);
L.deleteBegining();
System.out.println(L);
L.deleteEnd();
L.addNode(50);
L.addNode(60);
System.out.println(L);
System.out.println(L.contains(10));
System.out.println(L.contains(100));
L.remove(1000);
System.out.println(L);
}
}
Expert Solution
Step 1
Basic fundamentals of linked list is node of list which have two attributes data and next, previous pointer.
Which is required to be set as null at constructor.
Step by step
Solved in 2 steps
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY