-1

This code is working but i want my added words to show in vertical because its output is shown in horizontal if two or more words is inserted. Can you help me to improve my program? Here are the codes...

/**
 * Write a description of class Stacks here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

import java.util.*;
import java.io.*;

public class Queues_Lab2 {
    public static void main (String args[]) {
        Scanner s = new Scanner(System.in);
        LinkedList st = new LinkedList();

        int count = 0;
        int y = -1;

        boolean b = false, checker = false;

         while(!checker){
        System.out.println("\n\t\t\t_ M _ E _ N _ U _");
        System.out.println("[1] Enqueue");
        System.out.println("[2] Dequeue");
        System.out.println("[3] View");
        System.out.println("[4] Front");
        System.out.println("[5] Rear");
        System.out.println("[6] Exit");
        System.out.print("CHOICE: ");
        String choice = s.next();



    if(choice.equals("1")){
        System.out.print("Enter word: ");
        if(count == 10) {
            System.out.println("Overflow Not allowed!");
        }
        else{
            String enterWord = s.next();
            st.add(enterWord);

        System.out.print( st+ "is Inserted");
            count++;

       }
    }


     else if(choice.equals("2")){
        if(count == 0) {
         System.out.println("Underflow Not allowed!");
    }
    else{
        System.out.println(st.pop());
        System.out.println("Queue Elements: \n" + st);
    }
}
    else if(choice.equals("3")){
        System.out.println("Queue Elements: \n" +st );

    }

    else if(choice.equals("4")){
        System.out.println("Front Elements: \n" +st.peek());

    }

    else if(choice.equals("5")){
        int loc = st.size()-1;
        System.out.print("Rear Elements: \n" +st.get(loc));
    }

    else if(choice.equals("6")){
        System.out.println("ByeBye! :)");
        System.exit(0);
    }
    else{   
        System.out.println("Wrong Choice!!");

    }
    }
    }
}
3
Contributors
4
Replies
21
Views
3 Years
Discussion Span
Last Post by Phaelax
0

st is a linked list, not a string. Anytime you output an object like that it's going to use its default toString() method. You can either extend the LinkedList class and override the toString() method to keep the output implementation separate from your existing program, or simply write a new method in the Queues_Lab2 class to take a linkedlist as a parameter and output it vertically like you want by looping through all of the list's elements as stultuske said.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.