J
JC
Hello,
I'm running into a huge wall with linked lists.
If anyone outhere can help me, I'll appreciate it very much.
Here is my delima!
I need to create a simple list of numbers.
which I can do, by assigning the pointer to *next.
but I also have to do it in sortorder.
so if I enter the number 5, 4, 10, 2
when I display the list is should look like
2, 4, 5, 10.
Again, thank you for all of your help... sorry to be a pain with such a
simple problem... (hmm, i think I need some rest..._
JC
Here is my code...
-------------------------------------------------------
#include <iostream>
#include <fstream>
void numberIn (int& num);
const int nil = 0;
class node_type //declaration of class
{
public:
int number; //info
node_type *next;
};
int main()
{
node_type *first, *p, *q, *newnode;
int i, num;
first = new node_type;
p = first;
numberIn(num);
(*first).number = num;
(*first).next = nil;
for (i = 2; i <= 5; ++i)
{
numberIn(num);
newnode = new node_type; //create node
(*newnode).number = num;
(*newnode).next = nil;
(*p).next = newnode;
if (p > p->next)
{
first = p->next;
}
p = newnode;
if ( p->number > first->number)
// while ( p->number > first->number)
{
q = first;
p = q->next;
}
}
q = first;
cout << "\nThe list contains \n";
while (q != nil) //tests for end of list
{
cout << "number is :"<<(*q).number << "\n";
q = (*q).next;
}
return 0;
}
//------------------------------
void getdata (int& grade_value)
{
cout << "\n\t\tEnter grade => ";
cin >> num;
}
I'm running into a huge wall with linked lists.
If anyone outhere can help me, I'll appreciate it very much.
Here is my delima!
I need to create a simple list of numbers.
which I can do, by assigning the pointer to *next.
but I also have to do it in sortorder.
so if I enter the number 5, 4, 10, 2
when I display the list is should look like
2, 4, 5, 10.
Again, thank you for all of your help... sorry to be a pain with such a
simple problem... (hmm, i think I need some rest..._
JC
Here is my code...
-------------------------------------------------------
#include <iostream>
#include <fstream>
void numberIn (int& num);
const int nil = 0;
class node_type //declaration of class
{
public:
int number; //info
node_type *next;
};
int main()
{
node_type *first, *p, *q, *newnode;
int i, num;
first = new node_type;
p = first;
numberIn(num);
(*first).number = num;
(*first).next = nil;
for (i = 2; i <= 5; ++i)
{
numberIn(num);
newnode = new node_type; //create node
(*newnode).number = num;
(*newnode).next = nil;
(*p).next = newnode;
if (p > p->next)
{
first = p->next;
}
p = newnode;
if ( p->number > first->number)
// while ( p->number > first->number)
{
q = first;
p = q->next;
}
}
q = first;
cout << "\nThe list contains \n";
while (q != nil) //tests for end of list
{
cout << "number is :"<<(*q).number << "\n";
q = (*q).next;
}
return 0;
}
//------------------------------
void getdata (int& grade_value)
{
cout << "\n\t\tEnter grade => ";
cin >> num;
}