-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.cpp
More file actions
244 lines (200 loc) · 4.86 KB
/
Copy pathlinkedlist.cpp
File metadata and controls
244 lines (200 loc) · 4.86 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//
// linkedlist.cpp
// 340HW
//
// Created by ShanCho on 11/1/17.
// Copyright © 2017 ShanCho. All rights reserved.
//
#include <iostream>
//#include <stdio.h>
#include "linkedlist.h"
using namespace std;
//using namespace std;
//void add ( int N ) Add N to the front of the linked list.
void LinkedList::add(int N){
Node* temp = new Node; // point to last value .. the value just created .. assign new node
temp->value = N; // temp head is address not value
temp->next = head;
head = temp;
}
//bool remove ( int N ) Remove the first instance of N from the list, then return true. If the N was not found, return false.
bool LinkedList::remove(int N){
Node * temp = head;
Node * prev = head;
if(temp->value == N)
{
head = head->next;
delete(temp);
return ture;
}
else
{
temp = temp->next;
while(temp)
/*
//bool remove ( int N ) Remove the first instance of N from the list, then return true. If the N was not found, return false.
bool LinkedList::remove(int N){
if(head != nullptr){
if(head->value == N)
{
head = head->next;
--length;
return true;
}
else{
Node * temp1 = head;
while(temp1->next != nullptr )
{
if (temp1->next->value == N)
{
temp1->next = temp1->next->next;
--length;
return true;
}
temp1= temp1->next;
}
}
}
return false;
}
*/
//bool remove ( int N ) Remove the first instance of N from the list, then return true. If the N was not found, return false.
/*
bool LinkedList::remove(int N){
//bool found = 0;
Node * prev = NULL;
Node * curr = head;
if(curr != nullptr)
{
if(head->value == N)
{
head = head->next;
delete(curr);
return true;
}
else
{
// cout<<head->next->next->value;
while(curr != NULL)
{
//if(curr->value == N)
}
return false;
}
}
*/
/*
bool LinkedList::remove(int N){
Node * temp = new Node;
temp = head;
head = head->next;
delete temp;
return true;
}
else{
return false;
}
//bool remove ( int N ) Remove the first instance of N from the list, then return true. If the N was not found, return false.
bool LinkedList :: remove(int N){
Node *temp = head;
if(temp == nullptr){
return false;
}
if(temp->value == N){
head = temp->next;
length --;
free(temp);
return true;
}
while(temp->next != nullptr){
if(temp->next->value == N){
Node *next = temp->next->next;
free(temp->next);
temp->next = next;
length --;
return true;
}
temp = temp->next;
}
return false;
}
/*
// bool remove = false;
Node * temp = new Node;
temp = head;
if(temp->value == N) {
head = head -> next;
return true;
}
return 0;
}
*/
/*
//int find ( int N ) Find the first instance of N in the list and return its index. Return -1 if N was not found.
int LinkedList::find(int N)
{
Node * temp1 = head;
int find = 0;
while (temp1 != nullptr)
{
if(temp1->value == N){
return find;
}
temp1= temp1->next;
}
return -1;
}
//int count ( int N ) Return a count of the instances of N in the list.
int LinkedList::count(int N) {
Node* temp1 = head;
int count = 0;
while (temp1 != nullptr)
{
if(temp1->value == N){
count++;
}
temp1 = temp1->next;
}
return count;
}
//int at ( int N ) Return the value stored in the node at index N.
int LinkedList :: at(int N){
Node *temp = head;
int index = 0;
if(N < 0 || N > length-1){
return -1;
}
while(temp != nullptr){
if(N == index){
return temp->value;
}
temp =temp->next;
index ++;
}
return -1;
}
//int len() Return the current length of the list.
int LinkedList::len() {
return length;
}
//int len() Return the current length of the list.
int LinkedList::len()
{
Node * temp = head;
int size = 0;
while(temp != nullptr){
++size;
temp = temp->next;
}
return size;
}
*/
void LinkedList::print() {
Node * temp = new Node();
temp = head;
while (temp != nullptr) {
cout << temp->value;
temp = temp->next;
}
cout << endl;
}