forked from ATinyBanana233/HashTable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.cpp
More file actions
executable file
·150 lines (138 loc) · 3.26 KB
/
linkedlist.cpp
File metadata and controls
executable file
·150 lines (138 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//CMPT 225 assignment 5
//Author: Bei Bei Li
//implementation for linkedlist.h
#pragma once
#include <cstdlib>
#include <string>
#include <vector>
#include "linkedlist.h"
using namespace std;
// helper method for deep copy
void LinkedList::CopyList(const LinkedList& ll)
{
if (size){ //if exsit, clear
RemoveAll();
}
if (ll.size && ll.head){ //if ll exist
Node* L = ll.head;
head = new Node(L->data, L->next); //creating deep copy
L = L->next;
Node* current = head;
while (L != NULL){
current->next = new Node(L->data, L->next);
L = L->next; //move L to copy
current = current->next; //move current
}
}
size = ll.size;
}
// default constructor
LinkedList::LinkedList()
{
head = NULL;
size = 0;
}
// copy constructor
// deep copies source list
LinkedList::LinkedList(const LinkedList& ll)
{
head = NULL;
size = 0;
CopyList(ll);
}
// destructor
LinkedList::~LinkedList()
{
RemoveAll();
}
// overloaded assignment operator
// deep copies source list after deallocating existing memory (if necessary)
LinkedList& LinkedList::operator=(const LinkedList& ll)
{
if (this != &ll) { // if not equal then change
//RemoveAll();
CopyList(ll);
}
return *this;
}
// Inserts an item at the front of the list
void LinkedList::Insert(string s)
{
//Node* oldhead = head;
Node* ins = new Node(s, head); //create, oldhead becomes ptr next
if (ins != NULL){
head = ins; //set head
size++;
}
}
// Removes an item from the list.
// Returns true if item successfully removed
// False if item is not found
bool LinkedList::Remove(string s)
{
if (head == NULL){
return false;
}
if (head->data == s){ //if head is to be removed, ptr next becomes head
Node* oldhead = head;
head = head->next;
delete oldhead;
size--;
return true;
}
Node* current = head->next; //if in the linked list, set prev node's next ptr to current's next
Node* prev = head;
while (current != NULL){
if (current->data == s){
prev->next = current->next;
delete current;
size--;
return true;
}
current = current->next;
prev = prev->next;
}
return false;
}
// Removes all items from the list, calls helper function
void LinkedList::RemoveAll()
{
Node* current = head;
while (current != NULL){ //traverse to remove
head = head->next;
delete current;
current = head;
}
head = NULL;
size = 0;
}
// Checks if an item exists in the list
bool LinkedList::Contains(string s) const
{
if (head != NULL){
Node* current = head;
while (current != NULL){
if (current->data == s){
return true;
}
current = current->next;
}
}
return false;
}
// Return the number of items in the list
unsigned int LinkedList::Size() const
{
return size;
}
// Returns a vector containing the list contents using push_back
vector<string> LinkedList::Dump() const
{
vector<string> result;
Node* current = head;
while (current != NULL){
result.push_back(current->data); //using vector method to push_back
current = current->next; //traverse to push_back next
}
return result;
}