Inventory Linke list. How....

Posted on 2003-12-05
Last Modified: 2013-12-14
I wrote this program that I used the linked list to do the inventory. But I wnat to know how do I put in the following transactions: 1.Taking-in- updating the Quantity and price of the item(if it is in the list) or add it to the list if it is not.
2. Retail- sel x items for 105% of the price.
3. Order-form- List all the inventory.
4. Quit.

the following is the code:         I am using Borland C++5.01

#include      <iostream.h>
#include      <string.h>

// Necessary in Turbo C++
//enum bool      {false, true};
// The structure for the node - separately
// defined because it is self-referencing

typedef      struct      {
      char      itemcode[6];
      char      description[20];
      int      quantity;
        float      price;
}      item;
struct      node      {
      item            data;
      struct      node      *next;
} node;

// Pointer type to the node
typedef      struct node      *pnode;

class      list      {
      list(item x);
      pnode      newnode(void);
      pnode      newnode(item x);
      void      addfront(item x);
      void      addrear(item x);
      void      insertafter(item x, pnode p);
      void      place(item x);
      bool      isxthere(char x[]);
      pnode      getptr(char x[]);
      void      removenode(char x[]);
      void      write(void);
      pnode      lstart;

// list() -      The default constructor - Starts the list as empty
      lstart = NULL;

// list() -      An initializing constructor that creates a node
//            and places in it the initial value
list::list(item x)
      lstart = new struct node;
      strcpy(lstart -> data.itemcode, x.itemcode);
      strcpy(lstart -> data.description, x.description);
      lstart -> data.quantity = x.quantity;
      lstart -> data.price = x.price;
      lstart -> next = NULL;

// ~list() -       The destructor
      pnode      p, q;

      // Go through the entire list, p points to the node because freed
      //      q points to the node after p
        if (lstart != NULL)
            for (p = lstart, q = p -> next;  q != NULL; p = q, q = p-> next)      
                  delete p;

    // When all is finished lstart is NULL
      lstart = NULL;
      delete q;

// newnode() -      Creates a new node with a zero as data
//            by default
pnode      list::newnode(void)
      pnode      p;

      p = new struct node;
      p -> data.itemcode[0] = '\0';
      p -> data.description[0] = '\0';
      p-> data.quantity = 0;
      p -> data.price = 0;

// newnode() -      Creates a new node with the parameter x
//            as its value
pnode      list::newnode(item x)
      pnode      p;

      p = new struct node;
      p -> data = x;
      p -> next = NULL;


// addfront() -      Inserts a new node containing x at
//            the front of the list
void      list::addfront(item x)
      pnode      p;

      p = newnode(x);
      p ->next = lstart;
      lstart = p;

// addrear() -       Inserts a new node containing x at
//            the rear of the list
void      list::addrear(item x)
      pnode      p, q;

      // Scan through the list to find the end
        // q points to the last node
      for (p = lstart, q= NULL; p != NULL; q = p, p = p -> next)      
      // Invariant - p must be NULL so we use it to hold a pointer
      // to the new node
      p = newnode(x);
      q -> next = p;

// insertafter() -      Insert value x in a new node to be inserted
//                  after p
void      list::insertafter(item x, pnode p)
      pnode       q;
      q = newnode(x);
      q -> next = p -> next;
      p -> next = q;

// isxthere() -      Is there a node on the list containing x?
bool      list::isxthere(char x[])
      pnode      p;

      if (lstart == NULL)

        // Scan through the list looking for x
      for (p = lstart; p != NULL && strcmp(p-> data.itemcode, x) != 0;
                  p = p -> next)
      // Invariant - either p contains x or we have gone through
        //      the entire list
      return((bool)(strcmp(p -> data.itemcode, x) == 0));

// getptr() - Get the pointer for the node containing x
pnode      list:: getptr(char x[])
      pnode      p;

        // Scan through the list looking for x
      for (p = lstart; p != NULL && strcmp(p-> data.itemcode, x) != 0;
                  p = p -> next)

      if (strcmp(p->data.itemcode, x) == 0)
              // p contains x
            // We searched through the whole list and
                //      x wasn't there

// removenode() -      Remove the node containing x from the list
void      list::removenode(char x[])
      pnode      p, q;

        // Scan through the list - is x there?
      for (p = lstart, q = NULL;  p != NULL && strcmp(p-> data.itemcode, x) != 0;
                   p = p -> next)      
            q = p;

        // If so, remove it
      if (strcmp(p->data.itemcode, x) == 0)      {
            if (q == NULL)
                  // x is at the front
                  // Re-adjust the pointer to the
                        // front of the list
                  lstart = p -> next;
                      // Splice it out of the list
                  q -> next = p -> next;

                // In either case, delete the node
            delete p;              

// write() - Write the data contents of every node on the list
void      list::write(void)
      pnode      p;

      if (lstart == NULL)
            cout << "The list is empty" << endl;
            for  (p = lstart; p != NULL; p = p -> next)
                  cout << p -> data.itemcode << '\t'
                        << p -> data.description << '\t'
                        << p -> data.quantity << '\t'
                        << p -> data.price << endl;

void      list::place(item x)
      pnode p, q;

      for (p = lstart, q = NULL;  p != NULL &&
                  strcmp(p -> data.itemcode, x.itemcode) <= 0;
                  q = p, p = p -> next)

      if (p == lstart)
            insertafter(x, q);

int      main(void)
      list      mylist;
      item      stuff;

      strcpy(stuff.itemcode, "123");
      strcpy(stuff.description, "widget");
      stuff.price = 5.01;
      stuff.quantity = 5;;


I used Retail price as:
Retail= Price + (Price * (10/100)).

Can anybody help me please.
Question by:mlmcguire
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 11

Expert Comment

ID: 9882866
I don't understand the question: Are you asking how to add information to your list (I think your code does that) and find/update elements in the list --or-- are you asking how to handle user input to turn this into a command-processing program?

If the latter you will want a loop and some strings (strings are usually easier for users to remember for commands). In main you'll want something like:

string cmd;

cout << "Command: ";
cin >> cmd;
while (cmd != "exit") {
  if (cmd == "add") {
    cout << "ItemCode: ";
    cin >> stuf.itemcode;
    // and so on for the other fields of stuff;
  } else if (cmd == "list") {
  } else {
    cout << "Unknown command " << cmd << endl;
  cout << "Command: ";
  cin >> cmd;

You'll need to include the right headers for string.

Hope this helps, -bcl

Author Comment

ID: 9886026
I have four transactions to include in the linked list:

1.Taking-in-                updating the Quantity and price of the item(if it is in the list) or add it to the list if it is not.
2. Retail-                    sell x items for 105% of the price.
3. Order-form-            List all the inventory.
4. Quit.

Would I have to declare it in the main program.
LVL 11

Accepted Solution

bcladd earned 250 total points
ID: 9886157
There are two parts to the problem:

(1) Handling the different commands. You could use the structure I outlined above with any strings of your choosing as the command names and a branch in the n-way if statement for each of the commands.

(2) Making sure the inventory can support each command's functionality. I would suggest that you write functions, one for each command (except quit, probably) that manipulate the list according to the type of the command.

So, how should Taking-in- work? Well, you need to see if the named item is in the list; if it is then you can just change the quantity. If it isn't you can use the place operation to put the new product into the inventory.
Fortunately your linked list has the lookup ability built in already (good planning) and you just need to wire it up to be used when the user types in a product code.

A couple of things about your code:

(A) The standard (and most modern compilers) support the use of #include <iostream> (note, no .h) and #include <cstring> for the two files you include here. To use the string class you also need #include <string> (a different header file).

When you switch to the new include files you'll need to add one line AFTER the includes:

  using namespace std;

This lets you call standard output cout instead of by its full name std::cout.

(B) Using C++ strings is much, much easier than using C-style strings. I would urge you to read all about them and use them instead of char[] and char*. Easier to compare and they handle the \0 automatically and handle dynamic memory allocation correctly.

Hope this helps, -bcl

Expert Comment

ID: 10443578
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept bcladd's comment as answer.

Please leave any comments here within the next four days.


EE Cleanup Volunteer

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question