I need help with location, remove and peek function. I\'m not able to think of a
ID: 3833843 • Letter: I
Question
I need help with location, remove and peek function.
I'm not able to think of any logic that I can implement.
Objective:
The purpose of this project is to expose you to:
Applying analysis, design and implementation of a linked list using dynamic data structure, discrete methods such as recursion are utilized in solution, objects classes are used in constructing a multi file project. RAM organization and the use of “this” pointer to accomplish tasks; in addition to using referenced parameters as constant, all methods are implemented in a procedure like programming to implement class objects and operations.
.
Problem Specification:
You are to submit the analysis, design and implementation of a bag that maintains an ordered linked list.
Details:
Each node’s data consists of: id number (5 digits), Name (a string), and hours & rate both are of the same type (both are: integers, floats or doubles). This data is provided in the attached file. Your program must read the data from either one of the files and place it in the linked list in descending order (that is highest to lowest) based on the id number.
Each Node of the list contains the data above, and a pointer to the next node. You are to define the Node class in a header file. Methods that will maintain the list are stored in a C++ implementation file.
The client C++ file will contain code that interacts with the user using a menu like interface to test operations on the sorted list. You may choose to define a child class that inherits the Node class or may use object composition. However, every class must have two files a header and an implementation.
Methods may include but are not limited to the following:
Add is a method that adds a Node at its appropriate position.
Location is a method that returns a pointer to where action may take place. (Recursive)
Remove is a method that removes a selected Node if it exists, returns true or false.
Clear is a method that clears the list of all the Nodes, but before a Node is deleted, it calls a print method that prints the Node data to a file. All nodes that are created using the new operator must be de-allocated, deleted.
Peek is an accessor method that displays, on the screen, the data in the Node given the students name, if the Node with name does not exist the method will display that. The list is unchanged.
Gross is a method that calculates the gross (gross is not a data member).
Net is a method that calculates the net after taking 20% taxes (net is not a data member).
Other functions may be used such as getData(), setNext() … as explained in the book and in previous projects.
Requirements:
Specify each method’s purpose, describe its parameters, and write a pseudo-code version of its header as explained in book.
A static data member that keeps track of the number of nodes in the list.
Create a UML class diagram for all classes.
Use class templates to accommodate employee’s payroll data that are integer, float and double.
Create a header file and implementation file for the bag.
Create a header file that contains the class declaration for the class Node. Each node contains data and a pointer to the next node. The last node’s next field should always be NULL.
Create an implementation file that contains the methods for each class.
Define a default constructor that initializes the head pointer to NULL.
Define a destructor, may be virtual, if deleting nodes is done in other methods.
Define a method to determine if the list isEmpty.
Use guards against duplication of #include.
Create as many ”.h” header files as needed and write your own “.cpp” file that is a driver containing a menu like choices to drive the project. Loading the list is not a menu option, it’s done automatically.
text file Data1.txt (is as below)
21169
Ahmed, Marco
40 10
24085
ATamimi, Trevone
30 15
281393
Choudhury, Jacob
45 12
27276
De la Cruz, Edward
30 17
19959
Edwards, Faraj
90 5
25610
Edwards, Bill
17 22
24679
Gallimore, Christian
28 14
27081
Lopez, Luis
45 5
114757
Mora, Sam
23 8
27079
Moses, Samuel
48 10
19328
Perez, Albert
57 2
25283
Rivas, Jonathan
44 12
25628
Robinson, Albert
33 3
27685
Miranda, Michael
60 12
28055
Robinson, Iris
55 9
Explanation / Answer
Hi there I do not know what you have coded before, so I am going to use a linked list generic class that I have. Do not worry I will post the full class implementation too. I am assuming
location == index of a certain object. == indexOf() method
remove == delete a node == remove() method
peek == get details of a certain node. == at() method
Please checkout the code below, it is a library I wrote my self, it not only supports any type of objects but has been tested several times. Have a great day
Cheers,
Rj
Llist.h
====
/*
* Llist.h
*
* Created on: 13-Apr-2017
* Author: Rj
*/
#ifndef LLIST_H_
#define LLIST_H_
#include <iostream>
#include "RjExceptions.cpp"
template <typename T>
struct Node {
Node<T> *next;
T val;
};
/**
* Creates a singly linked list with a **head** and a **last**. **head** will be
* used to store the address of the first node ever created and the latest will
* be stored in the last.
*/
template <typename T>
class Llist {
// This will store the first node's address.
Node<T> *head;
// This is going to hold the latest node's address.
Node<T> *last;
public:
// CONSTRUCTOR
Llist();
// Adds a val to list.
void add(T val);
// Add object at given index.
void add(int indx, T val);
// Add to start.
void add_front(T val);
// Iterate.
void iterate();
// Reverse iterate.
void iterate_reverse();
// Get the element at index.
T at(int indx);
// Gets the index of certain object.
int indexOf(T val);
// Add object at given index.
void update(int indx, T val);
// Delete
void remove(int indx);
void pop_front();
void pop_back();
};
#endif /* LLIST_H_ */
Llist.cpp
====
/*
* Llist.cpp
*
* Created on: 13-Apr-2017
* Author: Rj
*/
#include "Llist.h"
/**
* Constructor function
* This function Initializes head and last to NULL.
*/
template <typename T>
Llist<T>::Llist() : head{ NULL }, last{ NULL }
{};
/**
* Adds a val to the list.
* @param { T } val - val of the object to add.
*/
template <typename T>
void Llist<T>::add(T val) {
// If it is the first node
// Just normal initializing does not produce new objects every time the method is
// called.
Node<T> *nd1 = new Node<T>;
nd1->next = NULL;
if (head == NULL) {
head = nd1;
}
else {
last->next = nd1;
}
nd1->val = val;
last = nd1;
};
/**
* @Overload
* Add given object at a given index.
* @param { int } indx - Index to append at.
* @param { T } val - Object to append.
*/
template <typename T>
void Llist<T>::add(int indx, T val) {
Node<T> *temp1{ head };
Node<T> *temp2{ NULL };
int i{ 0 };
do {
if (i == indx && i != 0) {
Node<T> *nd1 = new Node<T>;
// Copy Previous one's.
nd1->next = temp1;
nd1->val = val;
temp2->next = nd1;
}
++i;
temp2 = temp1;
temp1 = temp1->next;
} while(temp1 != NULL);
}
/**
* Iterates over the list from start to end and prints to the screen.
*/
template <typename T>
void Llist<T>::iterate() {
if (head == NULL) {
list_un_inited err;
throw err;
}
// Iterate through the list until you hit a NULL.
Node<T> *temp{ head };
do {
std::cout << temp->val << std::endl;
temp = temp->next;
} while(temp != NULL);
};
/**
* Returns the object at the given index.
* @param { int } indx - Index of the object to get.
*/
template <typename T>
T Llist<T>::at(int indx) {
Node<T> *temp{ head };
int i{ 0 };
do {
if (i == indx) {
return temp->val;
}
++i;
temp = temp->next;
} while(temp != NULL);
out_of_bounds_index err;
throw err;
}
template <typename T>
int Llist<T>::indexOf(T val)
{
Node<T> *temp{ head };
int i{ 0 };
do {
if (temp->val == val) {
return i;
}
++i;
temp = temp->next;
} while(temp != NULL);
out_of_bounds_index err;
throw err;
}
/**
* Adds an object to the start of an array.
* @param { T } val - Object to add.
*/
template <typename T>
void Llist<T>::add_front(T val) {
// Create a new node.
Node<T> *nd1 = new Node<T>;
// Set its prev to NULL.
nd1->next = head;
nd1->val = val;
// Change others
head = nd1;
};
/**
* Adds an object to the start of an array.
* @param { int } indx - Index of object to change.
* @param { T } val - Object to change index to.
*/
template <typename T>
void Llist<T>::update(int indx, T val) {
Node<T> *temp{ head };
int i{ 0 };
do {
if (i == indx) {
temp->val = val;
}
++i;
temp = temp->next;
} while(temp != NULL);
};
/**
* Deletes an object at given index
* @param { int } indx - Index of object to remove.
*/
template <typename T>
void Llist<T>::remove(int indx) {
Node<T> *temp1{ head };
Node<T> *temp2{ NULL };
int i{ 0 };
do {
if (i != 0 && i == indx) {
temp2->next = temp1->next;
}
++i;
temp2 = temp1;
temp1 = temp1->next;
} while(temp1 != NULL);
temp2->next = NULL;
};
/**
* Deletes the first element of the array.
*/
template <typename T>
void Llist<T>::pop_front() {
head = head->next;
};
/**
* Deletes the last element of an array.
*/
template <typename T>
void Llist<T>::pop_back() {
Node<T> *temp1{ head };
Node<T> *temp2{ NULL };
do {
temp2 = temp1;
temp1 = temp1->next;
} while(temp1->next != NULL);
temp2->next = NULL;
};
//============================================================================
// Name : SinglyLinkedListDriver.cpp
// Author : Ramachandra jr
// Version :
// Copyright : Your copyright notice
// Description : A test driver for linked list class.
//============================================================================
#include "Llist.cpp"
int main() {
Llist<char> mylist1{};
mylist1.add('a');
mylist1.add('c');
mylist1.add('d');
mylist1.iterate();
std::cout << "====" << std::endl;
mylist1.add(1, 'b');
mylist1.add_front('o');
mylist1.update(1, 'o');
mylist1.iterate();
std::cout << "====" << std::endl;
mylist1.pop_front();
mylist1.pop_back();
mylist1.iterate();
std::cout << "====" << std::endl;
std::cout << mylist1.indexOf('b') << std::endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.