This program is an exercise in building, traversing, and destroying linear linked lists. The use of recursion should be included at least once in each assignment to better prepare us for future programs. Avoid multiple traversals whenever possible.
Programming Assignment: For programming assignment, we are going to create a program that helps keeping track of song lists.
Your primary job will be to create an ADT for Song and an ADT for SongList
Part I: The Song ADT
The information about a song should include:
Artist name (e.g., Eminem)
Title (e.g., Lose Yourself)
Length (e.g., 5.23)
Number of likes (e.g., 654,000)
Part II: The SongList ADT
The data members for SongList should be a head pointer to a linear linked list of Song objects and the number of songs in the list. The songs should be organized by popularities with the most popular song as the first node in the list.
This ADT must have public member functions to perform the following:
- Constructor - Construct an object and initialize the data members
- Destructor - Release all dynamic memory and reset data members to their zero equivalent value
- Add a new song
- Edit the number of likes for a song
- Display all songs in the list
- Display all songs for an artist (in order of popularity)
- Remove all songs with fewer than M likes, where M is sent in as an argument
Part III: The driver or the test program
The test program needs to first load the test data set from external file at the beginning of the program.
The menu-based user interface should allow user to use/test ALL the functionalities of the program. Try to make the user interface easier to use.
- Always prompt user when you need input data.
- The prompt needs to be meaningful. Example works great. E.g. “Enter the minimum likes (e.g 1000): “
- When asking user to choose some existing data, index works great. You can display the data with index preceding each one first.
General Syntax Rules
- Do not use statically allocated arrays in your classes or structures. All memory must be dynamically allocated and kept to a minimum for the classes! You can only use statically allocated arrays for temporary storage, such as local variables.
- Global variables are not allowed. global constants are encouraged
-
Do not use the String class! (use cstring, arrays of characters instead); you may use the cstring library with strlen, strcpy, strcmp etc
- Avoid using return from within the body of a loop
- Avoid using while(1) type of syntax
- Strive towards structured programming techniques
- Remember that at least one function needs to be written using recursion!
- For functions that have non-void return types, make sure to return a value through each path through the function.
- NEVER pass class types by value (always by reference); NEVER return class types by value. If you don’t want the objects modified, put const there.
- Use the iostream library for all I/O; do not use stdio.h for I/O
- Use of external data files is required
- MOST IMPORTANT:
BACKUP your files before creating a TAR archive!!!!
Your projects will be judged based on the use of classes, member functions, arguments, data structures, pointers, efficiency, and how well the software demonstrates that it has been fully tested. The code needs to compile and run, and include a test program for the member functions provided. It is not appropriate to hard code in the test cases - all tests should be interactive with the user. Your user interface must be clear for us to thoroughly test all features.
Richard C.
#include "LinkedList.h" // Default Constructor creates the head node. LinkedList::LinkedList() { head->song = "head (contains no song data)"; head->artist = "head (contains no artist data)"; head->next = NULL; listLength = 0; } // Setter adds a node to the list at a given position. // Takes a node and list position as parameters. // Position must be between 1 and the number of data nodes. // Returns true if the operation is successful. bool LinkedList::insertNode(node* newNode, int position) { if ((position <= 0) || (position > listLength + 1)) { cout << "nError: the given position is out of range.n"; return false; } if (head->next == NULL) { head->next = newNode; listLength++; return true; } int count = 0; node* p = head; node* q = head; while (q) { if (count == position) { p->next = newNode; newNode->next = q; length++; return true; } p = q; q = p->next; count++; } if (count == position) { p->next = newNode; newNode->next = q; listLength++; return true; } cout << "nError: node was not added to list.n"; return false; } // Setter removes a node by its given position. // Returns true if the operation is successful. bool LinkedList::removeNode(int position) { if ((position <= 0) || (position > listLength + 1)) { cout << "nError: the given position is out of range.n"; return false; } if (head->next == NULL) { cout << "nError: there is nothing to remove.n"; return false; } int count = 0; node* p = head; node* q = head; while (q) { if (count == position) { p->next = q->next; delete q; listLength--; return true; } p = q; q = p->next; count++; } cout << "nError: nothing was removed from the list.n"; return false; } // Prints each node in the list in consecutive order, // starting at the head and ending at the tail. // Prints the data to the console. void LinkedList::printList() { node* p = head; node* q = head; cout << "n---------------------------n"; cout << "Song Playlist n"; while (q) { p = q; cout << "n-----------------------------n"; cout << "t position: " << count << endl; cout << "t song: " << p->song << endl; cout << "t artist: " << p->artist << endl; q = p->next; count++; } } // Destructor de-allocates memory used by the list. LinkedList::~LinkedList() { node* p = head; node* q = head; while (q) { p = q; q = p->next; if (q) delete p; } }10/13/21