Large program database linked list
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include "readline.h" struct part *inventory = NULL; /* points to first part */ /********************************************************** * 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); } //Same readline function for chapter 16 int ReadLine(char str[]) { int ch, i = 0; while(isspace(ch = getchar())) { ; } while (ch != '\n' && ch != EOF){ if(i < NAME_LEN) str[i++] = ch; ch = getchar(); } str[i] = '\0'; return i; } /********************************************************** * 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 = FindPart(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"); } /********************************************************** * 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) { char *partname[NAME_LEN+1]; 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: "); //scanf("%s:, partname); ReadLine(new_node->name); 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; } /********************************************************** * 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 = FindPart(number); if(p != NULL){ printf("Enter change in quantity on hand: "); scanf("%d", &change); p->on_hand += change; } else printf("Part not fount.\n"); } /********************************************************** * FindPart: 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 *FindPart(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; }