?
Solved

Graphics and Linked List Under C++

Posted on 1998-09-08
6
Medium Priority
?
217 Views
Last Modified: 2013-12-14
1 - I Have Borland C++ Compiler (DOS) 3.0 , My Problem .. When i tried to load my graphic program, i've got error message in this compiler. althought i loaded this program under different system it works .. but under my compiler doesn't.

2 - I tried to do my Linked list and i done it .. my problem  how can i do my SORTING PROGRAM under Linked List ...

0
Comment
Question by:seyedsaatchi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 6

Expert Comment

by:snoegler
ID: 1172308
1 : ???

Each graphics subsystem is usually compiler & system dependent. Can you give some more
infos( e.g. on which 'different' system did it work)?

2:

I remember there are various methods to sort linked lists. I know a quite simple method, it isn't
the fastest i think ...

a) Assign all your linked list items some sort of hash value which is sort order dependent,
perhaps like this:

unsigned long buildHash(char *text)
{
unsigned long ulValue=0;
int nPos;
for(nPos=0;nPos<4;nPos++) {
if(text[nPos]==0) break;
ulValue |= ((unsigned char)text[nPos])<<((3-nPos)*8);
}
return ulValue;
}
// example: HELLO -> 0x48454c4c

Then define a struct which takes the linked list item and its hash value:

struct _helper {
linkedlistitem *item;
unsigned long hash;
};

Build an array so that each linked-list item is contained, and then you can use the qsort()
function of C( or the STL implementation).
Then you can either use a bubble sort on the linked-list items which have the same hash
value - or you can proceed like above, assign those items with the same hash value a new
array, where the hash value starts with the 4th character.
In the worst case (all items are identical), you'll get O(N*N), in the best case, where all
(first) hash values are different, you'll get O(N).
0
 
LVL 4

Accepted Solution

by:
sganta earned 340 total points
ID: 1172309

Hi !
 The algorithm which I am giving is BUBBLE SORT.


      #include <stdio.h>

      #define NULL 0

      struct ptr {
                   int num;
                   struct ptr *next;
                 };

      typedef struct ptr pointer;

      void bubble_sort(pointer *node);

      void main(void)
      {
          pointer *list; /* This is a linked list */

          /* Here I am assuming that you've created a linked list
             i.e., ptr
          */

          bubble_sort(list);
      }

      void bubble_sort(pointer *node)
      {
         pointer *first,*second,*first_prev,*temp;
         int n=1;

         temp = node;
         first = node;
         first_prev = node;

         /* Get the no. of nodes in the linked list */
           
         while (first->link != NULL)
         {
            n++;
            first= first->link;
         }


         for (i=1; i<n; i++)
         {
            first = temp;
            first_prev = temp;
            second = first->next;

            for (j=1; j<=(n-i); j++)
            {
               if (first->num > second->num)
               {
                  if (first == temp)  /* If it is an header node */
                  {
                      first->next = second->next;
                      second->next = first;
                      temp = second;
                      first = second;
                   }
                  else
                  {
                      first->next = second->next;
                      second->next = first;
                      first_prev->next = second;
                      first = second;
                  }
                }
               
                first_prev = first;
                first = first->next;
                second = first->next;
             }                
          }

This is tested already and it works fine.
I hope this is acceptable to you.

If you have any specific doubts, pl. let me know.
0
 
LVL 6

Expert Comment

by:snoegler
ID: 1172310
Bubble sort is the slowest algorithm possible. Don't use it  ...
If you sort more than 1000 elements, even a Pentium II 400 goes down :)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:snoegler
ID: 1172311
sganta:
Don't get me wrong - i don't want to insult you in any way.
But bubble sort is *really* slow. If we're talking of only 200 elements, it's okay :)
0
 
LVL 4

Expert Comment

by:sganta
ID: 1172312
Snolger !
Thank you for your comment. It is good to share the knowledge.

Regards
sganta

0
 
LVL 6

Expert Comment

by:snoegler
ID: 1172313
sganta, please don't get me wrong - i just *hate* the bubblesort algorithm.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question