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 - #include "Number.h"
-
-
void createList(Number * & headref)
-
{
-
headref = NULL;
-
}
-
-
bool isEmpty(const Number * const head)
-
{
-
const Number *cur = head;
-
}
-
-
bool isFull(const Number * const head)
-
{
-
const Number *cur = head;
-
}
-
-
bool insertNumber(Number * & headref, const NumbType &data)
-
{
-
Number *cur=head;
-
if (cur==NULL)
-
{
-
cur->data = data;
-
}
-
else while (cur!=NULL)
-
{
-
cur=cur->next;
-
}
-
cur->data=data;
-
return true;
-
}
-
-
int findNumber(const Number * const head, const NumbType &data)
-
{
-
const Number *cur = head;
-
while (cur != NULL)
-
{
-
if (cur->data == data)
-
{
-
return 0;
-
}
-
cur = cur->next;
-
}
-
return -1;
-
}
-
-
void deleteList(Number * & headref)
-
{
-
Number *cur=headref;
-
while (cur != NULL)
-
{
-
cur = NULL;
-
cur = cur->next;
-
}
-
}
-
-
void showList(const Number * const head)
-
{
-
const Number *cur = head;
-
if (cur == NULL)
-
{
-
cout << " No Data " <<endl;
-
}
-
else
-
{
-
while (cur != NULL)
-
{
-
cout << cur->data;
-
if(cur->next != NULL)
-
cout <<" , ";
-
cur = cur->next;
-
}
-
}
-
}
-
-
int showmenu(char * menu[], char title[])
-
{
-
int i, item;
-
char buffer[80];
-
-
cout << "\n" << title;
-
for (i = 0; menu[i]; ++i)
-
cout << "\n" << i+1 <<": "
-
<< menu[i];
-
cout << "\nEnter Choice: ";
-
cin.getline(buffer,80);
-
if (strlen(buffer) > 0 && isdigit(buffer[0])) {
-
buffer[1] = '\0';
-
return atoi(buffer);
-
}
-
else return -1;
-
}
Number.h - #ifndef DATADEF_H
-
#define DATADEF_H
-
-
#include <iostream>
-
using std::cout;
-
using std::endl;
-
using std::cin;
-
using std::cerr;
-
-
typedef unsigned int NumbType;
-
-
struct Number {
-
NumbType data;
-
Number *next;
-
Number *prev;
-
};
-
-
void createList(Number * &headref);
-
// This function will create an empty list, meaning headref is assigned NULL
-
-
bool isEmpty(const Number * const head);
-
// This function returns true if the list is empty false otherwise
-
// It simply requies checking if head is NULL or not
-
-
bool isFull(const Number * const head);
-
// This function returns true if the list is full, false otherwise
-
// Since we are implementing the list on the heap, we can simply return false
-
-
int findNumber(const Number * const head, const NumbType & key);
-
// This fucntion will search the list pointed by head to find a Number
-
// containing key. Note that NumbType is defined to be unsigned int
-
// It will return an index to the found Number or -1 if not found
-
-
bool insertNumber(Number * &headref, const NumbType & data);
-
// This function will insert given data to the list pointed by headref
-
// Returns true if insertion is successful, false otherwise
-
-
void deleteList(Number * &headref);
-
// Deletes every Number in the list and makes the headref to be NULL
-
-
void showList(const Number * const head);
-
// Show every number in the list
-
-
int showmenu(char * [], char []);
-
NumberDriver.cpp - #include "Number.h"
-
-
int main()
-
{
-
char *mainmenu[] = {"Add an Item"
-
,"Find Items"
-
,"Show List"
-
,"Quit"
-
,NULL
-
};
-
-
Number * numberList;
-
NumbType mydata;
-
int which,position;
-
-
createList(numberList);
-
-
do {
-
which = showmenu(mainmenu, "Welcome To Linked List Demonstration");
-
switch (which)
-
{
-
case 1:
-
cout << "Enter an integer number: ";
-
cin >> mydata;
-
cin.ignore(80,'\n');
-
if (findNumber(numberList, mydata) != -1)
-
cout << "We already have that number in the list\n";
-
else if (!insertNumber(numberList,mydata)) {
-
cerr << "Critical: System is out of memory";
-
exit(1);
-
}
-
break;
-
case 2:
-
cout << "Enter an integer number to Find: ";
-
cin >> mydata;
-
cin.ignore(80,'\n');
-
if ((position = findNumber(numberList, mydata)) != -1)
-
cout << "Find number in position: " << position << endl;
-
else cout << "Cannot find number." << endl;
-
break;
-
case 3:
-
showList(numberList);
-
break;
-
case 4:
-
cout << "Good Bye\n";
-
break;
-
default:
-
cout << "\nInvalid Entry - Try Again!"
-
<< endl;
-
break;
-
}
-
} while ( which != 4); // end of main loop
-
-
deleteList(numberList);
-
-
return 0;
-
}
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 - #include "Number.h"
-
-
void createList(Number * & headref)
-
{
-
headref = NULL;
-
}
-
-
bool isEmpty(const Number * const head)
-
{
-
const Number *cur = head;
-
}
-
-
bool isFull(const Number * const head)
-
{
-
const Number *cur = head;
-
}
-
-
bool insertNumber(Number * & headref, const NumbType &data)
-
{
-
Number *cur=head;
-
if (cur==NULL)
-
{
-
cur->data = data;
-
}
-
else while (cur!=NULL)
-
{
-
cur=cur->next;
-
}
-
cur->data=data;
-
return true;
-
}
-
-
int findNumber(const Number * const head, const NumbType &data)
-
{
-
const Number *cur = head;
-
while (cur != NULL)
-
{
-
if (cur->data == data)
-
{
-
return 0;
-
}
-
cur = cur->next;
-
}
-
return -1;
-
}
-
-
void deleteList(Number * & headref)
-
{
-
Number *cur=headref;
-
while (cur != NULL)
-
{
-
cur = NULL;
-
cur = cur->next;
-
}
-
}
-
-
void showList(const Number * const head)
-
{
-
const Number *cur = head;
-
if (cur == NULL)
-
{
-
cout << " No Data " <<endl;
-
}
-
else
-
{
-
while (cur != NULL)
-
{
-
cout << cur->data;
-
if(cur->next != NULL)
-
cout <<" , ";
-
cur = cur->next;
-
}
-
}
-
}
-
-
int showmenu(char * menu[], char title[])
-
{
-
int i, item;
-
char buffer[80];
-
-
cout << "\n" << title;
-
for (i = 0; menu[i]; ++i)
-
cout << "\n" << i+1 <<": "
-
<< menu[i];
-
cout << "\nEnter Choice: ";
-
cin.getline(buffer,80);
-
if (strlen(buffer) > 0 && isdigit(buffer[0])) {
-
buffer[1] = '\0';
-
return atoi(buffer);
-
}
-
else return -1;
-
}
Number.h - #ifndef DATADEF_H
-
#define DATADEF_H
-
-
#include <iostream>
-
using std::cout;
-
using std::endl;
-
using std::cin;
-
using std::cerr;
-
-
typedef unsigned int NumbType;
-
-
struct Number {
-
NumbType data;
-
Number *next;
-
Number *prev;
-
};
-
-
void createList(Number * &headref);
-
// This function will create an empty list, meaning headref is assigned NULL
-
-
bool isEmpty(const Number * const head);
-
// This function returns true if the list is empty false otherwise
-
// It simply requies checking if head is NULL or not
-
-
bool isFull(const Number * const head);
-
// This function returns true if the list is full, false otherwise
-
// Since we are implementing the list on the heap, we can simply return false
-
-
int findNumber(const Number * const head, const NumbType & key);
-
// This fucntion will search the list pointed by head to find a Number
-
// containing key. Note that NumbType is defined to be unsigned int
-
// It will return an index to the found Number or -1 if not found
-
-
bool insertNumber(Number * &headref, const NumbType & data);
-
// This function will insert given data to the list pointed by headref
-
// Returns true if insertion is successful, false otherwise
-
-
void deleteList(Number * &headref);
-
// Deletes every Number in the list and makes the headref to be NULL
-
-
void showList(const Number * const head);
-
// Show every number in the list
-
-
int showmenu(char * [], char []);
-
NumberDriver.cpp - #include "Number.h"
-
-
int main()
-
{
-
char *mainmenu[] = {"Add an Item"
-
,"Find Items"
-
,"Show List"
-
,"Quit"
-
,NULL
-
};
-
-
Number * numberList;
-
NumbType mydata;
-
int which,position;
-
-
createList(numberList);
-
-
do {
-
which = showmenu(mainmenu, "Welcome To Linked List Demonstration");
-
switch (which)
-
{
-
case 1:
-
cout << "Enter an integer number: ";
-
cin >> mydata;
-
cin.ignore(80,'\n');
-
if (findNumber(numberList, mydata) != -1)
-
cout << "We already have that number in the list\n";
-
else if (!insertNumber(numberList,mydata)) {
-
cerr << "Critical: System is out of memory";
-
exit(1);
-
}
-
break;
-
case 2:
-
cout << "Enter an integer number to Find: ";
-
cin >> mydata;
-
cin.ignore(80,'\n');
-
if ((position = findNumber(numberList, mydata)) != -1)
-
cout << "Find number in position: " << position << endl;
-
else cout << "Cannot find number." << endl;
-
break;
-
case 3:
-
showList(numberList);
-
break;
-
case 4:
-
cout << "Good Bye\n";
-
break;
-
default:
-
cout << "\nInvalid Entry - Try Again!"
-
<< endl;
-
break;
-
}
-
} while ( which != 4); // end of main loop
-
-
deleteList(numberList);
-
-
return 0;
-
}
Where r u exactly getting the problem or what is the problem????
Please can u tell me the algorithm for this....
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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,...
|
by: Xarky |
last post by:
Hi,
I am writing a linked list in the following way.
struct list
{
struct list *next;
char *mybuff;
};
|
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...
|
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...
|
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
|
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>...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
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...
| |