Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Inventory Linke list. How....

Posted on 2003-12-05
Medium Priority
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 1000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

596 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