Solved

Printing linked lists

Posted on 1998-03-14
5
171 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Each year, investment in cloud platforms grows more than 20% (https://www.immun.io/hubfs/Immunio_2016/Content/Marketing/Cloud-Security-Report-2016.pdf?submissionGuid=a8d80a00-6fee-4b85-81db-a4e28f681762) as an increasing number of companies begin to…
Adding Computers to AD groups through an SCCM Task Sequence
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

777 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