473,386 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

problem about insert integer to link list

i have a problem about insert integer to link list.

sorry it's too long but i try many times, many ways , it's still have an error

function is fix . can't change anything about function. i can only make a code that work with function.


Number.cpp

Expand|Select|Wrap|Line Numbers
  1. #include "Number.h"
  2.  
  3. void createList(Number * & headref)
  4. {
  5.   headref = NULL;
  6. }
  7.  
  8. bool isEmpty(const Number * const head)
  9. {
  10.     const Number *cur = head;
  11. }
  12.  
  13. bool isFull(const Number * const head)
  14. {
  15.     const Number *cur = head;
  16. }
  17.  
  18. bool insertNumber(Number * & headref, const NumbType &data)
  19. {
  20.     Number *cur=head;
  21.     if (cur==NULL)
  22.     {
  23.         cur->data = data;
  24.     }
  25.     else while (cur!=NULL)
  26.     {
  27.         cur=cur->next;
  28.     }
  29.         cur->data=data;
  30.     return true;
  31. }
  32.  
  33. int findNumber(const Number * const head, const NumbType &data)
  34. {
  35.     const Number *cur = head;
  36.     while (cur != NULL)
  37.     {
  38.         if (cur->data == data)
  39.         {
  40.             return 0;
  41.         }
  42.         cur = cur->next;
  43.     }
  44.     return -1;
  45. }
  46.  
  47. void deleteList(Number * & headref)
  48. {
  49.     Number *cur=headref;
  50.     while (cur != NULL)
  51.     {
  52.         cur = NULL;
  53.         cur = cur->next;
  54.     }
  55. }
  56.  
  57. void showList(const Number * const head)
  58. {
  59.     const Number *cur = head;
  60.         if (cur == NULL)
  61.         {
  62.             cout << " No Data " <<endl;
  63.         }
  64.         else
  65.         {
  66.             while (cur != NULL)
  67.             {
  68.                 cout << cur->data;
  69.                 if(cur->next != NULL)
  70.                 cout <<" , ";
  71.                 cur = cur->next;
  72.             }
  73.         }
  74. }
  75.  
  76. int showmenu(char * menu[], char title[])
  77. {
  78.   int i, item;
  79.   char buffer[80];
  80.  
  81.   cout << "\n" << title;
  82.   for (i = 0; menu[i]; ++i)
  83.     cout << "\n" << i+1 <<": "
  84.          << menu[i];
  85.   cout << "\nEnter Choice: ";
  86.   cin.getline(buffer,80);
  87.   if (strlen(buffer) > 0 && isdigit(buffer[0])) {
  88.        buffer[1] = '\0';
  89.        return atoi(buffer);
  90.   }
  91.   else return -1;
  92. }

Number.h

Expand|Select|Wrap|Line Numbers
  1. #ifndef DATADEF_H
  2. #define DATADEF_H
  3.  
  4. #include <iostream>
  5. using std::cout;
  6. using std::endl;
  7. using std::cin;
  8. using std::cerr;
  9.  
  10. typedef unsigned int NumbType;
  11.  
  12. struct Number {
  13.     NumbType data;
  14.     Number *next;
  15.     Number *prev;
  16. };
  17.  
  18. void createList(Number * &headref);
  19. // This function will create an empty list, meaning headref is assigned NULL
  20.  
  21. bool isEmpty(const Number * const head);
  22. // This function returns true if the list is empty false otherwise
  23. // It simply requies checking if head is NULL or not
  24.  
  25. bool isFull(const Number * const head);
  26. // This function returns true if the list is full, false otherwise
  27. // Since we are implementing the list on the heap, we can simply return false
  28.  
  29. int findNumber(const Number * const head, const NumbType & key);
  30. // This fucntion will search the list pointed by head to find a Number
  31. // containing key. Note that NumbType is defined to be unsigned int 
  32. // It will return an index to the found Number or -1 if not found
  33.  
  34. bool insertNumber(Number * &headref, const NumbType & data);
  35. // This function will insert given data to the list pointed by headref
  36. // Returns true if insertion is successful, false otherwise
  37.  
  38. void deleteList(Number * &headref);
  39. // Deletes every Number in the list and makes the headref to be NULL
  40.  
  41. void showList(const Number * const head);
  42. // Show every number in the list
  43.  
  44. int showmenu(char * [], char []);
  45.  

NumberDriver.cpp


Expand|Select|Wrap|Line Numbers
  1. #include "Number.h"
  2.  
  3. int main()
  4. {
  5.    char *mainmenu[] = {"Add an Item"
  6.                       ,"Find Items"
  7.                       ,"Show List"
  8.                       ,"Quit"
  9.                       ,NULL
  10.                       };
  11.  
  12.    Number * numberList; 
  13.    NumbType mydata;
  14.    int which,position; 
  15.  
  16.    createList(numberList);
  17.  
  18.    do {
  19.       which = showmenu(mainmenu, "Welcome To Linked List Demonstration");
  20.       switch (which)
  21.       {
  22.         case 1:
  23.             cout << "Enter an integer number: ";
  24.             cin >> mydata;
  25.             cin.ignore(80,'\n');
  26.             if (findNumber(numberList, mydata) != -1) 
  27.               cout << "We already have that number in the list\n";
  28.             else  if (!insertNumber(numberList,mydata)) {
  29.               cerr << "Critical: System is out of memory"; 
  30.               exit(1);
  31.             }
  32.             break;
  33.         case 2:
  34.             cout << "Enter an integer number to Find: ";
  35.             cin >> mydata;
  36.             cin.ignore(80,'\n');
  37.             if ((position = findNumber(numberList, mydata)) != -1) 
  38.                cout << "Find number in position: " << position << endl;
  39.             else cout << "Cannot find number." << endl;
  40.             break;
  41.         case 3:
  42.             showList(numberList);
  43.             break;
  44.         case 4: 
  45.             cout << "Good Bye\n"; 
  46.             break;
  47.         default:
  48.             cout << "\nInvalid Entry - Try Again!"
  49.                  << endl;
  50.             break;
  51.       }
  52.    } while ( which != 4); // end of main loop
  53.  
  54.    deleteList(numberList);
  55.  
  56.    return 0;
  57. }
Aug 18 '06 #1
2 2877
i have a problem about insert integer to link list.

sorry it's too long but i try many times, many ways , it's still have an error

function is fix . can't change anything about function. i can only make a code that work with function.


Number.cpp

Expand|Select|Wrap|Line Numbers
  1. #include "Number.h"
  2.  
  3. void createList(Number * & headref)
  4. {
  5.   headref = NULL;
  6. }
  7.  
  8. bool isEmpty(const Number * const head)
  9. {
  10.     const Number *cur = head;
  11. }
  12.  
  13. bool isFull(const Number * const head)
  14. {
  15.     const Number *cur = head;
  16. }
  17.  
  18. bool insertNumber(Number * & headref, const NumbType &data)
  19. {
  20.     Number *cur=head;
  21.     if (cur==NULL)
  22.     {
  23.         cur->data = data;
  24.     }
  25.     else while (cur!=NULL)
  26.     {
  27.         cur=cur->next;
  28.     }
  29.         cur->data=data;
  30.     return true;
  31. }
  32.  
  33. int findNumber(const Number * const head, const NumbType &data)
  34. {
  35.     const Number *cur = head;
  36.     while (cur != NULL)
  37.     {
  38.         if (cur->data == data)
  39.         {
  40.             return 0;
  41.         }
  42.         cur = cur->next;
  43.     }
  44.     return -1;
  45. }
  46.  
  47. void deleteList(Number * & headref)
  48. {
  49.     Number *cur=headref;
  50.     while (cur != NULL)
  51.     {
  52.         cur = NULL;
  53.         cur = cur->next;
  54.     }
  55. }
  56.  
  57. void showList(const Number * const head)
  58. {
  59.     const Number *cur = head;
  60.         if (cur == NULL)
  61.         {
  62.             cout << " No Data " <<endl;
  63.         }
  64.         else
  65.         {
  66.             while (cur != NULL)
  67.             {
  68.                 cout << cur->data;
  69.                 if(cur->next != NULL)
  70.                 cout <<" , ";
  71.                 cur = cur->next;
  72.             }
  73.         }
  74. }
  75.  
  76. int showmenu(char * menu[], char title[])
  77. {
  78.   int i, item;
  79.   char buffer[80];
  80.  
  81.   cout << "\n" << title;
  82.   for (i = 0; menu[i]; ++i)
  83.     cout << "\n" << i+1 <<": "
  84.          << menu[i];
  85.   cout << "\nEnter Choice: ";
  86.   cin.getline(buffer,80);
  87.   if (strlen(buffer) > 0 && isdigit(buffer[0])) {
  88.        buffer[1] = '\0';
  89.        return atoi(buffer);
  90.   }
  91.   else return -1;
  92. }

Number.h

Expand|Select|Wrap|Line Numbers
  1. #ifndef DATADEF_H
  2. #define DATADEF_H
  3.  
  4. #include <iostream>
  5. using std::cout;
  6. using std::endl;
  7. using std::cin;
  8. using std::cerr;
  9.  
  10. typedef unsigned int NumbType;
  11.  
  12. struct Number {
  13.     NumbType data;
  14.     Number *next;
  15.     Number *prev;
  16. };
  17.  
  18. void createList(Number * &headref);
  19. // This function will create an empty list, meaning headref is assigned NULL
  20.  
  21. bool isEmpty(const Number * const head);
  22. // This function returns true if the list is empty false otherwise
  23. // It simply requies checking if head is NULL or not
  24.  
  25. bool isFull(const Number * const head);
  26. // This function returns true if the list is full, false otherwise
  27. // Since we are implementing the list on the heap, we can simply return false
  28.  
  29. int findNumber(const Number * const head, const NumbType & key);
  30. // This fucntion will search the list pointed by head to find a Number
  31. // containing key. Note that NumbType is defined to be unsigned int 
  32. // It will return an index to the found Number or -1 if not found
  33.  
  34. bool insertNumber(Number * &headref, const NumbType & data);
  35. // This function will insert given data to the list pointed by headref
  36. // Returns true if insertion is successful, false otherwise
  37.  
  38. void deleteList(Number * &headref);
  39. // Deletes every Number in the list and makes the headref to be NULL
  40.  
  41. void showList(const Number * const head);
  42. // Show every number in the list
  43.  
  44. int showmenu(char * [], char []);
  45.  

NumberDriver.cpp


Expand|Select|Wrap|Line Numbers
  1. #include "Number.h"
  2.  
  3. int main()
  4. {
  5.    char *mainmenu[] = {"Add an Item"
  6.                       ,"Find Items"
  7.                       ,"Show List"
  8.                       ,"Quit"
  9.                       ,NULL
  10.                       };
  11.  
  12.    Number * numberList; 
  13.    NumbType mydata;
  14.    int which,position; 
  15.  
  16.    createList(numberList);
  17.  
  18.    do {
  19.       which = showmenu(mainmenu, "Welcome To Linked List Demonstration");
  20.       switch (which)
  21.       {
  22.         case 1:
  23.             cout << "Enter an integer number: ";
  24.             cin >> mydata;
  25.             cin.ignore(80,'\n');
  26.             if (findNumber(numberList, mydata) != -1) 
  27.               cout << "We already have that number in the list\n";
  28.             else  if (!insertNumber(numberList,mydata)) {
  29.               cerr << "Critical: System is out of memory"; 
  30.               exit(1);
  31.             }
  32.             break;
  33.         case 2:
  34.             cout << "Enter an integer number to Find: ";
  35.             cin >> mydata;
  36.             cin.ignore(80,'\n');
  37.             if ((position = findNumber(numberList, mydata)) != -1) 
  38.                cout << "Find number in position: " << position << endl;
  39.             else cout << "Cannot find number." << endl;
  40.             break;
  41.         case 3:
  42.             showList(numberList);
  43.             break;
  44.         case 4: 
  45.             cout << "Good Bye\n"; 
  46.             break;
  47.         default:
  48.             cout << "\nInvalid Entry - Try Again!"
  49.                  << endl;
  50.             break;
  51.       }
  52.    } while ( which != 4); // end of main loop
  53.  
  54.    deleteList(numberList);
  55.  
  56.    return 0;
  57. }
Where r u exactly getting the problem or what is the problem????
Aug 22 '06 #2
Tenny
2
Please can u tell me the algorithm for this....
Nov 5 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Charles Russell | last post by:
I am setting a three column layout within an existing table. The table uses previously devloped code so I am force to use it. I have the following styles defined. <code><!--Preventative insert...
4
by: Mat DeLong | last post by:
I have never been stuck on programming something before to the point I give up... this is a first. I am programming what should be something very easy in C++... using Templates. Here is the code,...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
1
by: Thanks | last post by:
I have a routine that is called on Page_Init. It retrieves folder records from a database which I display as Link Buttons in a table cell. I set the table cell's bgcolor to a default color (say...
7
by: Lastie | last post by:
Hi all, I’ve got a ‘dropdownlist’ web control and I can add ‘listitem’ no problem. I can also bind data from an SQL database fine. My problem is that I want to do both at the same...
1
by: Net Virtual Mailing Lists | last post by:
Hello, I have 3 tables which are joined that I need to create a summation for and I just cannot get this to work. Here's an example: CREATE table1 ( id1 INTEGER, title1 VARCHAR
4
by: neilcancer | last post by:
i wrote a function to insert an elem into a list, but it was wrong,wrong,wrong! and i have no idea about why it was wrong. If anyone know, leave your advice, thank you. #include<stdio.h>...
15
by: mayurtandel | last post by:
i have problem whith store datetime value to database table the my system date formate is dd/mm/yyyy the date is store in mm/dd/yyyy formate what is the problem my code is as follow Public...
10
by: Aditya | last post by:
Hi All, I would like to know how it is possible to insert a node in a linked list without using a temp_pointer. If the element is the first element then there is no problem but if it is in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.