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

  • Home
  • SEARCH
  • 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 8812309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:34:34+00:00 2026-06-14T03:34:34+00:00

Please I need someone help. I’m having trouble with my homework. This homework is

  • 0

Please I need someone help. I’m having trouble with my homework.

This homework is very simple. Create a list(1,2,3) and delete the middle number by creating a function delnode. But it must use the function free().

Right now,I have created the list (1,2,3) by using linked list method. I want to delete the number 2 but it doesn’t work. It should be comes out with (1,3) but it comes out with (2,3).

#include <stdio.h>
#include <stdlib.h>


struct node{
    int number;
    struct node* next;
};

typedef struct node node;

//prototype function
node* allocateMemory(void);
node* insertNode(node*);
void delnode(node*);

int main(){
    int i,num;
    node* entr = allocateMemory();
    node* p = NULL;

    entr->number = 1;
    entr->next = NULL;

    num = 3;

    for(i=1;i<num;i++){
        if(!p){
            p=insertNode(entr);
        }else{
            p=insertNode(p);
        }

        p->number = i+1;
    }

    while(entr){
        if(entr->number == 2){
            entr->number == NULL;
            break;
        }
        entr=entr->next;
    }

    while(entr){
        printf("%d\n",entr->number);
        entr=entr->next;
    }

    return 0;
}

node* insertNode(node* current){
    node* newNode = allocateMemory();
    current->next = newNode;
    newNode->next = NULL;
    return newNode;
}


void delnode(node* current){
    node* temp = allocateMemory();
    temp = current->next->next;
    free(current->next);
    current = temp;
    free(temp);
    return ;
}

node* allocateMemory(void){
    return (node*)malloc(sizeof(node));
}
  • 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-14T03:34:35+00:00Added an answer on June 14, 2026 at 3:34 am

    Ok, this function delnode is all kinds of messed up. You don’t need to allocate more memory when you’re trying to delete a node. You need to link the previous to the next, and delete the current.

    | prev |      | current |      | next |
    |------|      |---------|      |------|
       /\              X               /\
       ||              X               ||
       |================================|
    

    It should look more like this.

    void delnode(node* current)
    {
      node* next = current->next;
      // Now we need to find the node previous to this.
      node* prev = entr; // From the start.
      while(prev->next != current) { prev = prev->next; };
      free(current); // delete the current node.
      prev->next = next; // Link the previous node to the next in the list.
    }
    

    If you’re trying to add three nodes, you need to change your loop.

    node* current = entr;
    for(i=0;i<3;i++)
    {
      int newCount = current->number++; // Increment count.
      current = insertNode(current); // Returns the new node.
      current->number = newCount; // Assign new count.
    }
    

    If you want to delete the node with number of 2:

    node* idx = entr;
    while(idx)
    {
      if(idx->number == 2) { delnode(idx); break; } // Delete node and break.
      idx = idx->next; // Else, go to next node.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please i need someone to help convert this query entity(c#) form. select * from
Someone help me please. Here's the thing, I need to create three dropdown lists
could someone please tell me what I need to do in order to create
Please i need help, this code below works fine on my localhost, php5.3+ but
please could someone help me with this. I can't seems to get my mind
I am very new to RegEx -- so can someone please help me figure
Can someone help me with this query please: I have data where customers have
please someone help out from this headaround problem of me; I have dynamically created
can someone help me please? I would like to order this multi dimensional array
I need someone to help me with this problem. My head doesn't want to

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.