Results 1 to 6 of 6
  1. #1
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default 2 Stacks in a linear array

    I need some help with this homework assignment:

    Design a method for keeping two stacks within a single linear array s[SPACESIZE] so that neither stack overflows until all of memory is used and an entire stack is never shifted to a different location within the array. Write Java methods push1, push2, pop1, and pop2 to manipulate the two stacks. (the two stacks grow toward each other.)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default

    Since a stack is virtually not limited by size, if I am going to use an array to manage 2 stacks, I have to initialize the stack to some value, so I picked 5000

  4. #4
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default

    Array<E> myArray;
    Last edited by fam2315; 06-11-2011 at 10:30 PM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I imagine that one stack will need to start at the floor of the array and grow up and the other stack will start at the roof of the array and grow down, and you will need two int index variables to represent the current location of the top of each stack, taking care that one index does not pass the other.

  6. #6
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default

    I think I have it correct now, I just had to walk through it mentally. Does my use of generics look correct? I am trying to say that my array is going to have items of type E

Similar Threads

  1. Linear Search
    By Teclis in forum New To Java
    Replies: 19
    Last Post: 04-20-2011, 05:28 AM
  2. problem with linear search
    By Metastar in forum New To Java
    Replies: 14
    Last Post: 09-14-2010, 08:01 PM
  3. linear systems of equations
    By tyang in forum New To Java
    Replies: 2
    Last Post: 03-24-2010, 03:13 PM
  4. Sorting an Array via Stacks
    By viperlasson in forum New To Java
    Replies: 0
    Last Post: 02-01-2010, 06:53 AM
  5. Help me(Linear Multiobjective programming)
    By vinaytvijayan in forum Advanced Java
    Replies: 2
    Last Post: 01-22-2008, 07:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •