Post

Array-based Sequences - LinkedList

Practice for Array-based Sequences such as LinkedList

Array-based Sequences - LinkedList

The objective of this assignment is to help students understand the internal structure and operations of Linked Lists by implementing them from scratch using Python classes, without using built-in list types.

Students will implement:

  • A Singly Linked List
  • A Doubly Linked List

Singly Linked List (SLL)

Task 1: Create Node Class

AttributeTypeDescription
dataanyValue stored in the node
nextNode / NoneReference to the next node

Task 2: Create SinglyLinkedList Class

Function NameParametersReturn TypeDescription
insert_at_beginningdataNoneInsert a new node at the beginning of the list
insert_at_enddataNoneInsert a new node at the end of the list
deletedataNoneDelete the first occurrence of the given value
searchdataboolReturn True if value exists, otherwise False
displayNoneNonePrint all elements in order
lengthNoneintReturn the number of nodes in the list
reverse (optional)NoneNoneReverse the linked list

Apply assignment: Student Management using Singly Linked List

Implement a student management system using a Singly Linked List, where each node stores information about one student.

Data Model: Each node represents a student with:

  • student_id (int)
  • name (string)
  • gpa (float)

Create StudentManagement class

FunctionDescription
insert_at_end(student)Add a new student to the list
delete_by_id(student_id)Remove a student by ID
search_by_id(student_id)Return student info if found
display()Display all students
count()Return number of students
average_gpa()Compute average GPA of all students

insert_at_end(student)

ItemDescription
Inputstudent: dictionary {id, name, gpa}
OutputNone
BehaviorInsert a new student at the end of the list

delete_by_id(student_id)

ItemDescription
Inputstudent_id: int
OutputNone
BehaviorRemove the first student with matching ID

search_by_id(student_id)

ItemDescription
Inputstudent_id: int
OutputStudent dictionary if found, otherwise None
BehaviorTraverse the list and search by ID

display()

ItemDescription
InputNone
OutputPrinted student list
BehaviorPrint all students in order

count()

ItemDescription
InputNone
OutputInteger
BehaviorReturn number of students

average_gpa()

ItemDescription
InputNone
OutputFloat
BehaviorReturn average GPA of all students

Doubly Linked List (DLL)

Task 1: Create Node Class

AttributeTypeDescription
dataanyValue stored in the node
nextDoublyNode / NoneReference to the next node
prevDoublyNode / NoneReference to the previous node

Task 2: Create SinglyLinkedList Class

Function NameParametersReturn TypeDescription
insert_at_beginningdataNoneInsert a new node at the beginning
insert_at_enddataNoneInsert a new node at the end
deletedataNoneDelete the first node with the given value
display_forwardNoneNoneDisplay list from head to tail
display_backwardNoneNoneDisplay list from tail to head
clear (optional)NoneNoneRemove all nodes from the list

Using Doubly Linked List

Implement a music playlist application using a Doubly Linked List, allowing users to move forward and backward through songs.

Data Model Each node represents a song with:

  • title (string)
  • artist (string)
  • duration (int, seconds)

Create MusicManagement class

FunctionDescription
insert_at_end(song)Add song to playlist
delete_by_title(title)Remove a song
play_forward()Display playlist from first to last
play_backward()Display playlist from last to first
total_duration()Calculate total playlist duration

insert_at_end(song)

ItemDescription
Inputsong: dictionary {title, artist, duration}
OutputNone
BehaviorAdd a song to the end of the playlist

delete_by_title(title)

ItemDescription
Inputtitle: string
OutputNone
BehaviorRemove the first song with matching title

Ex:

1
Song A -> Song B -> Song C -> None

play_forward()

ItemDescription
InputNone
OutputPrinted playlist (head → tail)
BehaviorTraverse using next pointers

Ex:

1
Song C -> Song B -> Song A -> None

total_duration()

ItemDescription
InputNone
OutputInteger (seconds)
BehaviorSum duration of all songs

Ex:

1
Total duration: 380 seconds
This post is licensed under CC BY 4.0 by the author.