| | |
Linked Lists: Find length and check for empty list
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 365
Reputation:
Solved Threads: 0
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:
THIS IS THE FULL CODE:
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:
C++ Syntax (Toggle Plain Text)
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;
C++ Syntax (Toggle Plain Text)
int listCount() { return count; } bool emptyList() { }
THIS IS THE FULL CODE:
C++ Syntax (Toggle Plain Text)
#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() { }
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
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
Always check input result, for example:
cin.clear() call.Always check input result, for example:
C++ Syntax (Toggle Plain Text)
if (cin >> number) { // that's ok } else { // eof or not-a-number // Recovery? It's the other story... }
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Letters apparition problem
- Next Thread: Homework Function Undefined error
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






