Question

In: Computer Science

I am just learning C++ and need to convert my previous code to a new linked...

I am just learning C++ and need to convert my previous code to a new linked list method. Please help.

My current LL program:

#include
#include
using namespace std;

struct LLnode{
string theData;
LLnode * fwdPtr;
};

void push_front(LLnode * &llh, string newData{
if(!llh){
LLnode * newNode = new LLnode;
newNode -> theData = newData;
newNode -> fwdPtr = nullptr;
llh = newNode;
}
else{
LLnode * trav = new LLnode;
trav = llh;
LLnode * newNode = new LLnode;
newNode -> theData = newData;
newNode -> fwdPtr = trav;
llh = newNode;

}
}

void push_back(LLnode * &llh, string newData){
LLnode * trav = new LLnode;
if(!llh){
llh = new LLnode;
llh -> theData = newData;
llh -> fwdPtr = nullptr;
}
else{
LLnode * newNode = new LLnode;
newNode -> theData = newData;
newNode -> fwdPtr = nullptr;

trav = llh;
while(trav -> fwdPtr){
trav = trav -> fwdPtr;
}
trav -> fwdPtr = newNode;
}
}

int list_length(LLnode * &llh){
int count = 0;
LLnode * trav = new LLnode;
trav = llh;
if(!llh){
return 0;
}
else{
while(trav){
count ++;
trav = trav -> fwdPtr;
}
return count;
}

}

string retrieve_front (LLnode * &llh){
if(!llh)
throw string ("Exception at retrieve back");
return (llh -> theData);
}

string retrieve_back (LLnode * &llh){
LLnode * trav = new LLnode;
trav = llh;
if(!llh)
throw string ("Exception at retrieve back");
while(trav -> fwdPtr){
trav = trav -> fwdPtr;
}
return trav -> theData;
}

void display_nodes(LLnode * &llh) {
LLnode * trav = new LLnode;
trav = llh;
if(!llh){
cout << "No nodes to display " << endl;
}
else{
cout << "Displaying nodes: " << endl;
while(trav){
cout << trav -> theData << ", ";
trav = trav -> fwdPtr;
}
}
cout << endl;
}

int main(){
LLnode * theLLHeader1 = nullptr;
cout << "Main: number of nodes in empty list " << list_length(theLLHeader1) << endl;
display_nodes(theLLHeader1);
push_front(theLLHeader1, "aaaaa");
push_back(theLLHeader1, "bbbbb");
push_front(theLLHeader1, "before aaaaa");
push_back(theLLHeader1, "after bbbbb");
cout << "Main: number of nodes after 4 pushed: " << list_length(theLLHeader1) << endl;
display_nodes(theLLHeader1);
cout << "Main: retrieve front: " << retrieve_front (theLLHeader1) << endl;
cout << "Main: retrieve back: " << retrieve_back (theLLHeader1) << endl;
cout << endl;

LLnode * theLLHeader2 = nullptr;

push_front(theLLHeader2, "33333");
push_front(theLLHeader2, "22222");
push_front(theLLHeader2, "11111");
push_back(theLLHeader2, "44444");
push_back(theLLHeader2, "55555");
push_back(theLLHeader2, "66666");
display_nodes (theLLHeader2);

return 0;
}

What I need to do:

Step 1

Convert LLnode struct into a header file. Any code module that refers to LLnode will need to contain a #include for this header file.

Create a linked list class named LL. Put into the same header file as struct. It will contain private data member: a linked list header, which is just a pointer variable of type LLnode for LLnode variables

I have already coded six linked list processing functions – push_front, push_back, list_length, retrieve_front, retrieve_back, and display_list. I need to convert these functions into public member functions of the class. The constructor for the LL class should set the linked list header to nullptr. There is no destructor. I also need to Templatize the class, and a requirement for templatized class functions is that they be coded in-line in the class definition. So, code the member functions in-line within the header file. You’ll have only two course files: the header file, and the .cpp for main.

I would like evidence of Couts so I can better understand the program. This would be much appreciated.

Step 2

Using a class template, I need to change struct and class to use any variable type. Main # 2 has been set up for debugging this step.

Step 3

Add member functions that perform the following functions:

void destroy_list ()
deletes each node in the list, and resets the header to nullptr

bool search_list (key value)
searches the list for a node with the given key. Returns true if found, false if not.

bool delete_node (key value)
deletes the node which contains the given key. If there is more than one node with the same key, delete_node deletes the first occurrence. Returns true if delete successful, false if the node was not found.

Main # 3 contains a test program for testing step 3.

Here are the main classes (1,2,3)::

//MAIN 1

int main() {

LL ll1;
cout << "length of empty list - " << ll1.list_length() << endl;
ll1.display_list();
ll1.push_front("aaaaa");
ll1.push_back("bbbbb");
ll1.push_front("before aaaaa");
ll1.push_back("after bbbbb");
cout << "length of list after 4 pushes - " << ll1.list_length() << endl;
ll1.display_list();
cout << endl;

LL ll2;
ll2.push_front("33333");
ll2.push_front("22222");
ll2.push_front("11111");
ll2.push_back("44444");
ll2.push_back("55555");
ll2.push_back("66666");
ll2.display_list();

return 0;
}

//MAIN 2

int main() {

LL ll1;
cout << "main: length of empty list - " << ll1.list_length() << endl;
cout << "main: trying to display empty list 1" << endl;
ll1.display_list();
cout << "main: trying to display initial size of ll1 - " << ll1.list_length() << endl;
ll1.push_front("aaaaa");
ll1.push_back("bbbbb");
ll1.push_front("before aaaaa");
ll1.push_back("after bbbbb");
cout << "main: length of ll1 after 4 pushes - " << ll1.list_length() << endl;
cout << "main: now trying to display ll1 after 4 push's" << endl;
ll1.display_list();
cout << "main: displaying final size of ll1 - " << ll1.list_length() << endl;
cout << endl;

LL ll2;
ll2.push_front("33333");
ll2.push_front("22222");
ll2.push_front("11111");
ll2.push_back("44444");
ll2.push_back("55555");
ll2.push_back("66666");
cout << "main: now trying to display ll2 after 6 push's" << endl;
ll2.display_list();

return 0;
}

//MAIN 3

int main() {

LL ll1;
cout << "main: length of empty list - " << ll1.list_length() << endl;
cout << "main: trying to display empty list 1" << endl;
ll1.display_list();
cout << "main: trying to display initial size of ll1 - " << ll1.list_length() << endl;
ll1.push_front("aaaaa");
ll1.push_back("bbbbb");
ll1.push_front("before aaaaa");
ll1.push_back("after bbbbb");
cout << "main: length of ll1 after 4 pushes - " << ll1.list_length() << endl;
cout << "main: now trying to display ll1 after 4 push's" << endl;
ll1.display_list();
cout << "main: displaying final size of ll1 - " << ll1.list_length() << endl;
ll1.destroy_list();
cout << "main: displaying size of list 1 after destroy - " << ll1.list_length() << endl;
cout << endl;

LL ll2;

ll2.push_front("33333");
ll2.push_front("22222");
ll2.push_front("11111");
ll2.push_back("44444");
ll2.push_back("55555");
ll2.push_back("66666");
cout << "main: now trying to display ll2 after 6 push's" << endl;
ll2.display_list();
cout << "main: now searching for node 44444" << endl;
if (ll2.search_list("44444"))
{
cout <<"main: found node 44444" << endl;
}
else
{
cout << "main: did not find node 44444" << endl;
}
cout << "main: now searching for node 44445" << endl;
if (ll2.search_list("44445"))
{
cout <<"main: found node 44445" << endl;
}
else
{
cout << "main: did not find node 44445" << endl;
}
cout << "main: now trying to delete node 44444" << endl;
if (ll2.delete_node("44444"))
{
cout <<"main: node 44444 deleted" << endl;
}
else
{
cout << "main: did not find 44444 for delete" << endl;
}
if (ll2.search_list("44444"))
{
cout <<"main: searched for 44444 after delete, found" << endl;
}
else
{
cout << "main: searched for 44444 after delete, not found" << endl;
}
cout << "main: displaying whole list after delete 44444" << endl;
ll2.display_list();
cout << "main: now trying to delete node 11111" << endl;
if (ll2.delete_node("11111"))
{
cout <<"main: node 11111 deleted" << endl;
}
else
{
cout << "main: did not find node 11111 for delete" << endl;
}
cout << "main displaying whole list after delete 11111" << endl;
ll2.display_list();
ll2.destroy_list();

return 0;
}

I will much appreciate any help. Thank you.

Solutions

Expert Solution

Hi, I created the header file and cpp for the both string and template type, which will accept any type of data. Your push_front needed some change. I updated it also some variable names. other than that no changes are required to your code. Here is your solution, let me know if you need any clarification or if you face any issues.

Source & Screens: LinkedList.h

#ifndef LINKEDLIST_H_
#define LINKEDLIST_H_

#include <iostream>
#include <string>

using namespace std;


// define struct of template
struct LLnode{
string data;
LLnode * next;
};

// declare class of template type
class LinkedList {
LLnode *head;
public:
LinkedList();
void push_front(string);
void push_back(string);
int size();
string retrieve_front();
string retrieve_back();
void display_nodes();
};

#endif /* LINKEDLIST_H_*/

Source & Screens: LinkedList.cpp

#include "LinkedList.h"

LinkedList ::LinkedList() {
// initialize head node to null
this->head = nullptr;
}

void LinkedList ::push_front(string newData) {
LLnode *newNode = new LLnode ;
if (!head)
{
newNode->data = newData;
newNode->next = nullptr;
head = newNode;
}
else
{
newNode->data = newData;
newNode->next = head;
head = newNode;
}
}

void LinkedList ::push_back(string newData) {
if (!head)
{
head = new LLnode ;
head->data = newData;
head->next = nullptr;
}
else
{
LLnode *current = head;
LLnode *newNode = new LLnode ;
newNode->data = newData;
newNode->next = nullptr;
while (current->next)
{
current = current->next;
}
current->next = newNode;
}
}

int LinkedList ::size() {
int count = 0;
LLnode *trav = new LLnode ;
trav = head;
if (!head)
{
return 0;
}
else
{
while (trav)
{
count++;
trav = trav->next;
}
return count;
}

}

string LinkedList ::retrieve_front() {
if (!head) throw string("Exception at retrieve back");
return (head->data);
}

string LinkedList ::retrieve_back() {
LLnode *trav = new LLnode ;
trav = head;
if (!head) throw string("Exception at retrieve back");
while (trav->next)
{
trav = trav->next;
}
return trav->data;
}

void LinkedList ::display_nodes() {
LLnode *trav = new LLnode ;
trav = head;
if (!head)
{
cout << "No nodes to display " << endl;
}
else
{
cout << "Displaying nodes: " << endl;
while (trav)
{
cout << trav->data << ", ";
trav = trav->next;
}
}
cout << endl;
}
int main() {
LinkedList list;
cout << "Main: number of nodes in empty list " << list.size()
<< endl;
list.display_nodes();
list.push_front("aaaaa");
list.push_back("bbbbb");
list.push_front("before aaaaa");
list.push_back("after bbbbb");
cout << "Main: number of nodes after 4 pushed: " << list.size()
<< endl;
cout<<"Length of list: "<<list.size()<<endl;
list.display_nodes();
cout << "Main: retrieve front: " << list.retrieve_front() << endl;
cout << "Main: retrieve back: " << list.retrieve_back() << endl;
cout << endl;

LinkedList list2;

// list2.push_front(33333);
// list2.push_front(22222);
// list2.push_front(11111);
// list2.push_back(44444);
// list2.push_back(55555);
// list2.push_back(66666);
// cout<<"Length of list: "<<list.size()<<endl;
// list2.display_nodes();

return 0;
}

Screens: Console Output
Source & Screens: LinkedListTemplate.h

#ifndef LINKEDLISTTEMPLATE_H_
#define LINKEDLISTTEMPLATE_H_

#include <iostream>
#include <string>

using namespace std;

// define struct of template
template <typename T>
struct LLnode{
T data;
LLnode * next;
};

// declare class of template type
template <class T>
class LinkedList {
LLnode <T> *head;
public:
LinkedList<T>();
void push_front(T);
void push_back(T);
int size();
T retrieve_front();
T retrieve_back();
void display_nodes();
};

#endif /* LINKEDLISTTEMPLATE_H_ */

Source & Screens: LinkedLIstTemplate.cpp


#include "LinkedListTemplate.h"
template <class T>
LinkedList<T>::LinkedList() {
// initialize head node to null
this->head = nullptr;
}
template <class T>
void LinkedList<T>::push_front(T newData) {
LLnode<T> *newNode = new LLnode<T>;
if (!head)
{
newNode->data = newData;
newNode->next = nullptr;
head = newNode;
}
else
{
newNode->data = newData;
newNode->next = head;
head = newNode;
}
}
template <class T>
void LinkedList<T>::push_back(T newData) {
if (!head)
{
head = new LLnode<T>;
head->data = newData;
head->next = nullptr;
}
else
{
LLnode<T> *current = head;
LLnode<T> *newNode = new LLnode<T>;
newNode->data = newData;
newNode->next = nullptr;
while (current->next)
{
current = current->next;
}
current->next = newNode;
}
}
template <class T>
int LinkedList<T>::size() {
int count = 0;
LLnode<T> *trav = new LLnode<T>;
trav = head;
if (!head)
{
return 0;
}
else
{
while (trav)
{
count++;
trav = trav->next;
}
return count;
}

}
template <class T>
T LinkedList<T>::retrieve_front() {
if (!head) throw string("Exception at retrieve back");
return (head->data);
}
template <class T>
T LinkedList<T>::retrieve_back() {
LLnode<T> *trav = new LLnode<T>;
trav = head;
if (!head) throw string("Exception at retrieve back");
while (trav->next)
{
trav = trav->next;
}
return trav->data;
}
template <class T>
void LinkedList<T>::display_nodes() {
LLnode<T> *trav = new LLnode<T>;
trav = head;
if (!head)
{
cout << "No nodes to display " << endl;
}
else
{
cout << "Displaying nodes: " << endl;
while (trav)
{
cout << trav->data << ", ";
trav = trav->next;
}
}
cout << endl;
}
int main() {
LinkedList<string> list;
cout << "Main: number of nodes in empty list " << list.size()
<< endl;
list.display_nodes();
list.push_front("aaaaa");
list.push_back("bbbbb");
list.push_front("before aaaaa");
list.push_back("after bbbbb");
cout << "Main: number of nodes after 4 pushed: " << list.size()
<< endl;
cout<<"Length of list: "<<list.size()<<endl;
list.display_nodes();
cout << "Main: retrieve front: " << list.retrieve_front() << endl;
cout << "Main: retrieve back: " << list.retrieve_back() << endl;
cout << endl;

LinkedList<int>list2;

list2.push_front(33333);
list2.push_front(22222);
list2.push_front(11111);
list2.push_back(44444);
list2.push_back(55555);
list2.push_back(66666);
cout<<"Length of list: "<<list2.size()<<endl;
list2.display_nodes();

return 0;
}

Screens: console output

Related Solutions

C++ Problem. I am providing the code. Just Please provide the new function and highlight it....
C++ Problem. I am providing the code. Just Please provide the new function and highlight it. implement the functions replaceAt, seqSearch, and remove. Test your new function too in main. Also, Test Old functions in main. Show the output. Also, modify the functions accordingly which have "See Programming Exercise 22". main.cpp : #include <iostream> using namespace std; #include "arrayListTypetempl.h" int main(){    arrayListType<int> intList;    arrayListType<char> charList;       intList.insertEnd(5);    intList.insertEnd(3);    intList.insertEnd(4);    intList.insertEnd(55);       charList.insertEnd('a');   ...
C++ Hello .I need to convert this code into template and then test the template with...
C++ Hello .I need to convert this code into template and then test the template with dynamic array of strings also if you can help me move the function out of the class that would be great.also There is a bug where the memory was being freed without using new operator. I cant seem to find it thanks in advance #include using namespace std; class DynamicStringArray {    private:        string *dynamicArray;        int size;    public:   ...
This is my C language code. I have some problems with the linked list. I can't...
This is my C language code. I have some problems with the linked list. I can't store the current. After current = temp, I don't know how to move to the next node. current = current-> next keeps making current into NULL. #include #include #include #include struct node{int data; struct node *next;}; int main() {     struct node *head, *current, *temp, *trash;     srand(time(0));     int randNumber = rand()%51;     if(randNumber != 49)     {         temp = (struct node*)malloc(sizeof(struct node));         current = (struct node*)malloc(sizeof(struct node));...
I need convert this java code to C language. There is no string can be used...
I need convert this java code to C language. There is no string can be used in C. Thank you! import java.util.Scanner; public class Nthword { public static void main( String args[] ) { String line; int word; Scanner stdin = new Scanner(System.in); while ( stdin.hasNextLine() ) { line = stdin.nextLine(); word = stdin.nextInt(); stdin.nextLine(); // get rid of the newline after the int System.out.println( "Read line: \"" + line + "\", extracting word [" + word + "]" );...
I am Writing a C-Program to read and write files. but none of my code is...
I am Writing a C-Program to read and write files. but none of my code is working like it should be. Please fix all code and supply output response. Please try to use existing code and code in comments. But if needed change any code that needs to be changed. Thank you in advance //agelink.c //maintains list of agents //uses linked list #include <stdio.h> #include <stdlib.h> #define TRUE 1 void listall(void); void newname(void); void rfile(void); void wfile(void); struct personnel {...
I am trying to make a new code that uses functions to make it. My functions...
I am trying to make a new code that uses functions to make it. My functions are below the code. <?php */ $input; $TenBills = 1000; $FiveBills = 500; $OneBills = 100; $Quarters = 25; $Dimes = 10; $Nickels = 5; $Pennies = 1; $YourChange = 0; $input = readline("Hello, please enter your amount of cents:\n"); if(ctype_digit($input)) { $dollars =(int)($input/100); $cents = $input%100;    $input >= $TenBills; $YourChange = (int)($input/$TenBills); $input -= $TenBills * $YourChange; print "Change for $dollars dollars...
Need this in C#. Below is my code for Problem 3 of Assignment 2. Just have...
Need this in C#. Below is my code for Problem 3 of Assignment 2. Just have to add the below requirement of calculating the expected winning probability of VCU. Revisit the program you developed for Problem 3 of Assignment 2. Now your program must calculate the expected winning probability of VCU through simulation. Run the simulation 10,000 times (i.e., play the games 10,000 times) and count the number of wins by VCU. And then, calculate the winning probability by using...
I need assistance translating a custom C++ program to MIPS. My C++ code is the following:...
I need assistance translating a custom C++ program to MIPS. My C++ code is the following: I have made numerous attempts on my own to no avail, any assistance is appreciated. Also, template code for this solution is provided below: #include int moveRobots(int *, int *, int, int ); int getNew(int, int); int main() { int x[4], y[4], i, j, myX = 25, myY = 25, move, status = 1; // initialize positions of four robots x[0] = 0; y[0]...
this is my code I want the opposite i want to convert a postfix expression to...
this is my code I want the opposite i want to convert a postfix expression to infix expression #include <iostream> #include <string> #define SIZE 50 using namespace std; // structure to represent a stack struct Stack {   char s[SIZE];   int top; }; void push(Stack *st, char c) {   st->top++;   st->s[st->top] = c; } char pop(Stack *st) {   char c = st->s[st->top];   st->top--;   //(A+B)*(C+D)   return c; } /* function to check whether a character is an operator or not. this function...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public static void main( String args[] ) { Scanner input = new Scanner( System.in ); // create array to hold user input int nums[] = new int[10]; int i = 0, truthCount = 0; char result = 'F', result2 = 'F'; // ask user to enter integers System.out.print("Please Enter 10 Different integers: "); // gather input into array for ( i = 0; i <...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT