473,385 Members | 1,829 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,385 software developers and data experts.

Sort list

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;
}


Jul 19 '05 #1
4 4329
JC <ke****@secret.com> wrote in message
news:P7********************@comcast.com...
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

This should get you started...

struct Node
{
void* Element;
struct Node *Next;
};
class List
{
public:
struct Node *Head;
void insert(void* data);//Find the right location in the list based on your
numbers
//& insert a new created node there
void* pop();//Read the head element
List() { Head = NULL; }
~List();
};
Jul 19 '05 #2
JC wrote:
Hello,

I'm running into a huge wall with linked lists.
You can use one of the standard containers ...

an std::multimap will allow you to insert random objects and will
provide a sorted iterator when you're finished inserting.

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;
node_type( int in_number )
: number( in_number ), next( 0 )
{
}
};

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;
p = first = new node_type( num );


for (i = 2; i <= 5; ++i)
{
numberIn(num);
newnode = new node_type; //create node
(*newnode).number = num;
(*newnode).next = nil;
try doing these things in the constuctor
newnode = new node_type( num );

Also - (*newnode).number can be more easily written as
newnode->number

(*p).next = newnode;

if (p > p->next)
{
first = p->next;
}
The last five lines make no sense
p = newnode;
if ( p->number > first->number)
// while ( p->number > first->number)
{
q = first;
p = q->next;
}
I think you need some logic like the one below.

p = first;
q = 0;

while ( p )
{
if ( p->number < newnode->number )
{
q = p;
p = p->next;
}
else
{
if ( q )
{
q->next = newnode;
newnode->next = p;
}
else
{
first = newnode;
newnode->next = p;
}
p = 0;
newnode = 0;
}
}

if ( newnode )
{
if ( q )
{
q->next = newnode;
}
else
{
first = newnode;
}
}

}

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 suspect you mean "cin >> grade_value"
}


Jul 19 '05 #3
"JC" <ke****@secret.com> wrote in message
news:P7********************@comcast.com...
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

One thing I've found handy for linked lists is to traverse the code and find
out
where you want to insert the new node before you begin the actual insertion
process. Like so...

1. format new node to go in list
2. traverse the list until you find the node that comes just before your new
node
3. save the "next" pointer from the current node
4. change the "next" pointer in the current node to point to the new node
(created in step 1)
5. set the "next" pointer in the new node to hold the saved pointer (from
step 3)

The list is now maintained in order, rather than having to insert at the end
and then re-sort. Of course this only matters if you care about the order
of retrieval.

Paul


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;
}

Jul 19 '05 #4
JC
Hi Gianni,

thank you for clearing things up... I think that my problem was that I was
trying to do too much with *first,
thanks for the detail information.

also, I want to thank Deming He and Paul for helping me understand Lists.
Now I have to get back to work... second part of the project...

Thank you again!
JC
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bp********@dispatch.concentric.net...
JC wrote:
Hello,

I'm running into a huge wall with linked lists.


You can use one of the standard containers ...

an std::multimap will allow you to insert random objects and will
provide a sorted iterator when you're finished inserting.

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;


node_type( int in_number )
: number( in_number ), next( 0 )
{
}
};

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;


p = first = new node_type( num );


for (i = 2; i <= 5; ++i)
{
numberIn(num);
newnode = new node_type; //create node
(*newnode).number = num;
(*newnode).next = nil;


try doing these things in the constuctor
newnode = new node_type( num );

Also - (*newnode).number can be more easily written as
newnode->number

(*p).next = newnode;

if (p > p->next)
{
first = p->next;
}


The last five lines make no sense
p = newnode;
if ( p->number > first->number)
// while ( p->number > first->number)
{
q = first;
p = q->next;
}


I think you need some logic like the one below.

p = first;
q = 0;

while ( p )
{
if ( p->number < newnode->number )
{
q = p;
p = p->next;
}
else
{
if ( q )
{
q->next = newnode;
newnode->next = p;
}
else
{
first = newnode;
newnode->next = p;
}
p = 0;
newnode = 0;
}
}

if ( newnode )
{
if ( q )
{
q->next = newnode;
}
else
{
first = newnode;
}
}

}

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 suspect you mean "cin >> grade_value"
}

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steve Pinard | last post by:
(Got a comm error trying to post first time, sorry if this is a duplicate) New to Python, so please bear with me. >>> import sys >>> print sys.modules.keys() # works fine >>>...
3
by: Brian McGonigle | last post by:
I'm a Perl programmer learning Python (up to chapter 7 in Learning Python, so go easy on me :-) and I find that I look to do things in Python the way I would do them in Perl. In Perl functions and...
12
by: Eva | last post by:
Hi, I try to implement quick sort. I sort vectors by their first value. 10 2 3 4 9 3 5 6 10 4 5 6 must be 9 3 5 6 10 2 3 4 10 4 5 6 The prog works great on maybe 500 vectors, but I have an...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
1
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
11
by: Leon | last post by:
I have six textbox controls on my webform that allows the user to enter any numbers from 1 to 25 in any order. However, I would like to sort those numbers from least to greatest before sending them...
3
by: Adam J. Schaff | last post by:
Hello. I recently noticed that the Sort method of the .NET ArrayList class does not behave as I expected. I expect 'A' < '_' < 'a' (as per their ascii values) but what I got was the opposite....
99
by: Shi Mu | last post by:
Got confused by the following code: >>> a >>> b >>> c {1: , ], 2: ]} >>> c.append(b.sort()) >>> c {1: , ], 2: , None]}
0
by: Amar | last post by:
Hi, I have a generic list with a some objects of a particular class in it. I have implemented a IComparer for the the class and pass it to the List. The list.sort method works fine when the value...
3
by: raylopez99 | last post by:
This is an example of using multiple comparison criteria for IComparer/ Compare/CompareTo for List<and Array. Adapted from David Hayden's tutorial found on the net, but he used ArrayList so the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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:
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...
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,...

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.