Sie sind auf Seite 1von 4

Topic 1: Single 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}

1. Check whether a linked list is empty or not


function isEmpty (L: list)  boolean
{send true if L is empty, false if not}
Dictionary
Algorithm
 first(L)=nil

2. Create an empty list


procedure createList (output L: list)
{I.S. –
F.S. L is an empty list}
Dictionary
Algorithm
first(L) nil

3. Insert new element to be the first element


Initial state:

or first
Final state

or

procedure insertFirst (input/output L: list, input P: address)


{I.S. L is either empty or not. P is a new element that will be inserted
F.S. P is the first element of L}
Dictionary
Algorithm
next(P)  first(L)
first(L) P

4. Create new element


procedure createNewElmt (input X: infotype, output P: address)
{I.S. X is an info
F.S. P has been allocated and contains X}
Dictionary
Algorithm
allocate(P)
info(P)  X
next(P)  nil
5. Insert a new element, P, as the next element of Prec

procedure insertAfter (input/output Prec, P: address)


{I.S. Prec is not nil
F.S. P is the next element of Prec}
Dictionary

Algorithm
next(P)  next(Prec)
next(Prec) P

6. Insert a new element, P, as the last element of list L


Initial state first Final state

or

procedure insertLast (input/output L: list, input P: address)


{I.S. L is either empty or not. P is a new element that will be inserted
F.S. P is the last element of L}
Dictionary

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:

procedure deleteFirst (input/output L: list, output P: address)


{I.S. L is not empty
F.S. the first element has been deleted and maintained in P. List L is probably empty}
Dictionary

Algorithm
P  first(L)
first(L)  next(first(L))
next(P)  nil {optional}

8. Delete the next element of Prec. Draw the process

procedure deleteAfter (input/output Prec, output P: address)


{I.S. Prec is not nil
F.S. P keeps the next element of Prec deleted}
Dictionary

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.

procedure deleteLast (input/output L: list, output P: address)


{I.S. L is not empty, possibly has only one element
F.S. the last element has been deleted and maintained in P. List L is probably empty}
Dictionary

Algorithm
{check if L has only one element and delete the element}

if ............................................................................
else

10. Print all info in list L

11. Find an info, X, in list L.

12. Concatenation of two lists.


procedure Concat (input L1, L2: list, output L3: list)
{I.S. L1 and L2 has been defined, but can be empty
F.S. a new list, L3, has been created containing all elements of L1 and L2}
Dictionary

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)

Draw the process:

Das könnte Ihnen auch gefallen