473,549 Members | 2,584 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem about insert integer to link list

1 New Member
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.cp p


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 2888
Pradeep
3 New Member
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.cp p


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 New Member
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
2309
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 so code does not get interpreted by mail app --> ..c1 { position:relative; margin-left:0;
4
2572
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, and the error I get: -------------- namespace LIST { template <typename T> struct Link; template <typename T> class List;
57
4241
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
1
3533
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 black for example). I am dynamically creating the LinkButton controls and adding them into the table cell and I've also hooked up an event handler for...
7
2021
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 time to allow me to have the first option in the list a ‘listitem’ saying something like ‘please pick an option’, and then the rest of options...
1
1155
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
5022
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> #include<stdlib.h> typedef struct lnode { int elem;
15
2583
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 Function AddIndent(ByVal cnSQL As SqlConnection, ByVal ds As SqlTransaction, ByVal strIndentNo As String, ByVal dteIndentDate As Date, ByVal...
10
10484
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 between then what is the best solution.
0
7457
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7965
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7817
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6051
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3504
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3487
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
771
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.