CSCE 210 Data Structures and Algorithms - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

CSCE 210 Data Structures and Algorithms

Description:

Think of a set of children's pop beads. Connecting beads to make a chain ... is to contain a word from the dictionary, and the number of times such word ... – PowerPoint PPT presentation

Number of Views:83
Avg rating:3.0/5.0
Slides: 21
Provided by: dramrg
Category:

less

Transcript and Presenter's Notes

Title: CSCE 210 Data Structures and Algorithms


1
CSCE 210Data Structures and Algorithms
  • Prof. Amr Goneid
  • AUC
  • Part 6. Linked Lists

2
Linked Lists
  • The Linked List Structure
  • Some Linked List Operations
  • Variations on Linked Lists

3
1. 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

4
The 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
5
Specifying 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

6
Specifying 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
7
Building 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

8
Building Nodes

p
hat
2
?
top
3
?
q
9
Connecting 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
10
2. 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

11
Insertion 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
12
Inserting 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
13
Insert 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
14
Delete Head Node
1
cursor
head
hat 2
the 5
top 3
if 4
3
2
cursor head head head-gtnext delete cursor
15
Deleting 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
16
Demo
  • http//www.cosc.canterbury.ac.nz/people/mukundan/d
    sal/LinkListAppl.html

17
3. Variations on Linked Lists
  • The Circular List
  • Notice that tail-gtnext head

head
tail
cursor
18
Variations on Linked Lists
  • The Doubly Linked List
  • To advance cursor cursor-gtnext
  • To back cursor cursor-gtback

next
back
cursor
19
Variations on Linked Lists
  • The Circular Doubly Linked List
  • The 2-D List

20
Learn on your own about
  • Circular Linked Lists
  • Doubly Linked Lists
  • Multi-Lists
Write a Comment
User Comments (0)
About PowerShow.com