Download presentation
Presentation is loading. Please wait.
Published byLogan Merritt Modified over 8 years ago
1
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays
2
2 Learning 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.
3
3 Queues & Stacks Structures used to list data to be used / jobs to be executed (see AS presentation Stacks and Queues Presentation). Stacks and Queues Presentation Stacks and Queues Presentation They can be held in linked lists or arrays.
4
4 Queues / Stacks in Linked Lists
5
5 Stacks & Queues Stacks Insert and read at the back end of stack - beginning of the linked list. Insert and read at the back end of stack - beginning of the linked list. 10987654321 Insert / Read Queues 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 items (jobs) may enter at other positions. Read from front queue - end of linked list. Read from front queue - end of linked list.10987654321 Insert Read
6
6 Queue / Stack in a linked List - Insertion Suppose we wish to insert an element / cell / item / node into a queue or stack held in a linked list.
7
7 Free List A linked list of unallocated (not used) regions of memory.
8
8 Queue / Stack in a linked List - Insertion 1.Check that the free list is not empty (no free space). If so report an error and stop. Free Null pointer element / cell / item / node
9
9 Queue / Stack in a linked List - Insertion 2. Call first free cell NEW. FREE Ex Data NEW Pointer
10
10 Queue / Stack in a linked List - Insertion 3. Insert data into NEW (first free space). FREE Ex Data New data Ex Data NEW
11
11 Queue / Stack in a linked List - Insertion 4. Remove the node from the stack by setting FREE to pointer in NEW. FREE Ex Data New data Ex Data NEW
12
12 Queue / Stack in the linked list is empty 5.Check if the list had previously no items of data by seeing if HEAD (of the list in which the NEW node is to be inserted) is NULL and if so: Head of the list to which the new data is being added to.
13
13 Queue / Stack in the linked list is empty 5.Check if the list had previously no items of data by seeing if HEAD (of the list in which the NEW node is to be inserted) is NULL and if so: a.Set pointer in NEW to NULL b.Set HEAD to NEW and stop. Head FREE Ex Data New data Ex Data NEW a. b.
14
14 Not empty linked list holding a stack / non- priority queue / priority queue where the NEW priority is the lowest 6.If the linked list is not empty and either holds a stack or a non-priority queue or a priority queue where NEW’s priority is lowest then NEW is placed at the beginning of the list (back) : a.Set pointer in NEW to (previous) HEAD. b.Set HEAD to NEW and stop. HEAD Data FREE Ex Data Data Ex DataData a. b.
15
15 Not empty linked list holding a stack / non- priority queue / priority queue where the NEW priority is the lowest 7.If the linked list is not empty and holds a priority queue where NEW’s priority is highest then NEW is placed at the end of the list (front) : a.Follow pointers to null pointer. b.Call this cell Previous. c.Set pointer in NEW to NULL. d.Make the pointer in PREVIOUS equal to NEW and stop. HEAD Data FREE Ex Data New Data Ex Data NEW PREVIOUS
16
16 Not empty linked list holding a priority queue where the NEW priority is higher than some but lower than others 8. Search list sequentially until the cell immediately before the cell with higher priority than NEW is found. Call this cell PREVIOUS. HEAD Data FREE Ex Data New Data Ex Data PREVIOUS NEW e.g.
17
17 Not empty linked list holding a priority queue where the NEW priority is higher than some but lower than others 9. Copy the pointer in PREVIOUS into TEMP. HEAD Data FREE PREVIOUS = TEMP Ex Data New Data Ex Data NEW
18
18 Not empty linked list holding a priority queue where the NEW priority is higher than some but lower than others 10. Make the pointer in PREVIOUS equal to NEW HEAD Data FREE Ex Data New Data NEW ex PREVIOUS
19
19 Not empty linked list holding a priority queue where the NEW priority is higher than some but lower than others 10.Make the pointer in NEW equal to TEMP and stop. HEAD Data FREE Ex DataData Ex Data = TEMP ( = ex pointer in ex PREVIOUS) ex PREVIOUS
20
20 Algorithm for Queue / Stack in a linked List - Insertion 1.Check that the free list is not empty. If it is empty report an error and stop. 2.Call first free cell NEW. 3.Insert data into NEW (first free space). 4.Remove the node from the stack by setting FREE to pointer in NEW.
21
21 Algorithm for Queue / Stack in a linked List - Insertion 5.Check if the list had previously no items of data by seeing if HEAD is NULL and if so: a.Set pointer in NEW to NULL b.Set HEAD to NEW and stop. 6.If the linked list is not empty and either holds a stack or a non-priority queue or a priority queue where NEW’s priority is lowest then NEW is placed at the beginning of the list (end if queue) : a.Set pointer in NEW to (previous) HEAD. b.Set HEAD to NEW and stop.
22
22 Algorithm for Queue / Stack in a linked List - Insertion 7.If the linked list is not empty and holds a priority queue where NEW’s priority is highest then NEW is placed at the end of the list (front) : a.Follow pointers to null pointer. b.Call this cell Previous. c.Set pointer in NEW to NULL. d.Make the pointer in PREVIOUS equal to NEW and stop. 8.Search list sequentially until the cell immediately before the cell with higher priority than NEW is found. Call this cell PREVIOUS. 9.Copy the pointer in PREVIOUS into TEMP. 10.Make the pointer in PREVIOUS equal to NEW 11.Make the pointer in NEW equal to TEMP and stop. Records in a Linked List
23
23 Queue / Stack in a linked List - Read Suppose we wish to read an item from a stack / queue held in a linked list. Note when an item is read from a queue it is 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. 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. 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. We humans think of “deleting” once an item is used but computers do so before using.
24
24 Stack in a linked List - Read Stack Last - In – First - Out (LIFO) data structure so only one end is used. Last - In – First - Out (LIFO) data structure so only one end is used. Read and insert from the beginning of the list (back).
25
25 Stack in a linked List - Read 1.Check that the list is not empty (no items to read). 2.If the list is empty report an error and stop. HEAD
26
26 Stack in a linked List - Read 3.Copy data in first cell. 4.Set TEMP to HEAD. Head Data Free Ex Data = TEMP Copy data
27
27 Stack in a linked List - Read 5.Set HEAD equal to pointer in first cell. Head Data Ex DataData Free Ex Data
28
28 Stack in a linked List - Read 6.Set pointer in first cell equal to (previous) FREE. Head Data Ex DataData Free Ex Data
29
29 Stack in a linked List - Read 7.Set FREE equal to TEMP and stop. Head Data Ex DataData Free Ex Data = TEMP (= ex HEAD)
30
30 Stack in a linked List - Read 1.Check that the list is not empty. 2.If the list is empty report an error and stop. 3.Copy data in first cell. 4.Set TEMP to HEAD. 5.Set HEAD equal to pointer in first cell. 6.Set pointer in first cell equal to (previous) FREE. 7.Set FREE equal to TEMP and stop.
31
31 Queue in a linked List - Read Queue First-In-First-Out (FIFO) data structure so both ends are used: First-In-First-Out (FIFO) data structure so both ends are used: Read from the end of the list (front). Insert at the beginning of the list (back).
32
32 Queue in a linked List - Read 1.Check that the list is not empty (no items to read). 2.If the list is empty report an error and stop. HEAD
33
33 Queue in a linked List - Read 3.Follow pointers to null pointer. 4.Copy data in this cell. Head Data Copy data Free Ex Data
34
34 Queue in a linked List - Read 5.Call this cell READ. Head Data READ Free Ex Data
35
35 Queue in a linked List - Read 6.Move null pointer to previous node. Head Data Ex Data READ Free Ex Data
36
36 Queue in a linked List - Read 7.Set pointer in READ equal to (previous) FREE. Head Data Free Data Ex Data Data READ Ex Data
37
37 Queue in a linked List - Read 8.Set FREE to READ and stop. Head Data Free READ Ex Data
38
38 Queue in a linked List - Read 1.Check that the list is not empty. 2.If the list is empty report an error and stop. 3.Follow pointers to null pointer. 4.Copy data in this cell. 5.Call this cell READ. 6.Move null pointer to previous node. 7.Set pointer in READ equal to (previous) FREE. 8.Set FREE to READ and stop.
39
39 Stacks in arrays
40
40 Stacks in arrays Insert and read at the back end of stack - end of the array. 10 9 8 7 6 5 4 3 2 1 Insert / Read
41
41 To insert an item into a stack in an array 1.Check to see if stack is full. 2.If the stack is full report an error and stop. 3.Increment the stack pointer. 4.Insert new data item into cell pointed to by the stack pointer and stop. Assume that cells are numbered from 1 upwards and that, when the stack is empty, the pointer is zero. Pointer to Top of stack Data
42
42 Stacks in an array – Read 1.Check to see if the stack is empty. 2.If the stack is empty report an error and stop. 3.Copy data item in cell pointed to by the stack pointer (so it can be used or executed next). 4.Decrement the stack pointer and stop. When an item is deleted from a stack, the item's value is copied and the stack pointer is moved down one cell. The data itself is not deleted. The data itself is not deleted.
43
43 Queues in arrays
44
44 Queues in an array Insert at back of queue - end of array. However, if the list holds a priority queue then higher priority items (jobs) may enter at other positions. However, if the list holds a priority queue then higher priority items (jobs) may enter at other positions. Read from front queue - beginning of array. Insert Read 10 9 8 7 6 5 4 3 2 1
45
45 Queues in an array - Insertion 1.Check to see if queue is full. 2.If the queue is full report an error and stop. 3.Insert new data item into cell pointed to by the head pointer. 4.Increment the head pointer and stop. Assume that the cells are numbered from 1 upwards and that, when the queue is empty, the two pointers point to the same cell. Data Head Pointer Tail Pointer
46
46 Queues in an array – Read & Delete 1.Check to see if the queue is empty (when the head and tail pointers point to the same cell). 2.If the queue is empty report error and stop. 3.Copy data item in cell pointed to by the tail pointer (so it can be used or executed next). 4.Increment tail pointer and stop.
47
47 Records in a Linked List Insertion: Exactly the same as demonstrated earlier: Exactly the same as demonstrated earlier: Slides 8 – 22. 8 Deletion: Almost the same as reading from a linked list (demonstrated earlier) but without the copying part (just the deletion part). Almost the same as reading from a linked list (demonstrated earlier) but without the copying part (just the deletion part). The deletion part is demonstrated again on the next 8 slides. Search: Almost the same as priority based insertion into a linked list (where the new priority is higher than some but lower than others - demonstrated earlier) but instead of searching for the cell immediately before the cell with higher priority, the search would be for a cell with data that matches what is being searched for; and also without the copying and deletion part. Almost the same as priority based insertion into a linked list (where the new priority is higher than some but lower than others - demonstrated earlier) but instead of searching for the cell immediately before the cell with higher priority, the search would be for a cell with data that matches what is being searched for; and also without the copying and deletion part. Discussed in more detail on slide 56. 56 Amend: The same as searching above but with the extra action of amendment. The same as searching above but with the extra action of amendment. Discussed in more detail on slide 57. 57
48
48 Linked List – Deletion of a Record 1.Check that the list is not empty (no items to read). 2.If the list is empty report an error and stop. HEAD
49
49 Linked List – Deletion of a Record 3.Search list to find the data to be deleted. 4.If the data is not in the list, report an error and stop. Head Data
50
50 Linked List – Deletion of a Record 5.Call the cell immediately before the cell to be deleted PREVIOUS. Head Data Free Data PREVIOUS e.g. Cell to be deleted
51
51 Linked List – Deletion of a Record 6.Set TEMP to pointer in PREVIOUS. Head Data Free Data PREVIOUS Cell to be deleted = TEMP
52
52 Linked List – Deletion of a Record 7.Set pointer in PREVIOUS equal to pointer in cell to be deleted. Head Data Free Data ex PREVIOUS Cell to be deleted
53
53 Linked List – Deletion of a Record 8.Set pointer in cell to be deleted equal to (previous) FREE. Head Data Free Data Cell to be deleted ex PREVIOUS
54
54 ex PREVIOUS Linked List – Deletion of a Record 9.Set FREE equal to TEMP and stop. Head Data Free Data Deleted Cell = TEMP (= ex pointer in ex Previous)
55
55 Algorithm for Linked List – Deleting Records 1.Check that the list is not empty. 2.If the list is empty report an error and stop. 3.Search list to find the cell to be deleted. 4.If the cell is not in the list, report an error and stop. 5.Call the cell immediately before the cell to be deleted PREVIOUS. 6.Set TEMP to pointer in PREVIOUS. 7.Set pointer in PREVIOUS equal to pointer in cell to be deleted. 8.Set pointer in cell to be deleted equal to FREE. 9.Set FREE equal to TEMP and stop.
56
56 Algorithm for linked list – Search for a record Assume: Ascending order of some key value and order is required to be maintained. Ascending order of some key value and order is required to be maintained. List is not empty List is not empty Pointer[Current] references the pointer value for any node. Pointer[Current] references the pointer value for any node. 1.Set CURRENT equal to HEAD 2.REPEAT Set CURRENT to POINTER in CURRENT cell by Current = Pointer[Current] Set CURRENT to POINTER in CURRENT cell by Current = Pointer[Current] 3.UNTIL SearchData = Data in CURRENT cell OR Pointer[Current] = Null
57
57 Algorithm for a linked list – Amend a Record 1.Check that the list is not empty. 2.If the list is empty report an error and stop. 3.Search the list to find the cell to be amended. 4.Amend the data but do not change the key (the one field that is unique and would be used to order the list). 5.Stop.
58
58 Plenary Jobs that require printing, by a network printer, are stored until the printer is ready. Their addresses are placed in a queue to await their turn for printing. Addresses of new jobs are placed at one end of the queue. These job addresses are taken from the other end when the printer is ready. If the queue is held in a linked list, describe an algorithm for: a)inserting an address into the queue, a)inserting an address into the queue, b)reading an address from the queue. b)reading an address from the queue.
59
59 Plenary Find print Q in head of list table a)Insert data into free space H of L points to new node new node points to old first value mention of insertion of high priority jobs into queue b)Check to ensure list not empty - follow pointers to null pointer - read address of print job - move null pointer to previous node - return node to free space
60
60 Plenary How can a data item be inserted into and read / deleted from a queue held in an array?
61
61 Plenary Inserted into: Error check, Q full Error check, Q full Insert data at ARRAY (Head pointer) Insert data at ARRAY (Head pointer) Increment Head pointer Increment Head pointer
62
62 Plenary Read / Deleted from: Error check, Q empty Error check, Q empty Read data at ARRAY (Tail pointer) Read data at ARRAY (Tail pointer) Increment Tail pointer Increment Tail pointer
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.