Linked list trouble

This is a discussion on Linked list trouble within the C++ Programming forums, part of the General Programming Boards category; Hello guys i am trying to create a linked list with templates but i am having some trouble. Header file: ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Heraklion, Greece, Greece
    Posts
    26

    Linked list trouble

    Hello guys i am trying to create a linked list with templates but i am having some trouble.
    Header file:
    Code:
    #include "../std_lib_facilities.h"
    #include <iostream>
    
    using namespace std;
    
    
    template <class T>
    class LinkedList{
    private:
        struct node{
            T data;
            node* next;
        };
    
        node* head;
        node* curr;
        node* temp;
    public:
        LinkedList(){
          head=NULL;
          curr=NULL;
          temp=NULL;
        
        }
        void AddNode(T addData){
         node* n=new node;
         n->next=NULL;
         n->data=addData;
     
         if(head==NULL){
             head=n;
         }else{
             curr=head;
             while(curr->next!=NULL){
                 curr=curr->next;
             }
             curr->next=n;
         }
        }
    
        
        void DisplayList(){
          curr=head;
          while (curr!=NULL){
          cout<<curr->data;
          
          }
        }
    };
    Source file:
    Code:
       
    #include "LinkedList.h"
    
    int main(){
        string a;
        cin>>a;
        LinkedList<string> Chuan;
        Chuan.AddNode(a);
    
    
    }
    Error:1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup

  2. #2
    Registered User
    Join Date
    Nov 2012
    Location
    Heraklion, Greece, Greece
    Posts
    26
    Nevermind,i fixed it.My project in visual c++ was not set in console mode.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    If you are using C++11, consider the following:
    - Move the data into the node data, ie n->data=addData should be n->data=std::move(addData). You can read about rvalues to understand this.
    - NULL should be nullptr (nullptr is typesafe, while NULL is just an integer).
    - You can use smart pointers for the pointers as an exercise (don't do this in a real world linked list).
    If you can't use C++11, then consider:
    - Don't take parameters by value, but by (const) reference instead, ie change void AddNode(T addData) to void AddNode(const T& addData).

    Then consider this:
    - You should implement a destructor. Very important.
    - Either keep track of the last node in the list, or make AddNode insert a new node at the beginning. Saves travelling the entire list to insert a new node.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list trouble...
    By JM1082 in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2011, 03:02 AM
  2. Trouble with linked list
    By mikeman in forum C++ Programming
    Replies: 8
    Last Post: 01-21-2010, 10:10 PM
  3. trouble printing linked list
    By deathrattle in forum C Programming
    Replies: 3
    Last Post: 12-02-2008, 04:33 PM
  4. Linked list trouble
    By sand_man in forum C Programming
    Replies: 2
    Last Post: 02-08-2005, 12:14 PM
  5. Having trouble with Linked List
    By hkmixxa in forum C++ Programming
    Replies: 7
    Last Post: 08-09-2004, 03:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21