37
C Programming Language: C ADTs: Lists and Linked Data Math 230 Assembly Language Programming (Computer Organization) Tuesday Feb 5, 2008 L07

C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

Page 1: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

C Programming Language:

C ADTs: Lists and Linked Data

Math 230

Assembly Language Programming

(Computer Organization)

Tuesday Feb 5, 2008

L07

Page 2: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

2

Overview

• strings and arrays, malloc, free

• struct and union

� Linked Lists

Page 3: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

3

Linked-List

linked-list of grades:

88 91 71 86

88 91 71 86

array of grades:

The data could be

held in a “struct”

containing an integer

and a pointer

The data here is

held in an array of

integers

What is the “data type” of the pointer?

The pointer is defined such that it points to another node. The node it points to is the same type of struct in which it resides.

Page 4: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

4

Linked-List Definition

• Definition: A linked list is a set of items where each item is

a part of a node that also contains a link to a node.

• It is a sequence of “nodes,” each containing

� data field(s) (the Item)

� one (or more) links to the next node; a “reference”

‘N’ ‘E’ ‘M’ ‘O’

Page 5: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

4

Linked-List Definition

• Definition: A linked list is a set of items where each item is

a part of a node that also contains a link to a node.

• It is a sequence of “nodes,” each containing

� data field(s) (the Item)

� one (or more) links to the next node; a “reference”

‘N’ ‘E’ ‘M’ ‘O’

node

Page 6: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

4

Linked-List Definition

• Definition: A linked list is a set of items where each item is

a part of a node that also contains a link to a node.

• It is a sequence of “nodes,” each containing

� data field(s) (the Item)

� one (or more) links to the next node; a “reference”

‘N’ ‘E’ ‘M’ ‘O’

nodelink

Page 7: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

4

Linked-List Definition

• Definition: A linked list is a set of items where each item is

a part of a node that also contains a link to a node.

• It is a sequence of “nodes,” each containing

� data field(s) (the Item)

� one (or more) links to the next node; a “reference”

‘N’ ‘E’ ‘M’ ‘O’

nodelinkitem

Page 8: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

5

Linked Lists

• The link in the final node can be represented a

number of ways

� As a null link that points to no node

� A reference to a dummy node that contains no item

� A reference back to the first node, making the list a

circular list.

‘N’ ‘E’ ‘M’ ‘O’

‘N’ ‘E’ ‘M’ ‘O’

‘N’ ‘E’ ‘M’ ‘O’

Page 9: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

5

Linked Lists

• The link in the final node can be represented a

number of ways

� As a null link that points to no node

� A reference to a dummy node that contains no item

� A reference back to the first node, making the list a

circular list.

‘N’ ‘E’ ‘M’ ‘O’

‘N’ ‘E’ ‘M’ ‘O’

‘N’ ‘E’ ‘M’ ‘O’

Page 10: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

5

Linked Lists

• The link in the final node can be represented a

number of ways

� As a null link that points to no node

� A reference to a dummy node that contains no item

� A reference back to the first node, making the list a

circular list.

‘N’ ‘E’ ‘M’ ‘O’

‘N’ ‘E’ ‘M’ ‘O’

‘N’ ‘E’ ‘M’ ‘O’

Page 11: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

7

Linked-List Code

Page 12: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

7

Linked-List Code

struct node {

Page 13: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

7

Linked-List Code

struct node {

int item;

Page 14: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

7

Linked-List Code

struct node {

int item;

struct node* next;

Page 15: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

7

Linked-List Code

struct node {

int item;

struct node* next;

};

Page 16: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

7

Linked-List Code

struct node {

int item;

struct node* next;

};

typedef int Item; //or char, float,char*,etc

typedef struct node* link;

struct node {

Item item;

link next;

};

typedef int Item; //or char, float,char*,etc

typedef struct node* link;

struct node {

Item item;

link next;

};

Page 17: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

8

The Utility of Lists

• Linked lists help us manage constantly

changing lists of data

• They help us with the insertion and deletion

of existing records

• Each item contains information on how to get

to the next item

• It is a set of items where each item is part of a

node that also contains a link to a node

Page 18: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

9

malloc() and Linked-Lists

• We’ve created one “node" data type

• We’ll have many instances of this one type

� Recall: we can have also have muliple instances of

int

• To create a new instance of a node and reserve

memory for it:

Use #include<stdlib.h>

Page 19: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

9

malloc() and Linked-Lists

• We’ve created one “node" data type

• We’ll have many instances of this one type

� Recall: we can have also have muliple instances of

int

• To create a new instance of a node and reserve

memory for it:

Use #include<stdlib.h>

link x = malloc(sizeof *x );

Page 20: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

10

free() - letting go of previously allocated memory

• When you’re done with the node, you should free the previously allocated memory using “free()”

• The interface:

• Its usage

• Helps you avoid memory leaks. Continuously allocating memory without freeing any could lead to a “crash.”

� memory leaks!

Page 21: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

10

free() - letting go of previously allocated memory

• When you’re done with the node, you should free the previously allocated memory using “free()”

• The interface:

• Its usage

• Helps you avoid memory leaks. Continuously allocating memory without freeing any could lead to a “crash.”

� memory leaks!

void free(void* ptr);

Page 22: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

10

free() - letting go of previously allocated memory

• When you’re done with the node, you should free the previously allocated memory using “free()”

• The interface:

• Its usage

• Helps you avoid memory leaks. Continuously allocating memory without freeing any could lead to a “crash.”

� memory leaks!

void free(void* ptr);

free(x);

Page 23: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

11

Accessing list-node information

• Dereference the pointer, then use the structure

member names

• The “item” in the node referenced by link x is

(*x).item or x->item

� (the data type of item is Item)

• The link to the next node is indicated by

(*x).next, or x->next

� (the data type of next is link)

‘N’ ‘M’ ‘O’‘U’

‘N’‘N’ ‘M’‘M’ ‘O’‘O’‘U’‘U’

t

x

Page 24: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

12

Fundamental Operations on Linked Lists

• Linked-list Deletion

• Linked-list Insertion

Page 25: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

13

Linked-list Deletion

• The node containing ‘E’ can be removed by

redirecting the node that points to it:

Page 26: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

13

Linked-list Deletion

• The node containing ‘E’ can be removed by

redirecting the node that points to it:

‘N’ ‘E’ ‘M’ ‘O’

Page 27: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

13

Linked-list Deletion

• The node containing ‘E’ can be removed by

redirecting the node that points to it:

‘N’ ‘E’ ‘M’ ‘O’

‘N’ ‘E’ ‘M’ ‘O’

Page 28: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

13

Linked-list Deletion

• The node containing ‘E’ can be removed by

redirecting the node that points to it:

‘N’ ‘E’ ‘M’ ‘O’

‘N’ ‘E’ ‘M’ ‘O’

‘N’ ‘M’ ‘O’

Page 29: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

14

Linked-list Deletion

• To “delete” the node containing ‘E’ (the 2 below are equiv)

t = x->next;

x->next = t->next;

t = x->next;

x->next = t->next;

x->next = x->next->next;x->next = x->next->next;

‘N’ ‘M’ ‘O’‘N’ ‘M’‘M’ ‘O’

‘N’ ‘E’ ‘M’ ‘O’‘N’ ‘E’ ‘M’ ‘O’‘N’‘N’ ‘E’‘E’ ‘M’‘M’ ‘O’‘O’

‘N’ ‘E’ ‘M’ ‘O’‘N’‘N’ ‘E’‘E’ ‘M’‘M’ ‘O’‘O’

Page 30: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

15

Linked-list Insertion

• The node containing ‘U’ can be inserted into the list

Page 31: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

15

Linked-list Insertion

• The node containing ‘U’ can be inserted into the list

‘N’ ‘M’ ‘O’

‘U’

Page 32: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

15

Linked-list Insertion

• The node containing ‘U’ can be inserted into the list

‘N’ ‘M’ ‘O’

‘U’

‘N’ ‘M’ ‘O’

‘U’

Page 33: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

15

Linked-list Insertion

• The node containing ‘U’ can be inserted into the list

‘N’ ‘M’ ‘O’

‘U’

‘N’ ‘M’ ‘O’

‘U’

‘N’ ‘M’ ‘O’‘U’

Page 34: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

16

Linked-list Insertion

• To insert node containing ‘U’ into a list

‘N’ ‘M’ ‘O’

‘U’

‘N’ ‘M’ ‘O’‘N’ ‘M’‘M’ ‘O’

‘U’‘U’

‘N’ ‘M’ ‘O’

‘U’

‘N’‘N’ ‘M’‘M’ ‘O’‘O’

‘U’‘U’

‘N’ ‘M’ ‘O’‘U’

‘N’‘N’ ‘M’‘M’ ‘O’‘O’‘U’‘U’

t->next = x->next;

x->next = t;

t->next = x->next;

x->next = t;

Page 35: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

17

Array vs Linked-List

• Given an array of fixed size, inserting an

element into the nth positon is tricky

• Access into a linked-list is not as efficient as

an array when finding the kth item simply

using a[k]

• Linked lists are an alternative to arrays

Page 36: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

18

NULL - End of List

• The NULL pointer is frequently used as a

sentinel to mark the end-of-string NULL.

� both serve similar purposes

Page 37: C Programming Language: C ADTs: Lists and Linked Datadept.swccd.edu/bsmith/m230/lectures/07linkedLists.pdf · 2008. 2. 20. · Linked-List linked-list of grades: 88 91 71 86 88 91

19

Summary

• Linked lists

� help us manage constantly changing lists of

data

� help us with insertion and deletion of existing

records

� is a basic data structure where each item

contains information on how to get to the next

item

� is a set of items where each item is part of a

node that also contains a link to a node