i am write a code to print element in stack in reverse order
i need two stack to push from first and pop to other
but i have something error ..

#include<iostream.h>
#include<process.h>
struct stack{
	stack s1,s2;
	int info;
};
class  stacks {
private:
stack top;
int x;
public:
	s1.stack(){top=-1;}	
	s2.stack(){top=-1;}
	void menu(){
		int choice;
		cout<<"\n1- push ,,,, 2- pop ,,,,, 3- traverse ,,, ,,5-exit\n";
		cin>>choice;
		switch(choice)
		{
		case 1:
				cout<<"enter num";
				cin>>x;
				s1.push (x);}
			break;
		case 2:
			if(isempty())
				cout<<"empty";
			else {
				x=s1.pop();
				cout<<"num delder="<<x;
				s2.push(x);
				cout<<"num enter="<<x;
			}
			break;
		case 3:
	if(isempty())
				cout<<"empty";
	else {
		cout<<"the list is ";
		s1.traverse();}
		break;
	 
		case 4:
			exit(0);
		default:
			cout<< " invalid choice ";
		} 
	}

bool isempty( )
{
	if (top==-1)
		return true ;
	else
		return false ;
} 
	void traverse() {
stack *i;
do{
	cout<<i->info;
	i=i->next;
}
while(i!=NULL);
	}


	
	void push(int x){
	 
			stack *p=new stack;
			p->info=x;
			p-<next=NULL;
			if(top==NULL)
				top=p;
			else 
			{
				p->next=top;
				top=p;
	}
	int pop()
	{
		stack*t;
		x=top->info;
		x=top;
		top=top->next;
		delste t;
		return x;
	}
};


	void main()
	{ 
		stacks m;
	while (1)
	m.menu();
}

after modify but still i have errors

#include<iostream.h>
#include<process.h>
struct stack{
	stack s1,s2;
	int info;
};
class  stacks {
private:
stack top;
int x;
public:
	stacks(){
		top=NULL;
	}	
	void menu(){
		int choice;
		cout<<"\n1- push ,,,, 2- pop ,,,,, 3- traverse ,,, ,,5-exit\n";
		cin>>choice;
		switch(choice)
		{
		case 1:
				cout<<"enter num";
				cin>>x;
				s1.push (x);
			break;
		case 2:
			if(top==NULL)
				cout<<"empty";
			else {
				x=s1.pop();
				cout<<"num delder="<<x;
				s2.push(x);
				cout<<"num enter="<<x;
			}
			break;
		case 3:
	if(top==NULL)
				cout<<"empty";
	else {
		cout<<"the list is ";
		s1.traverse();}
		break;
	 
		case 4:
			exit(0);
		default:
			cout<< " invalid choice ";
		} 
	}
 
	void traverse() {
stack *i;
do{
	cout<<i->info;
	i=i->next;
}
while(i!=NULL);
	}


	
	void push(int x){
	 
			stack *p=new stack;
			p->info=x;
			p-<next=NULL;
			if(top==NULL)
				top=p;
			else 
			{
				p->next=top;
				top=p;}
	}
	int pop()
	{
		stack *t;
		x=top->info;
		x=top;
		top=top->next;
		delete t;
		return x;
	}
};


	void main()
	{ 
		stacks m;
	while (1)
	m.menu();
}

Edited 5 Years Ago by programing: n/a

Your code is not easy to look at and your description is rather vague. To solve your problem all you have to do is use one stack for original input and use the other stack for reversal input like so :

#include <iostream>
#include <stack>
#include <algorithm>

using namespace std;

void printStackReversed(const std::stack<int>& data);

int main(){
 std::stack<int> inputStack
 int tmp = 0;
 //get user input and save it in our input stakc
 while(cin >> tmp){ inputStack.push(tmp); }
 printStackReversed(inputStack);
}

void printStackReversed(const std::stack<int>& data){
 std::stack<int> cpy(data);
 std::stack<int> rev; //will hold our reversed stack
 //while our copied stack is not empty
 while( !cpy.empty()){
  //add it to our reversed stack, placing the top element in the bottom
  rev.push( cpy.top()); 
  //remove it so we can do the same for the next element if any
  cpy.pop(); 
 }
 while(!rev.empty()){
  //print out the reversed stack
  cout << rev.top() << " ";
  rev.pop();
 }
}

I haven't tested the code but it should give you an idea, which is to utilize the LIFO property of stack

Edited 5 Years Ago by firstPerson: n/a

Since you're presumably supposed to be writing your own stack implementation (using a singly-linked list) as part of your assignment, I'll start with your first few lines:

struct stack{
    stack s1,s2;
    int info;
};

First of all, you can't declare s1 and s2 to be of type stack because you have not yet declared a type called stack . You would need to specify struct stack which you have declared.

Second, if you could recursively include a struct stack object within itself (which I doubt), you wouldn't want to. I think what you want here is a pointer: struct stack *s1, *s2; Third, since you're supposed to implement your stack using a singly-linked list, a node should have only one pointer in it, not two.

Fourth, simplify your understanding by separating the notion of a node in your linked-list from the notion of a stack (which wraps your linked-list with a specific set of allowable operations).

struct linkedListNode {
    int info;
    struct linkedListNode *next;
};

typedef struct linkedListNode LinkedListNode;  // notice the leading upper-case "L" for the type!

class Stack
{
private:
    LinkedListNode *top;
public:
    Stack();
    void push(int value);
    void pop();
    int top();
};

Stack::Stack() :
    top(NULL)
{}

void Stack::push(int value)
{
    LinkedListNode *newNode = new LinkedListNode();
    // TODO: check for allocation error here!
    newNode->info = value;
    newNode->next = top;
    top = newNode;
}

...

And that's just the from the first four lines! Try harder to think through what task(s) you need the program to perform, and then read through your own code to see where it isn't performing that task(s), and why.

Edited 5 Years Ago by raptr_dflo: formatting

This article has been dead for over six months. Start a new discussion instead.