Solved

Printing linked lists

Posted on 1998-03-14
5
165 Views
Last Modified: 2010-04-16
Is pushing/popping the only way to print out a linked list (using pointers) to the screen? Also, what is the most efficient way to create multiple linked lists in the same program? Sample algorithms would be helpful...
0
Comment
Question by:chadd082197
  • 2
  • 2
5 Comments
 

Author Comment

by:chadd082197
ID: 1217394
Edited text of question
0
 
LVL 1

Accepted Solution

by:
Nexial earned 50 total points
ID: 1217395
No, you do not need a stack
Dashes used to suppress EE elimination of leading spaces.

TYPE pointertype = ^NODETYPE;
NODETYPE = RECORD
-------------INFO : INFOTYPE;
-------------NEXT : POINTERTYPE
-----------END;


Procedure printlist (list:pointertype)
(* print out value of INFO field of each node of LL *)

var P : pointertype;

begin   (* print the list *)
--P := list;
--WHILE P <> NIL do
----begin (* print data & advance pointer *)
--------writeln(P^.INFO);
--------P := P^.NEXT
----end  (*while *)
end; (*printlist *)



   
0
 

Author Comment

by:chadd082197
ID: 1217396
Looks good...thanks. Any thoughts on the second question? I'm writing a program in which the user can create any number of single linked lists and then manipulate them in various ways (appending nodes, deleting nodes, printing, etc). What I don't know is how to reference the different lists (for example, if the user creates a second list (or ten lists, whatever) how do you keep them separate since you can't "name" a list?).
0
 
LVL 1

Expert Comment

by:Nexial
ID: 1217397
Several thoughts on the second question:

Easiest, but not extremely efficient is to create a single linked list containing the bases of the other linked lists, along with whatever you choose to use as the list identifier.   Then you can
insert/delete list bases at any time.   Use common procedures for
the list manipulations where you pass the procedure a pointer to the node (or the previous node) of the node being manipulated.

Better is for this list to be doubly linked, so that deletion is easier.

Even better (especially if there are a lot of lists) is for this list to be a btree so that insertion, deletion, and searching it are much faster.

See Knuth, Sorting and Searching (Vol III, Art of Computer Programming, for generic list manipulation theory and algorithms.

Source code like I provided before can be found on the net or in many textbooks in the library.
0
 
LVL 2

Expert Comment

by:omsec
ID: 1217398
...to save memory use NEW and DISPOSE
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lotus Notes – formerly IBM Notes – is an email client application, while IBM Domino (earlier Lotus Domino) is an email server. The client possesses a set of features that are even more advanced as compared to that of Outlook. Likewise, IBM Domino is…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now