Linked Lists

How do I modify a linked list?

Heres my header file:

#ifndef PRODUCTLIST_H
#define PRODUCTLIST_H

const int MAX_LEN = 21;


const int STORE_CAPACITY = 100;

struct Product{
char* title;
char* description;
int category;
float price;
};

struct Node{
Product * product;
Node * next;
};

class ProductList{
public:
ProductList();
~ProductList();

void addProduct(const Product&);
bool removeVideo(char[]);
void search(char[], Product&);
void searchC(int category, Product[]);
void displayAll();
void loadFile(char[]);
void saveFile(char[]);
void priceSort();

private:
Node * head;
int size;

void add(const Product&);
void add2(const Product&);
void addSort(const Product&);
void addSort2(const Product&);
};
#endif

Comments

  • What is it that you want the linked list to do? Do you want an singly linked list or a doubly linked or a circular? Give me a little more information and i can help you out with it.

    James


    : How do I modify a linked list?
    :
    : Heres my header file:
    :
    : #ifndef PRODUCTLIST_H
    : #define PRODUCTLIST_H
    :
    : const int MAX_LEN = 21;
    :
    :
    : const int STORE_CAPACITY = 100;
    :
    : struct Product{
    : char* title;
    : char* description;
    : int category;
    : float price;
    : };
    :
    : struct Node{
    : Product * product;
    : Node * next;
    : };
    :
    : class ProductList{
    : public:
    : ProductList();
    : ~ProductList();
    :
    : void addProduct(const Product&);
    : bool removeVideo(char[]);
    : void search(char[], Product&);
    : void searchC(int category, Product[]);
    : void displayAll();
    : void loadFile(char[]);
    : void saveFile(char[]);
    : void priceSort();
    :
    : private:
    : Node * head;
    : int size;
    :
    : void add(const Product&);
    : void add2(const Product&);
    : void addSort(const Product&);
    : void addSort2(const Product&);
    : };
    : #endif
    :

  • : What is it that you want the linked list to do? Do you want an singly linked list or a doubly linked or a circular? Give me a little more information and i can help you out with it.
    :
    : James
    :
    :
    : : How do I modify a linked list?
    : :
    : : Heres my header file:
    : :
    : : #ifndef PRODUCTLIST_H
    : : #define PRODUCTLIST_H
    : :
    : : const int MAX_LEN = 21;
    : :
    : :
    : : const int STORE_CAPACITY = 100;
    : :
    : : struct Product{
    : : char* title;
    : : char* description;
    : : int category;
    : : float price;
    : : };
    : :
    : : struct Node{
    : : Product * product;
    : : Node * next;
    : : };
    : :
    : : class ProductList{
    : : public:
    : : ProductList();
    : : ~ProductList();
    : :
    : : void addProduct(const Product&);
    : : bool removeVideo(char[]);
    : : void search(char[], Product&);
    : : void searchC(int category, Product[]);
    : : void displayAll();
    : : void loadFile(char[]);
    : : void saveFile(char[]);
    : : void priceSort();
    : :
    : : private:
    : : Node * head;
    : : int size;
    : :
    : : void add(const Product&);
    : : void add2(const Product&);
    : : void addSort(const Product&);
    : : void addSort2(const Product&);
    : : };
    : : #endif
    : :
    :
    : Well I've created a database that lets me add data/remove data/sort data now all I need to do is modify it. I'll give you some of the code I have now.
    void ProductList::add(const Product& product){
    //create a new node to contain the new data
    Node * newNode = new Node;
    newNode->product = new Product;
    newNode->product->category = product.category;
    newNode->product->price = product.price;
    newNode->product->title = new char[strlen(product.title)+1];
    strcpy(newNode->product->title, product.title);
    newNode->product->description = new char[strlen(product.description)+1];
    strcpy(newNode->product->description, product.description);
    newNode->next = NULL;

    //link the new node to the list
    newNode->next = head;
    head = newNode;
    }

    Thats part of my code for adding new products to my list.

  • [b][red]This message was edited by jk9694 at 2004-3-1 3:13:10[/red][/b][hr]
    [b][red]This message was edited by jk9694 at 2004-3-1 3:10:38[/red][/b][hr]
    OK it looks like you are using a singly linked list, which might be the wrong type if you are going to be working with a database. You might want to look at using a doubly linked list. A doubly linked list will let you move back and forth in the database where as a singly linked list will just let you go forward. What is your email and I will send you a small program that I wrote. But here is a sample from the add section and the struct of the node.

    void doublyLinkList::addDNode(const string &s)
    {
    //**************************************************************
    // If the List does not exist, or Node is to be added at the
    // beginning of the list
    //**************************************************************

    if(start == NULL || s<=start->name)
    {
    dNode *n = new dNode(s, NULL, start);

    if(start != NULL)
    {
    start->prev = n;
    }

    start = n;
    if(last==NULL)
    {
    last = start;
    }

    return;
    }

    //**************************************************************
    // If the node is to be added at the end of the List
    //**************************************************************

    if(s >= last->name)
    {
    dNode *n = new dNode(s, last, NULL);
    last->next = n;
    last = n;
    return;
    }

    //**************************************************************
    // If the node is to be inserted anywhere in the middle
    // of the List
    //**************************************************************

    for(current = start; current != last && s> current->name;
    current = current->next)
    {
    //Empty body of the loop
    }

    dNode *prior = current->prev;
    dNode *n = new dNode(s, prior, current);
    prior->next = n;
    current->prev = n;
    }


    and the structure of the node:


    struct dNode
    {
    public:
    string name;
    dNode *prev, *next;

    //***********************************************************
    //Constructor
    //***********************************************************

    dNode(const string &s, dNode *p = NULL, dNode *n = NULL)
    : name(s), prev(p), next(n)
    {

    }

    private:
    //***********************************************************
    //Do not allow node to be copy, assign, or return
    //***********************************************************

    dNode(const dNode &);
    dNode & operator = (const dNode &);
    };

    This node only deals with a string in it, but modifying it to hold more data is fairly simple.




  • : [b][red]This message was edited by jk9694 at 2004-3-1 3:13:10[/red][/b][hr]
    : [b][red]This message was edited by jk9694 at 2004-3-1 3:10:38[/red][/b][hr]
    : OK it looks like you are using a singly linked list, which might be the wrong type if you are going to be working with a database. You might want to look at using a doubly linked list. A doubly linked list will let you move back and forth in the database where as a singly linked list will just let you go forward. What is your email and I will send you a small program that I wrote. But here is a sample from the add section and the struct of the node.
    :
    : void doublyLinkList::addDNode(const string &s)
    : {
    : //**************************************************************
    : // If the List does not exist, or Node is to be added at the
    : // beginning of the list
    : //**************************************************************
    :
    : if(start == NULL || s<=start->name)
    : {
    : dNode *n = new dNode(s, NULL, start);
    :
    : if(start != NULL)
    : {
    : start->prev = n;
    : }
    :
    : start = n;
    : if(last==NULL)
    : {
    : last = start;
    : }
    :
    : return;
    : }
    :
    : //**************************************************************
    : // If the node is to be added at the end of the List
    : //**************************************************************
    :
    : if(s >= last->name)
    : {
    : dNode *n = new dNode(s, last, NULL);
    : last->next = n;
    : last = n;
    : return;
    : }
    :
    : //**************************************************************
    : // If the node is to be inserted anywhere in the middle
    : // of the List
    : //**************************************************************
    :
    : for(current = start; current != last && s> current->name;
    : current = current->next)
    : {
    : //Empty body of the loop
    : }
    :
    : dNode *prior = current->prev;
    : dNode *n = new dNode(s, prior, current);
    : prior->next = n;
    : current->prev = n;
    : }
    :
    :
    : and the structure of the node:
    :
    :
    : struct dNode
    : {
    : public:
    : string name;
    : dNode *prev, *next;
    :
    : //***********************************************************
    : //Constructor
    : //***********************************************************
    :
    : dNode(const string &s, dNode *p = NULL, dNode *n = NULL)
    : : name(s), prev(p), next(n)
    : {
    :
    : }
    :
    : private:
    : //***********************************************************
    : //Do not allow node to be copy, assign, or return
    : //***********************************************************
    :
    : dNode(const dNode &);
    : dNode & operator = (const dNode &);
    : };
    :
    : This node only deals with a string in it, but modifying it to hold more data is fairly simple.
    :
    :
    :
    : I have add at end function too.
    void ProductList::add2(const Product& product){
    //create a new node to contain the new data
    Node * newNode = new Node;
    newNode->product = new Product;
    newNode->product->category = product.category;
    newNode->product->price = product.price;
    newNode->product->title = new char[strlen(product.title)+1];
    strcpy(newNode->product->title, product.title);
    newNode->product->description = new char[strlen(product.description)+1];
    strcpy(newNode->product->description, product.description);
    newNode->next = NULL;

    //traverse to the last node
    Node * current = head;
    if(!head){
    head = newNode;
    newNode->next = NULL;
    }
    else{
    while(current->next != NULL){
    current = current->next;
    }
    current->next = newNode;
    }
    }
    : And 2 sorting functions
    void ProductList::addSort(const Product& product){
    //create a new node to contain the new data
    Node * newNode = new Node;
    newNode->product = new Product;
    newNode->product->category = product.category;
    newNode->product->price = product.price;
    newNode->product->title = new char[strlen(product.title)+1];
    strcpy(newNode->product->title, product.title);
    newNode->product->description = new char[strlen(product.description)+1];
    strcpy(newNode->product->description, product.description);
    newNode->next = NULL;

    Node * current = head;
    if(!head){
    head = newNode;
    return;
    }
    if(strcmp(product.title, head->product->title) <= 0){
    newNode->next = head;
    head = newNode;
    return;
    }
    while(current->next
    && strcmp(product.title, current->next->product->title)>0){
    current = current->next;
    }

    //link the new node to the list
    newNode->next = current->next;
    current->next = newNode;
    }

    void ProductList::addSort2(const Product& product){
    Node * newNode = new Node;
    newNode->product = new Product;
    newNode->product->category = product.category;
    newNode->product->price = product.price;
    newNode->product->title = new char[strlen(product.title)+1];
    strcpy(newNode->product->title, product.title);
    newNode->product->description = new char[strlen(product.description)+1];
    strcpy(newNode->product->description, product.description);
    newNode->next = NULL;

    Node * previous = NULL;
    Node * current = head;
    while(current && strcmp(current->product->title, product.title)<0){
    previous = current;
    current = current->next;
    }
    newNode->next = current;
    if(!previous)
    head = newNode;
    else
    previous->next = newNode;
    }

    my email addi is mike_roley@hotmail.com

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories