I will provide my LinkedList from my last lab. LinkedList.cpp ~~~~~~~~~~~~~~ #in
ID: 3937839 • Letter: I
Question
I will provide my LinkedList from my last lab.
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;
}
Explanation / Answer
class Exersize1
{
//calling functions of server class.
public:
node* insertAtBack(int);
void insert_begin();
void removeFromBack();
void insert_last();
void removeFrom_Position();
void size();
void clear();
void isEmpty();
void print();
single_llist()
{
start = NULL;
}
};