Question:
Help with deleting a node in a linked list in c?
anonymous
2014-02-10 17:54:38 UTC
everything works perfectly, minus my delete function. When I try to delete a number that is not in the linked list the program seg faults. I believe it is because I am not checking for NULL at the end, but I am confused and don't know where to put what code now. I appreciate the help.



HERE ARE THE DIRECTIONS FOR MY ASSIGNMENT;


You should implement the following two functions using the given struct:
typedef struct node_{
int value;
struct node_* next;
}node;

node* insert_top(node* head, int value);
node* delete(node* head, int value);
void print(node* head);

You should write a main function, which begins by creating an empty linked list. For the remainder of the program, the user will be prompted for 5 numbers the keyboard. Every time the user enters an integer, the insert function will be called and that number will be added into a linked list at the top, this means that the new value will become the new head of the linked list. Afterwards the user will be prompted for three numbers. For each number, the delete function will be called and that number will be removed from the linked list. If the number does not exist in the list then the linked list should not be changed. After each insertion and deletion the linked list will be printed onto the screen.

Sample Output:
$ gcc prelab3.c
$ ./a.out
insert: 3
3 -> NULL
insert: 4
4 -> 3 -> NULL
insert: 1
1 -> 4 -> 3 -> NULL
insert: 2
2 -> 1 -> 4 -> 3 -> NULL
insert: 9
9 -> 2 -> 1 -> 4 -> 3 -> NULL
delete: 2
9 -> 1 -> 4 -> 3 -> NULL
delete: 4
9 -> 1 -> 3 -> NULL
delete: 10
9 -> 1 -> 3 -> NULL




HERE IS MY CODE FOR THE ASSIGNMENT

#include
#include

typedef struct node_{
int value;
struct node_* next;
}node;

node* insert_top(node* head, int value);
node* delete(node* head, int value);
void print(node* head);

int main()
{

int insertValue, deleteValue;
int x = 0;
int y = 0;

//Creating empty linked list
node *head = (node*)malloc(sizeof(node));
head->value = insertValue;
head->next = NULL;


//While loop so I only get 5 numbers from the user to input
for(x = 0; x < 5; x++){
printf("Insert: ");
scanf("%d", &insertValue);

head = insert_top(head,insertValue);
print(head);
}


for(y = 0; y < 3; y++){
printf("delete: ");
scanf("%d", &deleteValue);

//call delete function
head = delete(head,deleteValue);
print(head);


}
}


node* insert_top(node *head, int value){

node *temp = (node*)malloc(sizeof(node));
temp->value = value;
temp->next = head;
head = temp;

return head;
}


node* delete(node* head, int deleteValue){

if(head->value == deleteValue){

node* temp = head;

head = head->next;
free(temp);

}
else{
node* temp2 = head;

while(temp2->next->value != deleteValue)
temp2 = temp2->next;

node* temp3 = temp2->next;

temp2->next = temp3->next;
free(temp3);
}

return head;
}

void print(node* head){

//Declare a new node and store the value for head in it
node* new = head;

//While new at next does not eqaul NULL
while(new->next != NULL){
//If the new node is null then print null
if(new==NULL){
printf("NULL");
break;
}

//print the numbers
printf("%d", new->value);
printf(" -> ");
new = new->next;
}
printf("NULL\n");

}
Three answers:
Jonathan
2014-02-10 21:12:34 UTC
Try the following. Note that you really should free up the entire list when done, so I added one more subroutine to do that for you.



#include

#include

typedef struct node_ {

    int value;

    struct node_ *next;

} node;

node * insert_top( node *head, int value );

node * delete( node *head, int value );

void print( node *head );

node * free_all( node *head );

int main( void ) {

    int x;

    node * head= 0;

    for ( x= 0; x < 5; ++x ) {

        int insertValue;

        printf( "Insert: " );

        scanf( "%d", &insertValue );

        head= insert_top( head, insertValue );

        print( head );

    }

    for ( x= 0; x < 3; ++x ) {

        int deleteValue;

        printf( "Delete: " );

        scanf( "%d", &deleteValue );

        head= delete( head, deleteValue );

        print( head );

    }

    head= free_all( head );

    return 0;

}

node * insert_top( node *head, int value ) {

    node *n= (node *) malloc( sizeof( node ) );

    n->value= value;

    n->next= head;

    return n;

}

node * delete( node *head, int value ) {

    node **p, *q;

    for ( p= &head; (q= *p) != 0; p= &q->next )

        if ( q->value == value ) {

            *p= q->next;

            free( q );

            break;

        }

    return head;

}

void print( node *head ) {

    if ( head != 0 ) {

        printf( "%d -> ", head->value );

        print( head->next );

    } else printf( "NULL\n" );

}

node * free_all( node *head ) {

    if ( head->next != 0 ) free_all( head->next );

    free( head );

    return 0;

}
anonymous
2014-02-10 20:56:06 UTC
Refer this*

https://www.cs.bu.edu/teaching/c/linked-list/delete/

http://www.geeksforgeeks.org/delete-a-given-node-in-linked-list-under-given-constraints/
anonymous
2014-02-10 21:31:29 UTC
jack = all_work && no_play ? dull_boy : bright_boy;





#include



struct node {

  int v;

  struct node *next;

};



struct node *new(int v, struct node *next) {

  static struct node L[5]; static int i;

  return L[i].v = v, L[i].next = next, L + i++;

}



void print(const struct node *np) {

  !np ? (printf("NULL\n"), (void) 0) : (printf("%d -> ", np->v), print(np->next));

}



void d_(int v, struct node *np) {

  np->next && (np->next->v == v ? (np->next = np->next->next) : (d_(v, np->next), NULL));

}



struct node *drop(int v, struct node *np) {

  return !np ? NULL : (np->v == v ? np->next : (d_(v, np), np));

}



int main(void)

{

  struct node *l = NULL;

  int i, n, r;



  for (i = 0; i < 5 && (r = scanf("%2d", &n)) == 1; i++)

    print(l = new(n, l));

  if (r == 1)

    for (i = 0; i < 3 && scanf("%2d", &n) == 1; i++)

      print(l = drop(n, l));

  return 0;

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...