Topic 1 Linked List
Topic 1 Linked List
Dictionary:
type infotype: .... {can be integer, char, records, string, ...}
type address: pointer to elmtList
type elmtList: <info: infotype, next: address>
type list: <first: address>
{use first(L) to refer to the first element, info(P) and next(P) to refer to the info and the next element of P}
or first
Final state
or
Algorithm
next(P) next(Prec)
next(Prec) P
or
Algorithm
if first(L)=nil then {empty list}
insertFirst(L,P)
else
last first(L)
while next(last) ≠ nil do
last next(last)
{next(last)=nil}
insertAfter(last, P)
7. Delete the first element of list L; draw the process for two conditions
First, if list L has only one element:
First, if list L has more than one element:
Algorithm
P first(L)
first(L) next(first(L))
next(P) nil {optional}
Algorithm
P next(Prec)
next(Prec) next(P)
next(P) nil {optional}
9. Delete the last element of list L. Draw the process and create the algorithm.
Algorithm
{check if L has only one element and delete the element}
if ............................................................................
else
Algorithm
createList(L3)
if first(L1)=nil then
first(L3) first(L2)
else
first(L3) first(L1)
last first(L1)
while next(last) ≠ nil do
last next(last)
{next(last)=nil}
next(last) first(L2)