Title: CSCE 210 Data Structures and Algorithms
1CSCE 210Data Structures and Algorithms
- Prof. Amr Goneid
- AUC
- Part 6. Linked Lists
2Linked Lists
- The Linked List Structure
- Some Linked List Operations
- Variations on Linked Lists
31. The Linked List Structure
- Arrange dynamically allocated structures into a
new structure called a linked list - Think of a set of childrens pop beads
- Connecting beads to make a chain
- You can move things around and re-connect the
chain - We use pointers to create the same effect
4The Simple Linked List
- A sequence of nodes linked by pointers
- First node pointed to by head. Contains a data
element (e) and a next pointer to next node. - Last nodes next is NULL.
- A cursor points to the current node. It can
advance in one way only to next node, e.g. to
traverse whole list.
e
NULL
Last
head
First
next
cursor
5Specifying Node Structure(Example)
- Suppose each node is to contain a word from the
dictionary, and the number of times such word
occurs in a document. - struct elType // specify data element
- string word int count
- struct node // specify node structure
- elType e node next
- node p, q // pointers to nodes of type node
6Specifying Node Structure
- Each of the pointers p, q can point to a struct
of type node - e.word (string)
- e.count (int)
- next (pointer to next node)
Struct of type node
word
count
next
String Integer Address
7Building Nodes
- Allocate storage of 2 nodes
- p new node
- q new node
- Assign data to nodes
- elType el1 , el2
- el1.word hat el1.count 2
- el2.word top el2. count 3
- p-gte el1 q-gte el2
8Building Nodes
p
hat
2
?
top
3
?
q
9Connecting Nodes A linked list of two nodes
- Suppose the address in q is stored in next
field of node pointed to by p and NULL is stored
in the last next field - p-gtnext q q-gtnext NULL
p
hat
2
next
top
3
NULL
q
102. Some Linked List Operations
- Insertion at head of list
- Inserting a node after a given node
- Insert at end of list
- Delete a head node
- Delete a non-head node
11Insertion at Head of List
Last
First
hat 2
top 3
head
2
3
elType el el.word if el.count 4 p new
node p-gt e el p-gtnext head head p
New
if 4
1
p
12Inserting after a given Node
cursor
head
top 3
hat 2
if 4
2
3
el.word the el.count 5 p new node p-gt
e el p-gt next cursor-gt next cursor-gtnext
p
p
the 5
New
1
13Insert at End of List
cursor
Last
hat 2
top 3
3
New
p new node p-gte el p-gtnext
NULL cursor-gtnext p
2
if 4
1
p
14Delete Head Node
1
cursor
head
hat 2
the 5
top 3
if 4
3
2
cursor head head head-gtnext delete cursor
15Deleting a Non-Head Node
cursor
cursor
prev
q
Successor
1
the
top
hat
3
Pre cursor points to node prev points to
predecessor node
2
node q q cursor cursor cursor-gtnext prev-gt
next cursor delete q
16Demo
- http//www.cosc.canterbury.ac.nz/people/mukundan/d
sal/LinkListAppl.html
173. Variations on Linked Lists
- The Circular List
- Notice that tail-gtnext head
head
tail
cursor
18Variations on Linked Lists
- The Doubly Linked List
- To advance cursor cursor-gtnext
- To back cursor cursor-gtback
next
back
cursor
19Variations on Linked Lists
- The Circular Doubly Linked List
- The 2-D List
20Learn on your own about
- Circular Linked Lists
- Doubly Linked Lists
- Multi-Lists