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

home / homework help / questions and answers / engineering / computer science /

ID: 641679 • Letter: H

Question

home / homework help / questions and answers / engineering / computer science / cop 3014 lab 06 the files funcs.h, func.cpp, node.h ...

Question

Edit question Edit points or time

COP 3014

Lab 06

The files funcs.h, func.cpp, Node.h and lab06.cpp have been provided
with this assignment. You will only modify one file, func.cpp. Look
for the comments marking where you'll be adding your code.

Please look at the methods declared in funcs.h.
Do not add any other methods to this file.

Modify the file funcs.cpp, which implements all of the methods that are
declared in funcs.h. lab06.cpp will invoke these methods. funcs.cpp will
be the only file that you change.

Use lab06.cpp, funcs.h, Node.h, and funcs.cpp together to test your solution.

The main program in lab06.cpp reads a series of strings until it encounters
the string "xxx". The string "xxx" is not added to the list, but is a sentinel
value that indicates that the user does not wish to add more strings to the list.

The strings are added to the linked list in alphabetical order. The list is
redisplayed after each new string is entered.

For example, the input could be:

bbb
ccc
aaa
xxx <-- this indicates that no more strings are to be added

At this point the program stops adding nodes to the list, and
displays the full list:

aaa
bbb
ccc

The main program has a second loop that reads strings that the user wishes
to remove from the list. The list is redisplayed after each string is
removed. Once again, "xxx" terminates the loop.

For example, the input could be:

yyy
aab
bbb
ccc
aaa
xxx <-- this indicates that no more strings are to be removed

If the string does not appear in the list, as in the case of the string
"yyy" in the above example, nothing should happen. That is, the program
should not crash.

When the program reads string "bbb", the node containing "bbb" should be
removed from the list and the node should be deleted.
Again, "xxx" indicates the end of the input.

Important: make sure you deallocate any node you remove from the list.
Otherwise you have a memory leak!

This assignment is due on the BlackBoard one week after it is assigned

Submit only the file "funcs.cpp" named as "yourFAUIDhere_funcs.cpp."

No late assignments.


Please
- use braces around any block
- use blocks as we did in our class examples
- indent your code as we have used indentations in our examples
- comment your code; do not over comment your code


Penalties:
o -2 for each missing block

o -2 for each missing indentation

o -2 for nodes not being deallocate after they are removed from
the list

o no credit if your cpp file does not compile or does not run

o some partial credit if your program crashes depending on
the problem


end of lab 06.

Explanation / Answer

i used vectors

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

bool compare(string a, string b){
cout << "compare(" << a << "," << b << ")" << endl;
return (a.compare(b) < 0);
}

int main () {
string mystrs[100];
string words;
int counter=0;
while(1){
  
  
cout<<"enter string";
  
cin>>words;
if(words=="xxx"){
break;
}
else{
mystrs[counter]=words;
}
counter++;
  
}

  
vector<string> myvector (mystrs, mystrs + 5);   
vector<string>::iterator it;

sort (myvector.begin(), myvector.end(), compare);

cout << "vector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it;

cout << endl;

return 0;
}