Linked List Program error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • APEJMAN
    New Member
    • Feb 2008
    • 33

    Linked List Program error

    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
    [code=cpp]
    #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(cons t 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>::It erator() {
    position = NULL;
    }

    template <typename T>
    T Iterator<T>::ge t() const {
    return position->data;
    }

    template <typename T>
    void Iterator<T>::fo rward() {
    position = position->next;
    }

    //moves position backward one node
    template <typename T>
    void Iterator<T>::ba ckward() {
    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(cons t LinkedList<T>& right) {
    first = NULL;
    last = NULL;
    Iterator<T> iter_right = right.end();
    while (iter_right.pos ition != NULL) {
    insert(begin(), iter_right.get( ));
    iter_right.back ward();
    }
    }

    //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.posi tion != NULL) {
    erase(iter_left );
    iter_left = begin();
    }
    Iterator<T> iter_right = right.end();
    while (iter_right.pos ition != NULL) {
    insert(begin(), iter_right.get( ));
    iter_right.back ward();
    }
    }
    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_bac k(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<typena me 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=newN ode;
    }

    else
    {
    newNode->prev=last;
    last->next=newNode ;
    last=newNode;
    }


    }
    //find function
    template<typena me T>
    void LinkedList<T>:: find(T value)
    {
    Node<T>* pos=first;
    while(pos){
    if (pos.get()==val ue){
    return pos;
    }
    else{
    pos=pos->next;
    }
    }
    return pos=NULL;

    }

    template<typena me 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=newN ode;
    }

    else
    {
    newNode->next=first;
    first->prev=newNode ;
    first=newNode;

    }
    }

    //print all the value of the list
    template<typena me 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<typena me 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<stri ng> rebelList1(4," ");
    Iterator<string > iter1 = rebelList1.end( );
    iter1.backward( );
    rebelList1.eras e(iter1);
    iter1.backward( );
    rebelList1.inse rt(iter1,"Co");
    iter1.forward() ;
    rebelList1.inse rt(iter1,"it");
    rebelList1.inse rt(iter1, rebelList1[2]);
    rebelList1[0] = "ldth\nha";
    rebelList1.pop_ back();
    rebelList1.push _back("all");
    rebelList1.push _front("oth\nb" );

    LinkedList<stri ng> rebelList2;
    rebelList2.push _back("Tatooine ");
    rebelList2.push _front("Lando") ;
    rebelList2.push _back(" sh");
    rebelList2.pop_ front();
    rebelList2.pop_ front();
    rebelList2.push _front("hey");
    rebelList2.reve rse();
    Iterator<string > iter2 = rebelList2.find (" sh");
    rebelList2.inse rt(iter2,"ou");
    rebelList2.reve rse();
    iter2 = rebelList2.find ("hey");
    rebelList2.inse rt(iter2,"oth\n b");
    iter2.backward( );
    rebelList2.inse rt(iter2,"n H");
    rebelList2.reve rse();
    rebelList2.inse rt(iter2,"ut t");
    rebelList2.push _front("ld c");

    LinkedList<stri ng> rebelList3;
    rebelList3.push _front("bel b");
    rebelList3.push _back("e");
    Iterator<string > iter3 = rebelList3.find ("e");
    rebelList3.inse rt(iter3, "re");
    rebelList3.reve rse();
    rebelList3.inse rt(iter3, "th");
    rebelList3.push _back("a");
    iter3.forward() ;
    rebelList3.inse rt(iter3, " ");

    LinkedList<stri ng> rebelList4 = rebelList3;
    rebelList4[0] = "y, Da";
    rebelList4[3] = "nes";
    rebelList4.reve rse();
    rebelList4[1] = "lenti";
    rebelList4.push _back("rt");
    rebelList4.push _front("y v");
    rebelList4.reve rse();
    rebelList4[2] = "da";

    LinkedList<stri ng> rebelList = rebelList1 + rebelList2;
    rebelList = rebelList4 + rebelList;
    rebelList.push_ front("h!\n");
    LinkedList<stri ng> oneWord(1,"s o");
    rebelList = rebelList + oneWord;
    rebelList[9] = "pp";
    rebelList.rever se();
    rebelList.push_ front("se i");
    rebelList = rebelList3 + rebelList;


    rebelList.print ();

    return 0;
    }[/code]
    Last edited by Ganon11; Feb 14 '08, 07:08 PM. Reason: Please use the [CODE] tags provided.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    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.

    [code=cpp]
    #include <list>
    #include <string>

    using namespace std;

    int main()
    {
    list<string> rebel_list;
    }
    [/code]

    Comment

    • APEJMAN
      New Member
      • Feb 2008
      • 33

      #3
      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

      [CODE=cpp]//find function
      template<typena me T>
      Iterator<T> LinkedList<T>:: find(T value)
      {

      bool Found;
      Iterator<T> iter;
      iter.position=f irst;


      Found = false;

      while ((! Found) && (iter.position! =NULL)){
      if (iter.position->data == value)
      return iter;
      else
      iter.position->next;
      }
      iter.position=N ULL;
      return iter ;
      }[/CODE]


      would you please help me
      Last edited by Ganon11; Feb 14 '08, 07:07 PM. Reason: Please use the [CODE] tags provided.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        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.

        Comment

        • dorongnong
          New Member
          • Feb 2008
          • 2

          #5
          I think your pop_back(), pop_front(), push_front(), and push_back() function should be written using erase and insert commands.

          Comment

          • APEJMAN
            New Member
            • Feb 2008
            • 33

            #6
            I fixed it, the push_back, find() reverse and push_front has small problems
            thanks

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              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.

              Comment

              Working...