| | |
finding the middle element of the list with 0(n)
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 50
Reputation:
Solved Threads: 3
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
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
C Syntax (Toggle Plain Text)
int find_mid(struct node *nrml) { struct node *adv=nrml; while(adv) { if(adv->next !=NULL) { //if(adv->next->next != NULL) adv = adv->next->next ; nrml = nrml -> next; } else { printf("with odd :\n"); return nrml->data; } } printf("with even :\n"); return nrml->data; } Driver code : #define ODD 9 #define EVEN 10 int main() { struct node *podd=NULL; struct node *peven=NULL; int n,d,i=0,ele; int odd[ODD]={1,2,3,4,5,6,7,8,9}; int even[EVEN]={1,2,3,4,5,6,7,8,9,10}; while(i<ODD) { insert(&podd,odd[i++]); } i=0; while(i<EVEN) { insert(&peven,even[i++]); } display(podd); ele = find_mid(podd); printf("\n%d",ele); printf("\n"); display(peven); ele = find_mid(peven); printf("\n%d",ele); return 0; }
•
•
Join Date: Oct 2009
Posts: 50
Reputation:
Solved Threads: 3
i have posted this 3 days ago but i dint get any reply.
this time hope some people will reply.
thanks in advance,
this time hope some people will reply.
thanks in advance,
•
•
•
•
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
insertC Syntax (Toggle Plain Text)
int find_mid(struct node *nrml) { struct node *adv=nrml; while(adv) { if(adv->next !=NULL) { //if(adv->next->next != NULL) adv = adv->next->next ; nrml = nrml -> next; } else { printf("with odd :\n"); return nrml->data; } } printf("with even :\n"); return nrml->data; } Driver code : #define ODD 9 #define EVEN 10 int main() { struct node *podd=NULL; struct node *peven=NULL; int n,d,i=0,ele; int odd[ODD]={1,2,3,4,5,6,7,8,9}; int even[EVEN]={1,2,3,4,5,6,7,8,9,10}; while(i<ODD) { insert(&podd,odd[i++]); } i=0; while(i<EVEN) { insert(&peven,even[i++]); } display(podd); ele = find_mid(podd); printf("\n%d",ele); printf("\n"); display(peven); ele = find_mid(peven); printf("\n%d",ele); return 0; }
0
#3 32 Days Ago
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.
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.
•
•
Join Date: Oct 2009
Posts: 50
Reputation:
Solved Threads: 3
0
#4 32 Days Ago
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
and i hope you also verify this and guide me rich.
find nthlast using 0(n) complexity
![]() |
Other Threads in the C Forum
- Previous Thread: Display in C for multiple threads
- Next Thread: Pthread support in Vxworks
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks bash binarysearch centimeter changingto char character convert copyimagefile cprogramme creafecopyofanytypeoffileinc createprocess() database dynamic execv fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include infiniteloop initialization input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation overwrite owf pdf pointer pointers posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming standard strchr string suggestions systemcall test testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi





