http://www.osix.net/modules/article/?id=275
muzzy writes "Here's some code for you kids. It demonstrates the concept of stack, implemented with a linked list mechanism to support virtually infinitely large stack sizes. Happy reading."
/* Simple Dynamically Allocating Stack Implementation in C
*
* Copyright (C) 2002 Muzzy of Worst Coders
*/
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);
include <iostream> using namespace std; #define SIZE 10 template <class StackType> class stack { StackType stck[SIZE]; int topOfStack; public: void init() { topOfStack = 0; } void push(StackType ch); StackType pop(); }; template <class StackType> void stack<StackType>::push(StackType ob) { try { if(topOfStack==SIZE) throw SIZE; } catch(int) { cout << "Stack is full.\n"; return; } stck[topOfStack] = ob; topOfStack++; } template <class StackType> StackType stack<StackType>::pop() { try { if( topOfStack == 0) throw 0; } catch(int) { cout << "Stack is empty.\n"; return 0; } topOfStack--; return stck[topOfStack]; } int main() { stack<char> 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 <3; i++) cout << "Pop stack1: " << stack1.pop() << endl; for(i = 0; i <4; i++) cout << "Pop stack2: " << stack2.pop() << endl; // demonstrate double stacks stack<double> 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 <3; i++) cout << "Pop doubleValueStack1: " << doubleValueStack1.pop() << endl; for(i = 0; i <4; i++) cout << "Pop doubleValueStack2: " << doubleValueStack2.pop() << endl; return 0; }
/** C Program to Check String is Palindrome using Stack.*/#include #include void push(char);char pop();char stack[100];int top = -1;void main(){char str[100];int i, count = 0, len;printf("Enter string to check it is palindrome or not : ");scanf("%s", str);len = strlen(str);for (i = 0; i < len; i++){push(str[i]);}for (i = 0; i < len; i++){if (str[i] == pop())count++;}if (count == len)printf("%s is a Palindrome string\n", str);elseprintf("%s is not a palindrome string\n", str);}/* Function to push character into stack */void push(char c){stack[++top] = c;}/* Function to pop the top character from stack */char pop(){return(stack[top--]);}
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... }
Stack Data Structure1.It is a Linear Data Structure.2.In which a data item is inserted & deleted at one end.3.It is a Last In - First Out (LIFO) Data Structure where the data item is inserted last into the stack is the first data item to be deleted from the stack.NOTE:Please refer to http://www.cosc.canterbury.ac.nz/mukundan/dsal/StackAppl.html site to understand the concept of stack using java applet.Just Copy & Paste it in your browser,but you need a Java RunTime Environment(JRE) installed onto your PC.4.Writting a value to stack is push operation.5.Reading a value from the stack is pop operation.6.Once an item is popped from the stack,it is no longer available.Algorithm1.push operation:if top of the stack is greater than equal to maximum number of entries into the stack,then print "Stack is already full.Cannot add more items."top of stack = tincrement the top of the stack2.pop operation:decrement top of the stackif top
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);
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.
there are two operations you can do with a STACK one is PUSH operation and the other is POP operation
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.
include <iostream> using namespace std; #define SIZE 10 template <class StackType> class stack { StackType stck[SIZE]; int topOfStack; public: void init() { topOfStack = 0; } void push(StackType ch); StackType pop(); }; template <class StackType> void stack<StackType>::push(StackType ob) { try { if(topOfStack==SIZE) throw SIZE; } catch(int) { cout << "Stack is full.\n"; return; } stck[topOfStack] = ob; topOfStack++; } template <class StackType> StackType stack<StackType>::pop() { try { if( topOfStack == 0) throw 0; } catch(int) { cout << "Stack is empty.\n"; return 0; } topOfStack--; return stck[topOfStack]; } int main() { stack<char> 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 <3; i++) cout << "Pop stack1: " << stack1.pop() << endl; for(i = 0; i <4; i++) cout << "Pop stack2: " << stack2.pop() << endl; // demonstrate double stacks stack<double> 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 <3; i++) cout << "Pop doubleValueStack1: " << doubleValueStack1.pop() << endl; for(i = 0; i <4; i++) cout << "Pop doubleValueStack2: " << doubleValueStack2.pop() << endl; return 0; }
/** C Program to Check String is Palindrome using Stack.*/#include #include void push(char);char pop();char stack[100];int top = -1;void main(){char str[100];int i, count = 0, len;printf("Enter string to check it is palindrome or not : ");scanf("%s", str);len = strlen(str);for (i = 0; i < len; i++){push(str[i]);}for (i = 0; i < len; i++){if (str[i] == pop())count++;}if (count == len)printf("%s is a Palindrome string\n", str);elseprintf("%s is not a palindrome string\n", str);}/* Function to push character into stack */void push(char c){stack[++top] = c;}/* Function to pop the top character from stack */char pop(){return(stack[top--]);}
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....
I HOPE THIS WILL HELP U OUT...ALGO GOES LIKE THIS: Reverrse prreffiix and evalluatte iittreverse given prefix expression;scan the reversed prefix expression;for each symbol in reversed prefixif operand thenpush its value onto stack S;if operator then {pop operand1;pop operand2;compute result = operand1 op operand2;push result back onto stack S;}return value at top of stack;AND EXAMPLE IS:Prefix: */-abc-+def Reversed: fed+-cba-/*ch action stackf push fe push f ed push f e d+ pop op1 f epop op2 fcalc &push f (d+e)- pop op1 fpop op2calc & push ((d+e)-f)c push ((d+e)-f) cb push ((d+e)-f) c ba push ((d+e)-f) c b a- pop op1 ((d+e)-f) c bpop op2 ((d+e)-f) ccalc & push ((d+e)-f) c (a-b)ch action stack/ pop op1 ((d+e)-f) cpop op2 ((d+e)-f)calc & push ((d+e)-f) ((a-b)/c)* pop op1 ((d+e)-f)pop op2calc & push ((a-b)/c)*((d+e)-f)
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... }
"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.
Push and pop are properties of a stack (also called a LIFO-- Last In, First Out-- queue).
In computing, PUSH and POP refer to the principal operations with a stack data structure, where pushadds a new item on the top of the stack and pop removes an item from the top of the stack.