No laughs, please
. I'm attempting to learn this stuff, but I don't understand some of the definitions. The below code was copied off of daniweb from 2004. I have a "test" that I'm taking where they don't expect me to be able to write C++ code, but I don't want to look completely brain dead either. The question goes something like make a class that does linked lists. I searched and found this.
Anywho, I want to follow this code so that I'll understand it, I'd rather not copy it directly. Please correct me where I'm wrong.
First, the <strong class="highlight". . . I have a vague understanding of what strong and weak does: strong means that we want to associate this class more closely (that'd be, we inherit from this class more than "???normal???"), where weak would me we have a loose association with the parent class, but we will probably implement most of the methods ourselves.
Second, the "highlight" class is a standard class defined in iostream?
From here, I think I understand the rest of the code. Basically, we define a "structure" that has elements we cannot touch from the OUTSIDE world, first and mySize. The public elements of the "structure" are data and next, which the user would presumably pass to us in some of the public methods.
Code:
#ifndef LINKEDLIST
#define LINKEDLIST
#include<iostream.h>
template<typename DataType>
<strong class="highlight">class</strong> LinkedList
{
private:
<strong class="highlight">class</strong> Node
{
public: // Makes it accessible (public) by <strong class="highlight">class</strong> Node
DataType data;
Node *next;
};
Node *first;
int mySize;
LinkedList::deleteLL()
{
Node *tempP=first, *disposeP;
while (tempP!=0)
{
disposeP=tempP;
tempP=tempP->next;
delete disposeP;
}
return;
}
Past this point, there is another public{ } set that has what I believe to be a constructor
Code:
LinkedList::LinkedList()
{
mySize=0;
first=0;
}
the destructor (??? which is _REQUIRED_ for every class???)
Code:
LinkedList::~LinkedList()
{
deleteLL();
}
and then the externally called methods (more comments below)
Code:
LinkedList& LinkedList::operator=(const LinkedList& s)
{
if (this!=&s)
{
deleteLL();
mySize=s.mySize;
first=copyLL(s.first)
}
return *this;
}
bool LinkedList::empty()
{
return (first==0);
}
void LinkedList::insert(DataType item, unsigned pos)
{
if (pos>mySize+1)
{
cout<<"Illegal position to insert:"<<pos<<endl;
return;
}
mySize++
Node* newNode=newNode;
newNode->data=item;
Node* prev=first;
for (int i=1, i<pos, i++)
prev=prev->next;
newNode->next=prev->next;
prev->next=newNode;
}
void LinkedList::delete (dataType item)
{
mySize--;
Node* tempP=first;
Node* prev=0;
while (tempP!=0 && tempP->data!=item)
{
prev=tempP;
tempP=tempP->next;
}
if (tempP!=0 && tempP->data==item)
{
prev->next=tempP->next;
delete tempP;
}
else
cout<<"Item to delete not found"<<endl;
return;
}
int LinkedList::locate(DataType item)
{
int position=0;
Node* ptr=first;
while(ptr->data<item &&ptr!=0)
{
position++;
ptr=ptr->next;
}
return position
}
void LinkedList::traverse()
{
Node *temp=first;
while (tempP!=0)
{
Process(tempP->data);
tempP=tempP->next;
}
return;
}
};
This is most confusing to me. From what I understand about C++, this would be the portion that overloads the "=" operator, but then I get into the weeds and I just don't understand. Ah, wait, I see, there is some funky indention in that code. Is it standard to indent the other methods from the overloading methods?
So, to use one of these methods, one would simply call something like:
Code:
LinkedList node = new LinkedList;
node.insert(someitem, 0);
Or, am I way off.
Any help would be appreciated!