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