Converting a general list using an array to now use a linked list

Oh boy. Linked lists are not my friend. While my understanding is better than it was last year, it is still pretty bad. I completed the "General List" assignment, and I feel like I did a good job. That used an array to hold the list and so it was easy to navigate through. Now, however, the assignment is to convert that program to a "General Linked List" and I'm stumped. Who out there is a link list ninja? Can anyone help? I was hoping to be able to tweak the functions only where they jump through the array, but I can't seem to figure out how to do it. For instance, the following is the General List code for a function. I can start with converting this one function, and then maybe be better able to understand how to convert the rest myself...

Thanks in advance to anyone who will help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//**************************************************************************
void list::insertBefore(ET value)
// Precondition: Current position is not already first position in array
// Postcondition: New number has been inserted before current position.
{
    if ((mySize + 1) < CAPACITY)    // If we are within the boundaries of the array
        if (mySize == 0)            // If we are in an empty array
        {
            myList[0] = value;      // Set num to first position
             pos++;                 // Move position to the right
             mySize++;              // mySize increases by one
        }
        else
        // If not an empty array -shuffle list before you insert new number
        {
            for (int i = mySize; i > pos; i--)
                myList[i] = myList[i - 1];
            myList[pos] = value;    // Insert value into current position
            pos++;                  // Move position holder to the right
            mySize++;               // mySize has increased by one
        }
}
Can't anyone help?
closed account (D80DSL3A)
A linked list is not like an array. Data is stored in a node structure, which also has a link to the next node. These nodes are allocated dynamically one at a time and are linked together as you build the list.

For a node which stores an integer you could declare:
1
2
3
4
5
struct node
{
    int data;
    node* next;// this is the link. It points to the next node in the list.
}

Do you have any code for building a linked list? Probably this was covered in your class or is in your textbook.
Search this site for threads about linked lists. You will find hundreds of them.
Topic archived. No new replies allowed.