• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 351
  • Last Modified:

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

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);
       
   
   
    return;
} /* 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);
    system("cls");
    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");
       return;
    }
   
    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");
       free(new_node);
       return;
       }
       
    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);
    system("cls");
    printf("Account had been added.");
   
    new_node->next = cur;
    if (prev == NULL)
       bankInfo = new_node;
    else
        prev->next = new_node;
                 
} /* insertAcct */  


0
nuclearcane
Asked:
nuclearcane
  • 2
  • 2
  • 2
  • +1
2 Solutions
 
Kent OlsenData Warehouse Architect / DBACommented:
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!
Kent
0
 
PaulCaswellCommented:
Nuclearcane,

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.

Paul

0
 
EmmsCommented:

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20890286.html

accepted answer by sunnycoder is well expressed.

cheers - emms

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
nuclearcaneAuthor Commented:
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.
0
 
Kent OlsenData Warehouse Architect / DBACommented:

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.


Kent
0
 
PaulCaswellCommented:
Kent,

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.

Paul
0
 
EmmsCommented:
dear all,

just for the update, i have graduated last december.


cheers - emms
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now