|
| 1 | +/* |
| 2 | +Copyright (C) Deepali Srivastava - All Rights Reserved |
| 3 | +This code is part of DSA course available on CourseGalaxy.com |
| 4 | +*/ |
| 5 | + |
| 6 | +package singleLinkedList; |
| 7 | +import java.util.Scanner; |
| 8 | + |
| 9 | +public class Demo |
| 10 | +{ |
| 11 | + public static void main(String[] args) |
| 12 | + { |
| 13 | + int choice,data,k,x; |
| 14 | + |
| 15 | + Scanner scan = new Scanner(System.in); |
| 16 | + |
| 17 | + SingleLinkedList List = new SingleLinkedList(); |
| 18 | + |
| 19 | + List.createList(); |
| 20 | + |
| 21 | + while(true) |
| 22 | + { |
| 23 | + System.out.println("1.Display list"); |
| 24 | + System.out.println("2.Count the number of nodes"); |
| 25 | + System.out.println("3.Search for an element"); |
| 26 | + System.out.println("4.Insert in empty list/Insert in beginning of the list"); |
| 27 | + System.out.println("5.Insert a node at the end of the list"); |
| 28 | + System.out.println("6.Insert a node after a specified node"); |
| 29 | + System.out.println("7.Insert a node before a specified node"); |
| 30 | + System.out.println("8.Insert a node at a given position"); |
| 31 | + System.out.println("9.Delete first node"); |
| 32 | + System.out.println("10.Delete last node"); |
| 33 | + System.out.println("11.Delete any node"); |
| 34 | + System.out.println("12.Reverse the list"); |
| 35 | + System.out.println("13.Bubble sort by exchanging data"); |
| 36 | + System.out.println("14.Bubble sort by exchanging links"); |
| 37 | + System.out.println("15.MergeSort"); |
| 38 | + System.out.println("16.Insert Cycle"); |
| 39 | + System.out.println("17.Detect Cycle"); |
| 40 | + System.out.println("18.Remove cycle"); |
| 41 | + System.out.println("19.Quit"); |
| 42 | + |
| 43 | + System.out.print("Enter your choice : "); |
| 44 | + choice = scan.nextInt(); |
| 45 | + |
| 46 | + if(choice==19) |
| 47 | + break; |
| 48 | + |
| 49 | + switch(choice) |
| 50 | + { |
| 51 | + case 1: |
| 52 | + List.displayList(); |
| 53 | + break; |
| 54 | + case 2: |
| 55 | + List.countNodes(); |
| 56 | + break; |
| 57 | + case 3: |
| 58 | + System.out.print("Enter the element to be searched : "); |
| 59 | + data=scan.nextInt(); |
| 60 | + List.search(data); |
| 61 | + break; |
| 62 | + case 4: |
| 63 | + System.out.print("Enter the element to be inserted : "); |
| 64 | + data=scan.nextInt(); |
| 65 | + List.insertInBeginning(data); |
| 66 | + break; |
| 67 | + case 5: |
| 68 | + System.out.print("Enter the element to be inserted : "); |
| 69 | + data=scan.nextInt(); |
| 70 | + List.insertAtEnd(data); |
| 71 | + break; |
| 72 | + case 6: |
| 73 | + System.out.print("Enter the element to be inserted : "); |
| 74 | + data=scan.nextInt(); |
| 75 | + System.out.print("Enter the element after which to insert : "); |
| 76 | + x=scan.nextInt(); |
| 77 | + List.insertAfter(data,x); |
| 78 | + break; |
| 79 | + case 7: |
| 80 | + System.out.print("Enter the element to be inserted : "); |
| 81 | + data=scan.nextInt(); |
| 82 | + System.out.print("Enter the element before which to insert : "); |
| 83 | + x=scan.nextInt(); |
| 84 | + List.insertBefore(data,x); |
| 85 | + break; |
| 86 | + case 8: |
| 87 | + System.out.print("Enter the element to be inserted : "); |
| 88 | + data=scan.nextInt(); |
| 89 | + System.out.print("Enter the position at which to insert : "); |
| 90 | + k=scan.nextInt(); |
| 91 | + List.insertAtPosition(data,k); |
| 92 | + break; |
| 93 | + case 9: |
| 94 | + List.deleteFirstNode(); |
| 95 | + break; |
| 96 | + case 10: |
| 97 | + List.deleteLastNode(); |
| 98 | + break; |
| 99 | + case 11: |
| 100 | + System.out.print("Enter the element to be deleted : "); |
| 101 | + data=scan.nextInt(); |
| 102 | + List.deleteNode(data); |
| 103 | + break; |
| 104 | + case 12: |
| 105 | + List.reverseList(); |
| 106 | + break; |
| 107 | + case 13: |
| 108 | + List.BubbleSortExData(); |
| 109 | + break; |
| 110 | + case 14: |
| 111 | + List.BubbleSortExLinks(); |
| 112 | + break; |
| 113 | + case 15: |
| 114 | + List.mergeSort(); |
| 115 | + break; |
| 116 | + case 16: |
| 117 | + System.out.print("Enter the element at which the cycle has to be inserted : "); |
| 118 | + data=scan.nextInt(); |
| 119 | + List.insertCycle(data); |
| 120 | + break; |
| 121 | + case 17: |
| 122 | + if(List.hasCycle()) |
| 123 | + System.out.println("List has a cycle"); |
| 124 | + else |
| 125 | + System.out.println("List does not have a cycle"); |
| 126 | + break; |
| 127 | + case 18: |
| 128 | + List.removeCycle(); |
| 129 | + break; |
| 130 | + default: |
| 131 | + System.out.println("Wrong choice"); |
| 132 | + }/*End of switch*/ |
| 133 | + System.out.println(); |
| 134 | + }/*End of while*/ |
| 135 | + scan.close(); |
| 136 | + System.out.println("Exiting"); |
| 137 | + }/*End of main()*/ |
| 138 | +} |
| 139 | + |
0 commit comments