Linked List (Node Class)

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

    Linked List (Node Class)

    I want to write several functions so that the program below would work.
    I am trying to find the functions for the main routine provided below and reveal a secret message. I dont wanne make any changes to the main routine. I just want to write all the functions, but I cant. is there any one here who could help me with that?

    Thanks


    [CODE=cpp]#include <iostream>
    #include <string>
    using namespace std;
    #include "list.cpp"


    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:06 PM. Reason: Please use the [CODE] tags provided.
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    You'll need to look up template classes and template programming. Here is a tutorial on them. Other than that, if you don't really know how linked lists work, go to Wikipedia or Google and get the information there and implement first without templates, then add them.

    Comment

    • APEJMAN
      New Member
      • Feb 2008
      • 33

      #3
      THIS IS MY list.h file that I made, which is not complete
      abd below that I have my list.cpp, which is the functions, but its not compete, is there any one here who can complete it?

      thanks
      [CODE=cpp]#include <string>
      #include <iostream>
      #ifndef LIST_H
      #define LIST_H

      using namespace std;


      //Node Class
      template <typename T>
      class Node {
      public:
      Node(T newData); //Constructor
      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.
      };





      // Iterator Class

      template <typename T>
      class Iterator {
      public:
      Iterator();
      T get() const;
      void forward();
      void backward();
      template <typename T>
      friend class LinkedList;
      private:
      Node<T>* position;
      };



      // LinkedList Class

      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);
      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;
      private:
      Node<T>* first; //Pointer to first node in list.
      Node<T>* last; //Pointer to last node in list.
      };


      /*************** *************** *************** ************/

      list.cpp

      #include <string>
      #include "list.h"
      #include <iostream>

      //The Node Constructor
      template <typename T>
      Node<T>::Node(T newData) {
      data = newData;
      prev = NULL;
      next = NULL;
      }


      /*************** *************** ****/
      //The Iterator Fuctions
      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;
      }

      template <typename T>
      void Iterator<T>::ba ckward() {
      position = position->prev;
      }
      /*************** *************** */



      /*************** *************** */
      //LinkedList Functions
      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();
      }
      }

      template <typename T>
      LinkedList<T>:: ~LinkedList() {
      while (first != NULL)
      erase(begin());
      }

      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;
      }

      template <typename T>
      Iterator<T> LinkedList<T>:: begin() const {
      Iterator<T> iter;
      iter.position = first;
      return iter;
      }

      template <typename T>
      Iterator<T> LinkedList<T>:: end() const {
      Iterator<T> iter;
      iter.position = last;
      return iter;
      }

      template <typename T>
      int LinkedList<T>:: size() const {
      Iterator<T> iter = begin();
      int length = 0;
      while (iter.position != NULL) {
      iter.forward();
      length++;
      }
      return length;
      }

      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>
      T& LinkedList<T>:: operator[] (int index) {
      Node<T>* pos = first;
      for (int i=1; i<= index; i++)
      pos = pos->next;
      return pos->data;
      }

      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;
      }

      /*************** *************** ****/[/CODE]
      Last edited by Ganon11; Feb 14 '08, 07:06 PM. Reason: Please use the [CODE] tags provided.

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        First off, we're not going to do your work for you. Also, please use the provided CODE tags so that your code is formatted for easy reading.

        Where precisely are you having your problem(s)? Let us know what error messages you get/what the output is that it shouldn't be.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Why are you using C++ to write a linked list when the standard library list template is already written???????

          Is this an academic exercise??

          Comment

          • APEJMAN
            New Member
            • Feb 2008
            • 33

            #6
            just I like to learn C++ as a hobby
            by the way I wrote the program

            thanks

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              The danger in doing something like this is your time investment. If this becomes your creation you will use it on a real job and interject more non-standard C++ software into the industry.

              If you know how a linked list works, then there is no point in writing one for yourself. Most of the STL is infrastructure of a very common nature.

              Comment

              Working...