1

What I have been asked to do: Q) A sentence consists of a list of characters terminated by a full stop. Write a function which returns a linked list of characters, where the characters are typed in by the user and added to the list. The returned list should include the full stop. It should be called as:

LinkedList<char> *sentence;
sentence = setUpSentence();

I have attempted to write pieces of this but I'm struggling as this is my first time working with linked lists & functions like this.

Main File

#include "LinkedList.h"
    #include "ListNode.h"
    #include "Node.h"
    #include <iostream>
    #include <stdlib.h>
    using namespace std;

    LinkedList<char> *setUpSentence() {
        //allocate the linked list objet
        LinkedList<char> *sentence = new LinkedList<char>();
        char ch;
        do {
            cout << "Enter characters to add, enter full stop to finish adding." << endl;
            ch = cin.get();
            sentence->addAtEnd(ch);
        } while (ch != '.');

        return sentence;
    }

    int main() {

        //call the function, store the returned pointer in sentence variable
        LinkedList<char> *sentence = setUpSentence();
        //working with the linked list
        sentence = setUpSentence();



        cout << sentence->getAtFront() << endl;

//delete to avoid memory leak
        delete sentence;
    }

The error I am getting when attempting to run this attempt at beginning to write this function is :

after entering 1 character into the console and pressing enter, the loop continues and outputs "enter characters to add...." but this appears twice each time after entering a character?

Any ideas?

Code from functions from the LinkedList.h file that are used in my main:

template <typename T>
void LinkedList<T>::addAtEnd(T item)
{
    if (size == 0)
        addAtFront(item);
    else
    {
        // ListNode<T>* temp = findAt(size - 1);
        ListNode<T> *l = new ListNode<T>(item, last, nullptr);
        last->next = l;
        last = l;
        size++;
    }
}

template <typename T>
T LinkedList<T>::getAtFront()
{
    if (size > 0)
    {
        current = first;
        return first->item;
    }
    else return NULL;
}

edit: addAtFront Method from LinkedList.h file

template <typename T>
void LinkedList<T>::addAtFront(T item)
{
    ListNode<T> *l = new ListNode<T>(item, NULL, first);
    first = l;
    if (last == NULL)
        last = l;
    size = 1;
}
6
  • That recursive call should obviously be questionable, especially considering the result is completely unused. Secondly, you didn't provide all the relevant code, leaving us to have to guess that addAtFront is implemented correctly. Commented Jan 27, 2017 at 12:29
  • @whozCraig my code updated with alex Petrenko's suggestions & addAtFront method included. Commented Jan 27, 2017 at 12:50
  • You can't delete sentence then use cout later on sentence. Commented Jan 27, 2017 at 13:04
  • Is there any problem with the code now? I don't think the original error applies any more for the changed code. Commented Jan 27, 2017 at 13:09
  • 1
    You should not be calling setUpSentence() 2 times. The first time will leak memory. Commented Jan 27, 2017 at 13:10

1 Answer 1

2

I don't know why you're trying to make a recursive call to setUpSentence() in the return statement, this does not make sense. I think it should look like this instead:

LinkedList<char> * setUpSentence() {
    // make sure to allocate the linked list object
    LinkedList<char> *sentence = new LinkedList<char>();
    char ch;
    do {
        cout << "Enter characters to add, enter full stop to finish adding." << endl;
        ch = cin.get();
        sentence->addAtEnd(ch);
    } while (ch != '.');

    return sentence;
}

int main() {
    // calling the function, store the returned pointer in sentence variable
    LinkedList *sentence = setUpSentence();

    // ... working with the linked list ...

    delete sentence;  // don't forget to delete it to avoid memory leak!
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.