473,749 Members | 2,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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).numb er = 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).num ber << "\n";
q = (*q).next;
}

return 0;
}
//------------------------------

void getdata (int& grade_value)
{
cout << "\n\t\tEnte r grade => ";
cin >> num;
}


Jul 19 '05 #1
4 4343
JC <ke****@secret. com> wrote in message
news:P7******** ************@co mcast.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).numb er = num;
(*newnode).next = nil;
try doing these things in the constuctor
newnode = new node_type( num );

Also - (*newnode).numb er 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).num ber << "\n";
q = (*q).next;
}

return 0;
}
//------------------------------

void getdata (int& grade_value)
{
cout << "\n\t\tEnte r grade => ";
cin >> num;
I suspect you mean "cin >> grade_value"
}


Jul 19 '05 #3
"JC" <ke****@secret. com> wrote in message
news:P7******** ************@co mcast.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).numb er = 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).num ber << "\n";
q = (*q).next;
}

return 0;
}
//------------------------------

void getdata (int& grade_value)
{
cout << "\n\t\tEnte r 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*******@mari ani.ws> wrote in message
news:bp******** @dispatch.conce ntric.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).numb er = num;
(*newnode).next = nil;


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

Also - (*newnode).numb er 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).num ber << "\n";
q = (*q).next;
}

return 0;
}
//------------------------------



void getdata (int& grade_value)
{
cout << "\n\t\tEnte r 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
2568
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 >>> print sys.modules.keys().sort() # returns None, why? None
3
1628
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 methods usually only return and undefined value in the event of an error, make an endless number of compound statements possible. Is there a version of sort() I could import from somewhere that returns a reference to the object on which it was...
12
2924
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 "Aborted(core
20
4072
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: li=;
1
12844
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
1946
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 to the database. How can accomplish this task? Thanks!
3
6451
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. Simple code: Dim y As New ArrayList y.Add("Abc") y.Add("abc") y.Add("_bc")
99
4660
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
1485
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 of the field I want to compare is different, but when all the elements are same, the list still gets sorted(meaning the sequence of the elements still change). For eg. Say the list is filled with object of type A, where in A has a property...
3
10556
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 format was different. Basically you can sort a class having members LastName (string), FirstName (string) and Age (int) according to whether you want to sort by Last Name, First Name or Age, using the .Sort function
0
8833
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9568
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9389
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9256
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8257
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6801
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3320
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
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.