The document provides code for implementing a stack using a linked list. It includes code for a LinkedList class with functions to manipulate a linked list like insert, remove, print etc. It also provides code snippets for a Stack class that inherits from LinkedList. The Stack class implements functions like push, pop and top to add/remove elements from the front of the linked list, mimicking a stack's LIFO behavior. The document asks to implement the Stack and LinkedList code to create a working stack using inheritance and linked list functions.
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
1. 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()
{
2. 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;
3. }
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;
4. }
}
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;
5. 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 << " ";
6. 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 */
7. 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;
} CSE30_lab9.pdf - Foxit PhantomPDF Express PDF FILE HOME EDIT COMMENT VIEW
FORM PROTECT SHARE HELP Find T .| G,, Snapshot iD Clipboard a75.00% Q Zoom In
aZoom Out Edit. protect Fit Page D Fit Width Fit Visible 1 Actual an Hand Select Select
Typewriter Note PDF Sign From From From From Files Scanner Blank Page Clipboard
Business Try Business Edition Text Annotation Rotate View Size Tools Zoom Create Upgrade
to Business Start CSE30 lab9.pdf Pages In this part of the lab, you will be implementing your
own Stack class, comprised of a class declaration (Stack.h) and class definition (Stack.cpp). You
will also create a simple main program to test your stack class (Exercise1.cpp). This part of the
Lab will use int as the type for elements in the Stack (Linked List nodes). In order to make the
Stack implementation as easy as possible, your stack class will inherit the LinkedList class so
that any functions available inside the LinkedList class can be used by the Stack class Take a
look at the Inheritanceexample files from lecture to see how to use inheritance. As you will see
when you implement the Stack class, inheriting the LinkedList class will make this part of the
lab very simple. Your Stack class will be comprised of the following functions, which you need
8. to implement (note that the Stack class does not need any variables, since those from the
inherited LinkedList are all you need): .Default Constructor (Stack)): does nothing
Destructor(Stack)): does nothing .void push (int value): inserts a new element (value) at the front
of the stack, by calling the appropriate LinkedList function int pop): removes the first element of
the Stack, by calling the appropriate LinkedList function. It also returns the value of the element
that has been popped int& top): returns a reference to the top element of the Stack. You also
need to create your own main program (Exercise1.cpp) to test the various Stack functions: Create
a stack of at least 10 elements in your main program and call various member functions to
manipulate the stack (as you have seen in your HW2 problem) .Call push to insert integer to the
stack Call top to return to the top of the stack Check the size of the stack by calling a function
inherited from Linked List. . .Check if the stack is empty by calling a function inherited from
Linked List Print the content of the stack by calling a function inherited from Linked List and
verify if 2 /4 75.00% 11:23 AMM 11/2/2016
Solution
Stack.cpp
#include "Stack.h"
#include
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
9. #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
#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();
10. 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;
11. 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() {
12. 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;
}
13. }
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