944,155 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 612
  • C RSS
Oct 29th, 2009
0

finding the middle element of the list with 0(n)

Expand Post »
Hello ,
i have written the code for finding the middle element of the list with
0(n) complexity.

please gurus, verify that and let me know if it needs any modifications

and ofcourse i have question to ask

when the no of elements are odd
we can easily find the middle element
suppose
1 2 3 4 5 the middle is 3
but when the no of elements are even
1 2 3 4 5 6
how can we decide the middle element
is there any most widely used way or experts follow.

please let me know


NOTE : in my previous post i have given the insert function you can have a look at it if you need
insert
  1. int find_mid(struct node *nrml)
  2. {
  3. struct node *adv=nrml;
  4. while(adv) {
  5. if(adv->next !=NULL) {
  6. //if(adv->next->next != NULL)
  7. adv = adv->next->next ;
  8. nrml = nrml -> next;
  9. }
  10. else {
  11. printf("with odd :\n");
  12. return nrml->data;
  13. }
  14. }
  15. printf("with even :\n");
  16. return nrml->data;
  17. }
  18.  
  19.  
  20. Driver code :
  21.  
  22. #define ODD 9
  23. #define EVEN 10
  24. int main()
  25. {
  26. struct node *podd=NULL;
  27. struct node *peven=NULL;
  28. int n,d,i=0,ele;
  29. int odd[ODD]={1,2,3,4,5,6,7,8,9};
  30. int even[EVEN]={1,2,3,4,5,6,7,8,9,10};
  31. while(i<ODD)
  32. {
  33. insert(&podd,odd[i++]);
  34. }
  35.  
  36. i=0;
  37. while(i<EVEN)
  38. {
  39. insert(&peven,even[i++]);
  40. }
  41.  
  42. display(podd);
  43. ele = find_mid(podd);
  44. printf("\n%d",ele);
  45.  
  46. printf("\n");
  47. display(peven);
  48.  
  49. ele = find_mid(peven);
  50. printf("\n%d",ele);
  51.  
  52. return 0;
  53. }
Reputation Points: 34
Solved Threads: 4
Junior Poster
Iam3R is offline Offline
110 posts
since Oct 2009
Nov 1st, 2009
0

Verify It and correct me

i have posted this 3 days ago but i dint get any reply.
this time hope some people will reply.

thanks in advance,


Click to Expand / Collapse  Quote originally posted by Iam3R ...
Hello ,
i have written the code for finding the middle element of the list with
0(n) complexity.

please gurus, verify that and let me know if it needs any modifications

and ofcourse i have question to ask

when the no of elements are odd
we can easily find the middle element
suppose
1 2 3 4 5 the middle is 3
but when the no of elements are even
1 2 3 4 5 6
how can we decide the middle element
is there any most widely used way or experts follow.

please let me know


NOTE : in my previous post i have given the insert function you can have a look at it if you need
insert
  1. int find_mid(struct node *nrml)
  2. {
  3. struct node *adv=nrml;
  4. while(adv) {
  5. if(adv->next !=NULL) {
  6. //if(adv->next->next != NULL)
  7. adv = adv->next->next ;
  8. nrml = nrml -> next;
  9. }
  10. else {
  11. printf("with odd :\n");
  12. return nrml->data;
  13. }
  14. }
  15. printf("with even :\n");
  16. return nrml->data;
  17. }
  18.  
  19.  
  20. Driver code :
  21.  
  22. #define ODD 9
  23. #define EVEN 10
  24. int main()
  25. {
  26. struct node *podd=NULL;
  27. struct node *peven=NULL;
  28. int n,d,i=0,ele;
  29. int odd[ODD]={1,2,3,4,5,6,7,8,9};
  30. int even[EVEN]={1,2,3,4,5,6,7,8,9,10};
  31. while(i<ODD)
  32. {
  33. insert(&podd,odd[i++]);
  34. }
  35.  
  36. i=0;
  37. while(i<EVEN)
  38. {
  39. insert(&peven,even[i++]);
  40. }
  41.  
  42. display(podd);
  43. ele = find_mid(podd);
  44. printf("\n%d",ele);
  45.  
  46. printf("\n");
  47. display(peven);
  48.  
  49. ele = find_mid(peven);
  50. printf("\n%d",ele);
  51.  
  52. return 0;
  53. }
Reputation Points: 34
Solved Threads: 4
Junior Poster
Iam3R is offline Offline
110 posts
since Oct 2009
Nov 3rd, 2009
0
Re: finding the middle element of the list with 0(n)
1> finding the mid:

Your function for finding the mid element is good enough. The complexity is O(n/2) not O(n) but anyway O(n/2) is also considered O(n) only when n is very large.

2> mid of even set of data

Its upto you or upto the requirement which will tell which one to select as the mid-element. Sometimes u may take the n/2 th element or sometimes u can take (n/2 +1)th element.
e.g. in the set {1,2,3,4}
U can either take 2 or 3 as the mid-element.
Reputation Points: 121
Solved Threads: 61
Posting Pro in Training
dkalita is offline Offline
402 posts
since Sep 2009
Nov 3rd, 2009
0
Re: finding the middle element of the list with 0(n)
thanks for your reply Dkalita, I indeed like your answer.
and i hope you also verify this and guide me rich.


find nthlast using 0(n) complexity
Reputation Points: 34
Solved Threads: 4
Junior Poster
Iam3R is offline Offline
110 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Display in C for multiple threads
Next Thread in C Forum Timeline: Pthread support in Vxworks





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC