Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct ClassInfo
- {
- string name;
- string building;
- string code;
- string days;
- string times;
- };
- struct NodeType;
- typedef NodeType* NodePtr;
- struct NodeType
- {
- ClassInfo classes;
- NodePtr next;
- };
- // Adds data to an existing node
- // Pre: Valid NodePtr w/ allocated memory
- // Post: NodePtr contains valid info or set to nullptr if no info
- void FillInfo(NodePtr&);
- // Allocates memory and initializes nodePtr->next to NULL
- // Pre: None
- // Post: Allocated nodePtr with ->next set to NULL
- NodePtr GetNode();
- // Inserts node into list in sorted location
- // Pre: Valid pointer for list and prefilled NodePtr
- // Post: Sorted list
- void InsertNode(NodePtr&, NodePtr);
- // Returns a pointer to the node with matching last name.
- // Pre: Valid NodePtr and string.
- // Post: Returns pointer to matching node or nullptr if not found.
- NodePtr SearchNode(NodePtr head, string name);
- // Prints out list
- // Pre: Valid nodeptr
- void PrintList(NodePtr);
- // Save data to file
- // Pre: Valid nodeptr
- void SaveFile(NodePtr);
- // Support function to capitalize a name
- // Pre: s must point to a valid string
- // Post: s will be modified to capitalize the first letter
- void NormalizeString(string& s);
- // Deletes the node containing the passed string.
- // Pre: NodePtr points to valid linked list and string points to a valid string
- // Post: Node containing the passed string has been removed from the list.
- NodePtr DeleteNode(NodePtr&, string);
- int main()
- {
- NodePtr headPtr = NULL;
- NodePtr newPtr = NULL;
- string name;
- bool done = false;
- do {
- newPtr = GetNode();
- FillInfo(newPtr);
- if (newPtr != nullptr){
- InsertNode(headPtr, newPtr);
- done = false;
- }
- else
- done = true;
- } while (!done);
- PrintList(headPtr);
- do {
- cout << "Enter a name to search (Enter to finish): ";
- getline(cin, name);
- if (name != "") {
- NormalizeString(name);
- newPtr = SearchNode(headPtr, name);
- if (newPtr != nullptr)
- cout << newPtr->classes.name << " found!" << endl;
- else
- cout << name << " not found!" << endl;
- }
- } while (name != "");
- do {
- cout << "Enter a name to delete (Enter to finish): ";
- getline(cin, name);
- if (name != "") {
- NormalizeString(name);
- newPtr = DeleteNode(headPtr, name);
- if (newPtr != nullptr){
- cout << name << " deleted!" << endl;
- delete newPtr;
- }
- else
- cout << name << " not found!" << endl;
- }
- PrintList(headPtr);
- } while (name != "");
- return 0;
- }
- NodePtr GetNode()
- {
- NodePtr newPtr = new NodeType;
- newPtr->next = nullptr;
- return newPtr;
- }
- void FillInfo(NodePtr& np)
- {
- np->classes.code = "";
- cout << "Enter class name (Enter to finish): ";
- getline(cin, np->classes.name);
- NormalizeString(np->classes.name);
- if (np->classes.name == ""){
- delete np;
- np = nullptr;
- }
- }
- void InsertNode(NodePtr& head, NodePtr np)
- {
- NodePtr currPtr = head;
- NodePtr prevPtr = nullptr;
- if (currPtr == nullptr){ // Empty list
- head = np;
- head->next = nullptr;
- return;
- } else {
- while ((currPtr != nullptr) &&
- (np->classes.name > currPtr->classes.name)){
- prevPtr = currPtr;
- currPtr = currPtr->next;
- }
- }
- if (prevPtr == nullptr){ // First node
- np->next = head;
- head = np;
- } else if (currPtr == nullptr){ // Last node
- prevPtr->next = np;
- np->next = nullptr;
- } else { // Middle node
- np->next = currPtr;
- prevPtr->next = np;
- }
- return;
- }
- NodePtr DeleteNode(NodePtr& headPtr, string name)
- {
- NodePtr np = headPtr;
- NodePtr prevPtr = nullptr;
- while ((np != nullptr) && (np->classes.name != name)){
- prevPtr = np;
- np = np->next;
- }
- if (np == nullptr) {
- cout << "Node not found!" << endl;
- return nullptr;
- } else if (prevPtr == nullptr){ // First node
- headPtr = headPtr->next;
- } else if (np->next == nullptr){ // Last node
- prevPtr->next = nullptr;
- } else if (np != nullptr) { // Middle node
- prevPtr->next = np->next;
- }
- return np;
- }
- void PrintList(NodePtr np)
- {
- while (np != NULL)
- {
- cout << np->classes.name << ", " << np->classes.code << endl;
- np = np->next;
- }
- }
- NodePtr SearchNode(NodePtr listPtr, string name)
- {
- NodePtr currPtr = listPtr;
- while ((currPtr != nullptr) &&
- (currPtr->classes.name != name)){
- // cout << currPtr->customer.lastName << endl;
- currPtr = currPtr->next;
- }
- return currPtr;
- }
- void NormalizeString(string& s)
- {
- s[0] = toupper(s[0]);
- for (int i = 1; i < s.length(); i++){
- s[i] = tolower(s[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement