Matt Cleary Matt Cleary - 4 months ago 18
C Question

How can I rewrite my removeString function with only one argument?

I'm working on an exercise in 'Programming with C'.

I have to create a linked-list and then create a function that removes an entry.

I have managed to do this without too many problems. However, the exercise specifically asks that I do this with just one argument. I cannot work out how to do this.

This is the exercise: "Write a function called

removeEntry()
to remove an entry from a linked list. The sole argument to the procedure should be a pointer to the list. Have the function remove the entry after the one by the argument."

My code is below, as you can see, I've made it work with two arguments. How can I possibly assign the next value in order to remove the one in the middle without using two? I've reread everything within the book, searched this site and used Google extensively and I still can't work this out.

Thank you.

#include <stdio.h>


struct entry
{
int value;
struct entry *next;
};


void removeEntry(struct entry *start, struct entry *remove);


int main (void)
{

//declarations
struct entry n1, n2, n3;
struct entry *list_pointer = &n1;


//creates list values and links
n1.value = 100;
n1.next = &n2;

n2.value = 200;
n2.next = &n3;

n3.value = 300;
n3.next = (struct entry *) 0;

//prints out all list values
while(list_pointer != (struct entry *) 0)
{
printf("%i\n", list_pointer->value);
list_pointer = list_pointer->next;
}

//resets list_pointer back to start
list_pointer = &n1;

//calls function and removes n2 from list by directly linking n1 to n3
removeEntry(&n1, &n3);

//print out amended list
while(list_pointer != (struct entry *) 0)
{
printf("%i\n", list_pointer->value);
list_pointer = list_pointer->next;
}

return 0;
}

void removeEntry(struct entry *start, struct entry *remove)
{
start->next = remove;
}


This is my code for exercise 10.3.

The dummy structure is
struct entry *list_pointer = &n1;
which is used to reference the start of the list throughout the program.

#include <stdio.h>

struct entry
{
int value;
struct entry *next;
};

void insertEntry(struct entry *addOn, struct entry *element);

int main (void)
{


struct entry n1, n2, n3, addOn;
struct entry *list_pointer = &n1;

n1.value = 100;
n1.next = &n2;

n2.value = 200;
n2.next = &n3;

n3.value = 300;
n3.next = (struct entry *) 0;

while(list_pointer != (struct entry *) 0)
{
printf("%i\n", list_pointer->value);
list_pointer = list_pointer->next;
}

list_pointer = &n1;
insertEntry(&addOn, &n1);
list_pointer = &addOn;

while(list_pointer != (struct entry *) 0)
{
printf("%i\n", list_pointer->value);
list_pointer = list_pointer->next;
}

return 0;
}

void insertEntry(struct entry *addOn, struct entry *element)
{

//element->next = addOn;
addOn->value = 400;
addOn->next = element;
//addOn->next = (struct entry *) 0;
}

Answer

I think this is what you're looking for:

void removeEntry(entry *start){
    if(start->next != NULL) {
        start->next = start->next->next;
    } 
    //else you can't really remove the next one, probably log or something then
}
Comments