Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

Printing linked lists

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...
  • 2
  • 2
1 Solution
chadd082197Author Commented:
Edited text of question
No, you do not need a stack
Dashes used to suppress EE elimination of leading spaces.

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

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 *)
--------P := P^.NEXT
----end  (*while *)
end; (*printlist *)

chadd082197Author Commented:
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?).
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.
...to save memory use NEW and DISPOSE

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now