Explanations
The Tool Store receives shipments of screwdrivers at various costs. The store’s policy is to charge a 10% mark-up and to sell screwdrivers received earlier before screwdrivers that were received later. This is a FIFO (first-in-first-out) policy.
If a Shipment is received we add that information to our database (Queue used here) and in case of a SALE we print the screwdrivers information and make the Sale. All the information and details are properly printed in case of both Sale and Receipt of new Inventory. Even if no screwdrivers are there in the Inventory a proper message is printed to the user for this.
Queue is an abstract data type just like Stack, but in queues the elements are inserted from one end called REAR and removal of elements takes place at the other end called the FRONT.
It is FIFO (First In First Out) data structure means the elements that enter the queue first are removed first and the elements are removed in the same order as they are entered.
We have used the Queue as a data structure for keeping track of the screwdrivers of a company in Inventory. All the new Shipments details are being added to the REAR (Head side) of Queue and all the removals are being made from the other side (FRONT). Also all the insertions and removals from the queue are being made in O (1) time.
We are using this structure for storing the information of one screwdriver:
typedef struct screwdriverstruct
{
char date[20];
char time[10];
int quantity;
double cost;
struct screwdriverstruct* next;
}SD;
AddScrewDriver
void addScrewDriver (SD* sd)
{
/**
* resizing array
*
*/
if(count==size)
{
size=size+10;
array = (SD *)realloc(array, sizeof(sd)*size);
}
count++;
sd->next=NULL;
/**
* if array is empty
*
*/
if(head==NULL)
{
head=sd;
tail=sd;
return;
}
/**
* if array has only 1 element
*
*/
else if(head==tail)
head->next=sd;
else
tail->next=sd;
tail=sd;
}
Here, we are first checking that size of array is not equal to current number of elements in the array. If that is the case, we will resize the array. After resizing the array, we will increase the count of current number of elements in array by 1.
A reference of ScrewDriver object is passed to this function. As argument Wewill set its next to NULL.
Then, we will check whether head is NULL, if so then we have to set the passed reference as Head and tail and exit the function. Also, if the array has only one element i,e, Head is equal to the tail, head->next is said to the passed object or else tail next is set to the passed reference. At last we set the passed reference as tail and by these steps the Screwdriver info is added to the array.
AddScrewDriver
removeScrewdriver
/**
*
* @param quantity quantity of Screwdrivers needed for sale
* @return total cost of the sale
*/
double removeScrewDriver (int quantity)
{
double total=0.0;
while(1)
{
/**
* No screwdrivers in array
*/
if(head==NULL)
{
tail=NULL;
printf(“%d SCREWDRIVERS NOT AVAILABLE FOR SALE.n”,quantity);
return;
}
/**
* Head has exactly the required number of screw drivers
*/
else if(head->quantity==quantity)
{
double cost=head->cost+head->cost*0.1;
total=total+cost*head->quantity;
quantity=quantity-head->quantity;
printf(“%d SCREWDRIVERS SOLD AT RM%.2f. – SALE: RM%.2fn”,head->quantity,cost,head->quantity*cost);
head=head->next;
count–;
return total;
}
/**
* Head has more screw drivers then the required
*/
else if(head->quantity>quantity)
{
double cost=head->cost+head->cost*0.1;
total=total+cost*quantity;
printf(“%d SCREWDRIVERS SOLD AT RM%.2f. – SALE: RM%.2fn”,quantity,cost,quantity*cost);
head->quantity=head->quantity-quantity;
return total;
}
/**
*
* Make the sale and remove the head
*/
else
{
double cost=head->cost+head->cost*0.1;
total=total+cost*head->quantity;
printf(“%d SCREWDRIVERS SOLD AT RM%.2f. – SALE: RM%.2fn”,head->quantity,cost,head->quantity*cost);
quantity=quantity-head->quantity;
head=head->next;
count–;
}
}
}
In this function, the quantity of screwdrivers to be removed is passed. We will go in an infinite loop . Inside loop, If head is null We will print the number of screwdrivers not available.
Else, if head Screw driver reference has the same number of elements that are required , We will get cost of these screwdrivers, remove the count of the number of elements in the array, and then remove this node by setting head=head->next;
Else if, head’s quantity is more then required quantity, we will decrease the available quantity at head, sell these screwdrivers and return from the program,
else, we will just make the sale, decrement the quantity of required screwdrivers, decrement the available nodes count by 1 and set head to head->next;
Here, in this image we are adding he information of three received shipments to the array by choosing the option 1 from the menu that is displayed.
In the above screenshot, the sale is being made for the required number fo screwdrivers by selecting option 2. If the required number of screwdrivers is not available, a proper message is being printed to the user.
In this screenshot also, a sale is being made and then the user exits by selecting option 3. When option 3 is chose, a message is being printed to the user and the program terminates.
Conclusion
Queue data structure is an excellent choice for such cases where we strictly needs a First In First Out Feature. All the nodes are removed in the same order as they are inserted. Strict order is maintained.
References
Belloc, H. (1967). On. Freeport, N.Y.: Books for Libraries Press.
Dybvig, R. (2003). The scheme programming language. Cambridge, Mass.: MIT Press.
Goyal, A. (2008). The C programming language. Oxford: Alpha Science International.
Lee, M. (2007). C programming. Delhi: Global Media.
Miller, L. and Quilici, A. (1987). C programming language. New York, N.Y.: Wiley.
Stroustrup, B. (2016). The C++ programming language. Upper Saddle Rilver: Addison-Wesley.
Tondo, C., Gimpel, S. and Kernighan, B. (1996). The C answer book. New Delhi: Prentice Hall.
Troy, D. and Kiper, J. (1989). The C programming language. Glenview, Ill.: Scott, Foresman.
Zahn, C. (1979). C notes. New York, N.Y.: Yourdon Press.