Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Main{
- public static void main(String[] args){
- Scanner in = new Scanner(System.in);
- LinkedList<Integer> list = new LinkedList<>();
- boolean argument=true;
- while(argument){
- int arg;
- System.out.println("**********");
- System.out.println("Enter choice:\n1. Insertion\n2. Deletion\n3. Display\n0. Exit");
- System.out.println("**********");
- arg = in.nextInt();
- switch(arg){
- case 1://insertion
- {
- System.out.println("Enter element to be inserted:");
- list.add(in.nextInt());
- break;
- }
- case 2://Deletion
- {
- if(list.isEmpty()){
- System.out.println("List Empty!");
- break;
- }
- System.out.println("**********");
- System.out.println("Enter choice:\n1. Delete from begining\n2. Delete from end");
- System.out.println("3. Delete from particular position\n0. Exit");
- System.out.println("**********");
- int delArg= in.nextInt();
- switch(delArg){
- case 1://del from front
- list.removeFirst();
- break;
- case 2://del from end
- list.removeLast();
- break;
- case 3:
- System.out.println("Enter the element to be deleted:");
- int delElement = in.nextInt();
- int flag=0;
- int i=-1;
- Iterator<Integer> itr = list.iterator();
- try{
- while(itr.hasNext()){
- i++;
- if(itr.next()==delElement){
- list.remove(i);
- flag=1;
- }
- }
- }
- catch(ConcurrentModificationException e){
- System.out.println("Exception occured");
- break;
- }
- if(flag!=1){
- System.out.println("Element not found!");
- }
- break;
- case 0:
- break;
- }
- break;
- }
- case 3://Display
- Iterator<Integer> it = list.iterator();
- System.out.print("[ ");
- try{
- while(it.hasNext()){
- System.out.print(it.next()+" ");
- }
- System.out.println("]");
- }
- catch(ConcurrentModificationException e){
- System.out.print("Exception occured");
- break;
- }
- break;
- case 0://End
- System.out.println("Exiting...");
- argument=false;
- break;
- default:
- System.out.println("Invalid Argument!");
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement