answersLogoWhite

0


Want this question answered?

Be notified when an answer is posted

Add your answer:

Earn +20 pts
Q: What is top element in stack?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is top pointer of stack?

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


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.


How do you pop out the smallest element from a stack?

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.


When an element is pushed in a stack is it possible to push it at a particular index?

No, the whole idea of a stack is that elements can only be added at the 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... }

Related questions

What is top pointer of stack?

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


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.


How do you pop out the smallest element from a stack?

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.


When an element is pushed in a stack is it possible to push it at a particular index?

No, the whole idea of a stack is that elements can only be added at the 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... }


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 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);


Algorithm for implementing push operation on stack?

1. If TOP = MAXSTK THEN [Stack already filled?] Print "OVERFLOW" Go to step 4 Endif 2. TOP = TOP + 1 [Increase TOP by 1] 3. Set ST[STOP] = ITEM [Insert ITEM in new TOP position] 4. End


Write a java program to implement stack in java?

You would need an array to store the actual stack. Better use an ArrayList or some other structure that you can redimension. You'll also need a variable to point to the "top of stack" - the last element added, which is the first element to be taken away.


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.


Why you will declare top as -1 for stacks to be empty in c program?

You would do this if you implement a stack using an array. Using a zero-based index to keep track of the top of the stack (the end of the array) means we must use the value -1 to indicate an empty stack. This is because an array of n elements will have indices 0 through n-1, where index n-1 represents the element at top of the stack. An empty stack has 0 elements, therefore the top of the stack is represented by index -1.


Why is the queue data structure called a LIFO?

It isn't! A queue is a FIFO structure, not a LIFO structure. FIFO is an acronym for First-In, First-Out and is analogous with first come, first served (as per a queue of people waiting to be served). LIFO is an acronym for Last-In, First-Out, which is analogous with a stack structure, where the last element added is always placed on top of the stack while the top-most element of the stack is always the first to be removed from the stack.