STACK OVERLOAD OPERATORS - +=, ==, <<, >> So, I\'m required to do the following.
ID: 645379 • Letter: S
Question
STACK OVERLOAD OPERATORS - +=, ==, <<, >>
So, I'm required to do the following... :
Extend the class by overloading the following operators using
friend functions:
Overload == to see if two stacks are the same (bool operator == (Stack leftStack, Stack rightStack))
Overload the += to create one tall stack out of two stacks - putting one on top of the other.
Overload the << to display the contents of a stack
Overload the >> to create a stack from a line of text (hint: each word is stored in one node of the stack)
I've been trying to do some reasearch on this, but I haven't been very successful. I tried to do the << operator, but I'm not sure if that one is correct... Please help!
Also, I'd really appreciate it if you could explain/comment the code; I just really want to understand this :( Thanks in advance!
--------------------------------------------------------------------------------------
#include
#include
using namespace std;
//Create a node struct
struct Node {
int data;
Node *next;
};
class Stack {
public:
Stack(); // constructor
~Stack(); // destructor
void push(int d); //pushes node onto top of stack
int pop(); //pops top node off the stack
bool isEmpty(); // check if stack is empty
string toString(); //return stack contents as a string
friend bool operator == (Stack &leftStack, Stack &rightStack); // are they the same?
friend Stack& operator += (Stack &leftStack, Stack &rightStack); // put one stack on the other
friend ostream& operator << (ostream &out, Stack &rightStack); // print in order
friend istream& operator >> (istream &in, Stack &rightStack); // string into stack
private:
Node *top; //points to top node in stack
};
Stack::Stack() {
//initialize stack to be empty
top = NULL;
}
Stack::~Stack() {
//destructor to delete all of the dynamic variables
while (!isEmpty()) {
pop();
}
}
void Stack::push(int d) {
Node *temp = new Node;
temp->data = d;
temp->next = top;
top = temp;
}
int Stack::pop() {
if (!isEmpty()) {
int temp = top->data;
Node *oldtop;
oldtop = top;
top = top->next;
delete oldtop;
return temp;
} else {
//can't pop from empty stack!
cout << "you can't pop from an empty stack!" << endl;
exit(1);
}
}
bool Stack::isEmpty(){
return (top==NULL);
}
//return contents of stack (top to bottom) as a string
string Stack::toString() {
string s = "top -> ";
if(isEmpty()) {
s = s + ".";
return s;
} else {
Node *current;
current = top;
while (current != NULL) {
s = s + toString(current->data) + " -> ";
current = current->next;
}
s = s + ".";
return s;
}
}
friend bool operator == (Stack &leftStack, Stack &rightStack); // are they the same?
friend Stack& operator += (Stack &leftStack, Stack &rightStack); // put one stack on the other
friend ostream& operator << (ostream &out, Stack &rightStack) {
for (int i = rightStack.top; i >= 0; i--)
out << rightStack.items[i] << " ";
return out;
}
friend istream& operator >> (istream &in, Stack &rightStack);
int main() {
//Use the main to test stack
Stack *s = new Stack();
cout << s->toString() << endl;
s->push(4);
s->push(6);
s->push(16);
cout << s->toString() << endl;
cout << "pop: " << s->pop() << endl;
cout << s->toString() << endl;
/* cout << "pop: " << s->pop() << endl;
cout << "pop: " << s->pop() << endl;
cout << "pop: " << s->pop() << endl;
*/delete s;
return 0;
}
Explanation / Answer
Hope this may helps you...
#include<stdio.h>
#include<iostream>
#include<conio.h>
#include<process.h>
using namespace std;
int s[100];
class stackk
{
public:
int top;
stackk():top(-1)
{}
void operator +(int d)
{
top++;
s[top]=d;
cout<<" data added is"<<s[top];
}
void operator --()
{
top--;
}
void operator ++()
{
int a=top;
while(a>=0)
{
cout<<s[a];
a--;
}
}
};
int main()
{
system("cls");
stackk s1;
int data,ch,f=1;
while(f==1)
{
cout<<" 1.push 2.pop 3.display";
cout<<" enter your choice";
cin>>ch;
switch(ch)
{
case 1:
cout<<" enter the data to push";
cin>>data;
s1+data;
break;
case 2:
--s1;
break;
case 3:
++s1;
break;
case 4:return 0;
break;
}
cout<<" press 1 to conitnue";
cin>>f;
}
return 1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.