answersLogoWhite

0


Best Answer

You don't. A stack is a last in first out (LIFO) structure so you only have access to the top element in the stack. If you want to locate the smallest element in the stack, you need to pop everything off the stack in order to find it, at which point the stack is completely ruined. The only way to restore a stack is to push every element onto another stack as they are popped off. The other stack will then be the reverse of the original, so you just repeat the process to transfer the elements back to the original stack.

You should really be asking why you are using a stack in the first place if the intent is to remove an element other than the top element. A forward list would be a much better option.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you pop out the smallest element from a stack?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you reverse the order of element on stack by using one additional stack and some additional non array variables?

You pop elements off of one stack and push them onto the other. This reverses the order of the elements. while ((element = pop(stack1)) != NULL) push(stack2, element);


Define list of operation on stack?

There are 4 main widely used stack operations.Operations:* POP - increase stack pointer and return top element * PUSH - putting element into stack's top * TOP - returns data of top element on stack * LENGTH/SIZE - returns number of elements inside stack For more detailed implementation details, please check web links.


What is the operation of delete in stack?

Delete is mostly commonly known as pop in stack. The last element inserted into the stack is removed from the stack. Here is an illustration:Consider the stack with the elements 1,2,3,4 inserted in order.1->2->3->4\topThe top of the stack will be pointing to 4. When pop operation is performed, 4 is removed from the stack and the top is made to point to 3. The stack then becomes:1->2->3\top


What is push operation in data structure?

Push inserts a value onto the top of the stack. Pop extracts the top value from the stack. These are the two primary operations that can be performed upon a stack. Prior to popping a value, you will first check the stack is not empty, store the top value, then pop the stack. For a stack of type T, you might use the following: if (!stack.empty()) { T value {stack.top()}; // copy top value stack.pop(); // remove value from stack // use value... }


Write an algorithm to remove an item from the top of the stack?

It is not possible to write a code to POP from the stack when there is no your stack implementation information.Because of that I am going to talk more about Stack in computer architecture and there will be additional link to specific examples(-e).In x86 architecture there is three registers (BP, SP and SS) which are connected with stack and only SP and SS is needed.SS - Stack Segment (base register);SP - Stack Pointer (offset);This is how the POP instruction works:# operand = [SS:SP] (top of the stack) # SP = SP + 2; (change SP to point to new top element)

Related questions

What function is used to pop and destory the stack in a single step?

Just initialize the stack pointer...stack = top;However, this will leak memory if the stored elements contain pointers, so it may be more prudent to pop each element off and delete it instead...while ((element = pop(stack)) != NULL) free (element);


How do you reverse the order of element on stack by using one additional stack and some additional non array variables?

You pop elements off of one stack and push them onto the other. This reverses the order of the elements. while ((element = pop(stack1)) != NULL) push(stack2, element);


Define list of operation on stack?

There are 4 main widely used stack operations.Operations:* POP - increase stack pointer and return top element * PUSH - putting element into stack's top * TOP - returns data of top element on stack * LENGTH/SIZE - returns number of elements inside stack For more detailed implementation details, please check web links.


What is the operation of delete in stack?

Delete is mostly commonly known as pop in stack. The last element inserted into the stack is removed from the stack. Here is an illustration:Consider the stack with the elements 1,2,3,4 inserted in order.1->2->3->4\topThe top of the stack will be pointing to 4. When pop operation is performed, 4 is removed from the stack and the top is made to point to 3. The stack then becomes:1->2->3\top


What is push operation in data structure?

Push inserts a value onto the top of the stack. Pop extracts the top value from the stack. These are the two primary operations that can be performed upon a stack. Prior to popping a value, you will first check the stack is not empty, store the top value, then pop the stack. For a stack of type T, you might use the following: if (!stack.empty()) { T value {stack.top()}; // copy top value stack.pop(); // remove value from stack // use value... }


Write an algorithm to remove an item from the top of the stack?

It is not possible to write a code to POP from the stack when there is no your stack implementation information.Because of that I am going to talk more about Stack in computer architecture and there will be additional link to specific examples(-e).In x86 architecture there is three registers (BP, SP and SS) which are connected with stack and only SP and SS is needed.SS - Stack Segment (base register);SP - Stack Pointer (offset);This is how the POP instruction works:# operand = [SS:SP] (top of the stack) # SP = SP + 2; (change SP to point to new top element)


What is stack define the operations of stack?

In modern computer languages, the stack is usually implemented with more operations than just "push" and "pop". The length of a stack can often be returned as a parameter. Another helper operation top (also known as peek and peak) can return the current top element of the stack without removing it from the stack. This section gives pseudocode for adding or removing nodes from a stack, as well as the length and top functions. Throughout we will use null to refer to an end-of-list marker or sentinel value, which may be implemented in a number of ways using pointers. In modern computer languages, the stack is usually implemented with more operations than just "push" and "pop". The length of a stack can often be returned as a parameter. Another helper operation top[1] (also known as peek and peak) can return the current top element of the stack without removing it from the stack. This section gives pseudocode for adding or removing nodes from a stack, as well as the length and top functions. Throughout we will use null to refer to an end-of-list marker or sentinel value, which may be implemented in a number of ways using pointers. record Node { data // The data being stored in the node next // A reference to the next node; null for last node } record Stack { Node stackPointer // points to the 'top' node; null for an empty stack } function push(Stack stack, Element element) { // push element onto stack new(newNode) // Allocate memory to hold new node newNode.data := element newNode.next := stack.stackPointer stack.stackPointer := newNode } function pop(Stack stack) { // increase the stack pointer and return 'top' node // You could check if stack.stackPointer is null here. // If so, you may wish to error, citing the stack underflow. node := stack.stackPointer stack.stackPointer := node.next element := node.data return element } function top(Stack stack) { // return 'top' node return stack.stackPointer.data } function length(Stack stack) { // return the amount of nodes in the stack length := 0 node := stack.stackPointer while node not null { length := length + 1 node := node.next } return length } As you can see, these functions pass the stack and the data elements as parameters and return values, not the data nodes that, in this implementation, include pointers. A stack may also be implemented as a linear section of memory (i.e. an array), in which case the function headers would not change, just the internals of the functions. Implementation A typical storage requirement for a stack of n elements is O(n). The typical time requirement of O(1) operations is also easy to satisfy with a dynamic array or (singly) linked list implementation. C++'s Standard Template Library provides a "stack" templated class which is restricted to only push/pop operations. Java's library contains a Stack class that is a specialization of Vector. This could be considered a design flaw because the inherited get() method from Vector ignores the LIFO constraint of the Stack. Here is a simple example of a stack with the operations described above (but no error checking) in Python. class Stack(object): def __init__(self): self.stack_pointer = None def push(self, element): self.stack_pointer = Node(element, self.stack_pointer) def pop(self): e = self.stack_pointer.element self.stack_pointer = self.stack_pointer.next return e def peek(self): return self.stack_pointer.element def __len__(self): i = 0 sp = self.stack_pointer while sp: i += 1 sp = sp.next return i class Node(object): def __init__(self, element=None, next=None): self.element = element self.next = next if __name__ == '__main__': # small use example s = Stack() [s.push(i) for i in xrange(10)] print [s.pop() for i in xrange(len(s))] The above is admittedly redundant as Python supports the 'pop' and 'append' functions to lists.


What is top pointer of stack?

top pointer of a stack is the pointer that refers to the top most element of the stack.


What is the pseudo code for returning the top element of a stack?

A stack is a singly-linked list where new elements are added (pushed) to the head, and existing elements are removed (popped) from the head. As such, the stack object need only maintain a pointer to the head node and implement the pop and push methods. A stripped-down implementation in C++ follows: // Forward declaration class stack; // Minimal declaration... class node { friend node * stack::pop(); node * next; }; // Minimal implementation of stack demonstrating pop implementation class stack { node * head; public: node * pop() { // store the head pointer node * popped = head; // update head pointer if not NULL if( head ) head = head.next; // return stored pointer return( popped ); } };


What the smallest form of a element?

The smallest form of an element is an atom. :))


How does push work on registers and variables?

The PUSH instruction decrements the stack pointer by the size of the operand and then stores its operand at the memory address pointed to by the stack pointer. This leaves the stack pointer always pointing to the last element pushed onto the stack.The POP instruction reverses the sequence, retrieving the operand first, and then incrementing the stack pointer by the size of the operand.Also, PUSH and POP do not work on variables - they only work on register values. You can pop/push a variable, however, by using a register and then storing/retrieving the register to/from memory.


What the smallest particle of an element that can be identified as that element is?

a quark or if you mean the smallest element hydrogen