Thread: C++ Compile problem in my code

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    1

    C++ Compile problem in my code

    Hello everyone , ı have some question about c++ Linked lists.

    First of all , I have an assignment related to c ++ linkedlists. The subject of the assignment was to sort the existing series of numbers from large to small and print them on the screen again.I wrote the necessary code and then run it.

    But there is a problem that my program is running sometimes, sometimes it stops running without errors and I cannot input any numbers. (it stops when entering numbers)

    So, ı dont know how to debugging. is there anyone here who can help me .

    this is my l.l function.
    Code:
    #ifndef LinkedList_h
    #define LinkedList_h
    
    
    #include <iostream>
    #include <cassert>
    
    struct Node
    {
        T data;
        Node<T> *next;
    };
    
    
    template <class T>
    class LinkedList
    {
    public:
        void insert_descending(T& new_item);
        void insert_head(T& new_item);
    
    friend std::ostream& operator<<(std::ostream &os,LinkedList<S> &list)
        {
            Node<S> *p = new Node<S>;
            p=list.head;
            while (p != NULL)
            {
                os<<p->data<<" ";
                p=p->next;
            }
            return os;
        }
    protected:
        int count;
        Node<T> *head;
        Node<T> *last;
    };
    template <class T>
    void LinkedList<T>::insert_head(T& new_item)
    {
        Node<T> *newNode = new Node<T>;
        newNode->data = new_item;
        newNode->next = head;
        head = newNode;
    
    
        if (last == NULL)
            last = head;
    
    
        count++;
    }
    
    
    
    
    template <class T>void LinkedList<T>::insert_descending(T& new_item) {
        
        Node<T> *newNode = new Node<T>;
        newNode->data = new_item;
        newNode->next = NULL;
        
        Node<T> *temp = head;
        if(temp->data <= newNode->data)
        {
            temp -> next = head;
              head = temp;
    
    
        count++;
        }
        else if(temp->data >= newNode->data) 
        {
    
    
            while(temp->next != NULL && temp->next->data > newNode->data)
            {
                temp = temp->next;
            }
            newNode->next = temp->next;
            temp->next = newNode;
            count++;
        }
        
    }

    and this is my main program .

    Code:
    #include <iostream>
    #include "LINKEDLIST.h"
    using namespace std;
    int main()
    {
        LinkedList<int> list;
        int num;
           cin >> num;
           list.insert_head(num);
            for(int i=0;i<9;i++)
            {   cin >> num;
                list.insert_descending(num);
            }    
        cout << "list : " << list;
    }
    EXAMPLE ABOUT RUNNING CORRECTLY
    C++ Compile problem in my code-screenshot_9-png
    AND EXAMPLE ABOUT STOPPING
    C++ Compile problem in my code-screenshot_10-png
    Last edited by heraldv; 04-24-2020 at 09:10 AM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    954
    Please don't post code that you've just tossed together. I had to fix up a couple of things before it would even compile.

    You never initialize any member variables in LinkedList. This would usually be done with a constructor. Node could benefit from a constructor, too.

    It would also be good to have a destructor for LinkedList that deletes the nodes.

    You shouldn't need a separate function to insert the first node.
    insert_descending should be able to handle that case.

    In the print function p should not be initialized to a new Node. It should just be initialized to head.

    What you call "last" is usually called "tail" (to go with "head").
    Code:
    #ifndef LinkedList_h
    #define LinkedList_h
     
    #include <iostream>
     
    template <class T>
    struct Node
    {
        T data;
        Node<T>* next;
        Node(T data, Node<T>* next=nullptr) : data(data), next(next) {}
    };
     
    template <class T>
    class LinkedList
    {
    public:
        LinkedList() : count(), head(), tail() {}
        ~LinkedList() { clear(); }
        void clear();
        void insert_descending(const T& new_item);
        void insert_head(const T& new_item);
        int size() const { return count; }
     
        template <class T1>
        friend std::ostream& operator<<(std::ostream& os, LinkedList<T1>& list);
     
    protected:
        int count;
        Node<T>* head;
        Node<T>* tail;
    };
     
    template <class T>
    void LinkedList<T>::clear()
    {
        while (head)
        {
            Node<T>* to_delete = head;
            head = head->next;
            delete to_delete;
        }
        tail = nullptr;
        count = 0;
    }
     
    template <class T1>
    std::ostream& operator<<(std::ostream& os, LinkedList<T1>& list)
    {
        Node<T1>* p = list.head;
        while (p)
        {
            os << p->data << ' ';
            p = p->next;
        }
        return os;
    }
     
    template <class T>
    void LinkedList<T>::insert_head(const T& new_item)
    {
        head = new Node<T>(new_item, head);
        if (!tail)
            tail = head;
        ++count;
    }
     
    template <class T>void LinkedList<T>::insert_descending(const T& new_item)
    {
        Node<T>* newNode = new Node<T>(new_item);
        Node<T>* prev = nullptr;
        Node<T>* node = head;
     
        while (node && node->data > newNode->data)
        {
            prev = node;
            node = node->next;
        }
     
        if (prev)
        {
            prev->next = newNode;
            newNode->next = node;
        }
        else
        {
            if (head)
                newNode->next = head;
            else
                tail = newNode;
            head = newNode;
        }
     
        ++count;
    }
     
    #endif
     
     
     
     
    #include <iostream>
    //#include "LINKEDLIST.h"
    using namespace std;
     
    int main()
    {
        LinkedList<int> list;
        for(int i = 0; i < 10; i++)
        {
            int num;
            cin >> num;
            list.insert_descending(num);
        }
        cout << "list: " << list << '\n';
        cout << "size: " << list.size() << '\n';
    }
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you compile C code within C++?
    By Lewis Smith in forum C++ Programming
    Replies: 11
    Last Post: 07-06-2014, 03:28 PM
  2. Code will not compile - code from a book!
    By Chubz in forum C++ Programming
    Replies: 19
    Last Post: 09-12-2004, 10:21 PM
  3. Replies: 3
    Last Post: 03-07-2003, 09:06 PM
  4. why won't this code compile?
    By vanilly in forum C Programming
    Replies: 9
    Last Post: 02-13-2003, 02:19 PM

Tags for this Thread