#ifndef _LINKED_LIST_
#define _LINKED_LIST_
#include
struct Node{
// Data Link
void *data;
// Pointers to previous and next
Node *next;
};
class LinkedList
{
private:
// Keeps count of the number of list elements
int listCount;
// Keeps track of the head of the list for traversal
Node *head;
public:
LinkedList();
~LinkedList();
// mutators
void add(const char ch);
bool del(char ch);
void listDecr(void);
void listIncr(void);
// accessors
std::string out(void);
bool find(char ch);
Node* getHead(void);
friend std::ostream& operator<<(std::ostream& out, LinkedList& list);
};
#endif // _LINKED_LIST_
#include "linkedlist.h"
using namespace std;
////////////////////////////////////////
LinkedList::~LinkedList(){
}
////////////////////////////////////////
ostream& operator<<(ostream &os, LinkedList& list){
os << list.out();
return os;
}
////////////////////////////////////////
LinkedList::LinkedList(){
this->head = new Node;
head->next = NULL;
head->data = NULL;
}
////////////////////////////////////////
Node* LinkedList::getHead(void){
return this->head;
}
////////////////////////////////////////
string LinkedList::out(void){
Node *current = this->getHead(); // sets to head for list traversal
string buff;
if(listCount == 1)
{
buff = *(static_cast(current->data));
}
else
{
while (current != NULL)
{
buff += *(static_cast(current->data));
current = current->next;
}
}
return buff;
}
////////////////////////////////////////
void LinkedList::listIncr(void){
this->listCount++;
}
////////////////////////////////////////
void LinkedList::listDecr(void){
this->listCount--;
}
////////////////////////////////////////
void LinkedList::add(char ch){
Node *current = this->getHead(); // sets to head for list traversal
Node *temp = new Node;
temp->data = NULL;
temp->next = NULL;
char *szBuff = &ch;
temp->data = static_cast (szBuff);
if(this->listCount == 0)
{
current->data = static_cast (szBuff);
this->listIncr();
}
else
{
while(current->next != NULL)
{
current = current->next;
}
current->next = temp;
this->listIncr();
}
}
////////////////////////////////////////
bool LinkedList::find(char ch){
Node *current = this->getHead(); // sets to head for list traversal
char *szBuff; // for casting void pointer to, before dereferencing and comparing
while(current != NULL)
{
szBuff = (static_cast (current->data));
if(*szBuff = ch)
{
return true;
}
current = current->next;
}
return false;
}
////////////////////////////////////////
bool LinkedList::del(char ch){
Node *current = this->getHead(); // sets to head for list traversal
char *szBuff;
while(current != NULL)
{
szBuff = (static_cast (current->next->data));
if(*szBuff = ch)
{
current->next = current->next->next;
this->listDecr();
return true;
}
current = current->next;
}
return false;
}
////////////////////////////////////////