Question: I need help implementing this C++ header file for an ordered link list. I cannot get my insert method to work. It is really confusing
I need help implementing this C++ header file for an ordered link list. I cannot get my insert method to work. It is really confusing because I am required to return an integer. If you could please help me that would be great.
#ifndef ORDEREDLINKEDLIST_H
#define ORDEREDLINKEDLIST_H
#include
template
struct Node
{
Type info;
Node *next;
};
template
class OrderedLinkedList
{
public:
OrderedLinkedList();
OrderedLinkedList(const OrderedLinkedList& other);
~OrderedLinkedList();
OrderedLinkedList
void copyList(Node
void deleteList(Node
int insert(const Type&);
Type* find(int) const;
Type* get(int) const;
int remove(int);
void clear();
int size() const;
void print() const;
private:
Node
Node
unsigned int count;
};
template
OrderedLinkedList
{
head = nullptr;
tail = nullptr;
count = 0;
}
template
OrderedLinkedList
{
Node
head = nullptr;
tail = nullptr;
copyList(other.head, other.tail);
}
template
void OrderedLinkedList
Node
Node
while (cur) {
tmp = cur->next;
delete cur;
cur = tmp;
}
}
template
OrderedLinkedList
{
this->deleteList(head);
}
template
OrderedLinkedList
{
if (this != &other) {
Node
head = nullptr;
tail = nullptr;
copyList(other.head, other.tail);
deleteList(old);
}
return *this;
}
template
void OrderedLinkedList
Node
head = nullptr;
Node
Node
while (cur) {
insert(cur->info);
cur = cur->next;
}
}
template
int OrderedLinkedList
{
Node
Node
Node
bool found;
newNode = new Node
newNode->info = item;
newNode->next = nullptr;
if (head == nullptr)
{
head = newNode;
tail = newNode;
count++;
return count;
}
else
{
cur = head;
found = false;
while (cur != nullptr && !found) {
if (cur->info >= item)
found = true;
else {
trailcur = cur;
cur = cur->next;
}
}
if (cur == head) {
newNode->next = head;
head = newNode;
count++;
return count;
}
else {
trailcur->next = newNode;
newNode->next = cur;
if (cur == nullptr)
tail = newNode;
count++;
return count;
}
}
}
template
Type* OrderedLinkedList
{
return nullptr;
}
template
Type* OrderedLinkedList
{
return nullptr;
}
template
int OrderedLinkedList
{
return 0;
}
template
void OrderedLinkedList
{
}
template
int OrderedLinkedList
{
return -1;
}
template
void OrderedLinkedList
{
}
#endif
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
