My class -
-
-
#ifndef NODE_H
-
#define NODE_H
-
-
#include <cstdlib> // Provides size_t and NULL
-
-
class Node
-
{
-
public:
-
-
// TYPEDEF
-
typedef int value_type;
-
-
// CONSTRUCTOR
-
Node(const value_type& init_data = value_type(), Node* init_link = NULL)
-
{
-
data_field = init_data;
-
link_field = init_link;
-
}
-
-
// Member functions to set the data and link fields:
-
void set_data(const value_type& new_data)
-
{
-
data_field = new_data;
-
}
-
-
void set_link(Node* new_link)
-
{
-
link_field = new_link;
-
}
-
-
// Constant member function to retrieve the current data:
-
value_type data() const
-
{
-
return data_field;
-
}
-
-
// Two slightly different member functions to retreive
-
// the current link. Similar to next()
-
const Node* link() const
-
{
-
return link_field;
-
}
-
-
Node* link()
-
{
-
return link_field;
-
}
-
-
private:
-
value_type data_field;
-
Node* link_field;
-
};
-
-
size_t list_length(const Node* head_ptr)
-
{
-
const Node *cursor;
-
size_t answer;
-
-
answer = 0;
-
for(cursor = head_ptr; cursor != NULL; cursor = cursor->link())
-
++answer;
-
-
return answer;
-
}
-
-
#endif
-
-
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: -
-
void split(const Node* L, Node*& P, Node*& Q)
-
{
-
int i = 0;
-
int L_mid = list_length(L)/2;
-
-
const Node *tmp = L;
-
-
for(i=0;i<L_mid;tmp=tmp->link(),++i)
-
{
-
}
-
-
P = L;
-
Q=tmp;
-
tmp->link()=NULL;
-
-
}
-
-
I'm not sure what to do. Any help is appreciated. Thanks
1 1889
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...
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
//
|
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
|
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.
|
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.
|
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;
| |
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.
|
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
|
by: saki |
last post by:
How do we reverse a singly linked list without using extra
memory.Extra pointers can however be used
|
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;
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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();...
|
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...
| |
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
|
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...
| |