Pop and peep at the same?

Of all the sources that I read, they say - the difference between peek and pop is that peek does not remove the top value. In the above example, from my lecture notes, apparently, they do the same thing using a different subtraction method. After both operations the vertex subtracts 1.

I'm right? Probably not, can someone explain how they differ?

int pop(void) { assert(top>0); return data[--top]; } int peek(void) { assert(top>0); return data[top-1]; } 
+4
source share
7 answers

top is a state variable for your stack, which is stored in a regular array. The variable top refers to the top of the stack, preserving the index of the array.

The first pop operation uses the decrement operator to change the top variable, and therefore the stack state: --top equivalent to top = top - 1 . The value is still in the array, but since the top variable now refers to a different index, this value is effectively deleted: the top of the stack is now another element. Now, if you call push , this popup value will be overwritten.

The second operation does not change the value of the top variable, it uses it only to return the value at the top of the stack. The top variable still refers to the same value as the top of the stack, and therefore the stack does not change.

+6
source

They will return the same value, but only pop changes the top:

 --top 

it is equivalent

 top = top -1 

when the top - 1 does not change the value of the vertex.

+4
source

it

 return data[--top]; 

changes the top value while this one

 return data[top-1]; 

Don't change it. Thus, when you call pop , the pointer to the top of the stack is modified to point to the new element, and peek leaves the pointer unchanged, so the functions behave in the right sense, describing

+2
source

In pop its execution is --top , which is equal to top=top-1 , so it changes the value of top .

While in peek , its execution is top-1 i.e. just decreases top by 1 and uses the value, top doesn't change.

+2
source

In general programming terms, the term pop refers to the method of returning an object from the stack, while at the same time removing it from the stack. The term "peek" is more general and can be used on other data / ADT containers than on stacks. "Peek" always means "give me the next item, but don’t remove it from the container."

Most often, β€œpeeking” is used together with the queue, like data containers, for example, the Windows API function, for checking the next message in the window message queue is called PeekMessage ().

+2
source

Function pop - extracts the top element and moves to the next one before it. Thus, the pointer is now moved to the next position of the element.top element is now reduced by 1.

Peek function - returns only the top element, but the pointer is still there. therefore, the position of the top element remains unchanged.

0
source

in fact, the pop and the peak are the same, but the fact is that the first saw the data and closed the window, but later the first took the data, seeing them and closed the box

-2
source

Source: https://habr.com/ru/post/1434289/


All Articles