473,398 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 8677
Ico
sa*****@yahoo.co.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.co.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.co.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.co.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_next = NULL;

while(original_head != NULL)
{
original_head_next = original_head->ptr;
origianl_head->ptr = new_head;
new_head = original_head;
original_head = original_head_next;
}

return(new_head);

}

Did you mean any other solution???

Dec 17 '06 #8
Ico wrote:
>
sa*****@yahoo.co.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.co.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_next = NULL;

while(original_head != NULL)
{
original_head_next = original_head->ptr;
origianl_head->ptr = new_head;
new_head = original_head;
original_head = original_head_next;
}

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
sa*****@yahoo.co.in wrote:
>
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?
/* ================================================== ===== */
/* believed necessary and sufficient for NULL terminations */
/* Reverse a singly linked list. Reentrant (pure) code */
nodeptr revlist(nodeptr root)
{
nodeptr curr, nxt;

if (root) { /* non-empty list */
curr = root->next;
root->next = NULL; /* terminate new list */
while (curr) {
nxt = curr->next; /* save for walk */
curr->next = root; /* relink */
root = curr; /* save for next relink */
curr = nxt; /* walk onward */
}
}
/* else empty list is its own reverse; */
return root;
} /* revlist */

which requires that a 'nodeptr' be defined to point to a struct
with at least a next field, of type nodeptr. Usage is of the form:

nodeptr root;
....
/* code to form the list */
...
root = revlist(root)

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Dec 18 '06 #11
Ico wrote
(in article
<45***********************@dreader29.news.xs4all.n l>):
sa*****@yahoo.co.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.
Wasting space on unneeded links is also frowned upon in the
embedded space.

--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Dec 18 '06 #12

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

Similar topics

19
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:...
6
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...
9
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
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.,...
5
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...
11
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
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
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()...
6
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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
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...
0
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
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...
0
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...

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.