SlideShare a Scribd company logo
1 of 13
Download to read offline
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;
} 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
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
#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();
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

More Related Content

Similar to Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf

Stack queue
Stack queueStack queue
Stack queueJames Wong
ย 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfforladies
ย 
In C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdfIn C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdfflashfashioncasualwe
ย 
#includeiostream#includecstdio#includecstdlibusing names.pdf
#includeiostream#includecstdio#includecstdlibusing names.pdf#includeiostream#includecstdio#includecstdlibusing names.pdf
#includeiostream#includecstdio#includecstdlibusing names.pdfKUNALHARCHANDANI1
ย 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfmalavshah9013
ย 
Table.java Huffman code frequency tableimport java.io.;im.docx
 Table.java Huffman code frequency tableimport java.io.;im.docx Table.java Huffman code frequency tableimport java.io.;im.docx
Table.java Huffman code frequency tableimport java.io.;im.docxMARRY7
ย 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdffreddysarabia1
ย 
Implement the unsorted single linked list as we did in the class and .pdf
Implement the unsorted single linked list as we did in the class and .pdfImplement the unsorted single linked list as we did in the class and .pdf
Implement the unsorted single linked list as we did in the class and .pdfarihantstoneart
ย 
DS UNIT4_OTHER LIST STRUCTURES.docx
DS UNIT4_OTHER LIST STRUCTURES.docxDS UNIT4_OTHER LIST STRUCTURES.docx
DS UNIT4_OTHER LIST STRUCTURES.docxVeerannaKotagi1
ย 
1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdfafgt2012
ย 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdffeelinggift
ย 
Link list part 2
Link list part 2Link list part 2
Link list part 2Anaya Zafar
ย 
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdfI keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdfarkmuzikllc
ย 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfarrowmobile
ย 
How do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdfHow do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdffmac5
ย 
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdfHow do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdfmail931892
ย 
Stack and queue
Stack and queueStack and queue
Stack and queueKatang Isip
ย 
--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf
--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf
--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdfAdrianEBJKingr
ย 
Linked Stack program.docx
Linked Stack program.docxLinked Stack program.docx
Linked Stack program.docxkudikalakalabharathi
ย 
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdfHow do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdfmail931892
ย 

Similar to Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf (20)

Stack queue
Stack queueStack queue
Stack queue
ย 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
ย 
In C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdfIn C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdf
ย 
#includeiostream#includecstdio#includecstdlibusing names.pdf
#includeiostream#includecstdio#includecstdlibusing names.pdf#includeiostream#includecstdio#includecstdlibusing names.pdf
#includeiostream#includecstdio#includecstdlibusing names.pdf
ย 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdf
ย 
Table.java Huffman code frequency tableimport java.io.;im.docx
 Table.java Huffman code frequency tableimport java.io.;im.docx Table.java Huffman code frequency tableimport java.io.;im.docx
Table.java Huffman code frequency tableimport java.io.;im.docx
ย 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
ย 
Implement the unsorted single linked list as we did in the class and .pdf
Implement the unsorted single linked list as we did in the class and .pdfImplement the unsorted single linked list as we did in the class and .pdf
Implement the unsorted single linked list as we did in the class and .pdf
ย 
DS UNIT4_OTHER LIST STRUCTURES.docx
DS UNIT4_OTHER LIST STRUCTURES.docxDS UNIT4_OTHER LIST STRUCTURES.docx
DS UNIT4_OTHER LIST STRUCTURES.docx
ย 
1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf
ย 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
ย 
Link list part 2
Link list part 2Link list part 2
Link list part 2
ย 
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdfI keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
ย 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
ย 
How do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdfHow do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdf
ย 
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdfHow do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
ย 
Stack and queue
Stack and queueStack and queue
Stack and queue
ย 
--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf
--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf
--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf
ย 
Linked Stack program.docx
Linked Stack program.docxLinked Stack program.docx
Linked Stack program.docx
ย 
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdfHow do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
ย 

More from arorastores

Need this in JAVA We have N numbers as an array, you need to find a .pdf
Need this in JAVA We have N numbers as an array, you need to find a .pdfNeed this in JAVA We have N numbers as an array, you need to find a .pdf
Need this in JAVA We have N numbers as an array, you need to find a .pdfarorastores
ย 
Modify the following source code so that when the mouse is clicked w.pdf
Modify the following source code so that when the mouse is clicked w.pdfModify the following source code so that when the mouse is clicked w.pdf
Modify the following source code so that when the mouse is clicked w.pdfarorastores
ย 
looking for help with this question, have another question that.pdf
looking for help with this question, have another question that.pdflooking for help with this question, have another question that.pdf
looking for help with this question, have another question that.pdfarorastores
ย 
In a population of Canadian Eskimos the autosomal recessive gene for.pdf
In a population of Canadian Eskimos the autosomal recessive gene for.pdfIn a population of Canadian Eskimos the autosomal recessive gene for.pdf
In a population of Canadian Eskimos the autosomal recessive gene for.pdfarorastores
ย 
How is that terrorists are able to use media to their advantage an.pdf
How is that terrorists are able to use media to their advantage an.pdfHow is that terrorists are able to use media to their advantage an.pdf
How is that terrorists are able to use media to their advantage an.pdfarorastores
ย 
Identify the three ventral body cavities and the two dorsal body cavi.pdf
Identify the three ventral body cavities and the two dorsal body cavi.pdfIdentify the three ventral body cavities and the two dorsal body cavi.pdf
Identify the three ventral body cavities and the two dorsal body cavi.pdfarorastores
ย 
I have a Student.java class constructor like thisAnd I need to re.pdf
I have a Student.java class constructor like thisAnd I need to re.pdfI have a Student.java class constructor like thisAnd I need to re.pdf
I have a Student.java class constructor like thisAnd I need to re.pdfarorastores
ย 
How do incomplete dominance and codominance differ There is no diff.pdf
How do incomplete dominance and codominance differ  There is no diff.pdfHow do incomplete dominance and codominance differ  There is no diff.pdf
How do incomplete dominance and codominance differ There is no diff.pdfarorastores
ย 
Fruits develop from microsporangia receptacles fertilized eggs ovarie.pdf
Fruits develop from microsporangia receptacles fertilized eggs ovarie.pdfFruits develop from microsporangia receptacles fertilized eggs ovarie.pdf
Fruits develop from microsporangia receptacles fertilized eggs ovarie.pdfarorastores
ย 
Find all equilibrium solutions of the equation dydx = y^2 - 1. Use o.pdf
Find all equilibrium solutions of the equation dydx = y^2 - 1. Use o.pdfFind all equilibrium solutions of the equation dydx = y^2 - 1. Use o.pdf
Find all equilibrium solutions of the equation dydx = y^2 - 1. Use o.pdfarorastores
ย 
Determine which protist causes each of the following diseases in huma.pdf
Determine which protist causes each of the following diseases in huma.pdfDetermine which protist causes each of the following diseases in huma.pdf
Determine which protist causes each of the following diseases in huma.pdfarorastores
ย 
Describe the transmission of the meningococcus, and discuss the path.pdf
Describe the transmission of the meningococcus, and discuss the path.pdfDescribe the transmission of the meningococcus, and discuss the path.pdf
Describe the transmission of the meningococcus, and discuss the path.pdfarorastores
ย 
dass Defining Species art C Gene flow and the biological species conc.pdf
dass Defining Species art C Gene flow and the biological species conc.pdfdass Defining Species art C Gene flow and the biological species conc.pdf
dass Defining Species art C Gene flow and the biological species conc.pdfarorastores
ย 
Describe one way in which you could use butterfly rearing in the fut.pdf
Describe one way in which you could use butterfly rearing in the fut.pdfDescribe one way in which you could use butterfly rearing in the fut.pdf
Describe one way in which you could use butterfly rearing in the fut.pdfarorastores
ย 
Critical Thinking Course...PhilosophyIn Chapter 8, we learned the .pdf
Critical Thinking Course...PhilosophyIn Chapter 8, we learned the .pdfCritical Thinking Course...PhilosophyIn Chapter 8, we learned the .pdf
Critical Thinking Course...PhilosophyIn Chapter 8, we learned the .pdfarorastores
ย 
Can I get a detailed answer on the following question. Bacterial ind.pdf
Can I get a detailed answer on the following question. Bacterial ind.pdfCan I get a detailed answer on the following question. Bacterial ind.pdf
Can I get a detailed answer on the following question. Bacterial ind.pdfarorastores
ย 
Another algebraic way to express a constraint is E_1 = E_2, where bot.pdf
Another algebraic way to express a constraint is E_1 = E_2, where bot.pdfAnother algebraic way to express a constraint is E_1 = E_2, where bot.pdf
Another algebraic way to express a constraint is E_1 = E_2, where bot.pdfarorastores
ย 
An infinitely long sheet of charge of width L lies in the xy -plane .pdf
An infinitely long sheet of charge of width L lies in the xy -plane .pdfAn infinitely long sheet of charge of width L lies in the xy -plane .pdf
An infinitely long sheet of charge of width L lies in the xy -plane .pdfarorastores
ย 
A study tracked incidence rates of a blood vessel disorder over time.pdf
A study tracked incidence rates of a blood vessel disorder over time.pdfA study tracked incidence rates of a blood vessel disorder over time.pdf
A study tracked incidence rates of a blood vessel disorder over time.pdfarorastores
ย 
A distant galaxy was found to have a redshift of 1.8. How far away i.pdf
A distant galaxy was found to have a redshift of 1.8. How far away i.pdfA distant galaxy was found to have a redshift of 1.8. How far away i.pdf
A distant galaxy was found to have a redshift of 1.8. How far away i.pdfarorastores
ย 

More from arorastores (20)

Need this in JAVA We have N numbers as an array, you need to find a .pdf
Need this in JAVA We have N numbers as an array, you need to find a .pdfNeed this in JAVA We have N numbers as an array, you need to find a .pdf
Need this in JAVA We have N numbers as an array, you need to find a .pdf
ย 
Modify the following source code so that when the mouse is clicked w.pdf
Modify the following source code so that when the mouse is clicked w.pdfModify the following source code so that when the mouse is clicked w.pdf
Modify the following source code so that when the mouse is clicked w.pdf
ย 
looking for help with this question, have another question that.pdf
looking for help with this question, have another question that.pdflooking for help with this question, have another question that.pdf
looking for help with this question, have another question that.pdf
ย 
In a population of Canadian Eskimos the autosomal recessive gene for.pdf
In a population of Canadian Eskimos the autosomal recessive gene for.pdfIn a population of Canadian Eskimos the autosomal recessive gene for.pdf
In a population of Canadian Eskimos the autosomal recessive gene for.pdf
ย 
How is that terrorists are able to use media to their advantage an.pdf
How is that terrorists are able to use media to their advantage an.pdfHow is that terrorists are able to use media to their advantage an.pdf
How is that terrorists are able to use media to their advantage an.pdf
ย 
Identify the three ventral body cavities and the two dorsal body cavi.pdf
Identify the three ventral body cavities and the two dorsal body cavi.pdfIdentify the three ventral body cavities and the two dorsal body cavi.pdf
Identify the three ventral body cavities and the two dorsal body cavi.pdf
ย 
I have a Student.java class constructor like thisAnd I need to re.pdf
I have a Student.java class constructor like thisAnd I need to re.pdfI have a Student.java class constructor like thisAnd I need to re.pdf
I have a Student.java class constructor like thisAnd I need to re.pdf
ย 
How do incomplete dominance and codominance differ There is no diff.pdf
How do incomplete dominance and codominance differ  There is no diff.pdfHow do incomplete dominance and codominance differ  There is no diff.pdf
How do incomplete dominance and codominance differ There is no diff.pdf
ย 
Fruits develop from microsporangia receptacles fertilized eggs ovarie.pdf
Fruits develop from microsporangia receptacles fertilized eggs ovarie.pdfFruits develop from microsporangia receptacles fertilized eggs ovarie.pdf
Fruits develop from microsporangia receptacles fertilized eggs ovarie.pdf
ย 
Find all equilibrium solutions of the equation dydx = y^2 - 1. Use o.pdf
Find all equilibrium solutions of the equation dydx = y^2 - 1. Use o.pdfFind all equilibrium solutions of the equation dydx = y^2 - 1. Use o.pdf
Find all equilibrium solutions of the equation dydx = y^2 - 1. Use o.pdf
ย 
Determine which protist causes each of the following diseases in huma.pdf
Determine which protist causes each of the following diseases in huma.pdfDetermine which protist causes each of the following diseases in huma.pdf
Determine which protist causes each of the following diseases in huma.pdf
ย 
Describe the transmission of the meningococcus, and discuss the path.pdf
Describe the transmission of the meningococcus, and discuss the path.pdfDescribe the transmission of the meningococcus, and discuss the path.pdf
Describe the transmission of the meningococcus, and discuss the path.pdf
ย 
dass Defining Species art C Gene flow and the biological species conc.pdf
dass Defining Species art C Gene flow and the biological species conc.pdfdass Defining Species art C Gene flow and the biological species conc.pdf
dass Defining Species art C Gene flow and the biological species conc.pdf
ย 
Describe one way in which you could use butterfly rearing in the fut.pdf
Describe one way in which you could use butterfly rearing in the fut.pdfDescribe one way in which you could use butterfly rearing in the fut.pdf
Describe one way in which you could use butterfly rearing in the fut.pdf
ย 
Critical Thinking Course...PhilosophyIn Chapter 8, we learned the .pdf
Critical Thinking Course...PhilosophyIn Chapter 8, we learned the .pdfCritical Thinking Course...PhilosophyIn Chapter 8, we learned the .pdf
Critical Thinking Course...PhilosophyIn Chapter 8, we learned the .pdf
ย 
Can I get a detailed answer on the following question. Bacterial ind.pdf
Can I get a detailed answer on the following question. Bacterial ind.pdfCan I get a detailed answer on the following question. Bacterial ind.pdf
Can I get a detailed answer on the following question. Bacterial ind.pdf
ย 
Another algebraic way to express a constraint is E_1 = E_2, where bot.pdf
Another algebraic way to express a constraint is E_1 = E_2, where bot.pdfAnother algebraic way to express a constraint is E_1 = E_2, where bot.pdf
Another algebraic way to express a constraint is E_1 = E_2, where bot.pdf
ย 
An infinitely long sheet of charge of width L lies in the xy -plane .pdf
An infinitely long sheet of charge of width L lies in the xy -plane .pdfAn infinitely long sheet of charge of width L lies in the xy -plane .pdf
An infinitely long sheet of charge of width L lies in the xy -plane .pdf
ย 
A study tracked incidence rates of a blood vessel disorder over time.pdf
A study tracked incidence rates of a blood vessel disorder over time.pdfA study tracked incidence rates of a blood vessel disorder over time.pdf
A study tracked incidence rates of a blood vessel disorder over time.pdf
ย 
A distant galaxy was found to have a redshift of 1.8. How far away i.pdf
A distant galaxy was found to have a redshift of 1.8. How far away i.pdfA distant galaxy was found to have a redshift of 1.8. How far away i.pdf
A distant galaxy was found to have a redshift of 1.8. How far away i.pdf
ย 

Recently uploaded

How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
ย 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
ย 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
ย 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
ย 
Call Girls in Uttam Nagar (delhi) call me [๐Ÿ”9953056974๐Ÿ”] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [๐Ÿ”9953056974๐Ÿ”] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [๐Ÿ”9953056974๐Ÿ”] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [๐Ÿ”9953056974๐Ÿ”] escort service 24X79953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
ย 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
ย 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
ย 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
ย 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
ย 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
ย 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answersdalebeck957
ย 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
ย 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactisticshameyhk98
ย 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationNeilDeclaro1
ย 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
ย 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
ย 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
ย 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
ย 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
ย 

Recently uploaded (20)

How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
ย 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
ย 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
ย 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
ย 
Call Girls in Uttam Nagar (delhi) call me [๐Ÿ”9953056974๐Ÿ”] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [๐Ÿ”9953056974๐Ÿ”] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [๐Ÿ”9953056974๐Ÿ”] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [๐Ÿ”9953056974๐Ÿ”] escort service 24X7
ย 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
ย 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
ย 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
ย 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
ย 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
ย 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
ย 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
ย 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
ย 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
ย 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
ย 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
ย 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
ย 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
ย 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
ย 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
ย 

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