Help !

Hello
how do you write a program using a link list for a particular stack that will store data of the type char. Calling a member function named pushing to push an item onto the stack and a member function named popping to pop the item off the stack.

a struct named StackFrame and a pointer named link as well as a typedef statement, a class named Stack , copy constructor, destructor and a bool; empty() method.

You need to prompt for a word from the keyboard and then show the word written backwards, you also need to give the user the chance to try again.


show us what you have so far. We won't do your homework for you, but we are glad to offer help when you are stuck.
// MY H FILE

#include <iostream>
#include <iomanip>

using namespace std;

//linked list structure
struct Stackframe
{
char data;
Stackframe* next;

};

//typedef definition
typedef Stackframe * nodetype;

//class definition
class stack
{
public:
stack();
~stack();
void Push(int);
void Pop(int &);
void display();
bool is_empty();

private:
nodetype top;

};// END OF H FILE


**********************************************************

// IMPLEMENTATION.H

#include <iostream>
#include <iomanip>

//header file
#include "stack.h"

using namespace std;

//implementation section
stack::stack()
{
top = NULL ;//ECH TIME A POINTER IS CREATED IT POINTS TO NOTHING

}//end of constructor

bool stack:: is_empty()
{
if(top==NULL)
return true;
else
return false;
}//end of empty check verification

void stack::Push(int n)
{
nodetype newNode = new Stackframe; //creating a new node
newNode->data = n; //assigning to the new node
if(is_empty())
{
newNode->next = NULL;
top = newNode;
}//end if

else
{
newNode->next = top;
top = newNode;
}//end else

}//end of push function

void stack::Pop(int & n)
{
if(is_empty())
{
cout<<"The stack cannot be deleted because it is empty"<<endl;
}//end if

else
{
nodetype temp;
temp = top;
top = top -> next;
n = temp->data;
temp ->next = NULL;
delete temp;
}//end else

}//end of pop function

void stack::display()
{
nodetype temp = top;
if(is_empty())
{
cout<<"nothing to display"<<endl;
}//end if

else
{
while(temp != NULL)
{
cout<<temp->data<<endl;
temp = temp->next; //move for next and display so on
}

}//end else

}//end of display funtion

stack::~stack()
{
if(is_empty())
{
cout<<"nothing to destroy";
}//end if

else
{
while(top->next != NULL)
{

nodetype temp;
temp = top;
top = top -> next;

temp ->next = NULL;
delete temp;
}
}//end else

}//end of destructor
END OF IMPLEMENTATION.H

***********************************************************
// INT MAIN FILE . CPP

#include <iostream>
#include <iomanip>

//header file
#include "implementation.h"

using namespace std;
//function prototype


int main()
{
stack mystack;
char word;
cout<<"Please enter a word : ";
cin>>word;
mystack.Push(word);

mystack.display();
return 0;
}//end of main function


this is what i have so far but having trouble with my int main()
Last edited on
first of all, when you post code, please enclose it in code tags. Click on the "<>" button in the Format options and then past your code between the opening and closing tags.

Second, on first look, your class looks pretty good. You have one problem, though, with data types. Your stack pushes and pops ints, but your Stackframe structure stores a char.

You don't really explain what your problem is, besides you are having problems with main(). You really need to be more explicit with what problems you are having (can't compile, won't run at all, unexpected output, core dump, etc.). But I think I see your problem. You define word to be of type char, so you store a single character in the stack ad then display it, not the whole word you are expecting. Is that what you are seeing?

What trouble are you having with main()?
Topic archived. No new replies allowed.