I'm teaching myself C with the book Programming in C by Stephen Kochan and I have come to the following exercise on pointers:
- Write a function called
insertEntry
to insert a new entry into a linked list. Have the procedure take as arguments a pointer to the list entry to be inserted (of typestruct entry
as defined in this chapter), and a pointer to an element in the list after which the new entry is to be inserted.
The struct entry
structure is defined as follows:
struct entry
{
int x;
struct entry *ptr;
};
This is my code:
#include <stdio.h>
void insert_entry (struct entry *new_entry, struct entry *prev_entry);
struct entry
{
int x;
struct entry *ptr;
};
int main (void)
{
struct entry n1, n2, n3, new_entry;
struct entry *list_ptr = &n1;
n1.x = 1;
n1.ptr = &n2;
n2.x = 2;
n2.ptr = &n3;
n3.x = 4;
n3.ptr = (struct entry *) 0;
insert_entry (&new_entry, &n2);
// Loop to display the list to check if insert_entry worked
while ( list_ptr != (struct entry *) 0 ) {
printf ("%i\n", list_ptr->x);
list_ptr = list_ptr->ptr;
}
}
void insert_entry (struct entry *new_entry, struct entry *prev_entry)
{
new_entry->ptr = prev_entry->ptr;
prev_entry->ptr = new_entry;
// Assign a value to make it easier to check if the function worked
new_entry->x = 3;
}
This code works just fine and gets the job done. But then there's exercise #3:
- The function developed in exercise 2 only inserts an element after an existing element in the list, thereby preventing you from inserting a new entry at the front of the list. How can you use this same function and yet overcome this problem? (Hint: Think about setting up a special structure to point to the beginning of the list.)
I have no idea how to continue. If I set up another structure I won't be able to pass it as an argument without changing the formal parameters of the insert_entry
function takes. But I can't use the list_ptr
pointer, because if I passed it as the second argument to the insert_entry
function the statement new_entry->ptr = prev_entry->ptr;
wouldn't make sense.
Other questions about this focus on exercise #2 but I wasn't able to find anything on this. Help would be eternally grateful. Thanks in advance.
*EDIT: This is what my code looks like now (thanks to u/ringzero):
#include <stdio.h>
void insert_entry (struct entry *new_entry, struct entry *prev_entry);
struct entry
{
int x;
struct entry *ptr;
};
int main (void)
{
struct entry n_start, n1, n2, n3, new_entry;
struct entry *list_ptr = &n_start;
n_start.ptr = &n1;
n1.x = 1;
n1.ptr = &n2;
n2.x = 2;
n2.ptr = &n3;
n3.x = 3;
n3.ptr = (struct entry *) 0;
insert_entry (&new_entry, &n_start);
// Loop to display the list to check if insert_entry worked
while ( list_ptr != (struct entry *) 0 ) {
printf ("%i\n", list_ptr->x);
list_ptr = list_ptr->ptr;
}
}
void insert_entry (struct entry *new_entry, struct entry *prev_entry)
{
new_entry->ptr = prev_entry->ptr;
prev_entry->ptr = new_entry;
// Assign a value to make it easier to check if the function worked
new_entry->x = 0;
}
n_start
is supposed to be a dummy structure. The only problem with the code is that the loop displays the value of n_start.x
. How can I make it so that it doesn't get displayed?