154 lines
3.0 KiB
C++
154 lines
3.0 KiB
C++
|
#include <iostream>
|
||
|
#include <new>
|
||
|
#include "list.h"
|
||
|
|
||
|
|
||
|
Node list;
|
||
|
List::List()
|
||
|
{
|
||
|
head = NULL;
|
||
|
num=0;
|
||
|
}
|
||
|
|
||
|
List::~List()
|
||
|
{
|
||
|
delete head;
|
||
|
}
|
||
|
|
||
|
void List::Append(string newword)
|
||
|
{
|
||
|
Node* tempPtr=new Node;
|
||
|
tempPtr->word= newword;
|
||
|
tempPtr->next= NULL;
|
||
|
//num++;
|
||
|
if (head==NULL)
|
||
|
{
|
||
|
head=tempPtr;
|
||
|
num++;
|
||
|
return;
|
||
|
//head->next=NULL;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
Node *current=head;
|
||
|
while(current ->next) current=current->next;
|
||
|
current->next = tempPtr;
|
||
|
}
|
||
|
|
||
|
num++;
|
||
|
}
|
||
|
|
||
|
void List::InsertAt(int pos, string newword)
|
||
|
{
|
||
|
Node* prev = new Node();
|
||
|
Node* curr = new Node();
|
||
|
Node* newNode = new Node();
|
||
|
newNode->word = newword;
|
||
|
|
||
|
int tempPos = 0; // Traverses through the list
|
||
|
|
||
|
curr = head; // Initialize current to head;
|
||
|
if(head != NULL)
|
||
|
{
|
||
|
while(curr->next != NULL && tempPos != pos)
|
||
|
{
|
||
|
prev = curr;
|
||
|
curr = curr->next;
|
||
|
tempPos++;
|
||
|
}
|
||
|
if(pos==0)
|
||
|
{
|
||
|
|
||
|
}
|
||
|
else if(curr->next == NULL && pos == tempPos+1)
|
||
|
{
|
||
|
Append(newword);
|
||
|
// Call function to append
|
||
|
}
|
||
|
else if(pos > tempPos+1 || pos>num || pos<0) throw ListBadPosition();
|
||
|
//Position not valid
|
||
|
else
|
||
|
{
|
||
|
prev->next = newNode;
|
||
|
newNode->next = curr;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
head = newNode;
|
||
|
newNode->next=NULL;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
void List::Delete(string someword)
|
||
|
{
|
||
|
Node *prev=head;
|
||
|
Node *current=head->next;
|
||
|
int wordNotFound=0;
|
||
|
while (current != NULL)
|
||
|
{
|
||
|
// If current word is in list, break out of loop
|
||
|
if(current->word == someword) break;
|
||
|
else
|
||
|
{
|
||
|
prev=current;
|
||
|
prev=current->next;
|
||
|
}
|
||
|
}
|
||
|
if(current==NULL) throw ListNotFound();
|
||
|
// Error handling: if current word is not in list, throw ListNotFound
|
||
|
else
|
||
|
{
|
||
|
prev->next = current ->next;
|
||
|
delete current;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
void List::Replace(string oldword, string newword)
|
||
|
{
|
||
|
Node *current=head;
|
||
|
int oldWordNotFound=0;
|
||
|
while (current != NULL)
|
||
|
{
|
||
|
// if oldword is found in list, replace oldword with newword
|
||
|
if(current->word==oldword)
|
||
|
{
|
||
|
current->word = newword;
|
||
|
return;
|
||
|
}
|
||
|
// if oldword not= to current word, increment wordNotFound
|
||
|
oldWordNotFound++;
|
||
|
current = current -> next;
|
||
|
if(oldWordNotFound==num)
|
||
|
break;
|
||
|
}
|
||
|
// if oldWordNotFound==num, throw ListNotFound
|
||
|
|
||
|
throw ListNotFound();
|
||
|
}
|
||
|
|
||
|
int List::Length() const
|
||
|
{
|
||
|
return num;
|
||
|
}
|
||
|
|
||
|
bool List::Find(string someword) const
|
||
|
{
|
||
|
Node *current=head;
|
||
|
int wordNotFound=0;
|
||
|
while (current != NULL)
|
||
|
{
|
||
|
// if current word does not equal someword, increment wordNotFound
|
||
|
if(current->word != someword)
|
||
|
{
|
||
|
wordNotFound++;
|
||
|
}
|
||
|
// If current word is in list, return true
|
||
|
if(current->word == someword) return true;
|
||
|
current = current->next;
|
||
|
}
|
||
|
// if wordNotFound == num, return false
|
||
|
if(wordNotFound == num) return false;
|
||
|
}
|