CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: linked list

  1. #1
    Join Date
    Jun 2011
    Posts
    21

    Question linked list

    Code:
    #include <iostream>
    using namespace std;
    
    template <class Type>
    struct nodeType{
      Type info;
      nodeType <Type> *link;
    };
    
    template <class Type>
    class linkedListType {
    public:
      linkedListType ();
      void print ()const;
      int length ()const;
      void insertFirst (const Type& newitem);
      void insertLast (const Type& newitem);
      void readIntoLinkedList ();
      nodeType <Type> *smallestNode();
      nodeType <Type> *findNode (const Type& item);
    
    
    private:
      nodeType <Type> *first;
      nodeType <Type> *last;
    };
    
    template <class Type>
    linkedListType<Type>::linkedListType (){
      first=NULL;
      last=NULL;
    }
    template <class Type>
    void linkedListType <Type>:: print()const{
      nodeType <Type> *current;
      current=first;
      while (current !=NULL)
        {
          cout<<current->info<<" ";
          current =current->link;
        }
       cout<<endl;
    }
    
    template <class Type>
    int linkedListType <Type>::length()const{
      int n=0;
      nodeType <Type> *current;
     current=first;
      while(current !=NULL)
        {
          n++;
          current=current->link;
        }
      return n;
    }
    
    template <class Type>
    void linkedListType <Type> ::insertFirst (const Type& newitem){
      nodeType <Type> *newNode;
      newNode=new nodeType <Type>;
      newNode->info=newitem;
      newNode->link=first;
      first=newNode;
      if (last==NULL)
        last=newNode;
    }
    template <class Type>
    void linkedListType <Type> ::insertLast (const Type& newitem){
      nodeType <Type> *newNode;
      newNode=new nodeType <Type>;
      newNode ->link =NULL;
      if (first==NULL)
       {
          first=newNode;
          last=newNode;
        }
      else{
        last->link=newNode;
        last=newNode;
      }
    }
    template <class Type>
    void linkedListType <Type>::readIntoLinkedList (){
      int num;
      cin>>num;
      while (num!=-999){
       insertLast (num);
        cin>>num;
      }
    }
    
    
    int main()
    {
      linkedListType <int> l1;
      l1.readIntoLinkedList ();
      cout<<"The given Linked List"<<endl;
      cout<<" ";
      l1.print();
    
    
      return 0;
    
    }
    why is not reading the numbers??
    this is what im getting
    The given Linked List
    0 0 0 0 0 0 0 0
    im suppose to get
    the given linked list
    4 5 4 8 45 4
    thanks

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: linked list

    Quote Originally Posted by ryamjones View Post
    why is not reading the numbers??
    Your
    Code:
    void linkedListType <Type> ::insertLast (const Type& newitem){}
    is missing
    Code:
    newNode->info=newitem;
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  3. #3
    Join Date
    Jun 2011
    Posts
    21

    Wink Re: linked list

    omg thanks would take me long to see that

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: linked list

    Quote Originally Posted by ryamjones View Post
    omg thanks would take me long to see that
    Your linked list also has memory leaks. Where is the linked list destructor that is supposed to clean up all the memory you allocated?

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: linked list

    Quote Originally Posted by ryamjones View Post
    omg thanks would take me long to see that
    Would be easy if you step through your code in debugger (like I did)
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  6. #6
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: linked list

    even the functions

    Code:
     
     nodeType <Type> *smallestNode();
      nodeType <Type> *findNode (const Type& item);
    seems to be not implemented. only prototype is present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured