1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

62
1 Algorithms Algorithms Queues, Stacks and Queues, Stacks and Records stored in Linked Records stored in Linked Lists or Arrays Lists or Arrays

Transcript of 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

Page 1: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

11

AlgorithmsAlgorithmsQueues, Stacks and Records Queues, Stacks and Records

stored in Linked Lists or Arraysstored in Linked Lists or Arrays

Page 2: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

22

Learning ObjectivesLearning Objectives

Describe algorithms for the insertion and reading of items in stacks and queues stored in linked-lists or arrays.

Describe algorithms for the insertion, searching, amendment and deletion of data items stored in a linked list of records.

Page 3: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

33

Queues & StacksQueues & Stacks

Structures used to list data to be used Structures used to list data to be used / jobs to be executed / jobs to be executed (see AS presentation (see AS presentation Stacks and Queues Presentation).).

They can be held in linked lists or They can be held in linked lists or arrays.arrays.

Page 4: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

44

Queues / Stacks in Queues / Stacks in Linked ListsLinked Lists

Page 5: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

55

Stacks & QueuesStacks & QueuesStacksStacks Insert and read at the back end of stack - beginning of Insert and read at the back end of stack - beginning of

the linked list.the linked list.

1010 99 88 77 66 55 44 33 22 11Insert / Read

QueuesQueues Insert at back of queue - beginning of linked list.Insert at back of queue - beginning of linked list.

However, if the list holds a priority queue then higher priority However, if the list holds a priority queue then higher priority items (items (jobsjobs) may enter at other positions.) may enter at other positions.

Read from front queue - end of linked list.Read from front queue - end of linked list.

1010 99 88 77 66 55 44 33 22 11Insert Read

Page 6: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

66

Queue / Stack in a linked List -Queue / Stack in a linked List -

InsertionInsertion

Suppose we wish to insert an element / Suppose we wish to insert an element / cell / item / node into a queue or stack cell / item / node into a queue or stack held in a linked list.held in a linked list.

Page 7: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

77

Free ListFree List

A linked list of unallocated (not used) A linked list of unallocated (not used) regions of memory.regions of memory.

Page 8: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

88

Queue / Stack in a linked List -Queue / Stack in a linked List -

InsertionInsertion1.1. Check that the free list is not empty Check that the free list is not empty (no free (no free

space)space). If so report an error and . If so report an error and stopstop..

Free

Null pointer

element / cell / item / node

Page 9: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

99

Queue / Stack in a linked List -Queue / Stack in a linked List -

InsertionInsertion2.2. Call first free cell NEW.Call first free cell NEW.

FREE Ex DataEx Data Ex Data

NEWNEW

Pointer

Page 10: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

1010

Queue / Stack in a linked List -Queue / Stack in a linked List -

InsertionInsertion

3.3. Insert data into NEW (first free space).Insert data into NEW (first free space).

FREE Ex DataNew data Ex Data

NEWNEW

Page 11: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

1111

Queue / Stack in a linked List -Queue / Stack in a linked List -

InsertionInsertion

4.4. Remove the node from the stack by setting Remove the node from the stack by setting FREE to pointer in NEW.FREE to pointer in NEW.

FREE Ex DataNew data Ex Data

NEWNEW

Page 12: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

1212

Queue / Stack in the linked list Queue / Stack in the linked list is emptyis empty

5.5. Check if the list had previously no items of data Check if the list had previously no items of data by seeing if HEAD by seeing if HEAD (of the list in which the NEW node is to (of the list in which the NEW node is to

be inserted)be inserted) is NULL and if so: is NULL and if so:

Head

of the list to which the new data is being added to.

Page 13: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

1313

Queue / Stack in the linked list Queue / Stack in the linked list is emptyis empty

5.5. Check if the list had previously no items of data Check if the list had previously no items of data by seeing if HEAD by seeing if HEAD (of the list in which the NEW node is to (of the list in which the NEW node is to

be inserted)be inserted) is NULL and if so: is NULL and if so: a.a. Set pointer in NEW to NULLSet pointer in NEW to NULL

b.b. Set HEAD to NEW and Set HEAD to NEW and stopstop..

Head

FREE Ex DataNew data Ex Data

NEWNEW a.

b.

Page 14: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

1414

Not empty linked list holding a Not empty linked list holding a stack / non- priority queue / priority queue where the stack / non- priority queue / priority queue where the

NEW priority is the lowestNEW priority is the lowest

6.6. If the linked list is not empty and either holds a If the linked list is not empty and either holds a stack stack oror a non-priority queue a non-priority queue oror a priority queue a priority queue where NEW’s priority is lowest then NEW is where NEW’s priority is lowest then NEW is placed at the beginning of the list placed at the beginning of the list (back)(back)::

a.a. Set pointer in NEW to Set pointer in NEW to (previous)(previous) HEAD. HEAD.

b.b. Set HEAD to NEW and Set HEAD to NEW and stopstop..

HEAD Data

FREE Ex DataData Ex Data

Data Data Data

a.b.

Page 15: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

1515

Not empty linked list holding a Not empty linked list holding a stack / non- priority queue / priority queue where the stack / non- priority queue / priority queue where the

NEW priority is the lowestNEW priority is the lowest

7.7. If the linked list is not empty and holds a priority queue If the linked list is not empty and holds a priority queue where NEW’s priority is highest then NEW is placed at where NEW’s priority is highest then NEW is placed at the end of the list the end of the list (front)(front)::

a.a. Follow pointers to null pointer.Follow pointers to null pointer.

b.b. Call this cell Previous.Call this cell Previous.

c.c. Set pointer in NEW to NULL.Set pointer in NEW to NULL.

d.d. Make the pointer in PREVIOUS equal to NEW and Make the pointer in PREVIOUS equal to NEW and stopstop..

HEAD DataData Data Data

FREE Ex DataNew Data Ex DataNEWNEW

PREVIOUSPREVIOUS

Page 16: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

1616

Not empty linked list holding a Not empty linked list holding a priority queue where the NEW priority is priority queue where the NEW priority is higher than some but lower than othershigher than some but lower than others

8.8. Search list sequentially until the cell Search list sequentially until the cell immediately before the cell with higher priority immediately before the cell with higher priority than NEW is found. Call this cell PREVIOUS.than NEW is found. Call this cell PREVIOUS.

HEAD DataData Data Data

FREE Ex DataNew Data Ex Data

PREVIOUSPREVIOUS

NEWNEWe.g.

Page 17: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

1717

Not empty linked list holding a Not empty linked list holding a priority queue where the NEW priority is priority queue where the NEW priority is higher than some but lower than othershigher than some but lower than others

9.9. Copy the pointer in PREVIOUS into Copy the pointer in PREVIOUS into TEMP.TEMP.

HEAD DataData Data Data

FREE

PREVIOUSPREVIOUS

= TEMP = TEMP

Ex DataNew Data Ex Data

NEWNEW

Page 18: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

1818

Not empty linked list holding a Not empty linked list holding a priority queue where the NEW priority is priority queue where the NEW priority is higher than some but lower than othershigher than some but lower than others

10.10. Make the pointer in PREVIOUS equal to Make the pointer in PREVIOUS equal to

NEWNEW

HEAD DataData Data Data

FREE Ex Data Ex DataNew Data

NEWNEW

ex PREVIOUSex PREVIOUS

Page 19: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

1919

Not empty linked list holding a Not empty linked list holding a priority queue where the NEW priority is priority queue where the NEW priority is higher than some but lower than othershigher than some but lower than others

10.10. Make the pointer in NEW equal to Make the pointer in NEW equal to

TEMP and TEMP and stopstop..

HEAD DataData Data Data

FREE Ex DataData Ex Data

= TEMP= TEMP ( = ex pointer in ex PREVIOUS) ( = ex pointer in ex PREVIOUS)

ex PREVIOUSex PREVIOUS

Page 20: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

2020

Algorithm for Algorithm for Queue / Stack in a Queue / Stack in a

linked List -linked List - InsertionInsertion

1.1. Check that the free list is not empty. If it Check that the free list is not empty. If it is empty report an error and stop.is empty report an error and stop.

2.2. Call first free cell NEW.Call first free cell NEW.

3.3. Insert data into NEW (first free space).Insert data into NEW (first free space).

4.4. Remove the node from the stack by Remove the node from the stack by setting FREE to pointer in NEW.setting FREE to pointer in NEW.

Page 21: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

2121

Algorithm for Algorithm for Queue / Stack in a Queue / Stack in a

linked List -linked List - InsertionInsertion5.5. Check if the list had previously no items of data by Check if the list had previously no items of data by

seeing if HEAD is NULL and if so:seeing if HEAD is NULL and if so:a.a. Set pointer in NEW to NULLSet pointer in NEW to NULL

b.b. Set HEAD to NEW and Set HEAD to NEW and stopstop..

6.6. If the linked list is not empty and either holds a If the linked list is not empty and either holds a stack stack oror a non-priority queue a non-priority queue oror a priority a priority queue where NEW’s priority is lowest then queue where NEW’s priority is lowest then NEW is placed at the beginning of the list NEW is placed at the beginning of the list (end if (end if queue)queue)::

a.a. Set pointer in NEW to Set pointer in NEW to (previous)(previous) HEAD. HEAD.

b.b. Set HEAD to NEW and Set HEAD to NEW and stopstop..

Page 22: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

2222

Algorithm for Algorithm for Queue / Stack in a Queue / Stack in a

linked List -linked List - InsertionInsertion7.7. If the linked list is not empty and holds a priority queue If the linked list is not empty and holds a priority queue

where NEW’s priority is highest then NEW is placed at where NEW’s priority is highest then NEW is placed at the end of the list the end of the list (front)(front)::

a.a. Follow pointers to null pointer.Follow pointers to null pointer.b.b. Call this cell Previous.Call this cell Previous.c.c. Set pointer in NEW to NULL.Set pointer in NEW to NULL.

d.d. Make the pointer in PREVIOUS equal to NEW and Make the pointer in PREVIOUS equal to NEW and stopstop. .

8.8. Search list sequentially until the cell immediately Search list sequentially until the cell immediately before the cell with higher priority than NEW is found. before the cell with higher priority than NEW is found. Call this cell PREVIOUS.Call this cell PREVIOUS.

9.9. Copy the pointer in PREVIOUS into TEMP.Copy the pointer in PREVIOUS into TEMP.10.10. Make the pointer in PREVIOUS equal to NEWMake the pointer in PREVIOUS equal to NEW

11.11. Make the pointer in NEW equal to TEMP and Make the pointer in NEW equal to TEMP and stopstop..

Records in a Linked List

Page 23: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

2323

Queue / Stack in a linked List -Queue / Stack in a linked List -

ReadReadSuppose we wish to read an item from a stack / Suppose we wish to read an item from a stack / queue held in a linked list.queue held in a linked list.

Note when an item is read from a queue it is Note when an item is read from a queue it is copied to another location.copied to another location.

So it can be used / executed next.So it can be used / executed next.

and then deleted from the stack/queue.and then deleted from the stack/queue. Once an item is read it is no longer in the queue / stack as the Once an item is read it is no longer in the queue / stack as the

next item is the one to be read after this one.next item is the one to be read after this one.We humans think of “deleting” once an item is used but computers We humans think of “deleting” once an item is used but computers do so before using.do so before using.

Page 24: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

2424

Stack in a linked List -Stack in a linked List - ReadRead

StackStack LLast - ast - IIn – n – FFirst - irst - OOut (LIFO) data structure so ut (LIFO) data structure so

only one end is used.only one end is used.Read and insert from the beginning of the list Read and insert from the beginning of the list (back).(back).

Page 25: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

2525

Stack in a linked List -Stack in a linked List - ReadRead1.1. Check that the list is not empty Check that the list is not empty (no items to (no items to

read)read)..

2.2. If the list is empty report an error and If the list is empty report an error and

stopstop..

HEAD

Page 26: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

2626

Stack in a linked List -Stack in a linked List - ReadRead3.3. Copy data in first cell.Copy data in first cell.4.4. Set TEMP to HEAD.Set TEMP to HEAD.

Head DataData Data Data

Free Ex DataEx Data Ex Data

= TEMP= TEMP Copy dataCopy data

Page 27: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

2727

Stack in a linked List -Stack in a linked List - ReadRead5.5. Set HEAD equal to pointer in first cell.Set HEAD equal to pointer in first cell.

Head DataEx Data Data Data

Free Ex DataEx Data Ex Data

Page 28: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

2828

Stack in a linked List -Stack in a linked List - ReadRead6.6. Set pointer in first cell equal to Set pointer in first cell equal to (previous)(previous)

FREE.FREE.

Head DataEx Data Data Data

Free Ex DataEx Data Ex Data

Page 29: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

2929

Stack in a linked List -Stack in a linked List - ReadRead7.7. Set FREE equal to TEMP and Set FREE equal to TEMP and stopstop..

Head DataEx Data Data Data

Free Ex DataEx Data Ex Data

= TEMP = TEMP (= ex HEAD)(= ex HEAD)

Page 30: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

3030

Stack in a linked List -Stack in a linked List - ReadRead

1.1. Check that the list is not empty.Check that the list is not empty.

2.2. If the list is empty report an error and stop.If the list is empty report an error and stop.

3.3. Copy data in first cell.Copy data in first cell.

4.4. Set TEMP to HEAD.Set TEMP to HEAD.

5.5. Set HEAD equal to pointer in first cell.Set HEAD equal to pointer in first cell.

6.6. Set pointer in first cell equal to (previous) Set pointer in first cell equal to (previous) FREE.FREE.

7.7. Set FREE equal to TEMP and stop.Set FREE equal to TEMP and stop.

Page 31: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

3131

Queue in a linked List -Queue in a linked List - ReadRead

QueueQueue FFirst-irst-IIn-n-FFirst-irst-OOut (ut (FIFOFIFO) data structure so both ) data structure so both

ends are used:ends are used:Read from the end of the list (Read from the end of the list (frontfront).).

Insert at the beginning of the list (Insert at the beginning of the list (backback). ).

Page 32: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

3232

Queue in a linked List -Queue in a linked List - ReadRead1.1. Check that the list is not empty Check that the list is not empty (no items to (no items to

read)read)..

2.2. If the list is empty report an error and If the list is empty report an error and

stopstop..

HEAD

Page 33: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

3333

Queue in a linked List -Queue in a linked List - ReadRead

3.3. Follow pointers to null pointer.Follow pointers to null pointer.

4.4. Copy data in this cell.Copy data in this cell.

Head DataData Data Data

Copy dataCopy data

Free Ex DataEx Data Ex Data

Page 34: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

3434

Queue in a linked List -Queue in a linked List - ReadRead

5.5. Call this cell READ.Call this cell READ.

Head DataData Data DataREADREAD

Free Ex DataEx Data Ex Data

Page 35: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

3535

Queue in a linked List -Queue in a linked List - ReadRead6.6. Move null pointer to previous node.Move null pointer to previous node.

Head DataData Data Ex DataREADREAD

Free Ex DataEx Data Ex Data

Page 36: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

3636

Queue in a linked List -Queue in a linked List - ReadRead7.7. Set pointer in READ equal to (previous) Set pointer in READ equal to (previous)

FREE.FREE.

Head DataData Data

Free DataEx Data Data

READREAD

Ex Data

Page 37: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

3737

Queue in a linked List -Queue in a linked List - ReadRead8.8. Set FREE to READ and Set FREE to READ and stopstop..

Head DataData Data

Free

READREAD

Ex Data

Ex DataEx Data Ex Data

Page 38: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

3838

Queue in a linked List -Queue in a linked List - ReadRead

1.1. Check that the list is not empty.Check that the list is not empty.2.2. If the list is empty report an error and stop.If the list is empty report an error and stop.3.3. Follow pointers to null pointer.Follow pointers to null pointer.4.4. Copy data in this cell.Copy data in this cell.5.5. Call this cell READ.Call this cell READ.6.6. Move null pointer to previous node.Move null pointer to previous node.7.7. Set pointer in READ equal to (previous) Set pointer in READ equal to (previous)

FREE.FREE.8.8. Set FREE to READ and stop.Set FREE to READ and stop.

Page 39: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

3939

Stacks in Stacks in arraysarrays

Page 40: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

4040

Stacks Stacks in arraysin arrays

Insert and read at the back end Insert and read at the back end of stack - end of the array.of stack - end of the array.

1010

99

88

77

66

55

44

33

22

11

Insert / Read

Page 41: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

4141

To insert an item into a stack To insert an item into a stack in in an an arrayarray

1.1. Check to see if stack is full.Check to see if stack is full.

2.2. If the stack is full report an If the stack is full report an error and stop.error and stop.

3.3. Increment the stack pointer.Increment the stack pointer.

4.4. Insert new data item into cell Insert new data item into cell pointed to by the stack pointed to by the stack pointer and stop.pointer and stop.

Assume that cells are numbered Assume that cells are numbered from 1 upwards and that, when from 1 upwards and that, when the stack is empty, the pointer is the stack is empty, the pointer is zero.zero.

Pointer to Top of stackData

Data

Data

Data

Page 42: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

4242

Stacks Stacks in in an an arrayarray – Read – Read

1.1. Check to see if the stack is empty.Check to see if the stack is empty.2.2. If the stack is empty report an error and stop.If the stack is empty report an error and stop.3.3. Copy data item in cell pointed to by the stack Copy data item in cell pointed to by the stack

pointer pointer (so it can be used or executed next)(so it can be used or executed next)..4.4. Decrement the stack pointer and stop.Decrement the stack pointer and stop.

When an item is deleted from a stack, the When an item is deleted from a stack, the item's value is copied and the stack pointer is item's value is copied and the stack pointer is moved down one cell. moved down one cell.

The data itself is not deleted. The data itself is not deleted.

Page 43: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

4343

Queues in Queues in arraysarrays

Page 44: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

4444

Queues Queues in in an an arrayarray

Insert at back of queue - end of array.Insert at back of queue - end of array. However, if the list holds a priority queue then However, if the list holds a priority queue then

higher priority items (higher priority items (jobsjobs) may enter at other ) may enter at other positions.positions.

Read from front queue - beginning of Read from front queue - beginning of array.array.

Insert

Read

1010

99

88

77

66

55

44

33

22

11

Page 45: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

4545

Queues Queues in in an an arrayarray - Insertion - Insertion1.1. Check to see if queue is full.Check to see if queue is full.2.2. If the queue is full report an If the queue is full report an

error and stop.error and stop.3.3. Insert new data item into cell Insert new data item into cell

pointed to by the head pointed to by the head pointer.pointer.

4.4. Increment the head pointer Increment the head pointer and stop.and stop.

Assume that the cells are Assume that the cells are numbered from 1 upwards numbered from 1 upwards and that, when the queue is and that, when the queue is empty, the two pointers empty, the two pointers point to the same cell.point to the same cell.

Data

Data

Data

Data

Head Pointer

Tail Pointer

Page 46: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

4646

Queues Queues in in an an arrayarray – Read & Delete– Read & Delete

1.1. Check to see if the queue is empty Check to see if the queue is empty (when (when the head and tail pointers point to the same the head and tail pointers point to the same cell)cell)..

2.2. If the queue is empty report error and If the queue is empty report error and stop.stop.

3.3. Copy data item in cell pointed to by the Copy data item in cell pointed to by the tail pointer tail pointer (so it can be used or executed next)(so it can be used or executed next)..

4.4. Increment tail pointer and stop.Increment tail pointer and stop.

Page 47: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

4747

Records in a Linked ListRecords in a Linked ListInsertion:Insertion:

Exactly the same as demonstrated earlier:Exactly the same as demonstrated earlier:Slides Slides 8 – 22. – 22.

Deletion:Deletion: Almost the same as reading from a linked list Almost the same as reading from a linked list (demonstrated (demonstrated

earlier)earlier) but without the copying part but without the copying part (just the deletion part)(just the deletion part)..The deletion part is demonstrated again on the next 8 slides.The deletion part is demonstrated again on the next 8 slides.

Search:Search: Almost the same as priority based insertion into a linked list Almost the same as priority based insertion into a linked list

(where the new priority is higher than some but lower than others -(where the new priority is higher than some but lower than others - demonstrated earlier)demonstrated earlier) but instead of searching for the cell but instead of searching for the cell immediately before the cell with higher priority, the search would immediately before the cell with higher priority, the search would be for a cell with data that matches what is being searched for; be for a cell with data that matches what is being searched for; and also without the copying and deletion part.and also without the copying and deletion part.

Discussed in more detail on slide Discussed in more detail on slide 56..

Amend:Amend: The same as searching above but with the extra action of The same as searching above but with the extra action of

amendment.amendment.Discussed in more detail on slide Discussed in more detail on slide 57..

Page 48: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

4848

Linked List –Linked List – Deletion of a Record Deletion of a Record

1.1. Check that the list is not empty Check that the list is not empty (no items to (no items to

read)read)..

2.2. If the list is empty report an error and If the list is empty report an error and

stopstop..

HEAD

Page 49: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

4949

Linked List –Linked List – Deletion of a Record Deletion of a Record

3.3. Search list to find the data to be deleted.Search list to find the data to be deleted.4.4. If the data is not in the list, report an error If the data is not in the list, report an error

and and stopstop..

Head DataData Data Data

Page 50: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

5050

Linked List –Linked List – Deletion of a Record Deletion of a Record

5.5. Call the cell immediately before the cell to Call the cell immediately before the cell to be deleted PREVIOUS.be deleted PREVIOUS.

Head DataData Data Data

Free DataData Data

PREVIOUS

e.g. Cell to be deleted

Page 51: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

5151

Linked List –Linked List – Deletion of a Record Deletion of a Record

6.6. Set TEMP to pointer in PREVIOUS.Set TEMP to pointer in PREVIOUS.

Head DataData Data Data

Free DataData Data

PREVIOUS

Cell to be deleted

= TEMP= TEMP

Page 52: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

5252

Linked List –Linked List – Deletion of a Record Deletion of a Record

7.7. Set pointer in PREVIOUS equal to Set pointer in PREVIOUS equal to pointer in cell to be deleted.pointer in cell to be deleted.

Head DataData Data Data

Free DataData Data

ex PREVIOUS

Cell to be deleted

Page 53: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

5353

Linked List –Linked List – Deletion of a Record Deletion of a Record

8.8. Set pointer in cell to be deleted equal to Set pointer in cell to be deleted equal to (previous)(previous) FREE. FREE.

Head DataData Data Data

Free DataData Data

Cell to be deleted

ex PREVIOUS

Page 54: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

5454

ex PREVIOUS

Linked List –Linked List – Deletion of a Record Deletion of a Record

9.9. Set FREE equal to TEMP and Set FREE equal to TEMP and stopstop..

Head DataData Data Data

Free DataData Data

Deleted Cell= TEMP = TEMP (= ex pointer in ex Previous)(= ex pointer in ex Previous)

Page 55: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

5555

Algorithm for Linked List –Algorithm for Linked List – Deleting Records Deleting Records

1.1. Check that the list is not empty.Check that the list is not empty.2.2. If the list is empty report an error and stop.If the list is empty report an error and stop.3.3. Search list to find the cell to be deleted.Search list to find the cell to be deleted.4.4. If the cell is not in the list, report an error and If the cell is not in the list, report an error and

stop.stop.5.5. Call the cell immediately before the cell to be Call the cell immediately before the cell to be

deleted PREVIOUS.deleted PREVIOUS.6.6. Set TEMP to pointer in PREVIOUS.Set TEMP to pointer in PREVIOUS.7.7. Set pointer in PREVIOUS equal to pointer in cell Set pointer in PREVIOUS equal to pointer in cell

to be deleted.to be deleted.8.8. Set pointer in cell to be deleted equal to FREE.Set pointer in cell to be deleted equal to FREE.9.9. Set FREE equal to TEMP and stop.Set FREE equal to TEMP and stop.

Page 56: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

5656

Algorithm for linked list – Algorithm for linked list – Search for a recordSearch for a record

Assume:Assume: Ascending order of some key value and order is Ascending order of some key value and order is

required to be maintained.required to be maintained. List is not emptyList is not empty Pointer[Current] references the pointer value for any Pointer[Current] references the pointer value for any

node.node.

1.1. Set CURRENT equal to HEADSet CURRENT equal to HEAD2.2. REPEATREPEAT

Set Set CURRENT to POINTER in CURRENT cell by CURRENT to POINTER in CURRENT cell by Current = Pointer[Current]Current = Pointer[Current]

3.3. UNTIL SearchData = Data in CURRENT cell UNTIL SearchData = Data in CURRENT cell OR OR Pointer[Current]Pointer[Current] = Null = Null

Page 57: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

5757

Algorithm for a linked list – Algorithm for a linked list – Amend a RecordAmend a Record

1.1. Check that the list is not empty.Check that the list is not empty.

2.2. If the list is empty report an error and stop.If the list is empty report an error and stop.

3.3. Search the list to find the cell to be Search the list to find the cell to be amended.amended.

4.4. Amend the data but do not change the Amend the data but do not change the key key (the one field that is unique and would be used to (the one field that is unique and would be used to

order the list)order the list)..

5.5. Stop.Stop.

Page 58: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

5858

PlenaryPlenary

Jobs that require printing, by a network printer, Jobs that require printing, by a network printer, are stored until the printer is ready. Their are stored until the printer is ready. Their addresses are placed in a queue to await their addresses are placed in a queue to await their turn for printing. Addresses of new jobs are turn for printing. Addresses of new jobs are placed at one end of the queue. These job placed at one end of the queue. These job addresses are taken from the other end when addresses are taken from the other end when the printer is ready. the printer is ready. If the queue is held in a linked list, describe an If the queue is held in a linked list, describe an algorithm for:algorithm for:

a)a) inserting an address into the queue,    inserting an address into the queue,    b)b) reading an address from the queue.reading an address from the queue.           

Page 59: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

5959

PlenaryPlenary

Find print Q in head of list tableFind print Q in head of list table

a)a) Insert data into free spaceInsert data into free spaceH of L points to new nodeH of L points to new nodenew node points to old first valuenew node points to old first valuemention of insertion of high priority jobs into queuemention of insertion of high priority jobs into queue

b)b) Check to ensure list not emptyCheck to ensure list not empty- follow pointers to null pointer- follow pointers to null pointer- read address of print job- read address of print job- move null pointer to previous node- move null pointer to previous node- return node to free space- return node to free space

Page 60: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

6060

PlenaryPlenary

How can a data item beHow can a data item be inserted into and inserted into and read / deleted from a queue held in an read / deleted from a queue held in an array?array?

Page 61: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

6161

PlenaryPlenary

Inserted into:Inserted into: Error check, Q fullError check, Q full Insert data at ARRAY (Head pointer)Insert data at ARRAY (Head pointer) Increment Head pointerIncrement Head pointer

Page 62: 1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

6262

PlenaryPlenary

Read / Deleted from:Read / Deleted from: Error check, Q emptyError check, Q empty Read data at ARRAY (Tail pointer)Read data at ARRAY (Tail pointer) Increment Tail pointerIncrement Tail pointer