For the List class, add a Boolean-valued function that determines whether the data items in the linked list are arranged in ascending order.
How can i do this using recursion
5 2169 sicarie 4,677
Recognized Expert Moderator Specialist
For the List class, add a Boolean-valued function that determines whether the data items in the linked list are arranged in ascending order.
How can i do this using recursion
Call the same method inside the method until you finish the ordering.
What do you have so far?
sicarie 4,677
Recognized Expert Moderator Specialist
Thanks sicarie for your reply and also for the link ....
for example if i have this :
bool list::isOrdered () const
{
Node *ptr = first ; Node *following;
bool isOrdered = true;
if(mysize >=2 )
{
following = ptr->next;
while (following !=Null && isOrdered)
{isOrdered=(ptr->data->following->data);
ptr=following;
following = following->next;}
}
return isOrdered;
}
how can i do it with recursion ?
it is little confusing for me :(
sicarie 4,677
Recognized Expert Moderator Specialist
Yeah, recursion is one of the trickier subjects to understand.
I'm going to warn you right now, I've only written two or three programs that used very light recursion, so if someone else wants to jump in here if they feel they have a better handle, they're more than welcome. And with the apology that I'm probably going to stumble through it a little bit as this is the one concept I have yet to try to explain to someone, I'll outline how I would go about it.
These three things are needed for recursion.
1. A simple base case which we have a solution for and a return value.
Ordered list. With recursion, the isOrdered() method base case is going to be either the tail or last element, or even possibly the head or beginning - if it goes all the wya through first, and then orders on it's way back out, and returns ... the first value?
2. A way of getting our problem closer to the base case. I.e. a way to chop out part of the problem to get a somewhat simpler problem.
We're ordering by 'mysize' so (if it is ascending order), I would start at the beginning, take a value, and test it agains the next. If it is larger than the next, swap the two.
3. A recursive call which passes the simpler problem back into the function.
I think it would be easiest to keep track of a large value, whichever is the largest, so pass the location of the larger of the two back to the function.
So we're going to take a list.
Get the first element
Check it against the next
if it is bigger
swap and pass the pointer to the first (now the second) to isOrdered again
else,
send the second to isOrdered
Somehow I think that algorithm is deficient, it can put a node at the end, but I'm not sure it will get the rest. What do you think?
Yeah, recursion is one of the trickier subjects to understand.
I'm going to warn you right now, I've only written two or three programs that used very light recursion, so if someone else wants to jump in here if they feel they have a better handle, they're more than welcome. And with the apology that I'm probably going to stumble through it a little bit as this is the one concept I have yet to try to explain to someone, I'll outline how I would go about it.
These three things are needed for recursion.
1. A simple base case which we have a solution for and a return value.
Ordered list. With recursion, the isOrdered() method base case is going to be either the tail or last element, or even possibly the head or beginning - if it goes all the wya through first, and then orders on it's way back out, and returns ... the first value?
2. A way of getting our problem closer to the base case. I.e. a way to chop out part of the problem to get a somewhat simpler problem.
We're ordering by 'mysize' so (if it is ascending order), I would start at the beginning, take a value, and test it agains the next. If it is larger than the next, swap the two.
3. A recursive call which passes the simpler problem back into the function.
I think it would be easiest to keep track of a large value, whichever is the largest, so pass the location of the larger of the two back to the function.
So we're going to take a list.
Get the first element
Check it against the next
if it is bigger
swap and pass the pointer to the first (now the second) to isOrdered again
else,
send the second to isOrdered
Somehow I think that algorithm is deficient, it can put a node at the end, but I'm not sure it will get the rest. What do you think?
sicarie I will try ..and tell you what I will have : )
Thanks a lot ...
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: M Hayouka |
last post by:
hi
can anybody give me a web that I can learn about a recursion function
I don't really understand it
|
by: Paul |
last post by:
Hi,
Global operator new and delete are overloaded and I am using stl map
to store pointers, but this code crashes, can some one shed some
light???
Compiler: MS VC++ 6.0
STL: Shipped with Visual Studio.
#include <malloc.h>
#include <map>
|
by: Bamber |
last post by:
Why does f get returned ? (compiled with gcc).
#include<stdio.h>
int func(int a);
main()
{ int f;
f=func(7);
printf("f=%d",f);
|
by: Snis Pilbor |
last post by:
Howdy,
Forgive me for not looking this up in the manuals or just trying
it outright, as I don't have access to a shell right now. Anyway, I
thought of this while juggling earlier: it would be neat if there were
a way to do a "sizeof" on the total set of declarations at the top of a
given function. For example:
#include <stdlib.h>
#include <stdio.h>
|
by: elsa |
last post by:
hi..
im trying to build a program using arrays to do the following things:
1- convert a digital number to binary using mathematical operators by using the function void binaryConversionUsingArithematicOperators(unsigned long)
2- convert a digital number to binary using bitwise operators by using the function void binaryConversionUsingBitwiseOperators(unsigned long)
3- check if the word is a palindrome using iteration by using this function
...
| |
by: Nithya Kalyani |
last post by:
Write a function using Recursion to do the following: You are the manager in a small factory. You have 7 workers and 7 jobs to be done. Each worker is assigned one and only one job. Each worker demands different pay for each job. (E.g.: Worker Archie demands $10 for welding, $15 for carpentry, etc. Worker Jughead demands $12 for welding, $5 for carpentry, etc.) Find the job-assignment which works out cheapest for you.
|
by: jack113256 |
last post by:
Hi everyone:
I have a question in using Callback function, there is my code:
/******* code start *********/
#include <stdio.h>
void a();
void b();
void run();
|
by: Tom_chicollegeboy |
last post by:
here is what I have to do:
This question involves a game with teddy bears. The game starts when I
give you some bears. You then start giving me back some bears, but you
must follow these rules (where n is the number of bears that you
have):
If n is even, then you may give back exactly n/2 bears. (Hint: To test
whether n is even, use the expression ((n % 2) == 0).)
If n is divisible by 3 or 4, then you may multiply the last two digits
|
by: balbirchand |
last post by:
which is better recursion or function ....???and when to use function ?and when to use recursion?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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: 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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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: 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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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: 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...
| |