answersLogoWhite

0


Best Answer

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

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

push(stack,top,maxstk,item)

[chek the overflow condition]

1) top=maxstk, return step=4,print =overflow

else

2)set top=top+1;[incresase by 1)

3)set stack[top]=item[insert item at top, item is to insert]

4)return

pop[stack,top,item]

1) if top=0, print= underflow

2)set item=stack[top]

3)set top=top-1

4)return

by-komal prasad;meri college

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

#include

#include

#include

#include

int main() {

std::array data {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};

std::stack s; // a stack of integers (initially empty)

for (auto datum : data) s.push (datum); // push elements from array

while (!s.empty()) {

std::cout << s.top() << std::endl; // print the top value

s.pop(); // extract the top value

}

assert (s.empty());

return 0;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you push and pop stack elements?
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);


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.


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.

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


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.


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


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


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.


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