import java.util.*;
public class MyLinkedList {
private Node head;
private class Node {
private String element;
Node link;
}
public MyLinkedList() {
head=null;
}
public void insertAtHead(String ele) {
Node curr = new Node();
curr.element = ele;
curr.link = head;
head=curr;
}
public void insertAtTail(String ele) {
Node curr = new Node();
curr.element = ele;
curr.link = head;
head=curr;
}
public void removeAtHead(String ele) {
Node curr = new Node();
curr.element = ele;
curr.link = head;
head=curr;
}
public void removeAtTail(String ele) {
Node curr = new Node();
curr.element = ele;
curr.link = head;
head=curr;
}
public static void main(String[] args) {
MyLinkedList s1 = new MyLinkedList();
s1.insertAtHead("Java I");
s1.insertAtTail("Java II");
s1.insertAtTail("Data Structure");
System.out.println(s1);
System.out.println(s1.removeAtHead ());
System.out.println(s1);
s1.insertAtHead("CISC120");
System.out.println(s1);
s1.insertAtTail("Android");
System.out.println(s1);
System.out.println(s1.removeAtTail ());
System.out.println(s1);
for (Node x = s1.head; x!=null; x = x.link)
System.out.println (x.element + "\t");
}
}
s1lkman
0
Newbie Poster
Recommended Answers
Jump to PostWhat exactly are the problems you are having (complete error messages, actual vs expected behaviour etc)?
Jump to Post1)Next time, you need to ask the right question. Not just dump your code and expect others to look through for you.
2)Your insert & remove methods definitions are exactly the same. The only method that has correct definition is the insertAtHead() method. The rest are wrong.
3)Now, ask the …
All 8 Replies
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
Taywin
312
Posting Virtuoso
s1lkman
0
Newbie Poster
s1lkman
0
Newbie Poster
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
Taywin
312
Posting Virtuoso
s1lkman
0
Newbie Poster
Taywin
312
Posting Virtuoso
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.