In: Computer Science
MAKE node class outside of stack class
class Stack{
private:
class node{
public:
node *next;
int data;
node(int d,node *n = NULL){
data = d;
next = n;
}
};
node *start;
public:
Stack();
Stack(const Stack&
original);
~Stack();
bool isEmpty() const ;
int top() const;
int pop() ;
void push(int);
};
Stack::Stack(){
start = NULL;
}
Stack::Stack(const Stack& original){
if (original.isEmpty()) {
start = NULL;
} else {
node* p = original.start; // points
to current node on other
node* tmp = new node(p->data);
// make a copy of the first node
start = tmp;
node* tail = tmp; // points to last
node of this list
while (p->next != NULL) {
p =
p->next;
tmp = new
node(p->data);
tail->next =
tmp;
tail =
tmp;
}
}
}
Stack::~Stack(){
node * curr = start;
node *next;
while(curr!=NULL){
next = curr->next;
delete curr;
curr = next;
}
}
bool Stack::isEmpty() const{
if(start==NULL)
return 1;
return 0;
}
int Stack::top() const{
if(isEmpty())
throw "Stack is
Empty";
return (start->data);
}
void Stack::push(int e){
node *p = new node(e);
if(isEmpty()){
start = p;
}else{
p->next = start;
start = p;
}
}
int Stack::pop() {
if(isEmpty())
throw "Stack is Empty";
else
{
node *p = start;
start = start->next;
int d = p->data;
delete p;
return d;
}
}
/* A TEST PROGRAM */
#include<iostream>
using namespace std;
class Stack{
private:
class node{
public:
node *next;
int data;
node(int d,node *n = NULL){
data = d;
next = n;
}
};
node *start;
public:
Stack();
Stack(const Stack&
original);
~Stack();
bool isEmpty() const ;
int top() const;
int pop() ;
void push(int);
};
Stack::Stack(){
start = NULL;
}
Stack::Stack(const Stack& original){
if (original.isEmpty()) {
start = NULL;
} else {
node* p = original.start; // points
to current node on other
node* tmp = new node(p->data);
// make a copy of the first node
start = tmp;
node* tail = tmp; // points to last
node of this list
while (p->next != NULL) {
p =
p->next;
tmp = new
node(p->data);
tail->next =
tmp;
tail =
tmp;
}
}
}
Stack::~Stack(){
node * curr = start;
node *next;
while(curr!=NULL){
next = curr->next;
delete curr;
curr = next;
}
}
bool Stack::isEmpty() const{
if(start==NULL)
return 1;
return 0;
}
int Stack::top() const{
if(isEmpty())
throw "Stack is
Empty";
return (start->data);
}
void Stack::push(int e){
node *p = new node(e);
if(isEmpty()){
start = p;
}else{
p->next = start;
start = p;
}
}
int Stack::pop() {
if(isEmpty())
throw "Stack is Empty";
else
{
node *p = start;
start = start->next;
int d = p->data;
delete p;
return d;
}
}
Code: Made node class outside of stack class
#include <cstddef>
class node {
public:
node *next;
int data;
node(int d, node *n = NULL) {
data = d;
next = n;
}
};
class Stack {
private:
node *start;
public:
Stack();
Stack(const Stack& original);
~Stack();
bool isEmpty() const;
int top() const;
int pop();
void push(int);
};
Stack::Stack() {
start = NULL;
}
Stack::Stack(const Stack& original) {
if (original.isEmpty()) {
start = NULL;
}
else {
node* p = original.start; // points
to current node on other
node* tmp = new node(p->data);
// make a copy of the first node
start = tmp;
node* tail = tmp; // points to last
node of this list
while (p->next != NULL) {
p =
p->next;
tmp = new
node(p->data);
tail->next =
tmp;
tail =
tmp;
}
}
}
Stack::~Stack() {
node * curr = start;
node *next;
while (curr != NULL) {
next = curr->next;
delete curr;
curr = next;
}
}
bool Stack::isEmpty() const {
if (start == NULL)
return 1;
return 0;
}
int Stack::top() const {
if (isEmpty())
throw "Stack is Empty";
return (start->data);
}
void Stack::push(int e) {
node *p = new node(e);
if (isEmpty()) {
start = p;
}
else {
p->next = start;
start = p;
}
}
int Stack::pop() {
if (isEmpty())
throw "Stack is Empty";
else
{
node *p = start;
start = start->next;
int d = p->data;
delete p;
return d;
}
}
/* A TEST PROGRAM */
#include<iostream>
using namespace std;
class node {
public:
node *next;
int data;
node(int d, node *n = NULL) {
data = d;
next = n;
}
};
class Stack {
private:
node *start;
public:
Stack();
Stack(const Stack& original);
~Stack();
bool isEmpty() const;
int top() const;
int pop();
void push(int);
};
Stack::Stack() {
start = NULL;
}
Stack::Stack(const Stack& original) {
if (original.isEmpty()) {
start = NULL;
}
else {
node* p = original.start; // points
to current node on other
node* tmp = new node(p->data);
// make a copy of the first node
start = tmp;
node* tail = tmp; // points to last
node of this list
while (p->next != NULL) {
p =
p->next;
tmp = new
node(p->data);
tail->next =
tmp;
tail =
tmp;
}
}
}
Stack::~Stack() {
node * curr = start;
node *next;
while (curr != NULL) {
next = curr->next;
delete curr;
curr = next;
}
}
bool Stack::isEmpty() const {
if (start == NULL)
return 1;
return 0;
}
int Stack::top() const {
if (isEmpty())
throw "Stack is Empty";
return (start->data);
}
void Stack::push(int e) {
node *p = new node(e);
if (isEmpty()) {
start = p;
}
else {
p->next = start;
start = p;
}
}
int Stack::pop() {
if (isEmpty())
throw "Stack is Empty";
else
{
node *p = start;
start = start->next;
int d = p->data;
delete p;
return d;
}
}