In: Computer Science
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:
Your Session struct will have following member functions:
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:
Output
// 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: