Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package linkedlist;
- import java.util.Scanner;
- public class DoublyLinkedList {
- public static void main(String[] args) {
- DoublyLinkedList2 list = new DoublyLinkedList2();
- Scanner sc = new Scanner(System.in);
- String[] menu = { "Doubly Linked List Operations", "[1] - Add to Head", "[2] - Add to Tail ",
- "[3] - Add an Item after the target", "[4] - Remove From Head ", "[5] - Remove from Tail",
- "[6] - Remove the target item", "[7] - Print Head", "[8] - Print Tail", "[9] - Print Forward",
- "[10] - Print Backward", "[0] - Exit" };
- int choice = 0;
- do {
- for (String menuPrint : menu) {
- System.out.println(menuPrint);
- }
- System.out.print("Enter the number of your choice: ");
- choice = sc.nextInt();
- switch (choice) {
- case 1:
- System.out.print("Enter number to add to head: ");
- list.addToHead(sc.nextInt());
- break;
- case 2:
- System.out.print("Enter number to add to tail: ");
- list.addToTail(sc.nextInt());
- break;
- case 3:
- System.out.print("Enter number to insert: ");
- int insertNumeber = sc.nextInt();
- System.out.print("Enter number of target position to insert: ");
- int targetPosition = sc.nextInt();
- // list.addToTail(sc.nextInt());
- break;
- case 4:
- list.deleteHead();
- break;
- case 5:
- list.deleteTail();
- break;
- case 6:
- System.out.print("Enter number of target position to remove: ");
- int targetRemove = sc.nextInt();
- list.deletTarget(targetRemove);
- break;
- case 7:
- list.printHead();
- break;
- case 8:
- list.printTail();
- break;
- case 9:
- list.printForward();
- break;
- case 10:
- list.printBackward();
- break;
- case 0:
- System.out.println("Exited...");
- break;
- }
- } while (choice != 0);
- }
- }
- //Other files
- package linkedlist;
- //doubly linked list file
- public class DoublyLinkedList2 {
- Node head;
- Node tail;
- public DoublyLinkedList2() {
- head = tail = null;
- }
- public DoublyLinkedList2(Node head, Node tail) {
- this.head = head;
- this.tail = tail;
- }
- public boolean isCget() {
- return head == null && tail == null ? true : false;
- }
- public void addToHead(int item) {
- if (isCget()) {
- Node newNode = new Node(null, item, null);
- head = tail = newNode;
- }
- else {
- Node newNode = new Node(null, item, head);
- head = newNode;
- head.next.previous = head;
- }
- }
- public void addToTail(int item) {
- if (isCget()) {
- Node newNode = new Node(null, item, head);
- tail = head = newNode;
- }
- else {
- Node newNode = new Node(null, item, head);
- tail = newNode;
- tail.next.previous = tail;
- }
- }
- public void printHead() {
- System.out.println("Head: " + head.item);
- }
- public void printTail() {
- System.out.println("Tail: " + tail.item);
- }
- public void printForward() {
- Node current;
- current = head;
- System.out.println("Printing Forward");
- while (current != null) {
- System.out.print(" " + current.item);
- current = current.next;
- }
- System.out.println("");
- }
- public void printBackward() {
- Node current;
- current = tail;
- System.out.println("Printing Backward");
- while (current != null) {
- System.out.print(" " + current.item);
- current = current.previous;
- }
- System.out.println("");
- }
- public void deleteHead() {
- if (this.head != null) {
- Node temp = this.head;
- this.head = this.head.next;
- temp = null;
- if (this.head != null)
- this.head.previous = null;
- }
- }
- // Delete last node of the list
- public void deleteTail() {
- int item = 0;
- if (this.head != null) {
- if (this.head.next == null) {
- this.head = null;
- } else {
- Node temp = new Node(tail, item, head);
- temp = this.head;
- while (temp.next.next != null)
- temp = temp.next;
- Node lastNode = temp.next;
- temp.next = null;
- lastNode = null;
- }
- }
- }
- public void deletTarget(int n) {
- if (head == null) {
- return;
- } else {
- Node current = head;
- int pos = n;
- for (int i = 1; i < pos; i++) {
- current = current.next;
- }
- if (current == head) {
- head = current.next;
- } else if (current == tail) {
- tail = tail.previous;
- } else {
- current.previous = current.next;
- current.next.previous = current.previous;
- }
- current = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment