Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedList;
- import java.util.Scanner;
- class LinkedLists{
- public static void main(String[] args){
- Scanner sc = new Scanner(System.in);
- LinkedList queue = new LinkedList();
- LinkedList stack = new LinkedList();
- int n;
- while(true){
- System.out.print("\n\n1.Queue 2.Stack 3.Exit: ");
- switch(sc.nextInt()){
- case 1: n = 1;
- while(n==1){
- System.out.print("\n1.Enqueue 2.Dequeue 3.Get First 4.Get Last 5.Display 6.Back: ");
- switch(sc.nextInt()){
- case 1: System.out.print("Enter a string: ");
- queue.add(sc.next());
- break;
- case 2: if(queue.size() > 0) queue.removeFirst();
- else System.out.println("Nothing to delete.");
- break;
- case 3: System.out.println(queue.getFirst());
- break;
- case 4: System.out.println(queue.getLast());
- break;
- case 5: for(int i = 0; i < queue.size(); i++)
- System.out.print(queue.get(i)+"\t");
- break;
- case 6: n=0;
- }
- }
- break;
- case 2: n = 1;
- while(n==1){
- System.out.print("\n1.Push 2.Pop 3. Peek 4.Display 5.Back: ");
- switch(sc.nextInt()){
- case 1: System.out.print("Enter a string: ");
- stack.add(sc.next());
- break;
- case 2: if(stack.size() > 0) stack.removeLast();
- else System.out.println("Nothing to delete.");
- break;
- case 3: if(stack.size() > 0) System.out.println(stack.getLast());
- else System.out.println("Can't peek.");
- break;
- case 4: for(int i = (stack.size()-1); i >= 0; i--)
- System.out.print(stack.get(i)+"\n");
- break;
- case 5: n = 0;
- }
- }
- break;
- case 3: return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement