mlmcguire
asked on
Inventory Linke list. How....
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!
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!
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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