473,671 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reverse a single linked list

Hi Everyone,

I want to actually reverse a single linked list without using many
variables, i have a recurssive solution, but i wanted an iterative one.
can anyone help me on this?

Dec 17 '06 #1
11 8703
Ico
sa*****@yahoo.c o.in wrote:
Hi Everyone,

I want to actually reverse a single linked list without using many
variables, i have a recurssive solution, but i wanted an iterative one.
Why would you want that, is your recursive solution not good enough ?

--
:wq
^X^Cy^K^X^C^C^C ^C
Dec 17 '06 #2
>
Why would you want that, is your recursive solution not good enough ?
Yes, recursive solution is good but uses a lot of stack space and it
might not be helpful in a envirnoment where stack space is limited like
embedded systems where recursive functions are normally not encouraged.

Dec 17 '06 #3
Ico
sa*****@yahoo.c o.in wrote:
>
>>
Why would you want that, is your recursive solution not good enough ?

Yes, recursive solution is good but uses a lot of stack space and it
might not be helpful in a envirnoment where stack space is limited like
embedded systems where recursive functions are normally not encouraged.
Use a doubly linked list then.
-
:wq
^X^Cy^K^X^C^C^C ^C
Dec 17 '06 #4
sa*****@yahoo.c o.in wrote:
Hi Everyone,

I want to actually reverse a single linked list without using many
variables, i have a recurssive solution, but i wanted an iterative one.
can anyone help me on this?
Since this has the odor of homework about it, I'll just
offer a few hints:

Hint #1: Do you know how to remove the first item from
a linked list?

Hint #2: Do you know how to add an item at the beginning
of a linked list?

--
Eric Sosman
es*****@acm-dot-org.invalid
Dec 17 '06 #5
>
Hint #2: Do you know how to add an item at the beginning
of a linked list?

--
Thanks very much for the hints, did you mean to say add an item at the
end of the list?
>From your hints, i think of a solution where in the last_node->ptr is
made to point to first node and this logic needs to be excecuted as
many times as many as strlen(string). Is this the solution you meant or
any other improvements are possible?

Dec 17 '06 #6
sa*****@yahoo.c o.in wrote:
> Hint #2: Do you know how to add an item at the beginning
of a linked list?

--

Thanks very much for the hints, did you mean to say add an item at the
end of the list?
Hint #3: No.
>>From your hints, i think of a solution where in the last_node->ptr is
made to point to first node and this logic needs to be excecuted as
many times as many as strlen(string). Is this the solution you meant or
any other improvements are possible?
No, and yes.

You are working too hard: There exists a much simpler
solution than those you seem to be thinking about.

--
Eric Sosman
es*****@acm-dot-org.invalid

Dec 17 '06 #7
>
>From your hints, i think of a solution where in the last_node->ptr is
made to point to first node and this logic needs to be excecuted as
many times as many as strlen(string). Is this the solution you meant or
any other improvements are possible?

No, and yes.

You are working too hard: There exists a much simpler
solution than those you seem to be thinking about.
Yeah, i think so, i just got a solution but i don't think it utilises
your hints, it goes like this

reverse(stuct list* node)
{
struct list* original_head = node;
struct list* new_head = NULL;
struct list* original_head_n ext = NULL;

while(original_ head != NULL)
{
original_head_n ext = original_head->ptr;
origianl_head->ptr = new_head;
new_head = original_head;
original_head = original_head_n ext;
}

return(new_head );

}

Did you mean any other solution???

Dec 17 '06 #8
Ico wrote:
>
sa*****@yahoo.c o.in wrote:
>
Why would you want that,
is your recursive solution not good enough ?
Yes, recursive solution is good but uses a lot of stack space and it
might not be helpful in a envirnoment
where stack space is limited like
embedded systems where recursive
functions are normally not encouraged.

Use a doubly linked list then.
A doubly linked list has nothing to do with OP's stated problem.

--
pete
Dec 17 '06 #9
sa*****@yahoo.c o.in wrote:
>>From your hints, i think of a solution where in the last_node->ptr is
made to point to first node and this logic needs to be excecuted as
many times as many as strlen(string). Is this the solution you meant or
any other improvements are possible?
No, and yes.

You are working too hard: There exists a much simpler
solution than those you seem to be thinking about.

Yeah, i think so, i just got a solution but i don't think it utilises
your hints, it goes like this

reverse(stuct list* node)
{
struct list* original_head = node;
struct list* new_head = NULL;
struct list* original_head_n ext = NULL;

while(original_ head != NULL)
{
original_head_n ext = original_head->ptr;
origianl_head->ptr = new_head;
new_head = original_head;
original_head = original_head_n ext;
}

return(new_head );

}

Did you mean any other solution???
That's the one I meant, although I would have written it
with fewer auxiliary variables. One way to look at this
approach is to think of it as removing the first node from
the old list (hint #1) and pushing it onto the start of the
new list (hint #2), and repeating until the old list is empty.

Start:
old -A -B -C ->|
new ->|

Step 1:
old -B -C ->|
new -A ->|

Step 2:
old -C ->|
new -B -A ->|

Step 3:
old ->|
new -C -B -A ->|

Or, to relate it to another common experience: Hold a
deck of playing cards face down, and deal the cards one at
a time (still face down) onto a pile on the table in front
of you. How does the order of the cards in the dealt pile
relate to the order in the original deck?

--
Eric Sosman
es*****@acm-dot-org.invalid
Dec 17 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
11284
by: John Keeling | last post by:
Dear all, I tried the test program below. My interest is to examine timing differences between insert vs. append & reverse for a list. My results on my XP Python 2.3.4 are as follows: time_reverse 0.889999389648 time_insert 15.7750005722 Over multiple runs ... the time taken to insert at the head of a list, vs. the time taken to append to a list and then reverse it is typically 16 or 17 times longer. I would have expected the insert...
6
6459
by: Zri Man | last post by:
I'm relatively new to DB2 and was reasonably amused to see the REVERSE SCAN availability for Indexes. My assumptions are as follows: DB2/UDB uses B-Tree for indexing by default and is likely the main offering for Indexing within the DB. Reverse Scans could possibly only happen on the the leaf node of the index
9
20639
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.
5
5501
by: Daniel | last post by:
I need to reverse the doubly linked list with dummy node. I think the solution is to exchange each node pointers' next and previous address. But what's wrong in my function? Thanks void reverse_list(NodePtr p) { NodePtr next, q=p, head=p->prev; while (q != head) { next = q->next; q->next = q->prev;
11
2820
by: Neo | last post by:
Hi Frns, Could U plz. suggest me how can i reverse a link list (without recursion)??? here is my code (incomplete): #include<stdio.h>
8
7576
by: vijay | last post by:
Hello, As the subject suggests, I need to print the string in the reverse order. I made the following program: # include<stdio.h> struct llnode { char *info;
20
6532
by: sirsnorklingtayo | last post by:
hi guys please help about Linked List, I'm having trouble freeing the allocated memory of a single linked list node with a dynamic char* fields, it doesn't freed up if I use the FREE() function in C.. But if I try to use a single linked list with a static char array fields I can free the memory allocated with out any problems using the FREE(). So, why freeing a single linked list with dynamic char* is hard and why the FREE() function is...
6
1582
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;
0
8474
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, 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...
0
8912
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...
0
8819
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 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...
0
7428
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...
1
6222
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 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...
0
5692
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
4222
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...
0
4403
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1807
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.