This lab is also on linked list only this time we are putting the list in sorted order. Also, we will be adding a deleteFromList function. This a to-do list with the list in sorted order by date. This can be done if the date is a string with year/month/day order. The to-do items can modeled as a struct or class. You can have any fields you want as long as you have at least a date field to sort by. What to do You will need at least three functions. (addToList) will add an item to the linked list, (deleteFromList) will delete an item from the list and (printList) will traverse the list and print out the values in the list. You might have more than 3 as you will need to ask the user for the info for one item, print an item and delete all nodes at the end of the program.
Expert Answer
Given below is the code for the question along with output. Please do rate the answer if it helped. Thank you.
todolist.h
#ifndef todolist_h
#define todolist_h
#include <iostream>
using namespace std;
typedef struct listitem
{
string date;
string description;
struct listitem * next;
}item;
class TodoList
{
item *head;
public:
TodoList();
~TodoList();
void addToList(string date, string desc);
void printList();
void deleteFromList(string date);
};
#endif /* todolist_h */
todolist.cpp
//
// todolist.cpp
// Test
//
// Created by raji on 10/08/2017.
// Copyright © 2017 raji. All rights reserved.
//
#include “todolist.h”
TodoList::TodoList()
{
head = NULL;
}
TodoList::~TodoList()
{
item* curr = head;
item* next ;
while(curr != NULL)
{
next = curr->next;
delete curr;
curr = next;
}
}
//add to list in sorted order of date
void TodoList::addToList(string date, string desc)
{
item* todo = new item;
todo->date = date;
todo->description = desc;
todo->next = NULL;
if(head == NULL)
head = todo;
else
{
item* curr = head;
item* prev = NULL;
while(curr != NULL)
{
if(curr->date > date)
break;
prev = curr;
curr = curr->next;
}
if(prev == NULL)
{
todo->next = head;
head = todo;
}
else
{
prev->next = todo;
todo->next = curr;
}
}
}
void TodoList::printList()
{
item* curr = head;
cout << “**** TODO LIST ****” << endl;
cout << “*******************” << endl;
while(curr != NULL)
{
cout << “date: ” << curr->date << endl;
cout << “desc: ” << curr->description << endl;
cout << “*******************” << endl;
curr = curr->next;
}
cout << endl;
}
void TodoList::deleteFromList(string date)
{
if(head == NULL) //list is empty
return;
else
{
item* curr = head;
item* prev = NULL;
while(curr != NULL)
{
if(curr->date == date)
break;
prev = curr;
curr = curr->next;
}
if(curr == NULL)
cout << “no todo item for the date ” << date << endl;
else
{
if(prev == NULL)
head = curr->next;
else
prev->next = curr->next;
delete curr;
}
}
}
todomain.cpp
#include “todolist.h”
#include <iostream>
int main()
{
int choice = 0;
string date , desc;
TodoList list;
while(choice != 4)
{
cout << “n????? What do you want to do ?????” << endl;
cout << “1. add item” << endl;
cout << “2. print all items” << endl;
cout << “3. delete item” << endl;
cout << “4. quit” << endl;
cout << “=> enter: ” ;
cin >> choice;
switch(choice)
{
case 1:
cout << “adding an item” << endl;
cout << “date? “;
cin >> date;
cin.ignore(); //remove new line
cout << “desc? “;
getline(cin ,desc);
list.addToList(date, desc);
break;
case 2:
list.printList();
break;
case 3:
cout << “Enter an item’s date to delete: “;
cin >> date;
list.deleteFromList(date);
break;
case 4:
break;
default:
cout << “Invalid menu choice!” << endl;
}
}
return 0;
}
output
????? What do you want to do ?????
1. add item
2. print all items
3. delete item
4. quit
=> enter: 1
adding an item
date? 20170715
desc? go to grocery store
????? What do you want to do ?????
1. add item
2. print all items
3. delete item
4. quit
=> enter: 1
adding an item
date? 20170623
desc? do laundry
????? What do you want to do ?????
1. add item
2. print all items
3. delete item
4. quit
=> enter: 2
**** TODO LIST ****
*******************
date: 20170623
desc: do laundry
*******************
date: 20170715
desc: go to grocery store
*******************
????? What do you want to do ?????
1. add item
2. print all items
3. delete item
4. quit
=> enter: 1
adding an item
date? 20180124
desc? birthday party
????? What do you want to do ?????
1. add item
2. print all items
3. delete item
4. quit
=> enter: 2
**** TODO LIST ****
*******************
date: 20170623
desc: do laundry
*******************
date: 20170715
desc: go to grocery store
*******************
date: 20180124
desc: birthday party
*******************
????? What do you want to do ?????
1. add item
2. print all items
3. delete item
4. quit
=> enter: 3
Enter an item’s date to delete: 20180124
????? What do you want to do ?????
1. add item
2. print all items
3. delete item
4. quit
=> enter: 2
**** TODO LIST ****
*******************
date: 20170623
desc: do laundry
*******************
date: 20170715
desc: go to grocery store
*******************
????? What do you want to do ?????
1. add item
2. print all items
3. delete item
4. quit
=> enter: 3
Enter an item’s date to delete: 20170623
????? What do you want to do ?????
1. add item
2. print all items
3. delete item
4. quit
=> enter: 2
**** TODO LIST ****
*******************
date: 20170715
desc: go to grocery store
*******************
????? What do you want to do ?????
1. add item
2. print all items
3. delete item
4. quit
=> enter: 4