list.cpp
1.33 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
#include <iostream>
using namespace std;
#include "list.h"
list::list ()
{
head = NULL;
current = NULL;
}
list::~list ()
{
while (head)
{
node *t = head->next;
delete head;
head = t;
};
}
void
list::insert (int a)
{
node *t = new node;
t->next = head;
head = t;
head->val = a;
}
void
list::goToHead ()
{
current = head;
}
int
list::getCurrentData ()
{
return current->val;
}
void
list::advance ()
{
current = current->next;
}
bool
list::moreData ()
{
if (current)
return true;
else
return false;
}
list::list (const list & l)
{
current=NULL;
node *src, **dst;
head = NULL;
src = l.head;
dst = &head;
while (src)
{
*dst = new node;
(*dst)->val = src->val;
(*dst)->next = NULL;
if(src==l.current)
current=*dst;
src = src->next;
dst = &((*dst)->next);
}
}
list & list::operator= (const list & l)
{
if (&l == this)
return *this;
current=NULL;
while (head)
{
node *t = head->next;
delete head;
head = t;
};
node *src, **dst;
head = NULL;
src = l.head;
dst = &head;
while (src)
{
*dst = new node;
(*dst)->val = src->val;
(*dst)->next = NULL;
if(src==l.current)
current=*dst;
src = src->next;
dst = &((*dst)->next);
}
return *this;
}