Help please I have attached LinkedListcpp and LinkedListh Pl
Help please, I have attached LinkedList.cpp and LinkedList.h
Please help with the implementation of the Stack.h and Stack.cpp as well
LinkedList.cpp
~~~~~~~~~~~~~~
#include \"LinkedList.h\"
LinkedList::LinkedList()
{
first = last = NULL;
}
LinkedList::~LinkedList()
{
while (first != NULL)
{
Node *temp;
temp = first;
first = first->next;
free(temp);
}
last = NULL;
}
void LinkedList::insertAtBack(int valueToInsert)
{
Node *temp = (Node *)malloc(sizeof(Node));
temp->val = valueToInsert;
temp->next = NULL;
if (last == NULL)
first = last = temp;
else
{
last->next = temp;
last = temp;
}
}
bool LinkedList::removeFromBack()
{
if (first == NULL)
return false;
else if (first == last)
{
free(first);
first = last = NULL;
}
else
{
Node *temp;
temp = first;
while (temp->next != last)
temp = temp->next;
last = temp;
temp = temp->next;
free(temp);
last->next = NULL;
}
return true;
}
void LinkedList::print()
{
Node *temp = first;
while (temp != last)
{
cout << temp->val << \" \";
temp = temp->next;
}
if (temp != NULL)
cout << temp->val;
}
bool LinkedList::isEmpty()
{
if (first == last)
return true;
return false;
}
int LinkedList::size()
{
int count = 0;
Node *temp = first;
if (temp == NULL)
return count;
while (temp != last)
{
count++;
temp = temp->next;
}
return count + 1;
}
void LinkedList::clear()
{
while (first != NULL)
{
Node *temp;
temp = first;
first = first->next;
free(temp);
}
last = NULL;
}
/*Exercise2 */
void LinkedList::insertAtFront(int valueToInsert)
{
Node *temp = (Node *)malloc(sizeof(Node));
temp->val = valueToInsert;
if (last == NULL)
first = last = temp;
else
{
temp->next = first;
first = temp;
}
}
bool LinkedList::removeFromFront()
{
if (first == NULL)
return false;
Node *temp = first;
first = first->next;
free(temp);
if (first == NULL)
last = NULL;
return true;
}
LinkedList.h
~~~~~~~~~~~
#include \"LinkedList.h\"
LinkedList::LinkedList()
{
first = last = NULL;
}
LinkedList::~LinkedList()
{
while (first != NULL)
{
Node *temp;
temp = first;
first = first->next;
free(temp);
}
last = NULL;
}
void LinkedList::insertAtBack(int valueToInsert)
{
Node *temp = (Node *)malloc(sizeof(Node));
temp->val = valueToInsert;
temp->next = NULL;
if (last == NULL)
first = last = temp;
else
{
last->next = temp;
last = temp;
}
}
bool LinkedList::removeFromBack()
{
if (first == NULL)
return false;
else if (first == last)
{
free(first);
first = last = NULL;
}
else
{
Node *temp;
temp = first;
while (temp->next != last)
temp = temp->next;
last = temp;
temp = temp->next;
free(temp);
last->next = NULL;
}
return true;
}
void LinkedList::print()
{
Node *temp = first;
while (temp != last)
{
cout << temp->val << \" \";
temp = temp->next;
}
if (temp != NULL)
cout << temp->val;
}
bool LinkedList::isEmpty()
{
if (first == last)
return true;
return false;
}
int LinkedList::size()
{
int count = 0;
Node *temp = first;
if (temp == NULL)
return count;
while (temp != last)
{
count++;
temp = temp->next;
}
return count + 1;
}
void LinkedList::clear()
{
while (first != NULL)
{
Node *temp;
temp = first;
first = first->next;
free(temp);
}
last = NULL;
}
/*Exercise2 */
void LinkedList::insertAtFront(int valueToInsert)
{
Node *temp = (Node *)malloc(sizeof(Node));
temp->val = valueToInsert;
if (last == NULL)
first = last = temp;
else
{
temp->next = first;
first = temp;
}
}
bool LinkedList::removeFromFront()
{
if (first == NULL)
return false;
Node *temp = first;
first = first->next;
free(temp);
if (first == NULL)
last = NULL;
return true;
}
Solution
Stack.cpp
#include \"Stack.h\"
#include <iostream>
using namespace std;
Stack::Stack(){} //Constructor
Stack::~Stack(){} //Destructor
void Stack::push(int value){
insertAtFront(value);
}
int Stack::pop(){
int x = first->val; //save first value
removeFromFront(); //remove front node
return x; //return the first value in the stack that was deleted
}
int& Stack::top(){
return first->val; //return top of stack
}
Stack.h
#ifndef STACK_H
#define STACK_H
#include \"LinkedList.h\"
using namespace std;
class Stack : public LinkedList // Inherit from LinkedList
{
public:
Stack(); //Constructor
~Stack(); //Destructor
void push(int value);
int pop();
int& top();
};
#endif
LinkedList.cpp
#include <iostream>
#include \"LinkedList.h\"
//Constructor
LinkedList::LinkedList() {
first = NULL;
last = NULL;
}
//Destructor
LinkedList::~LinkedList() { //SAME AS CLEAR function
Node* tmp = first;
Node* nextNode = first;
while(tmp != last) {
tmp = nextNode;
nextNode = tmp->next;
delete tmp;
}
delete last;
}
void LinkedList::insertAtBack(int v) {
Node* newNode = new Node();
newNode->val = v;
newNode->next = NULL;
if (first == NULL) //check if empty
first = newNode;
if (last == NULL)
last = newNode;
else {
last->next = newNode;
last = newNode; //Make the new node the last node
}
}
bool LinkedList::removeFromBack() {
if (first == NULL) //check if empty
return false;
if (first == last) { //check if only one node
delete first;
first = NULL;
last = NULL;
return true;
}
if (!isEmpty()) { //find the node right before the last
Node* newLast = first;
while(newLast->next != last)
newLast = newLast->next; //make it last
delete last; //delete last
last = newLast;
return true;
}
}
void LinkedList::print() {
int s = 0;
Node* tmp = first;
Node* newLast = last;
cout << last->val; //Prints the last value of the list
cout << \",\";
while(tmp!=newLast){
if(tmp->next == newLast) { //prints every value before last to first
cout << tmp->val;
cout << \",\";
newLast = tmp;
tmp = first;
}
tmp = tmp->next;
}
cout << first->val; //print first
}
bool LinkedList::isEmpty() {
if (first == NULL)
return true;
else
return false;
}
int LinkedList::size() {
int size = 0;
if (isEmpty())
return 0; //check if empty
Node* tmp = first;
Node* nextNode = first;
while(nextNode != last) {
tmp = nextNode;
nextNode = tmp->next; //go to next node
size++; //Increment size every time you move thru list
}
size++;
return size;
}
void LinkedList::clear() {
Node* tmp = first; //create temporary nodes
Node* nextNode = first;
while(tmp != last) {
nextNode = tmp->next; //move through the list
delete tmp; // delete each node
tmp = nextNode;
}
delete last; //delete the last node
first = NULL; // set the first AND last pointers to null once list is empty
last = NULL;
}
void LinkedList::insertAtFront(int value) {
Node* newNode = new Node(); //create new node
newNode->val = value; //set value
newNode->next = first; //set next pointer
first = newNode; //make it first
if (last == NULL) //check if this is the first node
last = newNode;
}
bool LinkedList::removeFromFront() {
if (first == NULL)
return false; //check if empty
if (first == last) { //check for one node
delete first;
first = NULL;
last = NULL;
return true;
}
if (!isEmpty()) { //create temporary node
Node* tmp = first;
first = tmp->next; //move the first
delete tmp; //delete the temp
return true;
}
}
LinkedList.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
using namespace std;
// Representation of an element in the linked list
struct Node
{
int val; // Value of the node
Node *next; // Pointer to the next node
};
class LinkedList
{
// Public Functions/Variables
public:
/* IMPLEMENT THESE FUNCTIONS FOR EXERCISE1 */
LinkedList(); // Constructor
~LinkedList(); // Destructor
void insertAtBack(int valueToInsert);
bool removeFromBack();
void print();
bool isEmpty();
int size();
void clear();
/* IMPLEMENT THSES FUNCTIONS FOR EXERCISE2 */
void insertAtFront(int valueToInsert);
bool removeFromFront();
// Private Functions/Variables
protected:
Node *first; // Pointer pointing to the begining of the list
Node *last; // Pointer pointing to the end of the list
};
#endif







