Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7886033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:13:12+00:00 2026-06-03T05:13:12+00:00

I was trying to write a function that deletes a node from the linked

  • 0

I was trying to write a function that deletes a node from the linked list, though I was having trouble.

Here is my algorithm:

  • Get the name of the node that I want to delete
    (every node has 3 details in it: name/age/gender)
  • Then I find its place in the list
  • and then I pass it forward

For example

Friend -> next = friend -> next -> next..

Though I need to find the first node in the linked list, and I’m not sure how to get to it.
This is what I’ve written:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
typedef struct friend
{
    char *name;
   int age;
   char gender;
   struct friend* next;
}friend;
void node_delete(friend* delete)
{
 friend* temp = malloc(sizeof(friend)); 
 char name[256];
 int i = 0, j =0; // Not being used, though I'd use it
 printf ("Please enter the friend's name you want to delete: \n");
 fgets (name, 256, stdin); // Getting the name of the person the user wants to delete
 fgets (name, 256, stdin);
 while (0 == (strcmp(temp -> next -> name, delete -> next -> name))) // As long as the           
 // name doesnt match, it'll go to the next name in the linked list
 {
       temp = friend -> next; // Going to the next name in the linked list
 }
 temp -> next = temp -> next -> next; // Replacing the node with the node after it..
// for ex. if I have 1 -> 2 -> 3, it'll be 1 -> 3
 free (delete);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:13:14+00:00Added an answer on June 3, 2026 at 5:13 am

    Well, you have had a good crack at this.

    I’m not entirely sure about your question but here is what I think you might be trying to do:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdlib.h>
    
    typedef struct friend
    {
        char *name;
        int age;
        char gender;
        struct friend* next;
    }friend;
    
    
    void node_delete(const char* name, friend* stFriend)
    {
        if (!stFriend->next) //end of list
        { 
            printf("%s is not a friend!\n", name);
        }
        else if ( !strcmp(name, stFriend->next->name) ) //name matches! remove link
        {     
            //here's where you can free your unwanted friend ptr ~NB: are you sure this friend is not a friend of someone else?
            //free(stFriend -> next);    
            stFriend->next=stFriend->next->next;
            printf("%s is no longer a friend!\n", name); 
        }
        else //name does not match -recurse
        {
            node_delete(name, stFriend->next);
        }
    }
    
    
    void print_friends(const friend* pstPerson)
    {
        if (pstPerson->next)
        {
            printf("next friend:%s\n", pstPerson->next->name);
            print_friends(pstPerson->next);
        }
        else 
        {
            printf("no more friends :(\n\n");
        }
    }
    
    int main()
    {
        friend stFriend0, stFriend1, stFriend2, stPerson;
        char name[256]={0};
    
        stFriend0.name="amber";
        stFriend0.next=0;
    
        stFriend1.name="betty";
        stFriend1.next=&stFriend0;
    
        stFriend2.name="catherine";
        stFriend2.next=&stFriend1;
    
        stPerson.name="violet";
        stPerson.next=&stFriend2;
    
        printf("%s's friends before:\n", stPerson.name);
        print_friends(&stPerson);
    
        printf("remove a friend: ");
        fgets (name, 256, stdin);
        strtok(name, "\n");
    
        node_delete(name, &stPerson);
    
        printf("\n\n%s's friends after:\n", stPerson.name);
        print_friends(&stPerson);
    
        printf("\n\n\ndone!\n");
    
        return 0;
    

    }

    online demo.

    It assumes that your node_delete(friend* delete) function takes a person, with some friends in the linked list and removes a friend who’s name matches the stdin input.

    (i also added another little function so you can see how much fun a linked list can be!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a function that will remove a query argument from a
I am trying to write a function that will filter a list of tuples
I am trying to create a function that deletes an entire list, but I
I am trying write a function that generates simulated data but if the simulated
I'm trying to write a function that formats every (string) member/variable in an object,
I am trying to write a function that will pull the name of a
I'm trying to write a function that is able to determine whether a string
i am trying to write a function that will make DataRow[column] return nullable typed
I'm trying to write a function that will let me red-shift or blue-shift a
I am trying to write a function that includes a file named header.php in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.