JustR
JustR

Reputation: 131

How to Implement stack data type inside a structure for a linked list in C++?

We are to make a video store program, but I've been stuck in "RENT A VIDEO" and "RETURN A VIDEO" because I have to use stack in the processing. My main idea is basically whenever a customer rents, the video ID rented will be pushed in to the customer node stack data type. Same goes to returning a video, it will pop() the returned video ID from the customer node stack data type. (I am not sure if I am supposed to use linked list or if it's the best way around.)

struct customerNode
{
    int customer_ID;
    string name, address;
    stack <string> rentedVid;
    stack <string> tempRent;
    struct customerNode* next;
};
struct customerNode* first = NULL;
struct customerNode* last = NULL;

void insertCustomer(int custID, string custN, string custA)
{
    struct customerNode* temp;
    temp = new customerNode;
    temp->customer_ID = custID;
    temp->name = custN;
    temp->address = custA;
    // stack rentedVid ??
    // stack tempRent ??

    if (first == NULL)
    {
        first = temp;
        last = temp;
    }
    else
    {
        last->next = temp;
        last = temp;
    }
}

(I also have a linked list for the video ID, title, copies and etc.) I am not confident with my program actually. The requirement: You must store your customers in a queue (i used linked list for now, but will change it later) when you retrieved them from the text file. They must also be stored in a queue during processing. Saving back to the text file will be done when the user chooses Exit Program. Rented videos will be stored in a stack and will be saved in the CUSTOMER-RENT text file when the user chooses Exit Program.

Upvotes: 1

Views: 97

Answers (1)

Glyphack
Glyphack

Reputation: 886

If it's a business application the stack might not be the best option. what if customer wants to rent multiple videos and not return them in order?

cpp's vector is a better option to store all of the user rented videos and search/delete them. linked list is also a good data structure the main benefit of linked list is it's suitable if you delete/insert elements in the beginning and end, not for random access.

Upvotes: 1

Related Questions