Thread: trying to implement singly linked list..please debug..very new to cpp

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    77

    Post trying to implement singly linked list..please debug..very new to cpp

    my code

    Code:
    #include<iostream>#include<conio.h>
    
    
    template <class T>
    class Node
    {
       friend class LinkList;
       private:
              T info;
              Node<T> *link;
    };
    
    
    template<class T>
    class LinkList
    {
          private:
                  Node<T> *start;
                  
          public:
                 LinkList(){start=NULL;}
                 ~LinkList();
                 bool isEmpty()const
                 {return start=NULL;}
                 int size()const;
                 void Display()const;
                 LinkList(T)&insert(T x);
                 LinkList(T)&Delete(T x);
    };
    
    
    template <class T>
    LinkList<T> :: ~LinkList()
    {
        while(start!=NULL)
        {
                          Node *del;
                          Del=start;
                          start=del->link;
                          dle->link=NULL;
                          delete del;
        }
    }
    
    
    template <class T>
    {
             int LinkList<T>::size()
             {
                 int c=0;
                 Node*ptr;
                 while(ptr!=NULL)
                 {
                                 c++;
                                 ptr=ptr->NULL;
                 }
                 return c;
    }
    
    
    template <class T>
    LinkList<T> &Linklist<T> :: insert(T x)
             {
                         Node *New= new Node;
                         New->info=x;
                         New->link=NULL;
                         New->link=start;
                         start=New;
                         return *this;
             }
             
    template <class T>
    LinkList<T> &LinkList<T> :: Delete(T x)
    {
                Node *ptr,*del;
                if(start==NULL)
                {
                               cout<<"Empty List"<<endl;
                               return *this;
                }
                
                if(start->info==x)
                {
                                  del=start;
                                  start=del->link;
                                  del->link=NULL;
                                  delete del;
                                  return *this;
                }
                ptr=start;
                while(ptr->link!=NULL && (ptr->link)->info!=x)
                ptr=ptr->link;
                
                if(ptr->link==NULL)
                del=ptr->link;
                ptr->link=del->link;
                del->link=NULL;
                delete del;
                return *this;
    }
    ​urgent solution needed

    not yet writen the main()...please check my class definitions and frienships.....I am getting compiler errors



  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quick check:

    Line #7: LinkList must be forward-declared. The friend declaration should be template<typename T> friend class LinkList or friend class LinkList<T>. LinkList is a not really a class. You cannot declare it a friend without specifying what type of it should be a friend. The former declares all types of LinkLists friends and the later only the LinkList<T>.

    Line #24: You are confusing assignment and comparison (= vs ==).

    Line #48, #58: Invalid braces.

    Line #52: ptr is uninitialized.

    Line #55: Syntax error. You are probably trying to advance to the next node, but you aren't.

    Delete function: Logic is wrong. What happens if you reach the end of the list and don't find the appropriate element to delete?

    This is just a small sample of what's wrong. You are trying to do way too much complexity at once. I suggest you go slowly and test it properly before moving on.

    Also learn to read, understand and fix compiler warnings. Posting the compile error in the future wouldn't hurt, either.
    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.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    77
    I didnt understand the line#7 issue please elaborate..I am very thankful for your help
    If possible please provile me with correct code for those classes(friendships)

    the class code only

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    77
    Code:
    #include <iostream>#include <conio.h>
    #include<process.h>
    
    
    using namespace std;
    
    
    template <class T>
    class NODE
    {
         
          private :
                  T INFO;
                  NODE<T> *LINK; 
                  
                  public:
                          friend  class LINKEDLIST ;
                    
                       };
    
    
    
    
        template <class T>
        class LINKEDLIST
        {
              private :
                      NODE<T> *START;
              
              public :
                     LINKEDLIST(){START=NULL;}
                     ~LINKEDLIST();
                     
              };
    I am also getting error in this code too when I am compiling it...

    1. [Linker error] undefined reference to `WinMain@16'
    2. ld returned 1 exit status

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Every program must contain a function called "main". If this is not supposed to be a complete program, then you need to tell your compiler that this is not a complete program, probably (given the appearance of "ld") by passing the -c switch to gcc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with singly linked list
    By saldar05 in forum C Programming
    Replies: 6
    Last Post: 03-08-2013, 12:30 AM
  2. Need help with Singly Linked List C++
    By luckyali444 in forum C++ Programming
    Replies: 3
    Last Post: 10-18-2011, 11:30 PM
  3. Singly Linked List
    By devarishi in forum C Programming
    Replies: 4
    Last Post: 12-24-2008, 12:00 AM
  4. need help with singly linked-list
    By vearns in forum C++ Programming
    Replies: 20
    Last Post: 04-09-2008, 09:48 AM
  5. singly linked list
    By Luigi in forum C++ Programming
    Replies: 1
    Last Post: 11-30-2002, 11:19 AM