Linked Lists: Understanding Nodes, Insertion, and Deletion in a Linked List - Prof. Sami N, Study notes of Computer Science

An explanation of linked lists, including the concept of nodes, insertion at the head, and deletion. It also introduces doubly linked lists and their advantages. Students will learn how to create, insert, and delete nodes in a linked list.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-pyj
koofers-user-pyj 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Linked Lists
Example
We would like to keep a list of inventory
records – but only as many as we need
An array is a fixed size
Instead – use a linked list
What are the disadvantages of using a
linked list?
Linked List
Node – one element of the linked list
Object – data s tored in the node – examples?
next – a referen ce to the next node in the list
last node points to NU LL
Object
next Ø
Object
next
Object
next
Linked List
head keeps track of the head of the list
tail keeps track of the last node in the list
tai l not always used
Object
next Ø
Object
next
Object
next
head
tail
Insertion at Head
Object1
next Ø
head Object2
next
Object3
next Insert here
new_node
tail
Insertion at Head
Create new_node
sto re object in new_node
Point new_node next to the node head points to
Object1
next Ø
head Object2
next
Object3
next
new_node
tail
pf3

Partial preview of the text

Download Linked Lists: Understanding Nodes, Insertion, and Deletion in a Linked List - Prof. Sami N and more Study notes Computer Science in PDF only on Docsity!

Linked Lists

Example

• We would like to keep a list of inventory

records – but only as many as we need

• An array is a fixed size

• Instead – use a linked list

• What are the disadvantages of using a

linked list?

Linked List

• Node – one element of the linked list

  • Object – data stored in the node – examples?
  • next – a reference to the next node in the list
    • last node points to NULL Object next (^) Ø Object next Object next

Linked List

• head keeps track of the head of the list

• tail keeps track of the last node in the list

  • tail not always used Object next (^) Ø Object next Object next head tail

Insertion at Head

Object head (^) next (^) Ø Object next Object next Insert here new_node tail

Insertion at Head

• Create new_node

  • store object in new_node

• Point new_node next to the node head points to

Object head (^) next (^) Ø Object next Object next new_node tail

Insertion at Head

• Create new_node

  • store object in new_node

• Point new_node next to the node head points to

• Point head to new_node

Object head (^) next (^) Ø Object next Object next new_node^ tail

Insertion at Head

• Does this algorithm work for the list below?

Object head (^) next (^) Ø Object next Object next head Ø Object next new_node tail tail

Insertion at Head

• Create new_node

– store object in new_node

• Point new_node next to the node head

points to

• Point head to new_node

• If tail points to NULL

– point tail to new_node

Insertion at Head

• Create new_node

  • store object in new_node

• Point new_node next to the node head points to

• Point head to new_node

• If tail points to NULL

  • point tail to new_node head Ø Object next new_node tail

Insertion at Tail

head Ø Object next new_node tail Object head (^) next (^) Ø Object next Object next Insert here tail new_node

Find

• find(3)

• find(16) - always remember to deal with special

cases

next (^) Ø

next

next head tail