my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main:
#include<iostream>
#include<cassert>
using namespace std;
struct nodeType
{
int info;
nodeType *link;
};
class linkedList
{
public:
void initializeList();
bool isEmptyList();
void print();
int length();
void destroyList();
void insertFirst(int newItem);
void insertLast(int newItem);
int front();
linkedList();
void copyList(const linkedList otherList);
void insertNewValue(int value);
void deleteItem(int deleteItem);
void Backward(int num);
private:
int count;
nodeType *first;
nodeType *last;
};
int linkedList::front()
{
assert(first != nullptr);
return last->info;
}
bool linkedList::isEmptyList()
{
return (first == nullptr);
}
void linkedList::insertFirst(int newItem)
{
nodeType *newNode;
newNode = new nodeType;
newNode->info = newItem;
newNode->link = first;
first = newNode;
count++;
if (last == nullptr)
last = newNode;
}
void linkedList::insertLast(int newItem)
{
nodeType *newNode;
newNode = new nodeType;
newNode->info = newItem;
newNode->link = nullptr;
if (first == nullptr)
{
first = newNode;
last = newNode;
count++;
}
else
{
last->link = newNode;
last = newNode;
count++;
}
}
linkedList::linkedList()
{
first = nullptr;
last = nullptr;
count = 0;
}
void linkedList::destroyList()
{
nodeType *temp;
while (first != nullptr)
{
temp = first;
first = first->link;
delete temp;
}
last = nullptr;
count = 0;
}
void linkedList::initializeList()
{
destroyList();
}
void linkedList::print()
{
nodeType *current;
current = first;
while (current != nullptr)
{
cout << current->info
<< " ";
current = current->link;
}
}
int linkedList::length()
{
return count;
}
void linkedList::insertNewValue(int value)
{
nodeType *curr = first; // set curr to first node of
the list
bool found = false; // set found to false
// loop
over the list
while (curr != nullptr)
{
if (curr->info == value) //
value found, set found to true and exit the loop
{
found =
true;
break;
}
curr = curr->link;
}
if (!found) // value not found in list, insert
value at end
insertLast(value);
else // value found in list, hence not inserted
cout << "ERROR: " <<
value << " already inserted in the list. " << endl;
}
void linkedList::deleteItem(int deleteItem)
{
nodeType *p, *q;
q = nullptr;
p = first;
while (p != nullptr)
{
// p->info as we are accessing
data and not the pointer
if (p->info == deleteItem)
{
q = p;
p->link =
p->link->link;
delete q;
return;
}
p = p->link;
}
}
void linkedList::copyList(const linkedList otherList)
{
nodeType *newNode, *current, *fin;
if (first != nullptr)
initializeList();
if (otherList.first == nullptr)
first = nullptr;
else
{
current = otherList.first;
first = new nodeType;
first->info =
current->info;
first->link = nullptr;
last = first;
current = current->link;
while (current != nullptr)
{
//
cout<<"here\n";
nodeType *p =
new nodeType();
count++;
p->info =
current->info;
p->link =
nullptr;
last->link =
p;
last =
last->link;
current =
current->link;
}
}
return;
}
void linkedList::Backward(int num)
{
// Code changes
// Addind node to the tail
nodeType *p = new nodeType();
count++;
p->info = num;
p->link = nullptr;
if (first == nullptr)
{
first = last = p;
return;
}
last->link = p;
last = last->link;
}
int main()
{
linkedList List;
linkedList listForward;
int num = -1, x;
cout << "Enter numbers ending with -999"
<< endl;
cin >> num;
while (num != -999)
{
List.Backward(num);
listForward.insertFirst(num);
cin >> num;
}
cout << endl;
cout << "The list in backward" <<
endl;
List.print();
cout << endl;
cout << "The list in forward" <<
endl;
listForward.print();
cout << endl;
cout << "Length of the list: " << List.length() << endl;
cout << "Enter a number to add in the list "
<< endl;
cin >> x;
List.insertNewValue(x);
cout << "The list after adding the number "
<< endl;
List.print();
cout << endl;
cout << "The length of the list after adding a number is " << List.length() << endl;
int el;
cout << "Enter the value to be deleted in the
list: " << endl;
cin >> el;
List.deleteItem(el);
List.print();
cout << endl;
cout << "Enter new value to be added in the
list: " << endl;
int k;
cin >> k;
List.insertNewValue(k);
List.print();
cout << endl;
cout << endl;
cout << endl << "Creating a copy of the
linked list" << endl;
linkedList newList;
newList.copyList(List);
cout << "Printing the new linked list\n";
newList.print();
List.destroyList();
cout << endl;
cout << "After destroying the list, the length
is " << List.length() << endl;
system("pause");
return 0;
}
void linkedList::deleteItem(int deleteItem)
{
nodeType *p, *q;
q = nullptr;
p = first;
if (p==nullptr)
{
cout<<"linked list not initialized";
return ;
}
while (p != nullptr)
{
// p->info as we are accessing
data and not the pointer
if (p->info == deleteItem)
{
q = p;
p->link =
p->link->link;
delete q;
return;
}
p = p->link;
}
Get Answers For Free
Most questions answered within 1 hours.