Re: Simple List Access by Index
On Dec 5, 9:34 pm, serkan.ka...@gmail.com wrote:
I've created these simple Node and List classes, but I want to be able
to do something like this: L[0] = 5; L[1] = 10; L[2] = 20; etc.... How
would I need to overload my operator= in the List class? I'm
relatively new to C++.
No need to overload = in list since L[0] is not a list, its a node.
Your code isn't using a single-linked list. You are allocating an
array:
top = new Node[num];
So what you have is more like a stack which isn't dynamic. tracking
next is therefore pointless.
If you wanted a linked list, you'd have to allocate a node each time
you push an element (a list can allocate elements in non-contiguous
space). Then each node would store the next node's address in the list
of elements to facilitate traversal. That isn't what you are doing.
See below for your Stack, make Node an embedded type, it needs not
present an interface to the outside world yet the container class gets
full access. If you template the container, you can store anything.
Templates are simple and bring many dividends.
#include <iostream>
using namespace std;
class Node
{
friend ostream& operator<<(ostream& out, const Node& N)
{
cout << N.data << " --> ";
}
int data;
Node* next;
public:
Node() {}
Node(int d, Node* n)
{
data=d; next=n;
}
~Node() {}
void setData(int d) { data=d; }
int getData() { return data; }
void printData() { cout << data << endl; }
int operator=(const Node& N) { return N.data; }
};
class List
{
Node N;
Node* top;
int num_;
public:
List(int num): num_(num) //create list
{
top = new Node[num];
}
~List() //destroy list
{
delete[] top;
}
Node& operator[](int i) {return top[i]; }
void operator=(Node& N)
{
//code to be placed here...
}
void print()
{
List L(num_);
for(int i=0; i<num_; i++)
{
cout << L[i];
}
}
};
int main()
{
List L(10); //list of ten nodes
//assign values to nodes here...
L.print(); //prints the list
cout << endl;
system("pause");
return 0;
}
Thanks.
The Stack tracks size and capacity, top and end. Take a close look at
the container's copy ctor. Keep in mind that the last thing you want
is a container managing another container's elements after a copy.
#include <iostream>
#include <ostream>
#include <string>
#include <stdexcept>
template< typename T >
class Stack
{
const std::size_t Capacity;
std::size_t Size;
struct Node
{
T data;
// default ctor
Node(T d = T())
: data(d) { }
// friend op<<
friend
std::ostream&
operator<<(std::ostream& os, const Node& N)
{
os << N.data;
return os << " --> ";
}
} *top;
Node* p_top;
Node* p_end;
public:
Stack(std::size_t sz)
: Capacity(sz), Size(0), p_top(0), p_end(0)
{
p_end = p_top = new Node[Capacity];
}
Stack( const Stack& copy ) : Capacity(copy.Capacity)
{
Size = copy.Size;
p_end = p_top = new Node[Capacity];
for(Node* p = copy.p_top; p != copy.p_end; ++p, ++p_end)
{
*p_end = *p;
}
}
~Stack() { delete [] p_top; }
// member functions
void push_back(const Node& node)
{
if(Size == Capacity)
throw std::range_error("Stack range error");
*p_end = node;
++p_end;
Size++;
}
// opertaors
Node& operator[](std::size_t i) { return p_top[i]; }
// friend op<<
friend
std::ostream&
operator<<(std::ostream& os, const Stack< T >& r_l)
{
if(r_l.Size == 0)
return os << "empty stack";
os << "Stack size: ";
os << r_l.Size << std::endl;
for( Node* p = r_l.p_top; p != r_l.p_end; ++p )
{
os << p->data;
os << " --> ";
}
return os << "p_end";
}
};
int main()
{
try
{
Stack< double > dstack(10); //list of ten nodes
std::cout << dstack<< std::endl;
dstack.push_back(1.1);
std::cout << dstack << std::endl;
dstack[0] = 0.1;
std::cout << "dstack[0] = " << dstack[0] << std::endl;
dstack.push_back(2.2);
dstack.push_back(3.3);
dstack.push_back(4.4);
std::cout << dstack<< std::endl;
Stack< std::string > sstack(8);
sstack.push_back(std::string("string 0"));
sstack.push_back(std::string("string 1"));
sstack.push_back(std::string("string 3"));
std::cout << sstack << std::endl;
Stack< std::string > copy(sstack);
std::cout << copy << std::endl;
}
catch( const std::exception& e )
{
std::cout << "Error: ";
std::cout << e.what() << std::endl;
}
}
/*
empty stack
Stack size: 1
1.1 --> p_end
dstack[0] = 0.1 -->
Stack size: 4
0.1 --> 2.2 --> 3.3 --> 4.4 --> p_end
Stack size: 3
string 0 --> string 1 --> string 3 --> p_end
// copy
Stack size: 3
string 0 --> string 1 --> string 3 --> p_end
*/
Finally, consider studying std::vector, std::list, std::stack and
std::deque.
study iterators too.
Your goal is to write code without pointers unless absolutely
necessary.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]