Skip to content

Convert string to list_entry

Frank

Asked May 22, 2010, 3:28 PM EDT2 answers
Share
LinkedInX
I have a class for lists. It's template class that takes a type of list_entry:
class cSingleList<list_entry>

I have an insert function defined as such:
public void insert(int position, list_entry entry)

What insert does is it takes an int so that we can insert a value (list_entry entry) at a certain position (int position) in the list.

Finally, I have a function:

public void read_file_insert(string filepath)
        {
            // Pre: A filepath to a file that only has integers delimited by a newline
            // and a cSingleList obejct.
            // Post: Reads the file given by the filepath and then inserts the data
            // into a cSingleList object by calling its insert function.

            string line = "";
            int index = 0;
            TextReader fin = new StreamReader(filepath);

            while ((line = fin.ReadLine()) != null)
            {
                this.insert(index, line);
                index++;
            }
        }

What this does is it reads through a file that contains integers and then we call the insert function, passing it the position we want (index) and then the value (line).

However, I always get an error stating:
Argument '2': cannot convert type 'string' to type 'list_entry'.

How can I convert a string (line) to type list_entry?

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Post your List_Entry class and I can help you further with the issue
    0
  • 0
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;

    namespace project3_FHN
    {
        class cSingleList<list_entry>
        {
            // This delegate will take references to other functions that take one
            // list_entry value as a parameter and listDelegate will pass the list_entry value
            // to that function
            public delegate void listDelegate(list_entry value);

            private cNode<list_entry> head;
            private int count;

            public cSingleList()
            {
                // Post: Creates a new cSingleList object and sets head to null
                head = null;
                count = 0;
            }

            public cSingleList(cNode<list_entry> nodePointer)
            {
                // Post: Creates a new cSingleList object and adds a node to the list
                // If head is null, head will now equal the new node. Also, increment count
                if (head == null)
                    head = nodePointer;
                count++;
            }

            public cNode<list_entry> set_position(int position)
            {
                // Pre: Position is is a valid position in the list
                // Post: We create a pointer that'll point to the head of the list. We
                // will then start stepping through the list until we reach the position
                // desired. We will then return the pointer, which will then be pointing
                // to that position.

                cNode<list_entry> pointer = head;
                for (int i = 0; i < position; i++)
                    pointer = pointer.next;
                return pointer;
            }

    public void read_file_insert(string filepath)
            {
                // Pre: A filepath to a file that only has integers delimited by a newline
                // and a cSingleList obejct.
                // Post: Reads the file given by the filepath and then inserts the data
                // into a cSingleList object by calling its insert function.

                string line = "";
                int index = 0;
                TextReader fin = new StreamReader(filepath);

                while ((line = fin.ReadLine()) != null)
                {
                    this.insert(index, line);
                    index++;
                }
            }


            public void insert(int position, list_entry entry)
            {
                /*
                 * Pre: A node class that has a next pointer that can be set through
                 * a property
                 * Post: We specify the position that we want to insert the new node
                 * into. First, we must make sure that the position desired is between
                 * 0 and count (return an out-of-bounds error if this occurs). If it's
                 * valid, we then set a pointer (ptr) to equal head We then step through the
                 * list in a loop until ptr reaches the position minus one (use set_pointer()).
                 * Now, we are pointing to the node just behind the position that we want.
                 * We set the new node's pointer to point to ptr->next. Then, we have
                 * ptr->next point to the new node. We now have a new node inserted.
                 */

                const string STR_ERROR_OVERFLOW = "No more memory! Overflow error!";
                string STR_ERROR_OUT_OF_BOUNDS = "You cannot insert a node at a position that is less than zero or greater than the size of this list, which is " + count;

                if (position < 0 || position > count)
                    throw new cOutOfBoundsException(STR_ERROR_OUT_OF_BOUNDS);
                else
                {
                    cNode<list_entry> ptr = null;
                    cNode<list_entry> newNode = new cNode<list_entry>(entry);
                    if (newNode == null)
                        throw new OverflowException(STR_ERROR_OVERFLOW);
                    if (position > 0)
                    {
                        ptr = set_position(position - 1);
                        newNode.next = ptr.next;
                        ptr.next = newNode;
                    }
                    else
                    {
                        newNode.next = head;
                        head = newNode;
                    }
                    count++;
                }
            }

            public void insert_sort(cDoubleList<list_entry> doublePtr)
            {
                /*
                 * Pre: A pointer to a double list
                 * Post: We sort the nodes in the list according to their data value
                 * We'll use the insertion sort method. We'll start at the head of our
                 * list, and start inserting the nodes into the double list.
                 */

                cNode<list_entry> sortedHead, firstUnsorted, lastSorted, current;
                int index = 0;

                if (head != null)
                {
                    lastSorted = head; // The first element is considered sorted until further notice
                    doublePtr.insert(0, head.retrieve()); // Insert the first element
                    sortedHead = doublePtr.set_position(0); // This keeps track of the head of the sorted list
                    while (lastSorted.next != null) // When this is false, the list is sorted
                    {
                        firstUnsorted = lastSorted.next;
                        if (firstUnsorted < sortedHead)
                        {
                            doublePtr.insert(0, firstUnsorted.retrieve());
                            lastSorted = firstUnsorted;
                            sortedHead = doublePtr.set_position(0); // A new sorted head has been made . . . get it.
                        }
                        else
                        {
                            current = sortedHead;
                            current = current.next;
                            if (current != null)
                            {
                                for (index = 0; firstUnsorted > current; index++)
                                {
                                    current = current.next;
                                    if (current == null)
                                    {
                                        index++; // Because index won't be able to be added one last time since current equals null, an error will be thrown instead
                                        break; // We've reached the end of the list
                                    }
                                }
                            }
                            
                            // The node is already in the correct place
                            if (firstUnsorted == current)
                            {
                                doublePtr.insert(index, firstUnsorted.retrieve());
                                lastSorted = firstUnsorted;
                            }
                            else
                            {
                                doublePtr.insert(index + 1, firstUnsorted.retrieve());
                                lastSorted = firstUnsorted;
                            }
                        }
                    } // END OF WHILE
                } // END OF HEAD NOT NULL
            }

            public void traverse(listDelegate func)
            {
                listDelegate funcPointer = new listDelegate(func);
                for (int i = 0; i < count; i++)
                {
                    funcPointer(set_position(i).retrieve());
                }
            }  
        }
    }

    I believe that's how it was. I've changed it since so that it only excepts integers, but it should be pretty accurate.
    0