Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Linked Lists :(

Options
  • 15-11-2003 7:05pm
    #1
    Closed Accounts Posts: 3,322 ✭✭✭


    Hi guys im writing this program for college to implement linked lists in C++ and actions on the list (inserting at front and back, displaying and deleting from the front of the list) Anyway it inserts at the front and displays the list ok, but when i insert at the back (tail) of the list and display it goese into an infinite loop displaying a memory address.. Heres the code anyway id appreciate any help - thanks
    #include <iostream>
    using namespace std;
    
    struct Node
    {
        int data;
        Node *next;
    };
    
    Node *Z = new Node;   
    
    Node *insertFront (int x, Node *head)
    {
        Node *temp = new Node;
        temp->data = x;
        temp->next = head;
        head=temp;
        return head;
    }
    
    Node *insertBack (int x, Node *head)
    {
        Node *temp = new Node;
        Node *tail;
        temp->data = x;
        if (head == Z)   
        {
            head=temp;
            return head;
        }
        else                
        {
            tail = head;
            while (tail->next != Z)
            {
                    tail = tail->next;
            }
            tail->next = temp;
            return head;
        }
    }
    
    void display(Node *head)
    {
        if (head == Z)
            cout << "List empty" << endl;
        else
        {
            while (head != Z)
            {
                    cout << head->data << " ";
                    head = head->next;
            }
        }
    }
    
    int deleteFront (Node **hptr)
    {
        int deleted_node = 0;
        Node *head;
        if (head == Z)
        {
            cout << "List Empty" << endl;
            return 0;
        }
        head = *hptr;               
        deleted_node = head->data;
        *hptr = head->next;  
        delete head;
        return deleted_node;
    }
    
    
    main()
    {
        Z->next=Z;
        Node *llist = Z;
        int choice, input_data = 0;
        
        while (choice != 5) {
        cout << "Press 1 to insert at front of list" << endl;
        cout << "Press 2 to insert at back of list" << endl;
        cout << "Press 3 to delete from front of list" << endl;
        cout << "Press 4 to display list" << endl;
        cout << "Press 5 to exit" << endl;
        cout << "Your choice: ";
        cin >> choice;
        
        switch (choice)
        {
            case 1:
            cout << "Enter number to insert at front of list: ";
            cin >> input_data;
            llist = insertFront (input_data, llist);
            break;
            
            case 2:
            cout << "Enter number to insert at back of list: ";
            cin >> input_data;
            llist = insertBack (input_data, llist);
            break;
            
            case 3:
            cout << "Deleting from front of list" << endl;
            deleteFront (&llist);
            break;
            
            case 4:
            cout << "Displaying list" << endl;
            display(llist);
            break;
            
            case 5:
            break;
            
            default:
            cout << "Please Enter 1-5" << endl;
            break;
        } // end switch
        } // end while
    } // end main
    


Comments

  • Closed Accounts Posts: 1,376 ✭✭✭joc_06


    Reiner's working ye hard i see??
    Same project for da last 6 years i think now.


  • Closed Accounts Posts: 3,322 ✭✭✭Repli


    I think your a bit mixed up.
    Im in DIT ? and havent a clue who Reiner is..
    !(Thanks for the help tho)


  • Closed Accounts Posts: 1,376 ✭✭✭joc_06


    Sorry buddy, that project has been floating around ul for years and years. If its any good to you, look in dit's library for a book by rojiani- i think he covers linked listed extremely well. I'll look over my old notes and see what i can dig up for ya. Pm me your email addy and i can send u on some good pdf's on the subject


  • Registered Users Posts: 1,372 ✭✭✭silverside


    You have a dangling Next pointer in the node you are trying to add to the end of the list. It should be set to Z. Thats why when you try to traverse the list, you are getting an access violation when it tries to dereference that pointer.

    Although I would use NULL everywhere you have Z.


  • Closed Accounts Posts: 3,322 ✭✭✭Repli


    Ah thanks mate
    I put temp->next = Z; before each return statement in the insertBack() function and it works perfectly.. spotted another error too.. deleteFront in main is supposed to return something woops lol


  • Advertisement
Advertisement