Repository navigation
Expand file tree
/
Copy pathlist.cpp
More file actions
110 lines (97 loc) · 2.19 KB
/
Copy pathlist.cpp
File metadata and controls
110 lines (97 loc) · 2.19 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
#ifndef _LIST_
#define _LIST_
#include <iostream>
template <typename Type>
struct Node {
Type data;
Node* next;
};
template <typename Type>
class List{
private:
Node<Type>* head;
public:
List(); //constructor
~List(); //destructor
void insert(Type); //inserts at head
void insert(Type, int); //inserts at given position
Type remove(); //removes at head of list
Type remove(int); //removes at given position
int search(Type); //returns position of given element, returns -1 if not found
void print(); //prints list
int size(); //returns size
};
template<typename Type>
List<Type>::List(){
head = null;
}
template<typename Type>
List<Type>::~List(){
Node<Type> *temp;
while(head->next){
temp = head;
head = head->next;
delete temp;
}
}
template<typename Type>
void List<Type>::insert(Type value){
Node<Type> *n = new Node<Type>();
n->data = value;
n->next = head;
head = n;
}
template<typename Type>
void List<Type>::insert(Type value, int position){
//TODO:
}
template<typename Type>
Type List<Type>::remove(){
if(head != null){
Type t = head->data;
Node<Type>* temp = head;
head = head->next;
delete temp;
return t;
}
}
template<typename Type>
Type List<Type>::remove(int position){
//TODO
}
template<typename Type>
int List<Type>::search(Type value){
Node<Type> *temp = head;
int position = 0;
bool found = false;
while(temp->next){
if(temp->data == value){
found - true;
break;
}
else{
temp = temp->next;
}
position++;
}
delete temp;
if found == true return position;
else return -1;
}
template<typename Type>
void List<Type>::print(){
Node<Type> *temp = head;
while(temp->next){
std::cout << temp->data << " " << std::endl;
}
}
template<typename Type>
int List<Type>::size(){
Node<Type> *temp = head;
int size = 0;
while(temp->next){
size++;
}
return size;
}
#endif