HOW CAN I Addlast ,DeleteLast,AddAt,and DeleteAt.

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...
icon
Related questions
Question

class Node{
int data;
Node next;
public Node (int d) {
this(d,null);
}
public Node (int d,Node n) {
data=d;
next=n;
}
}
public class List {
private Node head;

public List() {
head=null;
}
public void addBegenning(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 deleteBegging() {
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) {//if the d not exsist
Node tmp=head;
Node prev= null;
while (tmp.data!=d) {// tmp!=null &&
prev=tmp;
tmp=tmp.next;
}
prev.next=tmp.next;
}

public static void main(String[]args) {
List L = new List();
L.addBegenning(10);
L.addBegenning(20);
L.addBegenning(5);
L.addEnd(50);
L.addEnd(100);
L.addBegenning(1);
System.out.println(L);
L.deleteBegging();
System.out.println(L);
L.deleteEnd();
System.out.println(L);
L.contains(20);
System.out.println(L.contains(20));
System.out.println(L.contains(100));
}
}

 

HOW CAN I Addlast ,DeleteLast,AddAt,and DeleteAt.

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
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 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)
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
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY