Deleting a Linked List Node based on a user entered search term

Posted on 2005-04-14
Last Modified: 2010-04-15
Hi All,

I am creating a bank account database of some sort and need a suggestion on how to delete a node based on an account number entered by a user.  So far, my insert, search, and print functions seem to work just fine.  I can't get it to delete the node though when I use that function.  This code is lengthy, but like I said please focus on my void erase() function...basically it calls the struct node *find_account(int search) function, that function returns a node pointer back to erase() and then I figured from there I should be able to set that node to NULL, but I guess it isn't that simple.  This is just something I'm doing on my own to test my understanding of linked lists, not homework, so help is not urgent.  Thanks.

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <conio.h>
#include <string.h>

#define NameLen 50
/* Global Definitions */
struct node {
    int Account;
    double Balance;
    char fName[NameLen + 1];
    char lName[NameLen + 1];
    struct node *next;

struct node *bankInfo = NULL;

/* Prototype Declarations */
void print (void);
struct node *find_account(int search);
void search (void);
void insertAcct (void);
void erase (void);

int main ()

void print (void)
    struct node *p;
    printf("Account #     Balance             Name\n\n");
    for (p = bankInfo; p != NULL; p = p->next)
        printf("%7d       %8.2lf     %s\t\t%s\n", p->Account, p->Balance,
               p->lName, p->fName);
} /* print */

struct node *find_account(int search)
    struct node *p;
    for (p = bankInfo;
         p != NULL && search > p->Account;
         p = p->next)  
    if (p != NULL && search == p->Account)
        return p;
      return NULL;

void search (void)
    struct node *p;
    int search;
    printf("Enter the account number you wish to search: ");
    scanf("%d", &search);
    p = find_account(search);
    if (p != NULL) {
        printf("Account Name: %s, %s\n", p->lName, p->fName);
        printf("Account Balance: %8.2lf\n", p->Balance);
    } else
      printf("Account number not found.\n");        
} /* search */      

void insertAcct (void)
    struct node *cur, *prev, *new_node;
    new_node = (node *) malloc(sizeof(node));
    if (new_node == NULL) {
       printf("Database is full; can't add more accounts.\n");
    printf("Enter the new Account #: ");
    scanf("%d", &new_node->Account);

    for (cur = bankInfo, prev = NULL;
         cur != NULL && new_node->Account > cur->Account;
         prev = cur, cur = cur->next)
    if (cur != NULL && new_node->Account == cur->Account) {
       printf("Account number already exists.\n");
    printf("Enter starting balance: ");
    scanf("%lf", &new_node->Balance);
    printf("Enter last name: ");
    scanf("%s", &new_node->lName);
    printf("Enter first name: ");
    scanf("%s", &new_node->fName);
    printf("Account had been added.");
    new_node->next = cur;
    if (prev == NULL)
       bankInfo = new_node;
        prev->next = new_node;
} /* insertAcct */  

Question by:nuclearcane
    LVL 45

    Accepted Solution

    Hi nuclearcane,

    Long time no chat.  :)

    Deleting a node is pretty easy stuff, once you know the trick to it.  This is homework, so instead of giving you code, how about if I describe the process.  The code winds up being only a few lines.

    To delete a node, you must know the node immediately before the node that you want to delete.  This sounds silly, but it's true.  Picture a linked list of the alphabet:

      A -> B -> C -> D -> E -> F -> G -> ... -> Y -> Z

    Now let's assume that we want to delete node D.  How do we do that?

    Simple.  We change the pointer in the node that points to D to point to what D now points to.  Sounds complicate, but English is like that.  In practice, we want node C to point to node E.

    If we want to delete the last node (Z), everything works fine because Z points to NULL.  We'll change Y's next pointer to point to NULL and Z no longer terminates the chain.

    But it is a bit trickier to delete the first node.  To do that, you simply set HEAD (root, etc) to point to B, which is A->Next.

    That's all there is to it.  Try putting code to this and get back.  If you're still struggling I'll be glad to help some more.

    Good Luck!
    LVL 16

    Expert Comment


    This image might help.

    Imagine the list as a row of people, each with their right hand on the shoulder of the person to their right.

    If you were in the middle of the chain and you wished to remove yourself froim it you would take the hand of the person on your left (resting on your left shoulder) and place it on the shoulder of the person to your right (you already jave your hand on their shoulder).

    Now treat the two special cases as special (you are first or last in the chain).

    Once you have this picture covered and coded and working it's entertaining to imagine a doubly-linked list as everyone is holding hands. Remember that you can only do one thing at a time.


    LVL 1

    Assisted Solution


    accepted answer by sunnycoder is well expressed.

    cheers - emms


    Author Comment

    Here's a recommendation.  I think you can all just take a hike.  I explicity made it clear from the beginning that this was not homework but the so called "experts" deemed it hw.  What's the point of paying to use this crap if I have to figure out a question on my own?  I have canceled my subsciption. Do whatever you want with this question.
    LVL 45

    Expert Comment


    I was somewhat taken aback by nuclearcane's response.  At least until I checked his question history.  I've assisted on several of his questions and have made the homework qualification before.  It's never been an issue.

    He's only been around for a few months, and the subject of all 6 of his questions has been about maintaining linked lists.  He seems to accept the answer of the person providing the most code, not necessarily the best explanation.  One accepted answer utilized pointer to pointer logic.  I find it hard to believe that he's struggling with simple list management but can grok double dereferencing.

    I believe that nuclearcane is a student.  Whether he's in a formal classroom setting or is participating in a self study program is unknown and doesn't matter.  But I'm highly suspicious of his question and answer history.  And his little outburst is strangely coincidental with the spring end-of-term.

    The guy seemed alright.  I'm sorry to see him go.  But it's perhaps better that he leaves on his own before the EE administration culls him from the herd.

    LVL 16

    Expert Comment


    You've done right all down the line. I'm not sure what it was that specifically annoyed him, I suspect it was Emms timely reminder that EE is used by fellow students so we were no longer likely to provide source. At least, the excercise was so simillar to make it likely.

    Anyway, lets hope he pays another visit. Its a shame to lose someone on a bad note, however many rules we obey.

    LVL 1

    Expert Comment

    dear all,

    just for the update, i have graduated last december.

    cheers - emms

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now