Ceate a LinkedLists in C then write new methods as follows
Ceate a LinkedLists in C++ , then write new methods as follows:
Using class
add ( LinkedList::Link* l, int n ): will insert in the linked list, after link l, a chain of n new links. Each link will store an integer that will be set to receive, in order, a value starting from 0 until n-1. In the last link of the list always set the next pointer to be NULL in order to mark the end of the list.
print (): this method moves through the entire list printing out each integer value stored in the links.
cleanup (): updated cleanup function that will automatically delete all links in the linked list, including the data stored on each link
Your linked list struct should be named LinkedList and should be saved in a header file named LinkedList.h. The file linkedLists.cpp will be used to evaluate struct.
Sample output:
47.1
0
1
0
1
2
3
4
Solution
We can create two class
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Below is the content of LinkedList.h file
#include<cstddef>
/*
* LinkedList.h
*
* Created on: 15-Feb-2017
* Author: yourname
*/
#ifndef SRC_LINKEDLIST_H_
#define SRC_LINKEDLIST_H_
class Link {
public:
double item;
Link *next;
Link(double it) ;
Link(double it, Link *next) ;
};
class LinkedList {
public:
Link *head;
LinkedList();
virtual ~LinkedList();
LinkedList(double *val, Link *l);
void add(Link* l, int n);
void print();
void cleanup();
};
#endif /* SRC_LINKEDLIST_H_ */
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Below is the content of LinkedList.cpp file
/*
* LinkedList.cpp
*
* Created on: 15-Feb-2017
* Author: yourname
*/
#include<iostream>
#include \"LinkedList.h\"
LinkedList::~LinkedList() {
while (head) {
Link *temp = head;
head = head->next;
delete (temp);
}
head = NULL;
}
LinkedList::LinkedList() {
head = NULL;
}
LinkedList::LinkedList(double *val, Link *l) {
head = new Link(*val, l);
}
void LinkedList::add(Link *l, int n) {
for (int i = 0; i < n; ++i) {
l->next = new Link(i);
l = l->next;
}
}
void LinkedList::print() {
for (Link *it = head; it != NULL; it = it->next) {
std::cout << it->item << std::endl;
}
}
void LinkedList::cleanup() {
while (head) {
Link *temp = head;
head = head->next;
delete (temp);
}
head = NULL;
}
Link::Link(double it) {
item = it;
next = NULL;
}
Link::Link(double it, Link *next) {
item = it;
this->next = next;
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Below is the content of test.cpp file
/*
* test.cpp
*
* Created on: 15-Feb-2017
* Author: yourname
*/
#include \"LinkedList.h\"
using namespace std;
int main(int argc, const char * argv[]) {
LinkedList * linkedList = new LinkedList(new double(47.1), NULL);
linkedList->add(linkedList->head, 5);
linkedList->add(linkedList->head->next->next, 5);
linkedList->print();
linkedList->cleanup();
return 0;
}



