|
-
November 2nd, 2010, 04:51 PM
#1
C++ LinkedList help
Hi everyone.....I need some help. I'm implementing some older code and i'm trying to
work the bugs. This is my first year programming so be gentle..lol.
My problem come from when I try to make Linked list an int
LinkedList <Int> List;
Here's my program:
[code]
//////////////////////////////////////////////////////////////////
// //
// CST 211 //
// //
// Program By Brian Probert //
// //
// Date: 10/23/2010 //
// //
//////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
template <class T>
class LinkedList;
template <class T>
class ListElement
{
T datum;
ListElement* next;
ListElement (T const&, ListElement*);
public:
T const& Datum () const;
ListElement const* Next () const;
friend class LinkedList<T>; //richw: missing the "class" keyword
};
template <class T>
class LinkedList
{
ListElement<T>* head;
ListElement<T>* tail;
public:
LinkedList ();
~LinkedList ();
LinkedList (LinkedList const&);
LinkedList& operator = (LinkedList const&);
ListElement<T> const* Head () const;
ListElement<T> const* Tail () const;
bool IsEmpty () const;
T const& First () const;
T const& Last () const;
void Prepend (T const&);
void Append (T const&);
void Extract (T const&);
void Purge ();
void InsertAfter (ListElement<T> const*, T const&);
void InsertBefore (ListElement<T> const*, T const&);
};
//////////////////////////////////////////////////////////////////
// //
// CST 211 //
// //
// Program By Brian Probert //
// //
// Date: 10/23/2010 //
// //
//////////////////////////////////////////////////////////////////
#include "LinkedList.h"
//#include "Exceptional.h"
/***************************************************/
// class member function definitions //
/***************************************************/
template <class T>
ListElement<T>::ListElement (
T const& _datum, ListElement<T>* _next) :
datum (_datum), next (_next)
{}
template <class T>
T const& ListElement<T>: atum () const
{ return datum; }
template <class T>
ListElement<T> const* ListElement<T>::Next () const
{ return next; }
/***************************************************/
// class defualt destructor definition //
/***************************************************/
template <class T>
LinkedList<T>::LinkedList () :
head (0),
tail (0)
{}
/***************************************************/
// class destructor and purge member function //
/***************************************************/
template <class T>
void LinkedList<T>::Purge ()
{
while (head != 0)
{
ListElement<T>* const tmp = head;
head = head->next;
delete tmp;
}
tail = 0;
}
template <class T>
LinkedList<T>::~LinkedList ()
{ Purge (); }
/***************************************************/
// class accessor function definitions //
/***************************************************/
template <class T>
ListElement<T> const* LinkedList<T>::Head () const
{ return head; }
template <class T>
ListElement<T> const* LinkedList<T>::Tail () const
{ return tail; }
template <class T>
bool LinkedList<T>::IsEmpty () const
{ return head == 0; }
/***************************************************/
// class first and last function definitions //
/***************************************************/
template <class T>
T const& LinkedList<T>::First () const
{
if (head == 0)
throw domain_error ("list is empty");
return head->datum;
}
template <class T>
T const& LinkedList<T>::Last () const
{
if (tail == 0)
throw domain_error ("list is empty");
return tail->datum;
}
/***************************************************/
// class prepend function definitions //
/***************************************************/
template <class T>
void LinkedList<T>::Prepend (T const& item)
{
ListElement<T>* const tmp = new ListElement<T> (item, head);
if (head == 0)
tail = tmp;
head = tmp;
}
/***************************************************/
// class append function definitions //
/***************************************************/
template <class T>
void LinkedList<T>::Append (T const& item)
{
ListElement<T>* const tmp = new ListElement<T> (item, 0);
if (head == 0)
head = tmp;
else
tail->next = tmp;
tail = tmp;
}
/***************************************************/
// class copy constructor definitions //
/***************************************************/
template <class T>
LinkedList<T>::LinkedList (LinkedList<T> const& linkedList) :
head (0),
tail (0)
{
ListElement<T> const* ptr;
for (ptr = linkedList.head; ptr != 0; ptr = ptr->next)
Append (ptr->datum);
}
template <class T>
LinkedList<T>& LinkedList<T>: perator = (
LinkedList<T> const& linkedList)
{
if (&linkedList != this)
{
Purge ();
ListElement<T> const* ptr;
for (ptr = linkedList.head; ptr != 0; ptr = ptr->next)
Append (ptr->datum);
}
return *this;
}
/***************************************************/
// class extract function definitions //
/***************************************************/
template <class T>
void LinkedList<T>::Extract (T const& item)
{
ListElement<T>* ptr = head;
ListElement<T>* prevPtr = 0;
while (ptr != 0 && ptr->datum != item)
{
prevPtr = ptr;
ptr = ptr->next;
}
if (ptr == 0)
throw invalid_argument ("item not found");
if (ptr == head)
head = ptr->next;
else
prevPtr->next = ptr->next;
if (ptr == tail)
tail = prevPtr;
delete ptr;
}
/***************************************************************/
// class insert before and after function definitions //
/***************************************************************/
template <class T>
void LinkedList<T>::InsertAfter (
ListElement<T> const* arg, T const& item)
{
ListElement<T>* ptr = const_cast<ListElement<T>*> (arg);
if (ptr == 0)
throw invalid_argument ("invalid position");
ListElement<T>* const tmp =
new ListElement<T> (item, ptr->next);
ptr->next = tmp;
if (tail == ptr)
tail = tmp;
}
template <class T>
void LinkedList<T>::InsertBefore (
ListElement<T> const* arg, T const& item)
{
ListElement<T>* ptr = const_cast<ListElement<T>*> (arg);
if (ptr == 0)
throw invalid_argument ("invalid position");
ListElement<T>* const tmp = new ListElement<T> (item, ptr);
if (head == ptr)
head = tmp;
else
{
ListElement<T>* prevPtr = head;
while (prevPtr != 0 && prevPtr->next != ptr)
prevPtr = prevPtr->next;
if (prevPtr == 0)
throw invalid_argument ("invalid position");
prevPtr->next = tmp;
}
}
//////////////////////////////////////////////////////////////////
// //
// CST 211 //
// //
// Program By Brian Probert //
// //
// Date: 10/23/2010 //
// //
//////////////////////////////////////////////////////////////////
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include "LinkedList.h"
int main ()
{
using namespace std;
LinkedList <Int> Query;
system("PAUSE"); // Keep console window open
return 0;
}
[]
-
November 2nd, 2010, 05:58 PM
#2
Re: C++ LinkedList help
what is Int? did you mean int? Also please use code tags.Your code is almost unreadable.
-
November 2nd, 2010, 06:34 PM
#3
Re: C++ LinkedList help
Sorry thats a int but i have the same problem...any ideas.
-
November 3rd, 2010, 12:23 AM
#4
Re: C++ LinkedList help
 Originally Posted by BRVPRO
Sorry thats a int but i have the same problem...any ideas.
Please reformat your code by using code tags! It is unreadable without them.
Secondly, it is only your first year, and your assignment is to write a linked list in C++? Is this a data structures course, or is this supposed to be a general C++ course?
Writing a linked list class in C++ is not for the beginner -- even intermediate programmers don't get it right, even though they may have an understanding of what a linked list is. There are too many other issues (mostly dynamic memory management, exception safety, etc.) that come into play when writing a linked list in C++ -- it isn't just the theory of what a linked list is that is a problem. Did your teacher go over these C++ issues with you, or was the assignment just thrown at you to write a linked list?
Third, what is "Int" in your code? Is this supposed to be "int"?
Do you know that most C++ programmers that work in the field of creating software use std::list, and not write their own linked list class? So if you are trying to implement older code and there is no need for you to write your own linked list maybe that's your problem -- there is no need to implement it. Replace it (with std::list).
Code:
#include <list>
int main()
{
std::list<int> Queue;
}
That code is equivalent to that entire code that you posted.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; November 3rd, 2010 at 12:31 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|