-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVECTOR.CPP
108 lines (92 loc) · 1.8 KB
/
VECTOR.CPP
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
// Vector.cpp: implementation of the CVector class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Virtual Robot.h"
#include "Vector.h"
#include "listexception.h"
#include "vectorelement.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CVector::CVector() : firstPtr(0), lastPtr(0)
{
}
CVector::~CVector()
{
CVectorElement* curPtr = firstPtr, *tmp;
if(!IsEmpty())
{
while(curPtr != 0)
{
tmp = curPtr;
curPtr = curPtr->nextPtr;
delete tmp;
}
}
}
void CVector::Append(const int& data)
{
CVectorElement* newPtr = new CVectorElement(data);
if(IsEmpty())
firstPtr = lastPtr = newPtr;
else
{
lastPtr->nextPtr = newPtr;
lastPtr = newPtr;
}
}
void CVector::Prepend(const int& data)
{
CVectorElement* newPtr = new CVectorElement(data);
if(IsEmpty())
firstPtr = lastPtr = newPtr;
else
{
newPtr->nextPtr = firstPtr;
firstPtr = newPtr;
}
}
int CVector::First() const
{
if(IsEmpty())
throw CListException(LIST_EMPTY);
return firstPtr->GetData();
}
int CVector::Last() const
{
if(IsEmpty())
throw CListException(LIST_EMPTY);
return lastPtr->GetData();
}
bool CVector::IsEmpty() const
{
return (firstPtr==0);
}
void CVector::Clear()
{
CVectorElement* curPtr = firstPtr, *tmp;
if(!IsEmpty())
{
while(curPtr != 0)
{
tmp = curPtr;
curPtr = curPtr->nextPtr;
delete tmp;
}
firstPtr = lastPtr = 0;
}
}
CVectorElement* CVector::NewNode(const int& P)
{
CVectorElement* newPtr= new CVectorElement(P);
if(!newPtr)
throw CListException(LIST_OUT_OF_MEMORY);
newPtr->nextPtr = 0;
return newPtr;
}