473,326 Members | 2,148 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,326 software developers and data experts.

sorting a link list

I have written a program and am having trouble with the bubblesort... i get very confused on it and am not too familiar with link lists in the first place. The link list runs succesfully when i dont have the bubble sort in the program and it does what its supposed to do. However, i need to sort both list. if i just have some clarification on what to do with my bubble sort on the first list i can figure it out on the second. I understand there is a nested for loop for the bubblesort as you can see but i dont have it set up properly. any help would be appreciated and thank you in advance.
Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3.     cout << "Insert 12 numbers \n" ;
  4.     node_type *first,*p,*q,*newnode,*temp;
  5.     int i,a,b,j;
  6.     float sum,average;
  7.     sum = 0;
  8.     first = new node_type;
  9.     p = first;
  10.     getdata(a);
  11.     (*first).flist = a;
  12.     (*first).next = null;
  13.      for (i =0; i<=10;++i)
  14.     {
  15.         getdata(a);
  16.         newnode = new node_type;
  17.         (*newnode).flist = a;
  18.         (*newnode).next = null;
  19. //**********Links previous node to newnode******//
  20.         (*p).next = newnode;
  21.         p = newnode;
  22.     }
  23. //**********Traverses list and prints out nodes in chronological order ******//
  24.         q = first;
  25.         cout << "Unsorted list \n";
  26.         while (q != null)
  27.         {
  28.             cout << (*q).flist << "\n";
  29.             sum = sum + (*q).flist;
  30.             q = (*q).next;
  31.         }
  32.         average = sum/12;
  33.         cout << "\nThe average of the numbers is " << average << "\n";
  34.  
  35.         //*****************Bubblesort*********************//
  36.  
  37.         for ((*q).flist = first; (*q).next != null; (*q).flist = (*q).next)
  38.         {
  39.             for ((*p).flist = (*q).next; (*p).next != null; (*p).flist = (*p).next)
  40.             {
  41.                 if (q > p)
  42.                 {
  43.                     temp = q;
  44.                     q = p;
  45.                     p = temp;
  46.                 }
  47.             }
  48.         }
  49.  
  50.         q = first;
  51.         cout << "Sorted list \n";
  52.         while (q != null)
  53.         {
  54.             cout << (*q).flist << "\n";
  55.             q = (*q).next;
  56.         }
  57.  
  58. //**********start of second list(b)*************//
  59.     cout << "Insert 6 numbers \n" ;
  60.     first = new node_type;
  61.     p = first;
  62.     getdata2(b);
  63.     (*first).seclist = b;
  64.     (*first).next = null;
  65.      for (i =0; i<=4;++i)
  66.     {
  67.         getdata2(b);
  68.         newnode = new node_type;
  69.         (*newnode).seclist = b;
  70.         (*newnode).next = null;
  71. //**********Links previous node to newnode******//
  72.         (*p).next = newnode;
  73.         p = newnode;
  74.     }
  75. //**********Traverses list and prints out nodes in chronological order ******//
  76.         q = first;
  77.         cout << "Unsorted list \n";
  78.         while (q != null)
  79.         {
  80.             cout << (*q).seclist << "\n";
  81.             q = (*q).next;
  82.         }
  83. }
Aug 28 '07 #1
3 1637
Dreea
37
1.in your bubble sort your loop initialization is of an int (int flist) while you loop trough pointers so the first modification is to loop with pointers
Expand|Select|Wrap|Line Numbers
  1. for (q = first; (*q).next != null; q = (*q).next)
  2. {
  3. for (p = (*q).next; (*p).next != null; p = (*p).next)
  4.  
  5.  
2. when you test the ordinal relationship between your list elements you should do that :
Expand|Select|Wrap|Line Numbers
  1. (*q).flist > (*p).flist 
3. when interchanging values you can simply interchange the flist component of your list
Expand|Select|Wrap|Line Numbers
  1. temp = (*q).flist;
  2. (*q).flist = (*p).flist;
  3. (*p).flist = temp;
  4.  
Aug 28 '07 #2
sicarie
4,677 Expert Mod 4TB
ericstein81-

Please have a look at your PM (Private Messages) in the top right corner of the page.

Thanks!
Aug 28 '07 #3
Thank you Dreea. It was having a problem with sorting the last link on the list at first, but i got it to work.
Aug 28 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: p0wer | last post by:
Let's suppose I have this sample document: <root> <entry id="1" <date>2003-08-03</date> <param_1>5</param_1> <param_2>10</param_2> </entry> <entry id="2"> ...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
20
by: martin-g | last post by:
Hi. Mostly I program in C++, and I'm not fluent in C# and .NET. In my last project I began to use LinkedList<and suddenly noticed that can't find a way to sort it. Does it mean I must implement...
9
by: robin9876 | last post by:
I have a list of integers (example sub set of data below), what is the best method of sorting these in code? 1, 872 5, 1283 8, 343 9, 123
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
5
by: kylie991 | last post by:
I'm confused as to why this isnt working.. am i doing this right?? I have to create a function that does the following: - Insert a word to the linked list so that the list // ...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.