473,587 Members | 2,316 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need help with a union function in a linked list

3 New Member
Hi!
Programming language: C
I wondered if any of you are able to see what is wrong with my code. What I want to do is to find the union of a set of numbers. I use a singly linked list. and my union function is based on a simple merge algorithm.
There are four different sets: even numbers, odd numbers, prime numbers and non prime numbers.

When I run my program now, it prints out:
Even or Odd numbers: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 .. 50 OK!
Prime or non prime numbers: 48 49
Even or Prime: 48 50
Odd or Prime: 49 49

So apparently the first union(the union of even numbers and odd numbers) works fine, but in the other unions the numbers of the sets are missing...


could the cause of this be either the head node or the "tail" node?

Expand|Select|Wrap|Line Numbers
  1. set_t *set_union(set_t *a, set_t *b) //note:a and b are pointers to sorted sets
  2. {        
  3.     set_t *t;  //the merged list`s "tail"
  4.     set_t *h; // the merged list`s head. 
  5.  
  6.     //Find the smallest head node
  7.     if((cmpfunc(a->head->elem, b->head->elem) == -1)){
  8.         t->head = h->head = a->head;
  9.         a->head = a->head->next;
  10.     }
  11.     else {
  12.         t->head = h->head = b->head;
  13.         b->head = b->head->next;
  14.     }
  15.  
  16.     /* repeatedly pick the smallest head node /*
  17.     while(a->head->next != NULL && b->head->next != NULL) {    
  18.         if(cmpfunc(a->head->elem, b->head->elem) == -1){
  19.             t->head->next = a->head;
  20.             t->head = a->head;
  21.             a->head = a->head->next;     
  22.         }    
  23.         else {
  24.             t->head->next = b->head;
  25.  
  26.             t->head = b->head;
  27.             b->head = b->head->next;    
  28.         }            
  29.  
  30.     }    
  31.         /* Append the remaining non-empty list (if any) */
  32.                                 if(a->head != NULL){
  33.             t->head->next = a->head;
  34.         }
  35.         else {
  36.             t->head->next = b->head;
  37.         }
  38.     return h; //return the head of the merged list
  39.  
  40. }
I am sorry for the long message, but my problem proved to be rather difficult to express with only a few lines.

please note: *a and *b are pointers to the sets "Even numbers", "odd numbers", "prime numbers", and "non prime numbers"
And: Cmpfunc is a function that compares the size of object1 and object2.

Any help is greatly appreciated! Thanks!
Feb 12 '08 #1
1 2361
vekka
3 New Member
Sorry, I forgot to show the struct itself:
Expand|Select|Wrap|Line Numbers
  1. typedef struct set_node set_node_t;
  2. struct set_node {
  3.     set_node_t *next;
  4.     void *elem;
  5.  
  6. };
  7. struct set {
  8.     int size;
  9.     set_node_t *head;
  10.     cmpfunc_t cmpfunc;
  11. };
Feb 12 '08 #2

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

Similar topics

6
2866
by: Jamal | last post by:
I am working on binary files of struct ACTIONS I have a recursive qsort/mergesort hybrid that 1) i'm not a 100% sure works correctly 2) would like to convert to iteration Any comments or suggestion for improvements or conversion to iteration would be much appreciated
4
2922
by: dssuresh6 | last post by:
Whether browsing forward or backward can be done using a singly linked list. Is there any specific case where a doubly linked list is needed? For people who say that singly linked list allows traversal only in one direction, I would say that using appropriate loops/recursion, traversal in opposite direction is also possible. Then why the need...
0
1639
by: aredo3604gif | last post by:
I have coded a serie of singly linked lists in ANSI C which I have to use. The lists are then stored in a serie of buckets with chained hash table technique. In the various lists there are nodes that carry the same type of data in some of their node fields and some don't. In the various nodes , the struct datatype, there is a flag set to 1...
19
2070
by: ash | last post by:
hi friends, i have some questions whch is in my last year question papers.i need some help to get logic of these questions. 1) write a C function, that takes two strings as arguments and returns a pointer to the first occurrence of 1st string in 2nd string or NULL if it is not present. -- i tried to solve it but it seems that i am not...
21
3187
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code (i.e. the get_hash function) is borrowed from various snippets I found on the net. Thee free function could probably need some love. I have been...
5
3354
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that I could better understand the concept, he was trying to convey to me. I ran my own example and it crashed and burn "what a surprise!" : (. I ran...
5
2804
by: Richard Gromstein | last post by:
Hello, I have an exercise that I need to finish very soon and I really need help understanding what to do and how exactly to do it. I am working on reading the chapter right now and working on it myself, but I have a feeling I won't get far. Please help me complete the exercise. I am posting what needs to be done and will be updating with...
1
1621
by: dabbakal | last post by:
Hello, am a new member and this is my first posting. C++ is the first progrsaming language am taking and is just for 3 months. Having benefited from lot of posting i decided to join. But currently am trying to solve an exercise and it is proven difficult for me. I have written a program in c++ in linked list but am facing two problems. (1) when...
20
6523
by: sirsnorklingtayo | last post by:
hi guys please help about Linked List, I'm having trouble freeing the allocated memory of a single linked list node with a dynamic char* fields, it doesn't freed up if I use the FREE() function in C.. But if I try to use a single linked list with a static char array fields I can free the memory allocated with out any problems using the...
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8205
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. ...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
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...
1
5712
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...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2347
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
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.