Hi ive made a code does the following:

  1. user makes a list of items
  2. user adds more items to the list
  3. list is displayed

it uses Linked-Lists.
here is the code

#include <iostream>
using namespace std;

struct vote
{
       char product[10];
       vote *votePtr;
       };

void addNodes(vote *&startPtr,vote *&current, int total)
{
     cout<<"enter number of products to add to list:  ";
    cin>>total;
{
 vote *temp, *temp2;
 for(int i=0;i<total;i++)
 {      
         temp=new vote;
         cout<<"enter product name:";
         cin>>temp->product;


         temp->votePtr=NULL;
         if(startPtr==NULL)
         {
         startPtr=temp;
         current=startPtr;
         }
         else
         {
             temp2=startPtr;
             while(temp2->votePtr!=NULL)
             temp2=temp2->votePtr;
             temp2->votePtr=temp;
}
}
}
}

void insert(vote *&startPtr,vote *&current, int more)
{
     cout<<"How many more products you want to add? ";
    cin>>more;
{
 vote *temp, *temp2;
 for(int i=0;i<more;i++)
 {       
         temp=new vote;
         cout<<"enter product name:";
         cin>>temp->product;


         temp->votePtr=NULL;
         if(startPtr==NULL)
         {
         startPtr=temp;
         current=startPtr;
         }
         else
         {
             temp2=startPtr;
             while(temp2->votePtr!=NULL)
             temp2=temp2->votePtr;
             temp2->votePtr=temp;
}
}
}
}

void displayList(vote *&startPtr)
{
vote *yes;
yes=startPtr;
cout<<"RESULTS"<<endl;
while(yes!=NULL)
{
cout<<yes->product<<"  "<<endl;
yes=yes->votePtr;
}
}

void del()
{

 }

int main() {
    vote *startPtr=NULL;
    vote *current;
    int total=0;
    int more=0;

    addNodes(startPtr,current,total);  
    insert(startPtr,current,more);  
    del();  
    displayList(startPtr);

    system("pause");
    return 0;
}

i left out del for delete. the user should also be able to delete an item they previously added so it wont be displayed at the end. i am confused on how i would go about this etc. everything else works.
any help is much appreciated.

You must deal the case of head node being deleted separately. Otherwise you just copy the next pointer from deleted node to preceding node's next pointer.

You are passing unnecessary variables to function instead of defining them locally, then you try to refer variable current defined in main inside the functions, and indention and variable naming is terrible.

Here is slightly improved (still with temp named variables jungle) your code:

#include <iostream>
using namespace std;

struct node
{
    char product[10];
    node *nodePtr;
};

void addNodes(node *&startPtr)
{
    int total;
    node *t;
    cout<<"enter number of products to add to list:  ";
    cin>>total;
    {
        node *temp, *temp2;
        for(int i=0; i<total; i++)
        {
            temp=new node;
            cout<<"enter product name:";
            cin>>temp->product;


            temp->nodePtr=NULL;
            if(startPtr==NULL)
            {
                startPtr=temp;
                t=startPtr;
            }
            else
            {
                temp2=startPtr;
                while(temp2->nodePtr!=NULL)
                    temp2=temp2->nodePtr;
                temp2->nodePtr=temp;
            }
        }
    }
}

void insert(node *&startPtr)
{
    int more;
    node *t;
    cout<<"How many more products you want to add? ";
    cin>>more;
    {
        node *temp, *temp2;
        for(int i=0; i<more; i++)
        {
            temp=new node;
            cout<<"enter product name:";
            cin>>temp->product;


            temp->nodePtr=NULL;
            if(startPtr==NULL)
            {
                startPtr=temp;
                t=startPtr;
            }
            else
            {
                temp2=startPtr;
                while(temp2->nodePtr!=NULL)
                    temp2=temp2->nodePtr;
                temp2->nodePtr=temp;
            }
        }
    }
}

void displayList(node *&startPtr)
{
    node *hereweare;
    hereweare=startPtr;
    cout<<"RESULTS"<<endl;
    while(hereweare!=NULL)
    {
        cout<<hereweare->product<<"  "<<endl;
        hereweare=hereweare->nodePtr;
    }
}


int main()
{
    node *startPtr=NULL;

    addNodes(startPtr);
    insert(startPtr);
    displayList(startPtr);

    return 0;
}

ah i see thanks. still unsure how to delete it though. i searched some codes but they were only for deleting intergers

For the delete function, you have to do more or less the following:

  • Ask the user for the product name to delete.
  • Check if the startPtr has that product name

    • if it does, then delete the startPtr and set it to the next node, i.e., startPtr->nodePtr, and exit.
  • Loop through the nodes while keeping track of the previous node.

    • if the current node has the sought-after product name, then
    • set the previous node's nodePtr to the nodePtr of the current node
    • delete the current node and exit.
  • Print an error message saying that the node was not found.

thankyou for your input. after an hour with other web searches im still only able to come up with this

void delet(node *&startPtr)
{
node *del;     
char name;
cout<<"what item you want to delete?";
cin>>name;
del=startPtr;
while(del->product!=NULL){
if(del->product!=name){
startPtr->nodePtr;
else
cout<<"not found"<<endl;
}
}

proof im tyring but yeh still not enough :/

my lecturer presented this to help(as an example or sorts), but its actually causing more confusion for me.

nodeType* deleteNode(nodeType *head,int delItem)
{
  nodeType *current,*prev;
  bool found=false;
  current = head;
  prev = head;
  while(current!=NULL && !found) {          // search loop
    if(current->info==delItem) found = true;
    else {
          prev = current;
          current = current->link;
     }
  }
  if(found) {
    if (current==head) head = current->link;// special case delete head
    else
     prev->link = current->link;
    delete current;
  }
  return head;
}

I do not think that name was single character, teachers 'example' seems fully working version, even I would move the stuff on lines 15 to 18 to then clause of line 8 and return from middle of while after delete, removing the found flag stuff. Then you have to change little the checking of case head is to be deleted. Only thing missing from teachers version, is that it silently ignores the case when the delITem is never in info field. The case head is NULL pointer (list is empty) is handled by condition of while.

with my lecturer's other solutions i was able to make some changes and make a list then delete items. Here is the code:

nodeType* deleteNode(nodeType *head,string delItem)
{
  nodeType *current,*prev;
  bool found=false;
  current = head;
  prev = head;
  while(current!=NULL && !found) {         
    if(current->info==delItem) found = true;
    else {
          prev = current;
          current = current->link;
     }
  }
  if(found) {
    if (current==head) head = current->link;
    else
     prev->link = current->link;
    delete current;
  }
  return head;
}

thanks for replies, with this as an example i am able to "translate" it into my code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.