ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Birgi,
I am sorry, You are right.
I need a simple c/c++ examples, say You have a class or struct with integer value and pointer to next. I want to see the best way for inverting the list and finding loop in list. To be more specific, the first function should get a list and return the list inverted, the second should get a list and return node where the loop starts.
Thanks
For the first function check the following out, it should work. But it only gets one parameter and it is the integer pointer that is pointing to the head of the list.
void invertList(int* head){
if (head->next != null){ // because if there is only one element in the list you won't do anything.
int* prev = head;
head = head->next;
prev->next = null; // first element of the list is a special case.
while(head->next != null){ // after dealing with the first , for other elements same process
int* temp = head->next;
head->next = prev;
prev = head;
head = temp;
}
head->next = prev; // for the last connection which cannot be done in the loop
}
}
And I could not understand what you mean by loop for the second function...
Birgi,
Thanks a lot, I have solved the problem with You. My solution is:
next=0;
while(first != 0){
actual = first;
first=first->next;
actual->next=next;
next=actual;
}
Let me give You an examaple for the second problem.
Say, we have 5 instances with number names: 1,2,3,4,5
Example for a list is: 1->2->3
example for a list with loop is: 1->2->3->4->2->3->4->2...
Do You see what I mean? The loop starts at node 2, so I need to return the second node.
Thank a lot,
Leonid
My idea is to make 2 "for" loops,
first runs on the list from first node till null (end), while remembering the node place (by number), the second internal "for" loop runs from first node till current, and if it finds the node during less checks than it's place, than it is the start of the loop.
But I wonder if it is possible to do it using one loop.
Thanks
Birgi,
No, there is no information.
Probably, my solution is the only one, it just seems too simple to be asked in hard tests. I mean, when I get a question to implement that function in the best way, I have a feeling that there should be a trick and not the only simple way as I hve solved
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.