LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-09-2005, 01:43 AM   #1
leonidg
Member
 
Registered: Dec 2004
Posts: 73

Rep: Reputation: 15
Linked Lists


Hello friends,
I need to implement 2 functions in the best way:
1) linked list inversion
2) finding loops in linked list

Thanks in advance for any examples
Leonid
 
Old 03-09-2005, 01:53 AM   #2
Birgi
Member
 
Registered: Oct 2004
Distribution: Mandrake 10.1
Posts: 40

Rep: Reputation: 15
Leonidg,

I think you need to be more specific. What kinds of examples are you looking for? Which language? What are the functions doing?

Birgi
 
Old 03-09-2005, 02:03 AM   #3
leonidg
Member
 
Registered: Dec 2004
Posts: 73

Original Poster
Rep: Reputation: 15
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
 
Old 03-09-2005, 02:40 AM   #4
Birgi
Member
 
Registered: Oct 2004
Distribution: Mandrake 10.1
Posts: 40

Rep: Reputation: 15
Leonidg,

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
 
Old 03-09-2005, 03:01 AM   #5
leonidg
Member
 
Registered: Dec 2004
Posts: 73

Original Poster
Rep: Reputation: 15
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
 
Old 03-09-2005, 03:51 AM   #6
leonidg
Member
 
Registered: Dec 2004
Posts: 73

Original Poster
Rep: Reputation: 15
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

Last edited by leonidg; 03-09-2005 at 03:54 AM.
 
Old 03-09-2005, 12:17 PM   #7
Birgi
Member
 
Registered: Oct 2004
Distribution: Mandrake 10.1
Posts: 40

Rep: Reputation: 15
Leonidg,

Will you have any information about the loop at the beginning? I mean, the number of elements of it or such?

Birgi
 
Old 03-10-2005, 03:07 AM   #8
leonidg
Member
 
Registered: Dec 2004
Posts: 73

Original Poster
Rep: Reputation: 15
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked Lists and Queues Kroenecker Programming 2 04-09-2005 02:59 AM
queue and linked lists Palamides Programming 2 03-09-2005 09:08 PM
Linked Lists - What and Why? scuzzman Programming 9 12-31-2004 11:51 AM
c++ doubly linked lists durden2.0 Programming 4 02-25-2004 06:56 PM
c++ linked lists jclark00001 Programming 10 02-23-2003 03:40 PM


All times are GMT -5. The time now is 03:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration