answersLogoWhite

0

algorithm of push

if(top==Max-1)

{

cout<<"\n the stack is full";

}

else

top++;

arr[top]=item;

////////////////////////////////////////

algorithm of pop

if(top==-1)

{

cout<<"\n the stack is empty";

}

else

return arr[top];

top--;

}

User Avatar

Wiki User

13y ago

What else can I help you with?

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


What is the main difference between stack and array?

ArrayIt is a data structure that has group of same type elements in linear sequence. It requires continuous memory block to store it. Elements in Array is accessed by index. Array does not have any predefined functions.StackIt is a data structure that is a list of ordered elements. In most of the programming languages and computer architecture stack has limitation in size. Elements in stack might not be the same type. Stack has predefined functions: POP (get top element), PUSH (put element on top) and it works by LIFO(Last In First Out) principle. Elements from stack are removed in reverse order to the order of their addition.Example:POP 1;POP 2;POP 3;Stack: (top) 3 2 1 (bottom)PUSHPUSHPUSHWe get elements in this order: 3 2 1


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... }


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.


Write a Program for Array implementation of queue and stack using exception handling.?

include &lt;iostream&gt; using namespace std; #define SIZE 10 template &lt;class StackType&gt; class stack { StackType stck[SIZE]; int topOfStack; public: void init() { topOfStack = 0; } void push(StackType ch); StackType pop(); }; template &lt;class StackType&gt; void stack&lt;StackType&gt;::push(StackType ob) { try { if(topOfStack==SIZE) throw SIZE; } catch(int) { cout &lt;&lt; "Stack is full.\n"; return; } stck[topOfStack] = ob; topOfStack++; } template &lt;class StackType&gt; StackType stack&lt;StackType&gt;::pop() { try { if( topOfStack == 0) throw 0; } catch(int) { cout &lt;&lt; "Stack is empty.\n"; return 0; } topOfStack--; return stck[topOfStack]; } int main() { stack&lt;char&gt; stack1, stack2; int i; stack1.init(); stack2.init(); stack1.push('a'); stack2.push('x'); stack1.push('b'); stack2.push('y'); stack1.push('c'); stack2.push('z'); for(i = 0; i &lt;3; i++) cout &lt;&lt; "Pop stack1: " &lt;&lt; stack1.pop() &lt;&lt; endl; for(i = 0; i &lt;4; i++) cout &lt;&lt; "Pop stack2: " &lt;&lt; stack2.pop() &lt;&lt; endl; // demonstrate double stacks stack&lt;double&gt; doubleValueStack1, doubleValueStack2; // create two stacks // initialize the stacks doubleValueStack1.init(); doubleValueStack2.init(); doubleValueStack1.push(1.1); doubleValueStack2.push(2.2); doubleValueStack1.push(3.3); doubleValueStack2.push(4.4); doubleValueStack1.push(5.5); doubleValueStack2.push(6.6); for(i = 0; i &lt;3; i++) cout &lt;&lt; "Pop doubleValueStack1: " &lt;&lt; doubleValueStack1.pop() &lt;&lt; endl; for(i = 0; i &lt;4; i++) cout &lt;&lt; "Pop doubleValueStack2: " &lt;&lt; doubleValueStack2.pop() &lt;&lt; endl; return 0; }

Related Questions

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


How can I efficiently use a stack to sort elements in a data structure?

To efficiently use a stack to sort elements in a data structure, you can follow these steps: Push all elements into the stack. Create a temporary stack to store the sorted elements. While the original stack is not empty, pop an element from the original stack. Compare the popped element with the top element of the temporary stack. If the popped element is greater, push it onto the temporary stack. If the popped element is smaller, keep popping elements from the temporary stack and pushing them back onto the original stack until the temporary stack is empty or the top element is greater. Repeat steps 3-6 until the original stack is empty. The elements in the temporary stack will now be sorted in ascending order. By following these steps, you can efficiently use a stack to sort elements in a data structure.


What are the stack operation?

there are two operations you can do with a STACK one is PUSH operation and the other is POP operation


How stack pointer registers are involved in the execution of push and pop instruction?

If the stack is empty assume the stack pointer has a value of P. when you push something on the stack you increment P. when you pull something from stack you decrement P.


How can we do push and pop operations on both sides of stack?

This is not possible as this violates the basic definition of stack...That can have only two primitive operations "Push" "POP" If u want to implement what u want u can do that by some implementation of Push pop combination but that is what other data strutures like queue do....


What is the main difference between stack and array?

ArrayIt is a data structure that has group of same type elements in linear sequence. It requires continuous memory block to store it. Elements in Array is accessed by index. Array does not have any predefined functions.StackIt is a data structure that is a list of ordered elements. In most of the programming languages and computer architecture stack has limitation in size. Elements in stack might not be the same type. Stack has predefined functions: POP (get top element), PUSH (put element on top) and it works by LIFO(Last In First Out) principle. Elements from stack are removed in reverse order to the order of their addition.Example:POP 1;POP 2;POP 3;Stack: (top) 3 2 1 (bottom)PUSHPUSHPUSHWe get elements in this order: 3 2 1


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 pop in C plus plus?

"Pop" allows you to remove items off of the stack. The stack is an area in memory that contains the information of a program, such as function names and instructions, the values of variables, etc. The opposite of pop is "push". This allows you to add items to the stack.


What data structure the pushand pop?

Push and pop are properties of a stack (also called a LIFO-- Last In, First Out-- queue).


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.


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.


Write a Program for Array implementation of queue and stack using exception handling.?

include &lt;iostream&gt; using namespace std; #define SIZE 10 template &lt;class StackType&gt; class stack { StackType stck[SIZE]; int topOfStack; public: void init() { topOfStack = 0; } void push(StackType ch); StackType pop(); }; template &lt;class StackType&gt; void stack&lt;StackType&gt;::push(StackType ob) { try { if(topOfStack==SIZE) throw SIZE; } catch(int) { cout &lt;&lt; "Stack is full.\n"; return; } stck[topOfStack] = ob; topOfStack++; } template &lt;class StackType&gt; StackType stack&lt;StackType&gt;::pop() { try { if( topOfStack == 0) throw 0; } catch(int) { cout &lt;&lt; "Stack is empty.\n"; return 0; } topOfStack--; return stck[topOfStack]; } int main() { stack&lt;char&gt; stack1, stack2; int i; stack1.init(); stack2.init(); stack1.push('a'); stack2.push('x'); stack1.push('b'); stack2.push('y'); stack1.push('c'); stack2.push('z'); for(i = 0; i &lt;3; i++) cout &lt;&lt; "Pop stack1: " &lt;&lt; stack1.pop() &lt;&lt; endl; for(i = 0; i &lt;4; i++) cout &lt;&lt; "Pop stack2: " &lt;&lt; stack2.pop() &lt;&lt; endl; // demonstrate double stacks stack&lt;double&gt; doubleValueStack1, doubleValueStack2; // create two stacks // initialize the stacks doubleValueStack1.init(); doubleValueStack2.init(); doubleValueStack1.push(1.1); doubleValueStack2.push(2.2); doubleValueStack1.push(3.3); doubleValueStack2.push(4.4); doubleValueStack1.push(5.5); doubleValueStack2.push(6.6); for(i = 0; i &lt;3; i++) cout &lt;&lt; "Pop doubleValueStack1: " &lt;&lt; doubleValueStack1.pop() &lt;&lt; endl; for(i = 0; i &lt;4; i++) cout &lt;&lt; "Pop doubleValueStack2: " &lt;&lt; doubleValueStack2.pop() &lt;&lt; endl; return 0; }