hhgjgj
hhgjgj

Reputation: 3

how to check if linked list is empty

I am making a project and I need to add products into linked list. when I add products it also count the initialized value as a product. in my code I add the products "aaaa" with price 20 and "bbbb" with price 111, when I print the products prices its print: "0 20 111". the value "0" is the initialized value, and I don't the program to save it. I need to find out if the list is empty and if so to add the first item to the list. How do I do it?

This is my code:

main.cpp:

#include <iostream> 
//#include <string>
using namespace std;
#include "List.h"
int main(){
    int option;
    Product productsListHead;
    productsListHead.printProducts(&productsListHead);
    productsListHead.addProduct(&productsListHead,1,"aaaa",20);
    productsListHead.printProducts(&productsListHead);
    productsListHead.addProduct(&productsListHead,2,"bbbb",111);
    if(option==1){
        productsListHead.printProducts(&productsListHead);
    }
    return 1;
}

List.cpp:

#include <iostream>
#include <string>
#include <cstdlib>
#include "List.h"
using namespace std;

    void Product::addProduct(Product* head,int id,char* name, int price){
        Product* TheNewProduct = new Product;
        TheNewProduct->setNext(NULL);
        TheNewProduct->setId(id);
        TheNewProduct->setName(name);
        TheNewProduct->setPrice(price);
        Product *tmp = head;

        if ( tmp != NULL ) 
        {
            cout<<"the list is not empty"<<endl;
        // Nodes already present in the list
        // Parse to end of list
            while ( tmp->Next() != NULL ) {
                tmp = tmp->Next();
            }

            // Point the last node to the new node
            tmp->setNext(TheNewProduct);
        }
        else
        {
            cout<<"the list is empty"<<endl;
            // First node in the list
            head = TheNewProduct;
        }
    }
    void Product::printProducts(Product* head)
    {
        //Product* tmp=head;
        for ( ; head; head = head->Next() )
      {
          cout << head->Price() << endl;
      }
    }
    Product::Product(){
        next=NULL;
        id=0;
        name="";
        price=0;
    }

header files->List.h:

#pragma once
#ifndef LIST_H
#define LIST_H

//#include <string>
using namespace std;

class Product{
    private:
        int id;
        char* name;
        int price;
        Product* next;
    public:
        Product();//constructor
        //Product* Next(){return next;};
        //~Products();
        Product* Next() { return next; };
        int Id(){return id;};
        char* Name(){return name;};
        int Price(){return price;};

        void setId(int new_id){id=new_id;};
        void setPrice(int new_price){price=new_price;};
        void setNext(Product* new_next){next=new_next;};
        void setName(char* new_name){name=new_name;};
        void printProducts(Product* head);
        void addProduct(Product* head,int id, char* name, int price);
        //void updateNameProduct(int id, char* oldName, char* newName); 
    };
#endif

Upvotes: 0

Views: 4980

Answers (2)

Nic Foster
Nic Foster

Reputation: 2894

It looks like you've written your own linked list for some reason, maybe an assignment?

You could probably just make Product* next point to this when it's empty. So if next == this then the list is empty. Alternatively, if the List's head node is null then the list is empty.

In the future I would recommend splitting your linked list implementation from your Product code. You should be able to have a List<Product>, rather than Product being both a list and something else at the same time.

Upvotes: 1

Caleb
Caleb

Reputation: 124997

A linked list is empty when it contains no nodes. Typically, you access a linked list via a pointer to the head -- first node -- of the list. If there are no nodes, the head pointer is null and the list is empty.

Upvotes: 3

Related Questions