Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ linked list program to take in a file with instructions, alot of code alread

ID: 3662742 • Letter: C

Question

c++ linked list program to take in a file with instructions, alot of code already written.

. Your program will take in a “Command” file called “cmd.txt” that will instruct your program what operations to run. Your program should implement a C++ class which will be used to represent your linked list. It should implement the following member functions:

void prepend(int num)

void append(int num)

void removeFront()

void removeBack()

int search(int num)

void printList()

File Format

The command file will have the following format

<command> <0 or 1 arguments>

Commands

-1              prepend

-2              append

-3              removeFront

-4              removeBack

-5              search

-6              printlist

Example File

1 1 prepend 1 to the list

1 2 Prepend 2 to the list

1 3

2 7 Append 7 to the list

2 9

2 7

3

6

3

6

4

6

5 2

6

Expectations

You should not use any already implemented code such as a library for your linked list

Your code should be well formatted with proper spacing and proper naming

Your code should have well named variables. No a’s b’s or c’s as names unless it is for something like a loop counter

Your code should have the same output formatting you see below

I already have written most the code, but I need help importing the command file, taking in the commands, and altering the link list based on the commands given. I will rate ASAP when done

#include <iostream>
#include <fstream>
using namespace std;

struct node
{
int val;
node* next;
};

class link //class for linked list
{
public:
link();
void prepend(int);
void append(int);
void removeFront();
void removeBack();
int searchList(int);
void printList();
private:
node* head;
};

link::link()
{
head = NULL;
}

void link::prepend(int num) //function to add at the beginning
{
node* tmp;
tmp = new node;
tmp->val = num;
tmp->next = NULL;

if(head == NULL)
head = tmp;
else
{
tmp->next = head;
head = tmp;
}

}

void link::append(int num)//add at the end
{
node* tmp;
tmp = new node;
tmp = head;

while(tmp->next != NULL)
{
tmp = tmp->next;
}
node* tmp2;
tmp2 = new node;
tmp2->val = num;
tmp2->next = NULL;
tmp->next = tmp2;
}

void link::removeFront()
{
node* tmp;
tmp = new node;
tmp = head;
head = tmp->next;
delete(tmp);
}

void link::removeBack()
{
node* tmp;
tmp = new node;
tmp = head;
node* prev;
prev = new node;

while(tmp->next != NULL)
{
prev = tmp;
tmp = tmp -> next;
}

prev->next = NULL;
delete(tmp);
}

int link::searchList(int num)
{
node* tmp;
tmp = new node;
tmp = head;

while(tmp->val != num || tmp->next != NULL)
{
tmp = tmp->next;
}
if (tmp->val == num)
{
cout << "Found" << num << "in the list" << endl;
}
else
{
cout << "Did not find " << num << "in the list" << endl;
}
return 0;
}

void link::printList()
{
node* tmp;
tmp = new node;
tmp = head;

while(tmp != NULL)
{
cout << tmp->val << " " << "->";
tmp = tmp->next;
}
cout << "NULL" << endl;
}
int main()
{
ifstream myfile;
myfile.open("cmd.txt");
int com;//variable to hold command
  

//this is where I need help. I need to take in the file, enumerate through it, call my functions accordingly, and end when the file has completed. I do not know what the file will have //besides what I already posted.


while
return 0;
}

Explanation / Answer

wrote main funtion with file reading and parsing command

#include <iostream>
#include <fstream>
#include <sstream>
#include<string>
using namespace std;

int main()
{
    ifstream myfile;
    myfile.open("D://ravi//Cheg//cmd.txt");
    int com;//variable to hold command


//this is where I need help. I need to take in the file, enumerate through it, call my functions accordingly, and end when the file has completed. I do not know what the file will have //besides what I already posted.
    if (myfile.fail()) {
           cout << "file not found!";
       }

    link slink = link();
    string line,word;
    int val;
    int i;
    while (!myfile.eof()) {
           if (!getline(myfile, line))
               break;
           istringstream ss(line);

           i = 0;
           com = -1;
           val = -1;
           while (ss && i <=2) {
               word = "";
               if (getline(ss, word, ' ')) {

                   if(i == 0)
                       istringstream(word) >> com;
                   else if(i == 1)
                       istringstream(word) >> val;

                   i++;
               }
           }
           //cout << i << " : " << word << "-- " << com << " -- " <<val<<endl;
                           //command implementation
                           /*
                           * Commands

                               -1              prepend

                               -2              append

                               -3              removeFront

                               -4              removeBack

                               -5              search

                               -6              printlist
                           */
                           if(com == 1) {
                               cout << " command: prpend , val : " << val <<endl;
                               slink.prepend(val);
                           } else if( com == 2) {
                               cout << " command: append , val : " << val <<endl;
                               slink.append(val);
                           }
                           else if( com == 3) {
                               cout << " command: removeFront " <<endl;
                                slink.removeFront();
                           }
                           else if( com == 4) {
                               cout << " command: removeBack " <<endl;
                                slink.removeBack();
                           }
                           else if( com == 5) {
                                cout << " searching for " <<val<<" ";
                                slink.searchList(val);
                           }
                           else if( com == 6) {
                               cout << " command: printList " <<endl;
                                slink.printList();
                           }


       }

    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote