Back to 课程

Computer-science_A-level_Cie

0% Complete
0/0 Steps
  1. computers-and-components
    6 主题
  2. logic-gates-and-logic-circuits
    2 主题
  3. central-processing-unit-cpu-architecture
    6 主题
  4. assembly-language-
    4 主题
  5. bit-manipulation
    1 主题
  6. operating-systems
    3 主题
  7. language-translators
    2 主题
  8. data-security
    3 主题
  9. data-integrity
    1 主题
  10. ethics-and-ownership
    3 主题
  11. database-concepts
    3 主题
  12. database-management-systems-dbms-
    1 主题
  13. data-definition-language-ddl-and-data-manipulation-language-dml
    1 主题
  14. computational-thinking-skills
    1 主题
  15. algorithms
    14 主题
  16. data-types-and-records
    2 主题
  17. arrays
    2 主题
  18. files
    1 主题
  19. introduction-to-abstract-data-types-adt
    1 主题
  20. programming-basics
    1 主题
  21. constructs
    2 主题
  22. structured-programming
    1 主题
  23. program-development-life-cycle
    2 主题
  24. program-design-
    2 主题
  25. program-testing-and-maintenance
    3 主题
  26. user-defined-data-types
    1 主题
  27. file-organisation-and-access-
    3 主题
  28. floating-point-numbers-representation-and-manipulation
    3 主题
  29. protocols
    2 主题
  30. circuit-switching-packet-switching
    1 主题
  31. processors-parallel-processing-and-virtual-machines
    5 主题
  32. boolean-algebra-and-logic-circuits
    4 主题
  33. purposes-of-an-operating-system-os
    3 主题
  34. translation-software
    3 主题
  35. encryption-encryption-protocols-and-digital-certificates
    3 主题
  36. artificial-intelligence-ai
    4 主题
  37. recursion
    1 主题
  38. programming-paradigms
    4 主题
  39. object-oriented-programming
    7 主题
  40. file-processing-and-exception-handling
    2 主题
  41. data-representation
    5 主题
  42. multimedia
    3 主题
  43. compression
    2 主题
  44. networks-and-the-internet
    11 主题
课 Progress
0% Complete

Linked lists

How do you program a linked list?

  • To recap the main operations of linked lists and how they work, read our ADT Overview page

  • To program a Linked List you must be able to perform the following operations:

    • Create a linked list

    • Traverse a linked list

    • Add data to a linked list

    • Remove data from a linked list

Create a linked list

  • Define the class called ‘Node’ that represents a node in the linked list.

  • Each node has 2 fields ‘fruit’ (this stores the fruit value) and ‘next’ (this stores a reference to the next node in the list)

  • We create 3 nodes and assign fruit values to each node

  • To establish a connection between the nodes we set the ‘next’ field of each node to the appropriate next node

Pseudocode

CLASS Node DECLARE fruit : STRING DECLARE next : Node
ENDCLASS // Create nodes
DECLARE node1 : Node
DECLARE node2 : Node
DECLARE node3 : Node SET node1 = NEW Node
SET node2 = NEW Node
SET node3 = NEW Node // Assign fruit values
SET node1.fruit = "apple"
SET node2.fruit = "banana"
SET node3.fruit = "orange" // Connect nodes
SET node1.next = node2
SET node2.next = node3
SET node3.next = NULL

Python

class Node: def __init__(self, fruit): self.fruit = fruit self.next = None # Create nodes
node1 = Node("apple")
node2 = Node("banana")
node3 = Node("orange") # Connect nodes
node1.next = node2
node2.next = node3

Java

class Node { String fruit; Node next; Node(String fruit) { this.fruit = fruit; this.next = null; }
} public class LinkedListExample { public static void main(String[] args) { // Create nodes Node node1 = new Node("apple"); Node node2 = new Node("banana"); Node node3 = new Node("orange"); // Connect nodes node1.next = node2; node2.next = node3 }
}

Algorithm to traverse a linked list

  • We traverse the linked list starting from ‘node1’. We output the fruit value of each node and update the ‘current’ reference to the next node until we reach the end of the list

Pseudocode

// Start at the head of the list
DECLARE current : Node
SET current = node1 WHILE current != NULL OUTPUT current.fruit SET current = current.next
ENDWHILE

Python

# Traverse the linked list

current = node1

while current is not None:

print(current.fruit)

current = current.next

Java

// Traverse the linked list

current = node1;

while (current != null) {

System.out.println(current.fruit);

current = current.next;

Algorithm to add data to a linked list

  • We create a new node with the new data and check if the list is empty

  • If it isn’t, we find the last node and add the new node to the end

Pseudocode

// Create a new node
DECLARE newNode : Node
SET newNode = NEW Node
SET newNode.fruit = "orange"
SET newNode.next = NULL // If the list is empty
IF node1 = NULL THEN SET node1 = newNode
ELSE // Traverse to the end of the list DECLARE current : Node SET current = node1 WHILE current.next != NULL SET current = current.next ENDWHILE // Append the new node SET current.next = newNode
ENDIF

Python

# Add "orange" to the end of the linked list
new_node = Node("orange")

if node1 is None:
# If the list was empty, make "orange" the head
node1 = new_node
else:
# Find the last node and append "orange" to it
current = node1
while current.next is not None:
current = current.next
current.next = new_node

Java

// Add "orange" to the end of the linked list
Node new_node = new Node("orange");

if (node1 == null) {
// If the list was empty, make "orange" the head
node1 = new_node;
} else {
// Find the last node and append "orange" to it
current = node1;
while (current.next != null) {
current = current.next;
}
current.next = new_node;

}

Algorithm to remove data from a linked list

  • We traverse the list until we find the data to be removed

  • If the data to be removed is the first node, we update the head, else we update the node before to bypass the data to be removed

Pseudocode

// Start traversal from the head
DECLARE current : Node
DECLARE previous : Node SET current = node1
SET previous = NULL WHILE current != NULL IF current.fruit = "apple" THEN IF previous = NULL THEN // "apple" is at the head of the list SET node1 = current.next ELSE // Bypass the current node SET previous.next = current.next ENDIF BREAK ENDIF SET previous = current SET current = current.next
ENDWHILE

Python

# Find and remove "apple"

while current is not None:

if current.fruit == "apple":

if previous is None:

# If "apple" is the first node, update the head

node1 = current.next

else:

# Remove the node by updating the previous node's next pointer

previous.next = current.next

break

previous = current

current = current.next

Java

// Find and remove "apple"

 while (current != null) {

if (current.fruit.equals("apple")) {

if (previous == null) {

// If "apple" is the first node, update the head

node1 = current.next;

} else {

// Remove the node by updating the previous node's next pointer

previous.next = current.next;

}

break;

}

previous = current;

current = current.next;

}

Worked Example

The following diagram represents an Abstract Data Type (ADT) for a linked list.

Flowchart of rectangular boxes connected by arrows showing sequence: A to C to D to E, ending in Ø. Each box represents a step in a process.

The free list is as follows:

Flowchart showing a series of connected boxes with arrows. The last box contains the symbol Ø, indicating an end or null operation.

Explain how a node containing data value B is added to the list in alphabetic sequence.[4]

Answer

One mark per point

  1. Check for a free node [1 mark]

  2. Search for correct insertion point [1 mark]

  3. Assign data value B to first node in free list / node pointed to by start pointer of free list [1 mark]

  4. Pointer from A will be changed to point to node containing B (instead of C) [1 mark]

  5. Pointer from B will be changed to point to node containing C [1 mark]

  6. Start pointer in free list moved to point to next free node [1 mark]

Responses

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