I have a lot of experience in Python and Haskell but am new to C++, so I'm wondering if my code is idiomatic. For one of the first assignments in my class, I wrote a linked list class (the "cities" are nodes in the linked list, the teacher wanted to make things interesting, I guess). Here's the header I had to implement, the functions are described with comments:
#ifndef COMMUNICATIONNETWORK_H
#define COMMUNICATIONNETWORK_H
#include <iostream>
struct City{
std::string cityName;
std::string message; //Ignore this bit, it's not relevant
City *next;
City *previous;
City(){};
City(std::string initName, City *initNext, City *initPrevious, std::string initMessage)
{
cityName = initName;
next = initNext;
previous = initPrevious;
message = initMessage;
}
};
class CommunicationNetwork
{
public:
CommunicationNetwork();
~CommunicationNetwork();
void addCity(std::string, std::string); //Insert city with given name after city with given name
void printNetwork(); //Go through and print all city names
void deleteCity(std::string); //Remove city with given name
void deleteNetwork(); //Free all memory, name the cities being deleted, replace head with NULL
protected:
private:
City *head;
City *tail;
};
#endif // COMMUNICATIONNETWORK_H
Here's my implementation (the weird output formatting was mandatory):
#include "CommunicationNetwork.h"
#include <iostream>
#include <fstream>
using namespace std;
CommunicationNetwork::CommunicationNetwork()
{ head = new City; tail = new City; }
CommunicationNetwork::~CommunicationNetwork()
{ if(head) CommunicationNetwork::deleteNetwork(); }
void CommunicationNetwork::printNetwork()
{
if(head == NULL)
{
cout << "===CURRENT PATH===" << endl;
cout << "NULL" << endl;
cout << "==================" << endl;
return;
}
cout << "===CURRENT PATH===" << endl;
cout << "NULL <- ";
City* currentCity = head;
while(currentCity->next != NULL)
{
cout << currentCity->cityName << " <-> ";
currentCity = currentCity->next;
}
cout << currentCity->cityName << " -> NULL" << endl;
cout << "==================" << endl;
}
void CommunicationNetwork::addCity(string prevCity,
string newCity)
{
if(prevCity == "First")
{
head->previous = new City(newCity, head, NULL, "");
head = head->previous;
}
else
{
City* currentCity = head;
while(currentCity != NULL)
{
if(currentCity->cityName == prevCity)
{
currentCity->next = new City(newCity,
currentCity->next,
currentCity,
"");
currentCity->next->next->previous = currentCity->next;
break;
}
currentCity = currentCity->next;
}
}
}
void CommunicationNetwork::deleteCity(string city)
{
if(head->cityName == city)
{
head = head->next;
delete head->previous;
head->previous = NULL;
}
else if(tail->cityName == city)
{
tail = tail->previous;
delete tail->next;
tail->next = NULL;
}
else
{
City* currentCity = head->next;
while(currentCity->next != NULL)
{
if(currentCity->cityName == city)
{
currentCity->previous->next = currentCity->next;
currentCity->next->previous = currentCity->previous;
delete currentCity;
break;
}
currentCity = currentCity->next;
}
}
}
void CommunicationNetwork::deleteNetwork()
{
if(head == NULL)
return;
City* currentCity = head->next;
while(currentCity != NULL)
{
cout << "deleting " << currentCity->previous->cityName << endl;
delete currentCity->previous;
currentCity = currentCity->next;
}
cout << "deleting " << tail->cityName << endl;
delete tail;
head = NULL; tail = NULL;
}