Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Please Help Java Stack

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please Help Java Stack

    The Question is :
    Three stacks can be used to sort a list of numbers. Assuming stack in holds the input list of numbers, stack out is to hold the output list after sorting the numbers and temp is used during the sorting process. The sorting algorithm follows.

    1 set up stack in and print it
    2 while stack in is not empty repeat
    2.1 max = in.pop
    2.2 while there are still element in stack in repeat
    2.2.1 value = in.pop
    2.2.2 if value > max
    2.2.2.1 temp.push(max)
    2.2.2.2 max = value
    2.2.3 else
    2.2.3.1 temp.push(value)
    2.3 in = temp
    2.4 out.push(max)
    2.5 temp.clear
    3 print sorted stack

       import java.util.*;
       public class MainAssignment3
       {
       public static void main(String[]args) 
       {
       int Max =0;
       int Value =0;
     
       LinkedList<Integer>Input=new LinkedList<Integer>();
       LinkedList<Integer>Temp=new LinkedList<Integer>();
       LinkedList<Integer>OutPut=new LinkedList<Integer>();
     
       Input.addLast(90);
       Input.addLast(21);
       Input.addLast(33);
       Input.addLast(80);
       Input.addLast(67);
     
       System.out.println("The Input Stack is : " + Input);
     
       while(!Input.isEmpty())
          {
            Max = Input.removeLast();  
           Value = Input.removeLast();   
     
          System.out.println("MAx: " +Max);
          System.out.println("Value: " +Value);
     
         if (Value > Max)
         {
          Temp.push(Max);
          Max=Value;
     
          } else {
          Temp.push(Value);
     
         }
     
         Input=Temp;
         OutPut.push(Max);
         Temp.clear();
     
         }
         System.out.println("The Output Stack is: " + OutPut);   
     
      }
    }


    can any one guide me how to sort a number ?
    beause the OutPut Stack Should be [21, 33, 67, 80 ,90]
    But i Only Get [80], any one can guide how to do ? thanks


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Location
    china
    Posts
    12
    My Mood
    Sad
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Please Help Java Stack

    Why do you assign Temp to Input, leading to a cycle of only 1

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please Help Java Stack

    it's just we have to use it any how so tell me a way to correct this..

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Please Help Java Stack

    jCloud has referenced you to the exact point. And also, where do you place the last remaining values?

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Please Help Java Stack

    Quote Originally Posted by izzahmed View Post
    it's just we have to use it any how so tell me a way to correct this..
    NO!!!!
    You try to find it, a little brain storming will lead you to the solution. Don't be dependent. Think on your own and you will get it done.

  6. #6
    Junior Member
    Join Date
    Oct 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please Help Java Stack

    thanks Mr.777 got so many things to brain storming.. and found this is the best way. anyway thanks for the advice given..

  7. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please Help Java Stack

    and i would very much appreciate if u could just point the wrong part and rewrite the whole corrected code for me.

    thanks
    Last edited by izzahmed; November 18th, 2011 at 07:44 PM.

  8. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Please Help Java Stack

    Quote Originally Posted by izzahmed View Post
    and rewrite the whole corrected code for me.

    thanks
    NO!!!!! We don't. We aren't allowed to, We don't have enough time. We don't want you to be dependent. We want you to learn.

  9. #9
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: Please Help Java Stack

    I agree with Mr.777.
    hope you got the answers.

Similar Threads

  1. Java Program to Check whether a Expression is Valid using Stack
    By rainbow9 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 12th, 2011, 02:11 AM
  2. how to find stack size in java
    By aditiya in forum Java Theory & Questions
    Replies: 6
    Last Post: July 8th, 2011, 06:29 AM
  3. stack Palindrome , java
    By Faha in forum Object Oriented Programming
    Replies: 1
    Last Post: May 3rd, 2011, 04:20 PM
  4. What am I doing wrong with this stack? Java Programming?
    By babe20042004 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 1st, 2011, 12:19 PM
  5. Custom Java stack class (with generics) problem
    By TBBucs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 7th, 2010, 02:25 AM