Thread: Using erase function Help

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    48

    Using erase function Help

    I have got this program, inventory2.c, which allows a user to store a database of parts. It uses command-line switches to determine what course of action to take. I'm trying to add a command-line switch ' e ' which will allow you to erase a part from the database. I Insert a part, Part Number: 1, Name: Wrench, Quantity: 2, then i search for that part, it does what its suppose to, then i try to erase it. After i do my erase function, i use the s switch to search for it and the part isnt found, But when i use the insert switch again and enter part number 1, it says it already exists. Any help? Im stumped on what I need to do.

    Code:
    /*********************************************************
     * From C PROGRAMMING: A MODERN APPROACH, Second Edition *
     * By K. N. King                                         *
     * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. *
     * All rights reserved.                                  *
     * This program may be freely distributed for class use, *
     * provided that this copyright notice is retained.      *
     *********************************************************/
    
    /* inventory2.c (Chapter 17, page 434) */
    /* Maintains a parts database (linked list version) */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "readline.h"
    
    #define NAME_LEN 25
    
    struct part {
        int number;
        char name[NAME_LEN+1];
        int on_hand;
        struct part *next;
    };
    
    struct part *inventory = NULL;   /* points to first part */
    
    struct part *find_part(int number);
    void insert(void);
    void search(void);
    void update(void);
    void print(void);
    void erase(void);
    
    /**********************************************************
     * main: Prompts the user to enter an operation code,     *
     *       then calls a function to perform the requested   *
     *       action. Repeats until the user enters the        *
     *       command 'q'. Prints an error message if the user *
     *       enters an illegal code.                          *
     **********************************************************/
    int main(void)
    {
        char code;
    
        for (;;) {
        printf("Enter operation code: ");
        scanf(" %c", &code);
        while (getchar() != '\n')   /* skips to end of line */
            ;
        switch (code) {
        case 'i': insert();
            break;
        case 's': search();
            break;
        case 'u': update();
            break;
        case 'p': print();
            break;
        case 'e': erase();
            break;
        case 'q': return 0;
        default:  printf("Illegal code\n");
        }
        printf("\n");
        }
    }
    
    /**********************************************************
     * find_part: Looks up a part number in the inventory     *
     *            list. Returns a pointer to the node         *
     *            containing the part number; if the part     *
     *            number is not found, returns NULL.          *
     **********************************************************/
    struct part *find_part(int number)
    {
        struct part *p;
    
        for (p = inventory;
         p != NULL && number > p->number;
         p = p->next)
        ;
        if (p != NULL && number == p->number)
        return p;
        return NULL;
    }
    
    /**********************************************************
     * insert: Prompts the user for information about a new   *
     *         part and then inserts the part into the        *
     *         inventory list; the list remains sorted by     *
     *         part number. Prints an error message and       *
     *         returns prematurely if the part already exists *
     *         or space could not be allocated for the part.  *
     **********************************************************/
    void insert(void)                //Insert function that is saying the part already exists
    {
        struct part *cur, *prev, *new_node;
    
        new_node = malloc(sizeof(struct part));
        if (new_node == NULL) {
        printf("Database is full; can't add more parts.\n");
        return;
        }
    
        printf("Enter part number: ");
        scanf("%d", &new_node->number);
    
        for (cur = inventory, prev = NULL;
         cur != NULL && new_node->number > cur->number;
         prev = cur, cur = cur->next)
        ;
     
        if (cur != NULL && new_node->number == cur->number) {
        printf("Part already exists.\n");
        free(new_node);
        return;
        }
    
        printf("Enter part name: ");
        read_line(new_node->name, NAME_LEN);
        printf("Enter quantity on hand: ");
        scanf("%d", &new_node->on_hand);
    
        new_node->next = cur;
        if (prev == NULL)
        inventory = new_node;
        else
        prev->next = new_node;
    }
    
    /**********************************************************
     * search: Prompts the user to enter a part number, then  *
     *         looks up the part in the database. If the part *
     *         exists, prints the name and quantity on hand;  *
     *         if not, prints an error message.               *
     **********************************************************/
    void search(void)
    {
        int number;
        struct part *p;
    
        printf("Enter part number: ");
        scanf("%d", &number);
        p = find_part(number);
        if (p != NULL) {
        printf("Part name: %s\n", p->name);
        printf("Quantity on hand: %d\n", p->on_hand);
        } else
        printf("Part not found.\n");
    }
    
    /**********************************************************
     * update: Prompts the user to enter a part number.       *
     *         Prints an error message if the part doesn't    *
     *         exist; otherwise, prompts the user to enter    *
     *         change in quantity on hand and updates the     *
     *         database.                                      *
     **********************************************************/
    void update(void)
    {
        int number, change;
        struct part *p;
    
        printf("Enter part number: ");
        scanf("%d", &number);
        p = find_part(number);
        if (p != NULL) {
        printf("Enter change in quantity on hand: ");
        scanf("%d", &change);
        p->on_hand += change;
        } else
        printf("Part not found.\n");
    }
    /**********************************************************
     * erase:  Prompts the user to enter a part number.       *
     *         Prints an error message if the part doesn't    *
     *         exist; otherwise,it deletes the part from      *
     *         the database.                                  *
     **********************************************************/
    void erase(void) {                  //Erase function 
        int number;
        struct part *p;
    
        printf("Enter part number: ");
        scanf("%d", &number);
        p = find_part(number);
        if(p != NULL) {
        free(p);
        } else
        printf("Part not found.\n");
    }
    /**********************************************************
     * print: Prints a listing of all parts in the database,  *
     *        showing the part number, part name, and         *
     *        quantity on hand. Part numbers will appear in   *
     *        ascending order.                                *
     **********************************************************/
    void print(void)
    {
        struct part *p;
    
        printf("Part Number   Part Name                  "
           "Quantity on Hand\n");
        for (p = inventory; p != NULL; p = p->next)
        printf("%7d       %-25s%11d\n", p->number, p->name,
               p->on_hand);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > p = find_part(number);
    > if(p != NULL) {
    > free(p);
    Look at insert, and the care with which it deals with the linked list to make sure the next pointer always points to somewhere.

    You can't just free(p) and hope the rest happens by magic.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    I guess i'm still not following on what needs to happen. Anyway you can describe in words what needs to go on in my erase function besides what I have?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by clmoore3 View Post
    I guess i'm still not following on what needs to happen. Anyway you can describe in words what needs to go on in my erase function besides what I have?
    You have to update the next pointer of the previous node.
    Kurt

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    :0       :1       :2       :3       :4       :5
    +----+   +----+   +----+   +----+   +----+   +----+
    |    |   |    |   |    |   |    |   |    |   |    |
    |    |   |(2)*|-->|    |   |(4)*|-->|    |   |NULL|-->x
    |(1)*|-->|    |   |(3)*|-->|    |   |(5)*|-->|    |
    +----+   +----+   +----+   +----+   +----+   +----+
    
    // The numbers above the boxes represent the nodes, in number order
    // The numbers inside the boxes represent the node that its "next" pointer is pointing to
    // The arrows show the "physical" connection of the nodes
    
    // --------------------------------------------
    
    // Now if, for example, we simply free the memory that contains #3:
    
    :0       :1       :2                :4       :5
    +----+   +----+   +----+            +----+   +----+
    |    |   |    |   |    |            |    |   |    |
    |    |   |(2)*|-->|    |            |    |   |NULL|-->x
    |(1)*|-->|    |   |(3)*|--> ???     |(5)*|-->|    |
    +----+   +----+   +----+            +----+   +----+
    
    // ... we break the list.  The "next" pointer of #2 no longer points to valid
    // memory, and the pointer to #4 has been lost.
    
    // --------------------------------------------
    
    // Instead, let's correctly free the memory that contains #3.
    
    // First, store the pointer to #4, which is contained within #3
    
    :0       :1       :2       :3       :4       :5
    +----+   +----+   +----+   +----+   +----+   +----+
    |    |   |    |   |    |   |    |   |    |   |    |
    |    |   |(2)*|-->|    |   |(4)*|-->|    |   |NULL|-->x
    |(1)*|-->|    |   |(3)*|-->|    |   |(5)*|-->|    |
    +----+   +----+   +----+   +----+   +----+   +----+
                                          ^
                                          |
                        *temp = (4)*------+
    
    // Then free #3:
    
    :0       :1       :2                :4       :5
    +----+   +----+   +----+            +----+   +----+
    |    |   |    |   |    |            |    |   |    |
    |    |   |(2)*|-->|    |            |    |   |NULL|-->x
    |(1)*|-->|    |   |(3)*|--> ???     |(5)*|-->|    |
    +----+   +----+   +----+            +----+   +----+
                                          ^
                                          |
                        *temp = (4)*------+
    
    // Now to glue the list back together, by making the "next" pointer of the
    // previous node (#2) point to the node after the one we freed (#4)
    
    :0       :1       :2                :4       :5
    +----+   +----+   +----+            +----+   +----+
    |    |   |    |   |    |            |    |   |    |
    |    |   |(2)*|-->|    |            |    |   |NULL|-->x
    |(1)*|-->|    |   |(4)*|----------->|(5)*|-->|    |
    +----+   +----+   +----+            +----+   +----+
    
    // Bear in mind that removing nodes from the beginning or end of a list
    // will probably take some extra consideration for handling.

  6. #6
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    ^^ Now that is one heck of a diagram ^^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stl map erase
    By spikestar in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2010, 10:59 PM
  2. STL Set erase()
    By R.Stiltskin in forum C++ Programming
    Replies: 11
    Last Post: 03-27-2007, 08:09 AM
  3. Custom Vector template: Want to create erase/clear function
    By Bird Killer in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:37 AM
  4. string erase function problems
    By westm2000 in forum C++ Programming
    Replies: 5
    Last Post: 09-15-2003, 10:19 PM
  5. Equivelatn erase() function of string?
    By aker_y3k in forum C++ Programming
    Replies: 5
    Last Post: 10-15-2002, 02:53 PM