Linked List Program error | Member | | Join Date: Feb 2008
Posts: 33
| |
I know what I'm posting here is wired, but it's been 3 days I'm workin g on these codes, but I have no result
I post the code here
I dont wanne bother you, but if any one of you have time to read my program I appriciate it.
the program suppose to print a message on the screen -
#include <iostream>
-
#include <string>
-
using namespace std;
-
-
template <typename T>
-
class Node {
-
public:
-
Node(T newData);
-
template <typename T>
-
friend class LinkedList;
-
template <typename T>
-
friend class Iterator;
-
private:
-
T data;
-
Node* prev; //Pointer to previous node in list.
-
Node* next; //Pointer to next node in list.
-
};
-
-
template <typename T>
-
class Iterator {
-
public:
-
Iterator();
-
T get() const;
-
void forward();
-
void backward();
-
template <typename T>
-
friend class LinkedList;
-
private:
-
Node<T>* position;
-
};
-
-
template <typename T>
-
class LinkedList {
-
public:
-
LinkedList();
-
LinkedList(int numNodes, T defaultValue);
-
LinkedList(const LinkedList<T>& right);
-
~LinkedList();
-
LinkedList<T>& operator= (const LinkedList<T>& right);
-
LinkedList<T> operator+ (const LinkedList<T>& right); //operator+, concatenates two linked lists
-
Iterator<T> begin() const;
-
Iterator<T> end() const;
-
void insert(Iterator<T> iter, T value);
-
void erase(Iterator<T>& iter);
-
T& operator[] (int index);
-
T operator[] (int index) const;
-
int size() const;
-
-
void find(T value);
-
void pop_back();
-
void pop_front();
-
void push_front(T value);
-
void print();
-
void reverse();
-
-
private:
-
Node<T>* first; //Pointer to first node in list.
-
Node<T>* last; //Pointer to last node in list.
-
};
-
-
template <typename T>
-
Node<T>::Node(T newData) {
-
data = newData;
-
prev = NULL;
-
next = NULL;
-
}
-
-
template <typename T>
-
Iterator<T>::Iterator() {
-
position = NULL;
-
}
-
-
template <typename T>
-
T Iterator<T>::get() const {
-
return position->data;
-
}
-
-
template <typename T>
-
void Iterator<T>::forward() {
-
position = position->next;
-
}
-
-
//moves position backward one node
-
template <typename T>
-
void Iterator<T>::backward() {
-
position = position->prev;
-
}
-
-
-
-
//Linked LIst constructor
-
template <typename T>
-
LinkedList<T>::LinkedList() {
-
first = NULL;
-
last = NULL;
-
}
-
-
-
template <typename T>
-
LinkedList<T>::LinkedList(int numNodes, T defaultValue) {
-
first=NULL; last=NULL;
-
for (int i=1; i<=numNodes; i++)
-
insert(begin(),defaultValue);
-
}
-
-
template <typename T>
-
LinkedList<T>::LinkedList(const LinkedList<T>& right) {
-
first = NULL;
-
last = NULL;
-
Iterator<T> iter_right = right.end();
-
while (iter_right.position != NULL) {
-
insert(begin(), iter_right.get());
-
iter_right.backward();
-
}
-
}
-
-
//Linked list Destructor
-
template <typename T>
-
LinkedList<T>::~LinkedList() {
-
while (first != NULL)
-
erase(begin());
-
}
-
-
//Assignment Operator
-
template <typename T>
-
LinkedList<T>& LinkedList<T>::operator= (const LinkedList<T>& right) {
-
if (this != &right) {
-
Iterator<T> iter_left = begin();
-
while (iter_left.position != NULL) {
-
erase(iter_left);
-
iter_left = begin();
-
}
-
Iterator<T> iter_right = right.end();
-
while (iter_right.position != NULL) {
-
insert(begin(), iter_right.get());
-
iter_right.backward();
-
}
-
}
-
return *this;
-
}
-
-
-
-
//Operator + , concatenates two linked lists
-
template <typename T>
-
LinkedList<T> LinkedList<T>::operator+ (const LinkedList<T>& right)
-
{
-
LinkedList result(*this);
-
for (int i=0 ; i<right.size() ; i++)
-
result.push_back(right[i]);
-
return result;
-
}
-
-
template <typename T>
-
Iterator<T> LinkedList<T>::begin() const {
-
Iterator<T> iter;
-
iter.position = first;
-
return iter;
-
}
-
-
//To initialize the iteratore, this function set iterator at end
-
template <typename T>
-
Iterator<T> LinkedList<T>::end() const {
-
Iterator<T> iter;
-
iter.position = last;
-
return iter;
-
}
-
-
//Accessors, lookuo how many nodes we have in the current list
-
template <typename T>
-
int LinkedList<T>::size() const {
-
Iterator<T> iter = begin();
-
int length = 0;
-
while (iter.position != NULL) {
-
iter.forward();
-
length++;
-
}
-
return length;
-
}
-
-
//erasing a node
-
template <typename T>
-
void LinkedList<T>::erase(Iterator<T>& iter) {
-
Node<T>* pos = iter.position;
-
iter.forward();
-
//Set pointer from node before.
-
if (pos == first) //Check if first node.
-
first = pos->next;
-
else
-
(pos->prev)->next = pos->next;
-
//Set pointer from node after.
-
if (pos == last) //Check if last node.
-
last = pos->prev;
-
else
-
(pos->next)->prev = pos->prev;
-
delete pos; //Remove the node from memory.
-
return;
-
}
-
-
template <typename T>
-
void LinkedList<T>::insert(Iterator<T> iter, T value) {
-
Node<T>* pos = iter.position;
-
Node<T>* newNode = new Node<T>(value);
-
//Check if list is empty.
-
if (first == NULL) {
-
first = newNode;
-
last = newNode;
-
return;
-
}
-
newNode->prev = pos->prev;
-
newNode->next = pos;
-
if (newNode->prev == NULL) //Check if inserting at first position.
-
first = newNode;
-
else
-
(newNode->prev)->next = newNode;
-
pos->prev = newNode;
-
return;
-
}
-
-
template <typename T>
-
void LinkedList<T>::pop_back()
-
{
-
if(last==first)
-
{
-
delete first;
-
first=last=NULL;
-
}
-
else
-
{
-
last=last->prev;
-
delete last->next;
-
last->next=NULL;
-
}
-
last = NULL;
-
}
-
-
//pop front function, remove the first element from the list
-
template <typename T>
-
void LinkedList<T>::pop_front()
-
{
-
if(first==last)
-
{
-
delete first;
-
last=first=NULL;
-
}
-
else
-
{
-
first=first->next;
-
delete first->prev;
-
first->prev=NULL;
-
}
-
-
}
-
-
-
//push back, adds the given value x to the end of the list
-
template<typename T>
-
void LinkedList<T>::push_back(T value)
-
{
-
-
Node<T>* newNode = new Node<T>(value);
-
newNode->prev=NULL;
-
newNode->next=NULL;
-
-
if(first==NULL && last==NULL)
-
{
-
first=last=newNode;
-
}
-
-
else
-
{
-
newNode->prev=last;
-
last->next=newNode;
-
last=newNode;
-
}
-
-
-
}
-
//find function
-
template<typename T>
-
void LinkedList<T>::find(T value)
-
{
-
Node<T>* pos=first;
-
while(pos){
-
if (pos.get()==value){
-
return pos;
-
}
-
else{
-
pos=pos->next;
-
}
-
}
-
return pos=NULL;
-
-
}
-
-
template<typename T>
-
void LinkedList<T>::push_front(T value)
-
{
-
Node<T>* newNode= new Node<T>(value);
-
newNode->prev=NULL;
-
newNode->next=NULL;
-
-
if(first==NULL && last==NULL)
-
{
-
first=last=newNode;
-
}
-
-
else
-
{
-
newNode->next=first;
-
first->prev=newNode;
-
first=newNode;
-
-
}
-
}
-
-
//print all the value of the list
-
template<typename T>
-
void LinkedList<T>::print()
-
{
-
T value;
-
Node<T>* newNode=new Node<T>(value);
-
newNode = first;
-
while (newNode != NULL) {
-
cout<<newNode->data<<" ";
-
newNode = newNode->next;
-
}
-
cout<<endl;
-
-
}
-
-
template<typename T>
-
void LinkedList<T>::reverse()
-
{
-
-
-
Node<T>* i=first;
-
Node<T>* temp;
-
-
-
while(i){
-
temp=i->next;
-
i->next=i->prev;
-
i->prev=temp;
-
i=temp;
-
}
-
-
temp=last;
-
first=last;
-
last=temp;
-
-
}
-
-
template <typename T>
-
T& LinkedList<T>::operator[] (int index) {
-
Node<T>* pos = first;
-
for (int i=1; i<= index; i++)
-
pos = pos->next;
-
return pos->data;
-
}
-
-
//Accessor, look up elements in the list, for example cout<<myList[3]
-
template <typename T>
-
T LinkedList<T>::operator[] (int index) const {
-
Node<T>* pos = first;
-
for (int i=1; i<= index; i++)
-
pos = pos->next;
-
return pos->data;
-
}
-
-
int main() {
-
LinkedList<string> rebelList1(4," ");
-
Iterator<string> iter1 = rebelList1.end();
-
iter1.backward();
-
rebelList1.erase(iter1);
-
iter1.backward();
-
rebelList1.insert(iter1,"Co");
-
iter1.forward();
-
rebelList1.insert(iter1,"it");
-
rebelList1.insert(iter1, rebelList1[2]);
-
rebelList1[0] = "ldth\nha";
-
rebelList1.pop_back();
-
rebelList1.push_back("all");
-
rebelList1.push_front("oth\nb");
-
-
LinkedList<string> rebelList2;
-
rebelList2.push_back("Tatooine");
-
rebelList2.push_front("Lando");
-
rebelList2.push_back(" sh");
-
rebelList2.pop_front();
-
rebelList2.pop_front();
-
rebelList2.push_front("hey");
-
rebelList2.reverse();
-
Iterator<string> iter2 = rebelList2.find(" sh");
-
rebelList2.insert(iter2,"ou");
-
rebelList2.reverse();
-
iter2 = rebelList2.find("hey");
-
rebelList2.insert(iter2,"oth\nb");
-
iter2.backward();
-
rebelList2.insert(iter2,"n H");
-
rebelList2.reverse();
-
rebelList2.insert(iter2,"ut t");
-
rebelList2.push_front("ld c");
-
-
LinkedList<string> rebelList3;
-
rebelList3.push_front("bel b");
-
rebelList3.push_back("e");
-
Iterator<string> iter3 = rebelList3.find("e");
-
rebelList3.insert(iter3, "re");
-
rebelList3.reverse();
-
rebelList3.insert(iter3, "th");
-
rebelList3.push_back("a");
-
iter3.forward();
-
rebelList3.insert(iter3, " ");
-
-
LinkedList<string> rebelList4 = rebelList3;
-
rebelList4[0] = "y, Da";
-
rebelList4[3] = "nes";
-
rebelList4.reverse();
-
rebelList4[1] = "lenti";
-
rebelList4.push_back("rt");
-
rebelList4.push_front("y v");
-
rebelList4.reverse();
-
rebelList4[2] = "da";
-
-
LinkedList<string> rebelList = rebelList1 + rebelList2;
-
rebelList = rebelList4 + rebelList;
-
rebelList.push_front("h!\n");
-
LinkedList<string> oneWord(1,"s o");
-
rebelList = rebelList + oneWord;
-
rebelList[9] = "pp";
-
rebelList.reverse();
-
rebelList.push_front("se i");
-
rebelList = rebelList3 + rebelList;
-
-
-
rebelList.print();
-
-
return 0;
-
}
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,188
| | | re: Linked List Program error
Is there a reason you have chosen not to use the STL (Standard Template Library) list class which implements a linked list on a type provided as a template parameter?
i.e. -
#include <list>
-
#include <string>
-
-
using namespace std;
-
-
int main()
-
{
-
list<string> rebel_list;
-
}
-
| | Member | | Join Date: Feb 2008
Posts: 33
| | | re: Linked List Program error
I was trying to make it myself
and I actually did it
I fixed the errors in my prgram, but still I cant come up with a function for find()
here is what I have - //find function
-
template<typename T>
-
Iterator<T> LinkedList<T>::find(T value)
-
{
-
-
bool Found;
-
Iterator<T> iter;
-
iter.position=first;
-
-
-
Found = false;
-
-
while ((! Found) && (iter.position!=NULL)){
-
if (iter.position->data == value)
-
return iter;
-
else
-
iter.position->next;
-
}
-
iter.position=NULL;
-
return iter ;
-
}
would you please help me
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,375
| | | re: Linked List Program error
You are supposed to use the list template like Banfa suggested.
The reason for that template is to not have to debug the linked list over and over and over again.
Use the list template.
| | Newbie | | Join Date: Feb 2008
Posts: 2
| | | re: Linked List Program error
I think your pop_back(), pop_front(), push_front(), and push_back() function should be written using erase and insert commands.
| | Member | | Join Date: Feb 2008
Posts: 33
| | | re: Linked List Program error
I fixed it, the push_back, find() reverse and push_front has small problems
thanks
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,188
| | | re: Linked List Program error Quote:
Originally Posted by APEJMAN I was trying to make it myself
and I actually did it Glad you got it all sorted, if you were implementing it as an academic exercise, that is either for course work or for your own practice at programming, then implementing it yourself is fine.
In a commercial project there is never a good reason not to use the STL list. Even, if, as happens occasionally, the compiler you are using has a poor or broken version of the STL you can always download and compile a different, working, version of the STL.
If this was a programming exercise what you should do now is devise some performance tests and compare the performance of you implementation to the STL implementation.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|