473,657 Members | 2,566 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 1889
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
4704
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 know what this is wrong.. When I run this pgm, It can't find the Current Variable. //
9
20638
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
13566
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., head and tail are not connected. Of course, recursive function aproach to traverse the list is one way. But, depending upon the list size, it could overrun the stack pretty fast.
59
4207
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 after pruning. Can somebody show me how to do this ? I am a scientist and not a computer engineer/student. This will help me develop an application in data analysis. I will be grateful for your advice.
6
2525
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 { struct block *next;
3
3412
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 implemented add, delete, print, and search functions for the linked list but I am not sure why I am stuck with this one.
23
4363
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
4306
by: saki | last post by:
How do we reverse a singly linked list without using extra memory.Extra pointers can however be used
6
1581
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
5731
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 single list to create,delete,Display Can u give me Program and Explanation,to create,display,Delete in a Simple and Easy Way.Really it will be Helpful for Me. I have given some partial code,I want Program to create,display,Delete a Singly...
0
8310
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7333
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2731
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 we have to send another system
2
1620
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.