Thread: problems on linkedlist

  1. #1
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    problems on linkedlist

    Code:
    typedef struct Node{
           
            int data;
            struct Node* prev;   
            struct Node* next;  
            struct Node(int data)//Expected unqualified-id before "int"
            {
                   this->data = data;
                   prev = NULL;
                   next = NULL;
            }; 
    } NODE;
     
    
    
    class LinkedList{
          private:
                  NODE* front;
                  NODE* back;
          public:
                 LinkedList();
                 ~LinkedList();
                 void appendNode(int);
                 void displayNodes();
                 void displayNodesReverse();
                 void destroyList();
    };
    
    LinkedList::LinkedList(){
                             front=NULL;
                             back=NULL;
    }
    LinkedList::~LinkedList(){
                              destroyList();
    }
    void LinkedList::appendNode(int data){
         NODE* n=new NODE(data);//no matching function for call to Node::Node(int&)
         if(back==NULL)
         {
                       back=n;
                       front=n;
         }
         else{
              back->next=n;
              n->prev=back;
              back=n;
              }
    }
    void LinkedList::displayNodes(){
         NODE* temp = front;
         while(temp!=NULL){
                           
                           temp=temp->next;
                           }
    }
    void LinkedList::displayNodesReverse(){
          NODE* temp = back;
         while(temp!=NULL){
                           
                           temp=temp->prev;
                           }
    }
    void LinkedList::destroyList(){
          NODE* temp = back;
                while(temp!=NULL){
                           NODE* tmp2 = temp;
                           temp=temp->prev;
                           delete tmp2;
                           }
                back=NULL;
                front=NULL;
    }
    i have a problem with a linked list class, the problem is at the code below...how can you solve this kind of problem
    Turbo C Makes me Sick....i just want to learn data structures in latest c++

    Le Tormente (fr. the torment)

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    what problem?
    we are not mind readers.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The problem I suppose is the "expected unqualified-id before "int"" thing.

    Your problem is that you think that structs have the same syntax as in C. In fact structs have the same syntax as classes. That is, you don't need to typedef the struct and you don't need to use the struct keyword when declaring pointers and instances. And you definitely can't use the struct keyword before the constructor.

    (The C syntax is supported for backward compatibility, but apparently not for C++ features, such as constructor.)

    Code:
    struct Node{
    
            int data;
            Node* prev;
            Node* next;
            Node(int data)
            {
                   this->data = data;
                   prev = NULL;
                   next = NULL;
            };
    };
    
    typedef Node NODE;
    //or you could just use Node in the rest of the code
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    Question

    I got it.....this file works normally.....do you have an idea on sorting in a linked list?
    Turbo C Makes me Sick....i just want to learn data structures in latest c++

    Le Tormente (fr. the torment)

  5. #5
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466

    Sorting linked list

    ...is best done by swapping pointers and not done in the same way as an array. If I had to do it, I would probably choose the mergesort algorithm, as it lends itself well to a linked list structure and is a solid, fast, sort (not sure about stable...)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. Something about a linkedlist program..
    By ozumsafa in forum C Programming
    Replies: 8
    Last Post: 10-17-2007, 01:14 PM
  3. Something about typedef (data structures)(Linkedlist)
    By ozumsafa in forum C Programming
    Replies: 13
    Last Post: 09-29-2007, 04:44 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM