1. Homepage
  2. Programming
  3. CSC2059 Data structures and Algorithms - Practical 5: Lists

CSC2059 Data structures and Algorithms - Practical 5: Lists

Engage in a Conversation
UKQueen’s University BelfastQUBCSC2059Data structures and AlgorithmsC++

Practical 5: Lists CourseNana.COM

Acknowledgement CourseNana.COM

The practical is based on the module material of Prof. Ji Ming and maintained by Muhammad Fahim and module team. CourseNana.COM

Launch Visual Studio. CourseNana.COM

Create a new Solution Practical5 and name the first project List1. CourseNana.COM

Exercise 1. Linked List class expansion CourseNana.COM

In this project, we will add new member functions into the List class to expand this class. Download the provided template classes ListNode.h and List.h, and add these classes into the project List1. CourseNana.COM

(1) Inside the List class, add a new public function void print(); CourseNana.COM

which prints the data items of the list nodes in a list object, from the first node to the last node. CourseNana.COM

(2) Inside the List class, add a new public function bool remove(int p1, int p2); CourseNana.COM

This function is an extension of the member function int remove(int p); which removes a node in a list at position p. The new function will remove a range of nodes from position p1 to position p2 (inclusive). It returns true for successful removal and false for illegal parameters p1 and/or p2. CourseNana.COM

(3) Create a test1.cpp in the project and use the following main function to test the above new member functions. The expected outputs are shown below the test function. CourseNana.COM

int main() { CourseNana.COM

} CourseNana.COM

// create an alphabet list CourseNana.COM

List<char> chlist;
for (int i = 0; i < 26; i++)
CourseNana.COM

chlist.insert_end(97 + i); CourseNana.COM

// print chlist CourseNana.COM

chlist.print(); CourseNana.COM

// remove nodes from 22-25 CourseNana.COM

if (chlist.remove(22, 25)) chlist.print(); CourseNana.COM

// remove nodes from 0-3 CourseNana.COM

if (chlist.remove(0, 3)) chlist.print(); CourseNana.COM

// remove nodes from 5-10 CourseNana.COM

if (chlist.remove(5, 10)) chlist.print(); CourseNana.COM

return 0; CourseNana.COM

page3image23077824 CourseNana.COM

Exercise 2. Linked List for storage of data with an unknown size CourseNana.COM

In solution Practical5 add a new project List2, in which create a source file test2.cpp. Download the provided corpus.txt file and place it into the List2 folder along with the test2.cpp file. Add the template classes ListNode.h and List.h into the project. CourseNana.COM

The following shows an incomplete program, showing the use of a linked list (an object of our List class) for loading data of an unknown size into the program. The program uses C++ file I/O classes for opening the corpus file for reading the data into a linked list, each list node storing a word. To use the class you need to #include <fstream>. CourseNana.COM

As you can notice, the corpus contains many word items which include some non-alphabetic character(s), e.g., \’, \-, \., \,, \”, \&, \%, \; .... There is also a sentence code (e.g., 011a0101) at the beginning of each sentence which is not a word. You should ignore all these items and only store the proper words, in which each character is an alphabetic letter, in the list. CourseNana.COM

You are asked to complete this program, in test2.cpp, by completing the missing parts indicated by points 1-4 in the program. After execution, your program should print out the number of the proper words in this corpus file. CourseNana.COM

Hint: You will find it useful to write a function for testing for proper words (and non-proper words): CourseNana.COM

bool isWord(string item);
The incomplete program with missing parts indicated by 1-4 CourseNana.COM

int main() { CourseNana.COM

// allocate a string-type list pointed to by pc, used to store all // words in the corpus
List<string>* pc = new List<string>;
// open the corpus.txt file for input
CourseNana.COM

ifstream fi("corpus.txt");
string item;
// read each item in the file separated by space until the end of the file while (fi >> item) {
CourseNana.COM

// 1. test if the read item is a word - with only letters and // no other characters
// 2. if yes insert the word into the list pointed by pc
CourseNana.COM

} CourseNana.COM

       // close the file CourseNana.COM

fi.close(); CourseNana.COM

// 3. print the size of the list
// 4. free the memory used to store the corpus
return 0; CourseNana.COM

} CourseNana.COM

Get in Touch with Our Experts

WeChat WeChat
Whatsapp WhatsApp
UK代写,Queen’s University Belfast代写,QUB代写,CSC2059代写,Data structures and Algorithms代写,C++代写,UK代编,Queen’s University Belfast代编,QUB代编,CSC2059代编,Data structures and Algorithms代编,C++代编,UK代考,Queen’s University Belfast代考,QUB代考,CSC2059代考,Data structures and Algorithms代考,C++代考,UKhelp,Queen’s University Belfasthelp,QUBhelp,CSC2059help,Data structures and Algorithmshelp,C++help,UK作业代写,Queen’s University Belfast作业代写,QUB作业代写,CSC2059作业代写,Data structures and Algorithms作业代写,C++作业代写,UK编程代写,Queen’s University Belfast编程代写,QUB编程代写,CSC2059编程代写,Data structures and Algorithms编程代写,C++编程代写,UKprogramming help,Queen’s University Belfastprogramming help,QUBprogramming help,CSC2059programming help,Data structures and Algorithmsprogramming help,C++programming help,UKassignment help,Queen’s University Belfastassignment help,QUBassignment help,CSC2059assignment help,Data structures and Algorithmsassignment help,C++assignment help,UKsolution,Queen’s University Belfastsolution,QUBsolution,CSC2059solution,Data structures and Algorithmssolution,C++solution,