- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.cpp
More file actions
Latest commit
163 lines (130 loc) · 3.09 KB
/
Copy pathLinkedList.cpp
File metadata and controls
163 lines (130 loc) · 3.09 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
//
// LinkedList.cpp
// text2
//
// Created by Moumen Mohamed on 6/16/14.
// Copyright (c) 2014 Moumen Mohamed. All rights reserved.
//
#include"LinkedList.h"
/**
* Print the contents of the list
*/
voidLinkedList::Print() {
// Temp pointer
Node *tmp = head;
// No nodes
if ( tmp == NULL ) {
cout << "EMPTY" << endl;
return;
}
// One node in the list
if ( tmp->Next() == NULL ) {
cout << "( " +tmp->Data()+ ", ";
cout << tmp->Position();
cout << ")";
cout << " --> ";
cout << "NULL" << endl;
}
else {
// Parse and print the list
do {
cout << "( " +tmp->Data()+ ", ";
cout << tmp->Position();
cout << ")";
cout << " --> ";
tmp = tmp->Next();
}
while ( tmp != NULL );
cout << "NULL" << endl;
}
}
/**
* Append a node to the linked list
*/
intLinkedList::Append(string data, int position) {
int pos=0;
// Create a new node
Node* newNode = newNode();
newNode->setNode(data,position);
newNode->SetNext(NULL);
// Create a temp pointer
Node *tmp = head;
if ( tmp != NULL ) {
// Nodes already present in the list
// Parse to end of list
while ( tmp->Next() != NULL ) {
tmp = tmp->Next();
pos++;
}
// Point the last node to the new node
tmp->SetNext(newNode);
pos++;
}
else {
// First node in the list
head = newNode;
}
return pos;
}
/**
* Delete a node from the list
*/
voidLinkedList::Delete(string data) {
// Create a temp pointer
Node *tmp = head;
if ( tmp == NULL )
return;
// Last node of the list
if ( tmp->Next() == NULL ) {
delete tmp;
head = NULL;
}
else {
// Parse thru the nodes
Node *prev;
do {
if ( tmp->Data() == data ) break;
prev = tmp;
tmp = tmp->Next();
} while ( tmp != NULL );
// Adjust the pointers
prev->SetNext(tmp->Next());
// Delete the current node
delete tmp;
}
}
boolLinkedList::Exists(string data) {
// Create a temp pointer
Node *tmp = head;
if ( tmp == NULL )
returnfalse;
else {
// Parse thru the nodes
Node *prev;
do {
if ( tmp->Data() == data )
returntrue;
prev = tmp;
tmp = tmp->Next();
} while ( tmp != NULL );
}
returnfalse;
}
intLinkedList::getPositionValue(string data) {
// Create a temp pointer
int pos=-1;
Node *tmp = head;
if ( tmp == NULL )
return -1;
else {
// Parse thru the nodes
Node *prev;
do {
if ( tmp->Data() == data )
return tmp->Position();
prev = tmp;
tmp = tmp->Next();
} while ( tmp != NULL );
}
return -1;
}