Back to 课程

Computer-Science-A-level-Ocr

0% Complete
0/0 Steps
  1. 3-3-networks
    8 主题
  2. 3-2-databases
    7 主题
  3. 3-1-compression-encryption-and-hashing
    4 主题
  4. 2-5-object-oriented-languages
    7 主题
  5. 2-4-types-of-programming-language
    4 主题
  6. 2-3-software-development
    5 主题
  7. 2-2-applications-generation
    6 主题
  8. 2-1-systems-software
    8 主题
  9. 1-3-input-output-and-storage
    2 主题
  10. 1-2-types-of-processor
    3 主题
  11. 1-1-structure-and-function-of-the-processor
    1 主题
  12. structuring-your-responses
    3 主题
  13. the-exam-papers
    2 主题
  14. 8-2-algorithms-for-the-main-data-structures
    4 主题
  15. 8-1-algorithms
    10 主题
  16. 7-2-computational-methods
    11 主题
  17. 7-1-programming-techniques
    14 主题
  18. 6-5-thinking-concurrently
    2 主题
  19. 6-4-thinking-logically
    2 主题
  20. 6-3-thinking-procedurally
    3 主题
  21. 6-2-thinking-ahead
    1 主题
  22. 6-1-thinking-abstractly
    3 主题
  23. 5-2-moral-and-ethical-issues
    9 主题
  24. 5-1-computing-related-legislation
    4 主题
  25. 4-3-boolean-algebra
    5 主题
  26. 4-2-data-structures
    10 主题
  27. 4-1-data-types
    9 主题
  28. 3-4-web-technologies
    16 主题
课 Progress
0% Complete

Linked Lists

What is a linked list?

  • In A Level Computer Science, a linked list is a dynamic data structure that is used to hold an ordered sequence

  • The items which form the sequence do not have to be in contiguous data locations

  • Each item is called a node and contains a data field alongside another address which is called a pointer

  • For example:

Index

Data

Pointer

0

‘Apple’

2

1

‘Pineapple’

0

2

‘Melon’

3

 

 

Start = 1 NextFree = 3

  • The data field contains the value of the actual data which is part of the list

  • The pointer field contains the address of the next item in the list

  • Linked lists also store the index of the first item as a pointer

  • They also store a pointer identifying the index of the next available space

Examiner Tips and Tricks

  • Linked lists can only be traversed by following each item’s next pointer until the end of the list is located

Linked Lists: Traversing, Adding & Removing Data

Traverse a linked list

  • Check if the linked list is empty

  • Start at the node the pointer is pointing to (Start = 1)

  • Output the item at the current node (‘Pineapple’)

  • Follow the pointer to the next node repeating through each node until the end of the linked list.

  • When the pointer field is empty/null it signals that the end of the linked list has been reached

  • Traversing the example above would produce: ‘Pineapple’, ‘Apple’, ‘Melon’

Adding a node

  • An advantage of using linked lists is that values can easily be added or removed by editing the points

  • The following example will add the word ‘Orange’ after the word ‘Pineapple’

1. Check there is free memory to insert data into the node

2. Add the new value to the end of the linked list and update the ‘NextFree’ pointer

3

‘Orange’

 

Start = 1 NextFree = 4

3. The pointer field of the word ‘Pineapple’ is updated to point to ‘Orange’, at position 3

1

‘Pineapple’

3

4. The pointer field of the word ‘Orange’ is updated to point to ‘Apple’ at position 0

3

‘Orange’

0

4. When traversed this linked list will now output: ‘Pineapple’, ‘Orange’, ‘Apple’, ‘Melon’

Removing a node

  • Removing a node also involves updating nodes, this time to bypass the deleted node

  • The following example will remove the word ‘Apple’ from the original linked list

1. Update the pointer field of ‘Pineapple’ to point to ‘Melon’ at index 2

Index

Data

Pointer

0

‘Apple’

2

1

‘Pineapple’

2

2

‘Melon’

2. When traversed this linked list will now output: ‘Pineapple’, ‘Melon’

  • The node is not truly removed from the list; it is only ignored

  • Although this is easier it does waste memory

  • Storing pointers themselves also means that more memory is required compared to an array

  • Items in a linked list are also stored in a sequence, they can only be traversed within that order so an item cannot be directly accessed as is possible in an array

Worked Example

A coach company offers tours of the UK

A linked list stores the names of cities on a coach tour in the order they are visited. 

linked-lists-question-ocr-a-level-computer-science

linked-lists-question

The tour is amended. The new itinerary is London, Oxford, Manchester then York. Explain how Birmingham is removed from the linked list and how York is added. You may use the diagram to illustrate your answer. 

4 marks

Answer:

Example answer that gets full marks:

The first thing you need to do is change the Oxford pointer to go around Birmingham to point to Manchester. [1]

linked-lists-we-1-ocr-a-level-computer-science

linked lists WE 1

Then you create a node called York and this is placed at the next free space in the list. [1]

linked-lists-we-2-ocr-a-level-computer-science

linked lists WE 2

Manchester is kept in the same position and it’s pointer changed to point to York. [1]

linked-lists-we-3-ocr-a-level-computer-science

linked lists WE 3

The York node then points to Null. [1]

<img alt=”linked-lists-we-4-ocr-a-level-computer-science” class=”ContentBlock_figure__vJw2q” data-nimg=”1″ decoding=”async” height=”216″ loading=”lazy” sizes=”(max-width: 320px) 320w, (max-width: 640px) 640w, (max-width: 960px) 960w, (max-width: 1280px) 1280w, 1920w” src=”https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=3840/https://cdn.savemyexams.com/uploads/2024/01/linked-lists-we-4-ocr-a-level-computer-science.webp” srcset=”https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=16/https://cdn.savemyexams.com/uploads/2024/01/linked-lists-we-4-ocr-a-level-computer-science.webp 16w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=32/https://cdn.savemyexams.com/uploads/2024/01/linked-lists-we-4-ocr-a-level-computer-science.webp 32w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=48/https://cdn.savemyexams.com/uploads/2024/01/linked-lists-we-4-ocr-a-level-computer-science.webp 48w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=64/https://cdn.savemyexams.com/uploads/2024/01/linked-lists-we-4-ocr-a-level-computer-science.webp 64w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=96/https://cdn.savemyexams.com/uploads/2024/01/linked-lists-we-4-ocr-a-level-computer-science.webp 96w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=128/https://cdn.savemyexams.com/uploads/2024/01/linked-lists-we-4-ocr-a-level-computer-science.webp 128w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=256/https://cdn.savemyexams.com/uploads/2024/01/linked-lists-we-4-ocr-a-level-computer-science.webp 256w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=384/https://cdn.sav

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注