Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class node {
- int data;
- node next;
- node prev;
- static void insert(node start, int data)
- {
- node newNode = new node();
- newNode.data = data;
- if(start==null) {
- newNode.next=newNode.prev=null;
- start = newNode;
- } else {
- node t = start;
- while(t.next!=null)
- t = t.next;
- newNode.prev = t;
- t.next = newNode;
- newNode.next=null;
- }
- }
- static void delete(node start, int data) {
- node t = start;
- try {
- while(t.data!=data)
- t = t.next;
- t.prev.next = t.next;
- t.next.prev = t.prev;
- } catch (NullPointerException e) {
- System.out.println("Data NOT found!");
- }
- }
- static void traverse(node start) {
- node t = start;
- while(t!=null) {
- System.out.println(t.data);
- t=t.next;
- }
- }
- }
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- node start=null;
- int ch=0;
- while(ch!=4)
- {
- System.out.println("n1. Insert Node");
- System.out.println("2. Delete Node");
- System.out.println("3. Print nodes");
- System.out.println("4. Exit!");
- System.out.print("Enter Your Choice: ");
- ch = sc.nextInt();
- switch(ch)
- {
- case 1: System.out.print("nEnter the value of the node to insert: ");
- int val = sc.nextInt();
- node.insert(start, val);
- System.out.println("DEBUG : Start val: "+start.data);
- break;
- case 2: System.out.print("nEnter the value of the node you want to delete: ");
- node.delete(start, sc.nextInt());
- break;
- case 3: node.traverse(start);
- break;
- case 4: System.out.println("nBye!!");
- break;
- default: System.out.println("nWrong value: Please try again!");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment