//
// CS 202 program 09_linklist1.cpp
// Written by D.C. Williams for 10/21/03 class
//
// Program to create and access a dynamically allocated singly linked list 
// to hold inventory data.
//
// The magic of linked lists comes from the fact that each record includes
// a pointer to the next record in the list.
//


#include <iostream.h>
#include <stdio.h>

const int MAXCHAR = 20;   // for limiting the size of the strings 
                                                 
struct Record             // declare a global structure of the type "Record";
{                         // each instance will hold data plus a pointer to
                          // the next record

   char part_name[MAXCHAR];      // self explanatory
   char part_number[MAXCHAR];    // self explanatory
   char part_quantity[MAXCHAR];  // self explanatory
   Record *pnext_record;         // pointer (memory address of the next record)
};

void Show_list(Record *);              // function prototypeto display a record

int main()  
{

   char choice;
                                                                      
   Record *pfirst;              // pointer to the first (blank) record
   Record *pcurrent;            // create pointer for the current item 
      
   pfirst = new Record;         // dynamically allocate memory for that record 
                                // and store the address in "pfirst"
                                 
   pcurrent = pfirst;           // set these equal to start
   
   cout << "\n\nProgram to maintain inventory using a linked list.";
      
   do
   {   
      cout << "\n\n-------------------------------------------";   
      cout << "\n(c)reate record     (v)iew list     (q)uit"; 
      cout << "\n\nPlease select: ";
   
      cin >> choice;            // accept one char for switch()
      cin.ignore();             // flush input buffer to get rid of extra input     

      if (choice == 'q')
         exit(0);               // quit immediately
   
      else if (choice == 'c')
      {
         pcurrent->pnext_record = new Record; // store address of next record   
         pcurrent = pcurrent->pnext_record;   // set pointer to that address
      
         cout  << "\n\nEnter data for new record: " << endl;
   
         cout << "Part name = ";
         cin.getline(pcurrent->part_name,MAXCHAR); 
     
         cout << "\nPart number = ";
         cin.getline(pcurrent->part_number,MAXCHAR);

         cout << "\nQuantity = ";
         cin.getline(pcurrent->part_quantity,MAXCHAR);
   
         pcurrent->pnext_record = NULL;  // set pointer in newest record to NULL
      }
   
      else if (choice == 'v')
         Show_list(pfirst);    // show the whole list using the function
         
      else
      {
         cout << endl << "Invalid choice - press enter to re-select" << endl;
         cin.get(); cin.get();
      }
      
   } while (1);    // only quit if user selects 'q' from menu, otherwise repeat

   return 0;
}                


void Show_list(Record *pactive)
{
   pactive = pactive->pnext_record;  // go to the first record with data -
                                     // we had to leave one blank to be able
                                     // to use the do-while loop to create an
                                     // unspecified number of records
   while(pactive != NULL)
   {   
      cout << "\n-----------------"; 
      cout << "\nPart name: "<< pactive->part_name << endl;      
      cout << "Part number: " << pactive->part_number << endl;
      cout << "Quantity: "<< pactive->part_quantity;
      pactive = pactive->pnext_record;     
   }   
   return;
}


// ! // ! // !