using c++ Download the files: LinkedList.h, LinkedList.cpp and LinkedListsMain.c
ID: 3717014 • Letter: U
Question
using c++
Download the files: LinkedList.h, LinkedList.cpp and LinkedListsMain.cpp. Create LinkedList class, which is similar to NumberList class discussed in lecture. Test your implementation using the driver program LinkedListsMain by creating an object of LinkedList class called myList a. b. Add the following functions to the basic LinkedList class you created in Problem 1.a: 1. reverseList): reverses the order of the list. 2. locate value): returns the position of the specific value in the linked list. The first node in the list is at position 0, the second node is at position 1, and so on. If the value is not found on the list, the search should return-1 insertAt(node, pos): inserts a new node at the specified position. A position of 0 means that the value will become the first item on the list, a position of 1 means that the value will become the second item on the list, and so on. A position equal to or greater than the length of the list means that the value is placed at the end of the list. removeFrom(node, pos): deletes a node at the specified position. A value of 0 for the position means that the first node in the list (the current head) is deleted. The function does nothing if the specified position is greater than or equal to the length of the list. 3. 4. Test your implementation through the driver program and object myList by calling the following functions:Explanation / Answer
Solution:
linkedlist.h
#ifndef LinkedList_H
#define LinkedList_H
struct node
{
double data;
struct node *next;
}*start;
class LinkedList
{
public:
Node* head;
// Constructor
LinkedList();
// Destructor
~LinkedList();
// Linked list operations
void appendNode(double);
void insertAt(double,int)
void removeFrom(int);
int locate(double);
void displayList();
void reverseList();
};
//----------------------------------------------------
LinkedList::LinkedList(){
this->head = NULL;
}
LinkedList::~LinkedList(){
cout<<"List destroyed";
}
//---------------------------------------------------------
void LinkedList::appendNode(double item){
struct node *s;
struct node* temp = new (struct node);
temp->data = item;
temp->next=NULL;
s = this->head;
while(s->next != NULL)
s = s->next;
s->next = temp;
}
//-----------------------------------------------------------
void LinkedList::insertAt(double item, int pos){
int counter = 0;
struct node *s, *ptr;
struct node* temp = new (struct node);
temp->data = item;
temp->next=NULL;
int i;
s = this->head;
while(s!= NULL)
{
s = s->next;
counter++;
}
if(pos == 1)
{
if(this->head == NULL)
{
this->head = temp;
this->head->next = NULL;
}
else
{
ptr = this->head;
this->head = temp;
this->head->next = ptr;
}
}
else if(pos>1 && pos<= counter)
{
s = this->head;
for (ii = 1; i<pos;i++)
{
ptr = s;
s= s->next;
}
ptr->next = temp;
temp->next = s;
}
else
{
cout<<"out of range";
}
}
//---------------------------------------------
void removeFrom(int pos)
{
int i,counter = 0;
if(this->head == NULL)
return;
struct node *s,*ptr;
s=this->head;
if(pos == 1)
this->head = s->next;
else
{
while(s!=NULL)
{ s = s->next;
counter++;
}
if(pos>0 && pos<=counter)
{
s= this->headl
for(i=1;i<pos;i++)
{
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else
cout<<"position out of range";
free(s);
}
}
//--------------------------------------------------------------
int LinkedList::locate(double value){
int pos = 0;
bool flag = false;
if (this->head == NULL)
{
cout<<"List is empty"<<endl;
return;
}
struct node *s;
s = this->head;
while (s != NULL)
{
pos++;
if (s->data == value)
{
flag = true;
return(pos);
}
s = s->next;
}
if (!flag)
return (-1);
}
//---------------------------------------------------------------
void LinkedList::reverse(){
struct node *ptr1, *ptr2, *ptr3;
if (this->head == NULL)
{
cout<<"List is empty"<<endl;
return;
}
if (this->head->next == NULL)
{
return;
}
ptr1 = start;
ptr2 = ptr1->next;
ptr3 = ptr2->next;
ptr1->next = NULL;
ptr2->next = ptr1;
while (ptr3 != NULL)
{
ptr1 = ptr2;
ptr2 = ptr3;
ptr3 = ptr3->next;
ptr2->next = ptr1;
}
this->head = ptr2;
}
//-----------------------------------------------------------------------
void LinkedList::displayList(){
struct node *temp;
if (this->head == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = this->head;
cout<<"Elements of list are: "<<endl;
while (temp != NULL)
{
cout<<temp->data<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}
// **********************************************************************
linkedlistmain.cpp
#include <iostream>
using namespace std;
int main()
{
appendNode(2);
appendNode(4);
appendNode(6);
insertAt(8, 0);
displayList();
reverseList();
displayList();
removeFrom(99);
insertAt(10, -1);
cout << “calling locate …” << locate(5) << endl;
removeFrom(1);
insertAt(7, 1);
displayList();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.