-1

I tried implementing Linked List using C++ using a structure. I've included three functions - Size, Insertion and Deletion from the end. The program compiled successfully. During execution, when I tried to give input for the LLInsert() function, there was just a cursor blinking on my execution window. I don't know if the function returned to main. Also the LLSize() doesn't return 0 when I try to find the Size of a empty list. I can't seem to figure out what I'm doing wrong. Here is my code.

    #include<iostream>

    using namespace std;

    struct LL {
        LL *next = NULL;
        int data;
    };

    int LLSize(LL *head) {
        LL *current = new LL;
        current = head;
        int count = 0;

        while(current != NULL) {
            current = current -> next;
            count ++;
        }

        return count;
    }

    void LLInsert(LL *head,int value) {
        LL *current = new LL;
        current = head;
        LL *Newnode = new LL;
        Newnode -> data = value;
        Newnode -> next = NULL;

        if(head == NULL) {
            head = Newnode;
            return;
        }

        while(current->next != NULL) {
            current = current->next;
        }
        current->next = Newnode;

        return;
    }

    int LLDelete(LL *head) {
        LL *current = new LL;
        current = head;
        int deleteddata;

        while(current->next->next != NULL) {
            current = current->next;
        }
        current->next->data = deleteddata;
        current->next = NULL;

        return deleteddata;
    }

    int main() {
        int choice;
        LL *A;

        while(1) {
        cout << "1. Size\n2. Insert\n3. Delete\n4. Exit" << endl;
        cout << "Enter a choice : ";
        cin >> choice;

        switch(choice) {
            case 1 : {
                cout << "\nLength = " << LLSize(A) << endl;
                break;
            }
            case 2 : {
                int value;
                cout << "\nEnter the element to insert : ";
                cin >> value;
                LLInsert(A,value);
                break;
            }
            case 3 : {
                cout << LLDelete(A);
                break;
            }
            case 4 : {
                exit(0);
            }
            default : {
                cout << "\nInvalid choice. Enter a valid choice " << endl;
                break;
            }
        }
        }
    } 
  • If you had read [any good beginners book](https://stackoverflow.com/a/388282/440558) you should know about *references*. Think about that for a while, and go back to your books to read about it. – Some programmer dude Feb 07 '18 at 15:35
  • 2
    Read about member functions. Instead of `int LLSize(LL *head)` you should put `int size() const` into the `struct LL` block. Also getting the size should not create new lists. – nwp Feb 07 '18 at 15:35
  • It looks like you never initialized A. Your insert and delete pass by value. – drescherjm Feb 07 '18 at 15:36
  • 2
    Hint: `LL *current = new LL; current = head;` is just like `int x = 2; x = 8;`. There are many other issues. Read your C++ text book. – Jabberwocky Feb 07 '18 at 15:36
  • You are also supposed to `delete` everything you get from `new` and you don't do that. Luckily for you you don't need to and should instead read about and use [`std::unique_ptr`](http://en.cppreference.com/w/cpp/memory/unique_ptr) which takes care of that for you. – nwp Feb 07 '18 at 15:38
  • You should research "C++ linked list example" and compare the results to yours. – Thomas Matthews Feb 07 '18 at 16:01

1 Answers1

0

Don't use using namespace.

Create a type for the list and a type for the nodes

struct LL {
    LL* next;
    int data;
};

struct L {
    LL* head;
};

Use references and don't allocate new memory in each function

int LLSize(L& list) {
LL *current = list.head;

Check if the head of the list is set and use nullptr

if (list.head == nullptr) {

Use an instance of the list and not a pointer

int main() {
    int choice;
    L A;

Use a debugger like gdb to analyze your program.

Clean up at the end. Delete memory you allocated with new. One delete for each new.

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62