Thanks! Here goes . . .
Code:
//node.h
#ifndef _NODE_H_
#define _NODE_H_
#include "list.h"
#include <string>
using std::string;
class Node {
friend class List;
private:
string data;
Node *next;
public:
Node(string item, Node *ptr) {data = item; next = ptr;}
};
#endif
---------
//stack.h
#include "list.h"
class Stack :private List {
public:
string pop() {
string retval = showHead();
removeHead();
return retval;
}
void push(string item) {insertAtHead(item);}
void print() {List::print();}
};
-------
//list.h
#ifndef _LIST_H_
#define _LIST_H_
#include "node.h"
#include <string>
using std::string;
class List {
private:
Node *head;
Node *tail;
public:
List() {head=NULL; tail=NULL;}
void insertAtHead(string item);
void insertAtTail(string item);
string showHead();
void removeHead();
void print();
};
#endif
---------
//list.cpp
#include "list.h"
#include <iostream>
using std::cout;
using std::endl;
void List::print() {
Node* ptr = head;
while (ptr != NULL) {
cout << ptr->data << ", ";
ptr = ptr->next;
}
cout << endl;
}
void List::insertAtHead(string item) {
// insert item at the front of the list pointed to by head
// assumes that this is a valid list
head = new Node(item, head);
if (tail == NULL) {
tail = head;
}
}
void List::insertAtTail(string item) {
// insert item at the end of the list pointed to by head
// assumes that this is a valid list
if (head == NULL) { // if the list is empty,
insertAtHead(item); // the beginning is the same as the end
}
else {
tail->next = new Node(item, NULL);
tail = tail->next;
}
}
void List::removeHead() {
// decapitate the list pointed to by head
// assumes that this is a valid list
if (head != NULL) { // something to delete
Node *deleteMe = head;
head = head -> next;
delete (deleteMe);
if (head == NULL) { // the list is now empty
tail = NULL;
}
}
}
string List::showHead() {
// show the first value of the list pointed to by head
// assumes that this is a valid list
if (head != NULL) {
return head->data;
}
else {
return "ERROR";
}
}
-----------
//main.cpp
#include "list.h"
#include "stack.h"
int main() {
List l1;
l1.insertAtHead("Hello");
l1.insertAtHead("Welcome");
l1.insertAtHead("Greetings");
l1.print();
l1.removeHead();
l1.print();
Stack s;
s.push("Hello");
s.push("Welcome");
s.push("Greetings");
s.print();
s.pop();
s.print();
return 0;
}