6 Replies - 245 Views - Last Post: 08 May 2012 - 01:14 PM Rate Topic: -----

#1 kev3kev3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-April 12

Need features of Arrays and Stack in one

Posted 08 May 2012 - 04:56 AM

Is it possible to have a fixed length stack or have an array which is obviously a fixed size but can use push to add elements to the top of the array and then the least recently used element is removed from the array?
Is This A Good Question/Topic? 0
  • +

Replies To: Need features of Arrays and Stack in one

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Need features of Arrays and Stack in one

Posted 08 May 2012 - 05:13 AM

1 - If you create your own Stack class, you can program how many elements it can contain.

2 - You can also do as you describe with Arrays. In fact, some Stack implementations use arrays just as you describe. (Assuming you meant the word "last" rather than "least".)
Was This Post Helpful? 0
  • +
  • -

#3 kev3kev3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-April 12

Re: Need features of Arrays and Stack in one

Posted 08 May 2012 - 07:04 AM

So if i have a standard array of ints:

 

int [] array = new int [5]




i then fill the array with the contents

[0] = 1
[1] = 2
[2] = 3
[3] = 4
[4] = 5

Can I now somehow call a push method to add number 6 for example to element 0 move all the contents down by one lose the 4th element number 5.

??
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7649
  • View blog
  • Posts: 12,905
  • Joined: 19-March 11

Re: Need features of Arrays and Stack in one

Posted 08 May 2012 - 07:06 AM

You could just use an ArrayList. If it has to be fixed capacity for some reason, you could extend it or wrap it.

Probably I'd wrap it, since ArrayList has a lot of methods you don't want to expose for this functionality.

This post has been edited by jon.kiparsky: 08 May 2012 - 07:11 AM

Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Need features of Arrays and Stack in one

Posted 08 May 2012 - 07:32 AM

Quote

Can I now somehow call a push method to add number 6 for example to element 0 move all the contents down by one lose the 4th element number 5.

Another way to think of it is to use a cursor or pointer that points to the "top" of the stack. In your example, once the stack is full - whichever kind of arrays you use - the pointer "wraps" to the other end, effectively pushing the oldest item off the bottom of the stack. Do some doodling with pencil and paper to visualize how this could work.
Was This Post Helpful? 1
  • +
  • -

#6 kev3kev3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-April 12

Re: Need features of Arrays and Stack in one

Posted 08 May 2012 - 10:30 AM

View PostGregBrannon, on 08 May 2012 - 07:32 AM, said:

Quote

Can I now somehow call a push method to add number 6 for example to element 0 move all the contents down by one lose the 4th element number 5.

Another way to think of it is to use a cursor or pointer that points to the "top" of the stack. In your example, once the stack is full - whichever kind of arrays you use - the pointer "wraps" to the other end, effectively pushing the oldest item off the bottom of the stack. Do some doodling with pencil and paper to visualize how this could work.



Hi,

What you have said above is totally foreign to me, can you put this in an example?

Thanks
Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Need features of Arrays and Stack in one

Posted 08 May 2012 - 01:14 PM

You started off with, "Is it possible . . .?" and we've answered your question. If what's possible is beyond your ability to understand and therefore to program, then the remaining possibilities are:

1) we teach you the programming concepts and language tools necessary for you to understand and write the program, or

2) we do the work for you.

We do neither here. If the task you've been given is so formidable and the concepts to complete it so foreign, then I suggest you discuss your shortcomings with your instructor.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1