Hello..


This is what my program is suppose to do:

-find length of list
-check for empty list
-insert in back of list
-delete from front of list

I have everything done except finding the length of list and check for empty list.


I need help getting the length of my Linked list. I'm using a counter, but where do I place the counter in my program?

Also, I need help checking for empty list. There is no code in the book that shows me how to complete this task, so I don't even know how to check for it. Any help is appreciated...Thanks


I will post the 2 pieces of code that i'm working with and post the whole program below....

Here is my code:

void createList(nodeType*& first, nodeType*& last);
void printList(nodeType*& first);
int listCount();
bool emptyList();
void insertBack(nodeType*& last);
void deleteFront(nodeType*& first);

int main()
{
    
    nodeType *first, *last;
    int num;
    int emptyList;
    int count = 0;
    
    createList(first,last);
    printList(first);
    
    if (emptyList(first))
   cout<<"Empty list"<<endl;
    else
   cout<<"There are"<<listCount(first)<<" items in the linked list"<<endl;
int listCount()
{
  
  return count; 
    
}

bool emptyList()
{
     
     
}

THIS IS THE FULL CODE:

#include <iostream>

using namespace std;
struct nodeType
{
    int info;
    nodeType *link;
};

void createList(nodeType*& first, nodeType*& last);
void printList(nodeType*& first);
int listCount();
bool emptyList();
void insertBack(nodeType*& last);
void deleteFront(nodeType*& first);

int main()
{
    
    nodeType *first, *last;
    int num;
    int emptyList;
    int count = 0;
    
    createList(first,last);
    printList(first);
    
    if (emptyList(first))
   cout<<"Empty list"<<endl;
    else
   cout<<"There are"<<listCount(first)<<" items in the linked list"<<endl;
    
    
    insertBack(last);
    printList(first);
    
    deleteFront(first);
    printList(first);
    
  system("PAUSE");
   return 0;
}

void createList(nodeType*& first, nodeType*& last)
{
    int number;
    nodeType *newNode;
    
    first = NULL;
    last = NULL;
    
    cout<<"Enter an integer (-999 to stop): ";
    cin>>number;
    cout<<endl;
    
     while (number != -999)
     {
         newNode = new nodeType; // create new node
         newNode->info = number;
         newNode->link = NULL;
    
         if (first == NULL)
         {
            first = newNode;
            last = newNode;
         }
         else
         {
           last->link = newNode;
           last = newNode;
         }
         cout<<"Enter an integer (-999 to stop): ";
         cin>>number;
         cout<<endl;
     }
        
}

void printList(nodeType*& first)
{

    cout<<"Inside printList...printing linked list...\n"<<endl;
        nodeType *current;
        current = new nodeType;
        current = first;    
        while (current != NULL)
        {
          cout << current->info<<endl;
          current = current->link;
          count++;
        }
}  

 void insertBack(nodeType*& last)
{
      
  int num;
  nodeType *first,*newNode;
    
  
  cout<<"There are 3 items in the linked list."<<endl;
  cout<<"Enter a number to add to the END of the list: "<<endl;
  cin>>num;
  

         newNode = new nodeType;
         newNode->info = num;
         newNode->link = NULL;
    
         if (first == NULL)
         {
           first = newNode;
           last = newNode;
           
         }
         else
         {
           last->link = newNode;
           last = newNode;
         }
         
}

void deleteFront(nodeType*& first)
{

  nodeType *last,*current,*trailcurrent;
  bool found;

  
  cout<<endl;
  cout<<"Inside deleteFront...removing item from front of list..."<<endl;
  cout<<endl;
    
         if (first->info == 1)
         {
           current = first;
           first = first->link;
         }
         if (first == NULL)
         {
            last = NULL;
            
            delete current;
         }
         else
         {
         found = false;
         trailcurrent = first;
         
         current = first->link;
         }
         
         
         
}

int listCount()
{
  
  return count; 
    
}

bool emptyList()
{
     
     
}

Recommended Answers

All 2 Replies

Well, you obvioulsy need to increment counter each time you add a node, and decrement it each time you remove a node. It's really not hard, just put it in insert, delete, and create list.

For checking if list is empty... simply check if first==NULL.

But don't you need some wrapper class around everything? This way it's meaningless to write empty() function

If unlucky user accidentally types a wrong char your program comes to loop forever because subsequent cin inputs do nothing until cin.clear() call.
Always check input result, for example:

if (cin >> number) {
   // that's ok
} else {
   // eof or not-a-number
   // Recovery? It's the other story...
}
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.