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!!");

    }
    }
    }
}

Recommended Answers

All 4 Replies

how about looping over st and printing it's elements, instead of printing st itself?

like? what do you mean like?
just print the elements of the LinkedList, instead of the List itself.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.