Question

In: Computer Science

c++   In this lab you will create a program to make a list of conference sessions...

c++   In this lab you will create a program to make a list of conference sessions you want to attend. (List can be of anything...)

You can hard code 10 Sessions in the beginning of your main program.

For example, I have

session UK1("Descaling agile",    "Gojko Adzic",       60);

       session UK2("Theory of constraints", "Pawel Kaminski", 90);

and then:

       List l;

       l.add(&UK1);

       l.add(&UK2);

Your Session struct should have the following member data:

  1. The Title
  2. The Speaker
  3. The session length (an integer)
  4. A link to the next session.

Your Session struct will have following member functions:

  1. A default constructor that blanks/zeros the title, speaker and session length.
  2. A constructor that takes three inputs: one for the title, one for the speaker and one for the session length.   (You can combine this with the default or not, your choice.)
  3. A print function that prints out the title, speaker and session length.

Linked List (Session.h, Session.cpp)

You will create a class that contains a list of Sessions. This class will have a pointer to the top of the linked list as private member data and the following member functions:

  1. A default constructor.
  2. A destructor – The destructor should contain a cout statement that shows when it was called. It should delete the linked list. It should print the title of each element it deletes.
  3. A print method that prints the linked list. This function should use the Sessions print function.
  4. An add method that adds a Session to the linked list. This method most keep the list IN ORDER by Session Speaker. See sample flowers code for an example of this. You can use the string comparison operators (<,>) to do this.
  5. A method that finds and prints a Session by Speaker. Assume that there is only one session per speaker. This function should take advantage of the fact that the list is sorted.
  6. Your .h file should have some code in it to make sure it only compiles once. You can use either the #ifndef method or the pragma once method. If you use pragma once, add some comments explaining what it does. (If you've already done this on a previous lab you are good to go.)          

Output

  • Once you’ve created your linked list you should print out the titles, speakers and session time. See example output.
  • Then, allow your user to enter a speaker and have your program search your linked list. Print the Session if you find it or <speaker name > "Not Found" if you don't.
  • Notice that my destructor printed as it deleted.
  • Align your list output.

Solutions

Expert Solution

// Session.h
#ifndef SESSION_H
#define SESSION_H

#include <string>

struct session
{
std::string title;
std::string speaker;
int length;
session *next;

session(std::string title="",std::string speaker="", int length=0);
void print();
};

class List
{
private:
session *top;
public:
List();
~List();
void print();
void add(session *s);
void find(std::string speaker);
};

#endif
//end of Session.h

// Session.cpp
#include "Session.h"
#include <iostream>
#include <iomanip>
using namespace std;

// constructor to set the title (default ""), speaker (default "") and length (default 0)
session::session(std::string title, std::string speaker, int length)
{
this->title = title;
this->speaker = speaker;
this->length = length;
next = nullptr;
}

// function to display the title, speaker and length of the session
void session:: print()
{
cout<<left<<setw(30)<<title<<left<<setw(25)<<speaker<<left<<setw(10)<<length<<endl;
}

// constructor to create an empty Session linked list
List::List()
{
top = nullptr;
}

// destructor to delete the session nodes of the linked list
List::~List()
{
cout<<"Destructor called"<<endl;
session *temp;
// loop over the list
while(top != nullptr)
{
cout<<top->title<<endl; // display the title
temp = top;
top = top->next;
delete(temp); // delete the node
}
}

// function to display the sessions in the list
void List::print()
{
if(top == nullptr) // empty list
cout<<"No sessions exist"<<endl;
else
{
// display the list
cout<<left<<setw(30)<<"Title"<<left<<setw(25)<<"Speaker"<<left<<setw(10)<<"Length"<<endl;
session *curr = top;
// loop over the list
while(curr != nullptr)
{
curr->print();
curr = curr->next;
}
}
cout<<endl;
}

// function to add the session s to the list ordered by speaker
void List:: add(session *s)
{
if(top == nullptr) // empty list, insert at the top
top = s;
else
{
session *curr = top;
session *prev = nullptr;
// loop to get the position of insert in sorted order
while(curr != nullptr)
{
if(curr->speaker > s->speaker)
break;
prev = curr;
curr = curr->next;
}

// insert at top
if(prev == nullptr)
{
s->next = top;
top = s;
}else
{
// insert between prev and next
prev->next = s;
s->next = curr;
}
}
}

// function to find the course with the given speaker
void List:: find(std::string speaker)
{
bool found = false;
session *curr = top;
// loop over the list
while(curr != nullptr)
{
if(curr->speaker == speaker) // if session with the speaker is found
{
found = true;
break;
}else if(curr->speaker > speaker) // if the current speaker session is alphabetically after input speaker (since the list is sorted)

break;

curr = curr->next;
}

// if session with the speaker is not found
if(!found)
cout<<speaker<<" Not Found"<<endl;
else
curr->print();
}

//end of Session.cpp

// main.cpp : C++ driver program to test the List class
#include <iostream>
#include "Session.h"
using namespace std;

int main()
{
// create 10 sessions
session UK1("Descaling agile","Gojko Adzic",60);
session UK2("Theory of constraints", "Pawel Kaminski", 90);
session UK3("Data structures", "John Smith", 50);
session UK4("Algorithms", "Harley Quinn", 70);
session UK5("Programming With Java", "Michelle Day", 100);
session UK6("Theory of computing", "Henry Cavil", 120);
session UK7("Ethics", "Harry Febil", 90);
session UK8("Programming with C++", "Sumita Arora", 75);
session UK9("Computer Networks", "Shaun Hope", 80);
session UK10("Networking", "Antoine Suarez", 45);

List l; // create an empty list
// insert the 10 sessions into the list
l.add(&UK1);
l.add(&UK2);
l.add(&UK3);
l.add(&UK4);
l.add(&UK5);
l.add(&UK6);
l.add(&UK7);
l.add(&UK8);
l.add(&UK9);
l.add(&UK10);

l.print(); // display the list

string speaker;
// input of speaker name to search
cout<<"Enter the name of the speaker to find: ";
getline(cin,speaker);

l.find(speaker); // find the speaker in the list
cout<<endl;

return 0;
}
//end of main.cpp

Output:


Related Solutions

Tony Gaddis C++ Conference Sessions An upcoming conference about C/C++ programming has three sessions planned: -...
Tony Gaddis C++ Conference Sessions An upcoming conference about C/C++ programming has three sessions planned: - A session on the new features of C++ 17 - A session on functional programming in C/C++ - A session on lamda functions Attendees can subscribe for any session. The organizers want to keep track of which attendees are attending which session. Write a program that stores this information in a two-dimensional array of Booleans, where each row represents an attendee and each column...
C or C++ program Create a list of 5 things-to-do when you are bored (in the...
C or C++ program Create a list of 5 things-to-do when you are bored (in the text file things.txt, one line each), where each thing is described as a string of characters of length in between 10 and 50. Design a C program to read these things in (from stdin or by input redirection) and store them in the least memory possible (i.e., only the last byte in the storage for each string can be the null character). After reading...
C Programming Language: For this lab, you are going to create two programs. The first program...
C Programming Language: For this lab, you are going to create two programs. The first program (named AsciiToBinary) will read data from an ASCII file and save the data to a new file in a binary format. The second program (named BinaryToAscii) will read data from a binary file and save the data to a new file in ASCII format. Specifications: Both programs will obtain the filenames to be read and written from command line parameters. For example: - bash$...
A scenario…there is a local accounting conference coming up. To break up the formal sessions you...
A scenario…there is a local accounting conference coming up. To break up the formal sessions you have been asked to be part of a debate titled ‘The core nature of Accounting has changed little since the times of Pacioli’. You are to prepare background notes on both the affirmative and negative positions.
C++ Create a program that use the linkedbag 3. Develop a program to maintain a list...
C++ Create a program that use the linkedbag 3. Develop a program to maintain a list of homework assignments. When an assignment is assigned, add it to the list, and when it is completed, remove it. You should keep track of the due date. Your program should provide the following services: • Add a new assignment. • Remove an assignment. • Provide a list of the assignments in the order they were assigned. • Find the assignment(s) with the earliest...
c++ Create a program that creates a sorted list from a data file. The program will...
c++ Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name...
C++ Only Please 10.15 LAB: Warm up: Contacts You will be building a linked list. Make...
C++ Only Please 10.15 LAB: Warm up: Contacts You will be building a linked list. Make sure to keep track of both the head and tail nodes. (1) Create three files to submit. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp - main() function (2) Build the ContactNode class per the following specifications: Parameterized constructor. Parameters are name followed by phone number. Public member functions InsertAfter() (2 pts) GetName() - Accessor (1 pt) GetPhoneNumber - Accessor (1 pt) GetNext()...
Write a program in C++ that will make changes in the list of strings by modifying...
Write a program in C++ that will make changes in the list of strings by modifying its last element. Your program should have two functions: 1. To change the last element in the list in place. That means, without taking the last element from the list and inserting a new element with the new value. 2. To compare, you need also to write a second function that will change the last element in the list by removing it first, and...
C program! Create a list of 5 things-to-do when you are bored (in the text file...
C program! Create a list of 5 things-to-do when you are bored (in the text file things.txt, one line each), where each thing is described as a string of characters of length in between 10 and 50. Design a C program to read these things in (from stdin or by input redirection) and store them in the least memory possible (i.e., only the last byte in the storage for each string can be the null character). After reading things in,...
1) a. Write down a C++ program which will create a list (simple linear linked list)...
1) a. Write down a C++ program which will create a list (simple linear linked list) of nodes. Each node consists of two fields. The first field is a pointer to a structure that contains a student id (integer) and a gradepoint average (float). The second field is a link. The data are to be read from a text file. Your program should read a file of 10 students (with student id and grade point average) and test the function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT