1 Replies - 646 Views - Last Post: 22 June 2012 - 04:07 AM Rate Topic: -----

#1 leeto  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 38
  • Joined: 17-June 12

Function push() and pop()

Posted 22 June 2012 - 03:28 AM

Push() - It should store the element at the current position in the embedded array. Increment the current position afterwards. There is no need for checking the current index because the array will throw an exception when the stack is full. Make sure the current index is not changed when the Array class threw an exception.
what should do in other words how I understand this :

Assume there is array of Point name arr1
(2,2) with index 0
(4,4) with index 1
(6,6) with index 2
(8,8) with index 3

if I call push then for element with index 2
arr1.push(2)
then

(2,2) with index 0
(4,4) with index 1
(8,8) with index 2 // index down
(6,6) with index 3 // index up

Pop() - function that decrements the current position and then returns the element at that position. Make sure the current index is not changed when the Array class throws an exception.

As far as I'm understand, Pop() should be the same action but in other direction. However I really not understand enough Is it makes big difference if function store or store not "element in current position" ???

Is This A Good Question/Topic? 0
  • +

Replies To: Function push() and pop()

#2 nuclearfroggy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 82
  • View blog
  • Posts: 167
  • Joined: 04-August 08

Re: Function push() and pop()

Posted 22 June 2012 - 04:07 AM

push() and pop() are functions used in conjunction with the stack data structure. The stack structure is probably best thought of as a physical "stack", like a stack of books.

When you push a book, it's like putting another book on the pile. Imagine you've got a tonne of books in the pile. You definitely don't want to take from the bottom! All actions on this stack involve the very top. So popping from the stack mean taking the top book off the stack.

In terms of arrays, then visualise the bottom as array[0] and the top as the stack as stored in a variable, which keeps track of the index. When you push an element onto the stack, if we say top refers to the top of the stack you set array[top] = el, and then increment current_position.

When you pop, you are discarding the element from the top of the stack.
After checking that the stack isn't empty, you could simply decrement "top". You may want pop() to return the top of the stack before discarding it - this isn't hard to implement as you've kept track of the top of the stack.

I'll try and give an example similar to your example-
[0,1,2,3,4] <- indexes
{empty} push 2
{2} push 4
{2,4} push 6
{2,4,6} push 8
{2,4,6,8} push 2
{2,4,6,8,2} pop
{2,4,6,8} pop
{2,4,6} ... so on
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1