473,505 Members | 16,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ how to reverse singly linked list

5 New Member
My class

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #ifndef NODE_H
  4. #define NODE_H
  5.  
  6. #include <cstdlib> // Provides size_t and NULL
  7.  
  8. class Node
  9. {
  10. public:
  11.  
  12. // TYPEDEF
  13. typedef int value_type;
  14.  
  15. // CONSTRUCTOR
  16. Node(const value_type& init_data = value_type(), Node* init_link = NULL)
  17. {
  18. data_field = init_data; 
  19. link_field = init_link;
  20. }
  21.  
  22. // Member functions to set the data and link fields:
  23. void set_data(const value_type& new_data) 
  24. {
  25. data_field = new_data;
  26. }
  27.  
  28. void set_link(Node* new_link) 
  29. {
  30. link_field = new_link;
  31. }
  32.  
  33. // Constant member function to retrieve the current data:
  34. value_type data() const 
  35. {
  36. return data_field;
  37. }
  38.  
  39. // Two slightly different member functions to retreive
  40. // the current link. Similar to next()
  41. const Node* link() const 
  42. {
  43. return link_field;
  44. }
  45.  
  46. Node* link() 
  47. {
  48. return link_field;
  49. }
  50.  
  51. private:
  52. value_type data_field;
  53. Node* link_field;
  54. };
  55.  
  56. size_t list_length(const Node* head_ptr)
  57. {
  58. const Node *cursor;
  59. size_t answer;
  60.  
  61. answer = 0;
  62. for(cursor = head_ptr; cursor != NULL; cursor = cursor->link())
  63. ++answer;
  64.  
  65. return answer;
  66. }
  67.  
  68. #endif
  69.  
  70.  
My problem:

Write a function to split a singly linked list L into two smaller linked lists P and Q.
The length of L is unknown and the splitting point should be the middle of L. Assume that L has at least two nodes.

What I have:

Expand|Select|Wrap|Line Numbers
  1.  
  2. void split(const Node* L, Node*& P, Node*& Q)
  3. {
  4. int i = 0;
  5. int L_mid = list_length(L)/2;
  6.  
  7. const Node *tmp = L;
  8.  
  9. for(i=0;i<L_mid;tmp=tmp->link(),++i)
  10. {    
  11. }
  12.  
  13. P = L;
  14. Q=tmp;
  15. tmp->link()=NULL;
  16.  
  17. }
  18.  
  19.  
I'm not sure what to do. Any help is appreciated. Thanks
Oct 17 '11 #1
1 1882
weaknessforcats
9,208 Recognized Expert Moderator Expert
It might help if you write the steps you are going to take on paper before starting to code. Like:

1) make a pass of L and count he nodes
2) locate midpoint of L
etc...
Oct 17 '11 #2

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

Similar topics

4
4700
by: HS-MOON | last post by:
I'm asking you to help me. I'm a beginner of studying c++. I'm trying to make the Singly Linked List(Ordered). Anyway, I've been debugging all day. I can't sort it out!! As you see, I don't...
9
20626
by: Perpetual Snow | last post by:
How can I reverse a linked list with no memory allocation? I'm searching for an algorithm which is constant in runtime and space. Thanks
19
13542
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
59
4153
by: Anando | last post by:
Hi, I have a linear singly linked list of 100 elements. I would like to prune it such that only user specified elements (say only the elements 1, 13, 78 and 100 of the original list) survive...
6
2509
by: Alien | last post by:
Hi, I am having problem with ordering a singly linked list. Since a singly linked list goes only one way, is it possible to order them? If my structs look like the one below. struct block...
3
3392
by: jou00jou | last post by:
Hello, I am trying to sort a singly linked list for the following typedef typedef struct message { int messageId; char * messageText; struct message * next; } message; I have successfully...
23
4335
by: Himanshu Chauhan | last post by:
Hi! I was wondering, In the first parse of a singly linked list of unknown length, is it possible to know when we are at middle of the linked list? Regards --Himanshu
4
4285
by: saki | last post by:
How do we reverse a singly linked list without using extra memory.Extra pointers can however be used
6
1571
by: Hamster | last post by:
Hi, Need help with my code on reversing a singly linked list, here's my code: void reverseList ( List& listObj) { ListNode*pHeadnew=listObj.lastPtr, *pTailnew=listObj.firstPtr;
7
5723
by: davidson1 | last post by:
Hello friends, I want ur help regarding singly linked list in C,I tried in net,But it is confusing and difficult.I want singly linked list in a simple way of Understanding.Please Help Me I want...
0
7218
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,...
0
7307
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,...
0
7370
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7021
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...
0
7478
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...
1
5035
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...
0
1532
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 ...
1
755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
409
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...

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.