Last active
December 10, 2015 12:28
-
-
Save yssharma/4433818 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Node { int data; Node next; public Node(int data) { this.data = data; } } public class LinkedList { private Node root; public void addAtEnd(int data) { if (root == null) { root = new Node(data); return; } Node tmp = root; while (tmp.next != null) { tmp = tmp.next; } tmp.next = new Node(data); } public void addAtFront(int data) { Node newRoot = new Node(data); newRoot.next = root; root = newRoot; } public void addAtIndex(int data, int index) { if (root == null) return;return data; } public void printList() { if (root == null) { System.out.println("List is Empty !!"); return; } Node tmp = root; System.out.println(); while (tmp != null) { System.out.print(">" + tmp.data); tmp = tmp.next; } } public staticvoid main(String[] args) { LinkedList list = new LinkedList(); list.printList(); list.addAtEnd(1); list.printList(); list.addAtFront(2); list.printList(); list.addAtEnd(3); list.printList(); list.addAtIndex(4, 2); list.printList(); list.addAtIndex(5, 4); list.printList(); list.addAtEnd(6); list.printList(); list.addAtFront(7); list.printList(); list.addAtEnd(8); list.printList(); list.removeFromEnd(); list.printList(); list.removeFromEnd(); list.printList(); list.removeFromFront(); list.printList(); list.removeFromIndex(0); list.printList(); list.removeFromIndex(2); list.printList(); } } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment