Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 04/06/2019 - Linked List Practice Problem for Programming 2
- ** Prepared by: Cris Lawrence Adrian G. Militante
- ** Answered by:
- ** Given the structures and function prototypes below, write the code for all the functions defined.
- **
- */
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct ProductDetails{
- char prodID[9];
- char prodName[32];
- char shelf[5];
- }ProductDetails;
- typedef struct Product{
- ProductDetails details;
- float price;
- int stocks;
- int itemsSold;
- }Product;
- typedef struct ProdNode{
- Product prod;
- struct ProdNode* nextProd;
- }ProdNode, *ProductList;
- typedef struct WarehouseDetails{
- char warehouseName[32];
- char warehouseID[3];
- char address[128];
- }WarehouseDetails;
- typedef struct Warehouse{
- WarehouseDetails details;
- ProductList inventory;
- }Warehouse;
- Warehouse initWH(void);
- int findProd(ProductList, char*);
- void insertProdUnique(ProductList*, Product);
- void deleteProd(ProductList*, char*);
- ProductList getShelfItems(ProductList, char*);
- void displayProduct(ProductList);
- void displayWarehouseInv(Warehouse);
- //ProductList deleteShelfItems(ProductList* list, char shelf[]);
- //void insertSorted(ProductList list);
- //void insertLast(ProductList, Product);
- /* Instructions
- ** 1) initWH: The function will initialize the inventory of a warehouse to be EMPTY, and will ask the user to input
- the warehouse details.
- Example: Input warehouse name: Blankedale
- Input warehouse ID: BL
- Input warehouse address: Germany
- 2) findProd: The function will check the linked list and return 1 if the passed prodID matches any product in the list.
- Else, it will return 0.
- 3) insertProdUnique: The function will insert after the last node of the linked list, the new Product if that product's ID
- does not yet exist in the list. Else, update the details and price of the product to be the same as the
- new Product. Also, update the stocks to be equal to the sum of the previous stock and the new product stock.
- itemsSold field should not be updated.
- 4) deleteProd: The function will go through the list and delete the product with the prodID that has been passed
- ONLY IF the product to be deleted has zero stocks. Display relevant error messages when needed
- ("E.G. Cannot delete product. Product still has stocks.")
- 5) getShelfItems: The function will go through the list of products and return a new ProductList that contains all of
- the items that have belong to the same shelf.
- 6) displayWarehouseInv: The function will display the warehouse name and all of the products within the warehouse,
- together with their details.Format the display as you see fit.
- */
- int main(void)
- {
- Warehouse blankdale;
- ProductList shelfItems=NULL;
- Product newProds[] = {{{"00000001", "APPLES", "FR01"}, 32.00, 10, 50},
- {{"00000002", "PINEAPPLES", "FR01"}, 66.00, 20, 50},
- {{"00000003", "ORANGES", "FR01"}, 21.00, 30, 50},
- {{"00000004", "MELONS", "FR02"}, 120.00, 0, 50},
- {{"00000005", "SQUASH", "FR02"}, 55.00, 35, 50},
- {{"00000006", "BANANA", "FR01"}, 40.00, 0, 50},
- {{"00000007", "GRAPES", "FR01"}, 5.00, 22, 50},
- {{"00000008", "MANGO", "FR01"}, 37.00, 32, 50},
- {{"00000009", "CHERRY", "FR02"}, 7.00, 45, 50},
- {{"00000001", "PEACH", "FR02"}, 22.00, 68, 50}
- };
- /* Make the necessary function calls to check if your functions were successfully written */
- blankdale = initWH();
- insertProdUnique(&blankdale.inventory,newProds[0]);
- insertProdUnique(&blankdale.inventory,newProds[1]);
- insertProdUnique(&blankdale.inventory,newProds[2]);
- insertProdUnique(&blankdale.inventory,newProds[3]);
- insertProdUnique(&blankdale.inventory,newProds[4]);
- displayWarehouseInv(blankdale);
- printf("%s",findProd(blankdale.inventory, "00000001") == 1 ? "Item Found" : "Item Not Found");
- getch();
- system("CLS");
- deleteProd(&blankdale.inventory, "00000002");
- displayWarehouseInv(blankdale);
- getch();
- system("CLS");
- shelfItems = getShelfItems(blankdale.inventory, "FR01" );
- displayProduct(shelfItems);
- return 0;
- }
- Warehouse initWH(void)
- {
- Warehouse temp;
- puts("Enter warehouse name:");
- gets(temp.details.warehouseName);
- puts("Enter warehouse id:");
- gets(temp.details.warehouseID);
- puts("Enter warehouse address:");
- gets(temp.details.address);
- temp.inventory=NULL;
- return temp;
- }
- int findProd(ProductList list, char id[])
- {
- ProductList trav;
- for(trav=list; trav!=NULL && strcmp(trav->prod.details.prodID, id) !=0; trav=trav->nextProd){}
- return trav == NULL? 0:1;
- }
- void insertProdUnique(ProductList* list, Product newProd)
- {
- ProductList temp, *trav;
- for(trav=list; *trav!=NULL && strcmp(newProd.details.prodID, (*trav)->prod.details.prodID) !=0; trav= &(*trav)->nextProd){}
- if(*trav==NULL){
- temp=(ProductList)malloc(sizeof(ProdNode));
- if(temp!=NULL){
- temp->prod=newProd;
- temp->nextProd=*trav;
- *trav=temp;
- }
- }else{
- (*trav)->prod.details = newProd.details;
- (*trav)->prod.price = newProd.price;
- (*trav)->prod.stocks += newProd.stocks;
- puts("Item was found. Record were updated");
- }
- }
- void deleteProd(ProductList* list, char id[])
- {
- ProductList temp, *trav;
- for(trav=list; trav!=NULL && strcmp(id, (*trav)->prod.details.prodID) !=0; trav= &(*trav)->nextProd){}
- if(*trav!=NULL){
- if((*trav)->prod.stocks==0){
- temp=*trav;
- *trav=temp->nextProd;
- free(temp);
- }else{
- puts("Cannot delete product. Still has stocks");
- }
- }
- }
- ProductList getShelfItems(ProductList list, char* shelf)
- {
- ProductList shelfItems = NULL;
- ProductList temp, trav, *trav2;
- for(trav=list; trav!=NULL; trav=trav->nextProd){
- if(strcmp(shelf,trav->prod.details.shelf)==0){
- for(trav2=&shelfItems; *trav2!=NULL; trav2= &(*trav2)->nextProd){}
- temp= (ProductList) malloc (sizeof(ProdNode));
- if(temp!=NULL){
- temp->prod= trav->prod;
- temp->nextProd = *trav2;
- *trav2 = temp;
- }
- }
- }
- return shelfItems;
- }
- void displayProduct(ProductList list)
- {
- for(;list!=NULL; list=list->nextProd){
- printf("Product: %s\n",list->prod.details.prodName);
- printf("ID: %s\n",list->prod.details.prodID);
- printf("Price: %.2f\n",list->prod.price);
- printf("Shelf: %s\n",list->prod.details.shelf);
- printf("Sold: %d\n",list->prod.itemsSold);
- printf("Stock: %d\n---------------\n",list->prod.stocks);
- }
- }
- void displayWarehouseInv(Warehouse temp)
- {
- char buffer[200];
- sprintf(buffer,"Warehouse Name: %s\nWarehouse ID: %s\nAddress: %s",temp.details.warehouseName, temp.details.warehouseID, temp.details.address);
- puts(buffer);
- displayProduct(temp.inventory);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement