Solved

Inventory Linke list. How....

Posted on 2003-12-05
5
460 Views
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      {
public:
      list(void);
      list(item x);
      ~list(void);
      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);
private:
      pnode      lstart;
};

// list() -      The default constructor - Starts the list as empty
list::list(void)
{
      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
list::~list(void)
{
      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;
      return(p);      
}

// 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;
      return(p);      

}

// 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)
            return(false);

        // 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
            return(p);
      else
            // We searched through the whole list and
                //      x wasn't there
            return(NULL);
}

// 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;
            else
                      // 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;
        else
            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)
            addfront(x);
      else
            insertafter(x, q);
}

int      main(void)
{
      list      mylist;
      item      stuff;

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

      return(0);
}

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


Can anybody help me please.
Thanks!
0
Comment
Question by:mlmcguire
  • 2
5 Comments
 
LVL 11

Expert Comment

by:bcladd
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
    mylist.place(stuff);
  } else if (cmd == "list") {
    mylist.write();
  } else {
    cout << "Unknown command " << cmd << endl;
  }
  cout << "Command: ";
  cin >> cmd;
}

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

Hope this helps, -bcl
0
 

Author Comment

by:mlmcguire
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.
0
 
LVL 11

Accepted Solution

by:
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
0
 
LVL 5

Expert Comment

by:migoEX
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

migoEX
EE Cleanup Volunteer
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

776 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