#pragma once
#include <string>
using namespace std;
class node {
public:
node * next;
string data;
};
class linkedList {
private:
node * head;
public:
linkedList() {
head = NULL;
}
void addFront(string item) {
node * temp = new node;
temp->data = item;
temp->next = head;
head = temp;
}
void addBack(string item) {
node * temp = new node;
temp->data = item;
temp->next = NULL;
// empty list
if (head == NULL) {
head = temp;
}
// not empty list
else {
node * current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = temp;
}
}
// returns the first item on the list and removes it
string pop() {
string t = head -> data;
node * deleteThis = head;
head = head->next;
delete deleteThis;
return t;
}
void remove(string item) {
if (head == NULL) {
}
else if (head->next == NULL) {
head = NULL;
}
else {
node * previous = head;
node * current = head;
//cout << "in else, item = " <<item<< endl;
while (current != NULL) {
if (current->data != item) {
current = current->next;
if (previous->next != current) {
previous = previous->next;
}
}
else {
break;
}
}
cout << previous->data << " ++ " << current->data << endl;
if (current->next == NULL) {
previous->next = NULL;
}
if (previous == head) {
head = head->next;
//delete previous;
}
}
}
void display() {
cout << "\n\n== START OF DISPLAY ==" << endl;
node * zomb = head;
while (zomb != NULL) {
cout << zomb->data << endl;
zomb = zomb->next;
}
cout << "== END OF DISPLAY ==\n\n" << endl;
}
};
Comments
0 B
|👍
/👎