need help please
May 3, 2013 at 5:51pm UTC
class point2d
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
point2d::point2d()
{
x = y = 0;
}
point2d::point2d(int initx, int inity)
{
setx(initx);
sety(inity);
}
int point2d::getx()
{
return x;
}
int point2d::gety()
{
return y;
}
void point2d::setx(int initx)
{
if (initx >= 0)
x = initx;
else
x = 0;
}
void point2d::sety(int inity)
{
if (inity >= 0)
y = inity;
else
y = 0;
}
bool point2d::operator ==(const point2d& p2){
if (x==p2.x && y==p2.y)
return true ;
else
return false ;
}
bool point2d::operator !=(const point2d& point2){
if (x!=point2.x || y!=point2.y)
return true ;
else
return false ;
}
point2d point2d::operator +(const point2d& p2){
point2d temp;
temp.x=x+p2.x;
temp.y=y+p2.y;
return temp;
}
point2d point2d::operator -(const point2d& p2){
point2d temp;
temp.x=x-p2.x;
temp.y=y-p2.y;
return temp;
}
ostream& operator <<(ostream& a, point2d p2){
a<<"(" <<p2.x<<"," <<p2.x<<")" ;
return a;
}
istream& operator >>(istream& a, point2d p2){
a>>p2.x>>p2.y;
return a;
}
const point2d& point2d::operator =(const point2d& p2){
if (this != &p2)
{
this ->x=p2.x;
this ->y=p2.y;
}
return *this ;
}
class linkedList (includes class point2d)
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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
template <typename T>
class LinkedList
{
public :
LinkedList();
LinkedList(LinkedList<T>& list);
virtual ~LinkedList();
void addFirst(T element);
void addLast(T element);
T getFirst() const ;
T getLast() const ;
T removeFirst() throw (runtime_error);
T removeLast();
void add(T element);
void add(int index, T element);
void clear();
bool contains(T element) const ;
T get(int index) const ;
int indexOf(T element) const ;
bool isEmpty() const ;
int lastIndexOf(T element) const ;
void remove(T element);
int getSize() const ;
T removeAt(int index);
T set(int index, T element);
Iterator<T> begin() const
{
return Iterator<T>(head);
};
Iterator<T> end() const
{
return Iterator<T>(tail->next);
};
private :
Node<T>* head;
Node<T>* tail;
int size;
};
template <typename T>
LinkedList<T>::LinkedList()
{
head = tail = NULL;
size = 0;
}
template <typename T>
LinkedList<T>::LinkedList(LinkedList<T>& list)
{
head = tail = NULL;
size = 0;
Node<T>* current = list.head;
while (current != NULL)
{
this ->add(current->element);
current = current->next;
}
}
template <typename T>
LinkedList<T>::~LinkedList()
{
clear();
}
template <typename T>
void LinkedList<T>::addFirst(T element)
{
Node<T>* newNode = new Node<T>(element);
newNode->next = head;
head = newNode;
size++;
if (tail == NULL)
tail = head;
}
template <typename T>
void LinkedList<T>::addLast(T element)
{
if (tail == NULL)
{
head = tail = new Node<T>(element);
}
else
{
tail->next = new Node<T>(element);
tail = tail->next;
}
size++;
}
template <typename T>
T LinkedList<T>::getFirst() const
{
if (size == 0)
throw runtime_error("Index out of range" );
else
return head->element;
}
template <typename T>
T LinkedList<T>::getLast() const
{
if (size == 0)
throw runtime_error("Index out of range" );
else
return tail->element;
}
template <typename T>
T LinkedList<T>::removeFirst() throw (runtime_error)
{
if (size == 0)
throw runtime_error("No elements in the list" );
else
{
Node<T>* temp = head;
head = head->next;
if (head == NULL) tail = NULL;
size--;
T element = temp->element;
delete temp;
return element;
}
}
template <typename T>
T LinkedList<T>::removeLast()
{
if (size == 0)
throw runtime_error("No elements in the list" );
else if (size == 1)
{
Node<T>* temp = head;
head = tail = NULL;
size = 0;
T element = temp->element;
delete temp;
return element;
}
else
{
Node<T>* current = head;
for (int i = 0; i < size - 2; i++)
current = current->next;
Node<T>* temp = tail;
tail = current;
tail->next = NULL;
size--;
T element = temp->element;
delete temp;
return element;
}
}
template <typename T>
void LinkedList<T>::add(T element)
{
addLast(element);
}
template <typename T>
void LinkedList<T>::add(int index, T element)
{
if (index == 0)
addFirst(element);
else if (index >= size)
addLast(element);
else
{
Node<T>* current = head;
for (int i = 1; i < index; i++)
current = current->next;
Node<T>* temp = current->next;
current->next = new Node<T>(element);
(current->next)->next = temp;
size++;
}
}
template <typename T>
void LinkedList<T>::clear()
{
while (head != NULL)
{
Node<T>* temp = head;
head = head->next;
delete temp;
}
tail = NULL;
size = 0;
}
template <typename T>
T LinkedList<T>::get(int index) const
{
if (index < 0 || index > size - 1)
throw runtime_error("Index out of range" );
Node<T>* current = head;
for (int i = 0; i < index; i++)
current = current->next;
return current->element;
}
template <typename T>
int LinkedList<T>::indexOf(T element) const
{
// Implement it in this exercise
Node<T>* current = head;
for (int i = 0; i < size; i++)
{
if (current->element == element)
return i;
current = current->next;
}
return -1;
}
template <typename T>
bool LinkedList<T>::isEmpty() const
{
return head == NULL;
}
template <typename T>
int LinkedList<T>::getSize() const
{
return size;
}
template <typename T>
T LinkedList<T>::removeAt(int index)
{
if (index < 0 || index >= size)
throw runtime_error("Index out of range" );
else if (index == 0)
return removeFirst();
else if (index == size - 1)
return removeLast();
else
{
Node<T>* previous = head;
for (int i = 1; i < index; i++)
{
previous = previous->next;
}
Node<T>* current = previous->next;
previous->next = current->next;
size--;
T element = current->element;
delete current;
return element;
}
}
My problematic code
1 2
list.get(0)=list.get(1);
list.get(0).setx(3); list.get(0).sety(3);
class linked list is to take point2d as its type but the methods setx&sety as well as my operator= overload function wont change the values of the objects??
Im not sure where the problem is
May 3, 2013 at 6:44pm UTC
The prototype of your get() method is T get(int index) const ;
.
In other words, it returns a copy of the stored element.
If you want to modify the content of a stored element, you need to create a non const method that retruns a reference.
May 3, 2013 at 7:49pm UTC
I've emailed my teacher for reassurance but I believe that he doesn't want us to alter linkedlist, but rather change our main and point2d class functions to work with it (point2d was created for a previous project)
May 3, 2013 at 8:29pm UTC
but rather change our main and point2d class functions to work with it
The linked list does not give you access to the stored elements, so there's no way you can modify them.
I see two possibilities:
- If you store point2d objects, you have to make a copy, modify the copy, erase the original from the list and insert the copy at the position the original was.
- Store pointers to your point2d objects instead of the objects. It's ugly but it will work. To ease memory management you could use std::unique_ptr or std::shared_ptr.
May 3, 2013 at 9:16pm UTC
I'm not that great with pointers and that's a lot of the problem I'm having with this project but uhm...
1 2
point2d *point1=new point2d(15,30);
cout<<*point1;
this code results in an object of point2d with x=15, and y=15?
sorry to be such a noob but im just lost..
implementation of constructor and set methods below
1 2 3 4 5 6
point2d::point2d(int initx, int inity)
{
setx(initx);
sety(inity);
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void point2d::setx(int initx)
{
if (initx >= 0)
this ->x = initx;
else
this ->x = 0;
}
void point2d::sety(int inity)
{
if (inity >= 0)
this ->y = inity;
else
this ->y = 0;
}
May 3, 2013 at 9:29pm UTC
In your point2d::operator<<() you made a mistake and output twice the X coordinate.
May 3, 2013 at 9:33pm UTC
lol... i feel like an idiot
May 3, 2013 at 10:06pm UTC
my teacher says that it's not necessary but it's okay to alter LinkedList..
so i tried doing this (just adding a reference operator in front of returned data)
1 2 3 4 5 6 7 8 9 10 11 12
template <typename T>
T LinkedList<T>::get(int index)
{
if (index < 0 || index > size - 1)
throw runtime_error("Index out of range" );
Node<T>* current = head;
for (int i = 0; i < index; i++)
current = current->next;
return &(current->element);
}
these are the errors that i get:
In file included from main.cpp:20:0:
main.cpp:42:19: instantiated from here
LinkedList.h:280:28: error: conversion from ‘point2d*’ to non-scalar type ‘point2d’ requested
once again i apologize for being so noobish at this.. according to him we got screwed on the academic calendar and we havent went over much of this material
May 3, 2013 at 10:10pm UTC
is it possible to make it a friend function to give it access to the private members?
May 3, 2013 at 10:24pm UTC
You need not change the body of the function, only its prototype:
1 2 3 4 5 6 7 8 9 10 11 12
template <typename T>
T& LinkedList<T>::get(int index)
{
if (index < 0 || index > size - 1)
throw runtime_error("Index out of range" );
Node<T>* current = head;
for (int i = 0; i < index; i++)
current = current->next;
return current->element;
}
May 3, 2013 at 10:29pm UTC
B-e-a-utiful :D, worked perfect thanks!
all these pointers and reference variable are making my brain hurt >.>
im going to keep this forum up atm because im not done with the project, but thanks again
Topic archived. No new replies allowed.