473,387 Members | 1,791 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,387 software developers and data experts.

Reading link list backwards.

Hello,

Can anyone tell me how to read a link list backwards?? The link list is single link list not a doubly one..
Jun 20 '07 #1
6 2789
mac11
256 100+
Hello,

Can anyone tell me how to read a link list backwards?? The link list is single link list not a doubly one..
I suppose you could traverse the list starting at the head and put everything into a stack as you go. That way when you get to the end of the list you can pop from the stack to get everything in reverse.

The list won't really be reversed though, you'd have to change the links to do that. This just allows you to print (whatever) your elements in reverse order.
Jun 20 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
Another approach is to create a new empty list.

Read the first list and insert each node read from the first before the first node of the second list.

When you get to the last node of the first list, it will be inserted before the first node of the second list.

The second list is now the reverse of the first list.
Jun 21 '07 #3
Aren't those two answers kind of the same, though?
Without it being doubly-linked and assuming you aren't using any other lists, it's impossible directly.
Jun 24 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
Yes. A stack can be implemented as a linked list where the new element is "pushed" in front. However, a stack can also be implemented where the new element is "pushed" at the end. The ""pop" removes the first (or last) element. All that is required is that the "pop" remove the last element added.

This is functionally the same as a reversed linked list, but with a stack you are supposed to only "push" and "pop" elements. You are not supposed to fiddle with the underlying linked list.

If you need to reverse a linked list then you need to reverse it as a linked list.
Jun 24 '07 #5
svlsr2000
181 Expert 100+
i think you can do something like this using recursion

Expand|Select|Wrap|Line Numbers
  1.  
  2. void printreverse(Node * nodeptr)
  3. {
  4. if(node)
  5. {
  6. printreverse(nodeptr->next)
  7. process(nodeptr)
  8. }
  9. }
  10.  
Jun 25 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
i think you can do something like this using recursion
I agree. But if the recursion is deep you could exceed stack memory limits and crash the program. However, for a trial problem or a homework assignment, this is a reasonable approach.
Jun 25 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Jay | last post by:
I have a very large text file (being read by a CGI script on a web server), and I get memory errors when I try to read the whole file into a list of strings. The problem is, I want to read the file...
11
by: Matt DeFoor | last post by:
I have some log files that I'm working with that look like this: 1000000000 3456 1234 1000000001 3456 1235 1000020002 3456 1223 1000203044 3456 986 etc. I'm trying to read the file...
8
by: sudhirlko2001 | last post by:
How to swap two nodes of doubly Linklist
6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
4
by: Henk | last post by:
Hi, I am new to the c-programming language and at the moment I am struggling with the following: I want to read a file using fread() and then put it in to memory. I want to use a (singel)...
6
by: Neil Patel | last post by:
I have a log file that puts the most recent record at the bottom of the file. Each line is delimited by a \r\n Does anyone know how to seek to the end of the file and start reading backwards?
4
by: plmanikandan | last post by:
Hi, I am new to link list programming.I need to traverse from the end of link list.Is there any way to find the end of link list without traversing from start(i.e traversing from first to find the...
3
by: maruf.syfullah | last post by:
Consider the following Class definitions: class AClass { int ai1; int ai2; public: CClass* c; AClass(){}
1
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.