Skip to content

Instantly share code, notes, and snippets.

@1995eaton
Created December 6, 2014 21:16
Show Gist options
  • Save 1995eaton/72de5896f8d6d8d390cf to your computer and use it in GitHub Desktop.
Save 1995eaton/72de5896f8d6d8d390cf to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
class Node
{
friend class LinkedList;
private:
Node* _next;
std::string _data;
public:
Node(void)
{
_next = NULL;
}
Node(std::string data)
{
_next = NULL;
_data = data;
}
Node(std::string data, Node* next)
{
_next = next;
_data = data;
}
inline std::string getData(void)
{
return _data;
}
inline Node* getNext(void)
{
return _next;
}
};
class LinkedList
{
private:
Node* pHead, *pTail;
public:
LinkedList(void);
LinkedList(std::string);
~LinkedList(void);
void add();
void print(void);
//void clear(void);
};
LinkedList::LinkedList()
{
pTail = pHead = NULL;
}
LinkedList::LinkedList(std::string data)
{
pHead = new Node(data);
pTail = NULL;
}
LinkedList::~LinkedList()
{
//clear();
}
void LinkedList::add()
{
std::string strin;
std::cout << "Tell me the player's name: "; std::cin >> strin;
if (pHead == nullptr)
{
pTail = pHead = new Node(strin);
}
else
{
pTail = pTail->_next = new Node(strin);
}
};
void LinkedList::print()
{
Node* toPrint = pHead;
while (toPrint != nullptr)
{
std::cout << toPrint->getData() << " is in the lobby" << std::endl;
toPrint = toPrint->getNext();
}
}
int main()
{
LinkedList myLobby;
int choice;
do
{
std::cout << "What you wanna do: ";
std::cin >> choice;
switch (choice)
{
case 0:
std::cout << "bye!" << std::endl; break;
case 1:
myLobby.add(); break;
case 2:
myLobby.print(); break;
}
} while (choice != 0);
std::cin.get();
return 0;
}
#include <iostream>
#include <string>
template<typename T>
class LinkedList {
private:
struct Node;
Node *head, *tail;
public:
LinkedList() : head(new Node), tail(head) {}
~LinkedList() {
Node *node = head;
while (node != nullptr) {
Node *temp = node;
node = node->next;
delete temp;
}
}
void add();
void print();
};
template<typename T>
struct LinkedList<T>::Node {
T value;
Node *next = nullptr;
};
template<typename T>
void LinkedList<T>::add() {
std::cout << "Tell me the player's name: ";
std::cin >> tail->value;
tail = tail->next = new Node;
}
template<typename T>
void LinkedList<T>::print() {
for (Node *node = head;
node->next != nullptr;
node = node->next) {
std::cout << node->value
<< " is in the lobby\n";
}
}
int main() {
LinkedList<std::string> myLobby;
int choice;
do {
std::cout << "What you wanna do: ";
std::cin >> choice;
switch (choice) {
case 0:
std::cout << "bye!" << std::endl;
break;
case 1:
myLobby.add();
break;
case 2:
myLobby.print();
break;
}
} while (choice != 0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment