1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as...

17
1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: This required 2 classes. 1 st class covered linked-lists extempo, 2 nd class discussed malloc() and free(). For Fall05, use this for lecture-1, and develop a 2 nd lecture for covering malloc/free

Transcript of 1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as...

Page 1: 1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: Must add malloc and free.

1

Linked Lists

Math 130

Lecture # 22

B Smith:

Must add malloc and free to the discussion, as well as walking a list.

B Smith:

Must add malloc and free to the discussion, as well as walking a list.

B Smith:

This required 2 classes. 1st class covered linked-lists extempo, 2nd class discussed malloc() and free(). For Fall05, use this for lecture-1, and develop a 2nd lecture for covering malloc/free

B Smith:

This required 2 classes. 1st class covered linked-lists extempo, 2nd class discussed malloc() and free(). For Fall05, use this for lecture-1, and develop a 2nd lecture for covering malloc/free

Page 2: 1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: Must add malloc and free.

6

Overview

• Linked Lists

• Lists vs Arrays

• List Insertion

• List Deletion

Page 3: 1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: Must add malloc and free.

7

Recall Arrays

• An array is a grouped list of two or more variables of the same type

• Array elements are contiguous.• Subscripts can be any integer expression• Each array element is a separate variablegrades[0]

grades[1]

grades[2]

grades[3]

88 91 71 86

grades:88

91

71

86

Page 4: 1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: Must add malloc and free.

8

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 5: 1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: Must add malloc and free.

9

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

linkitem

Page 6: 1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: Must add malloc and free.

10

“End” of a Linked List

• 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’

B Smith:

4/8/05 Fri: Stopped here and worked at the board to define the nodes.

B Smith:

4/8/05 Fri: Stopped here and worked at the board to define the nodes.

Page 7: 1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: Must add malloc and free.

11

Linked List Code

struct node {

int item;

struct node* next;

};

struct node {

int item;

struct node* next;

};

struct node {

int item;

link next;

};

typedef struct node* link;

struct node {

int item;

link next;

};

typedef struct node* link;

B Smith:

Stopped here on 11/2/05. Rate: 3. Used a physical paper link and chairs to simulate linked list.

B Smith:

Stopped here on 11/2/05. Rate: 3. Used a physical paper link and chairs to simulate linked list.

Alternatively, the node could be represented as shown below

Page 8: 1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: Must add malloc and free.

12

Telephone List

• What data structure would you use to organize the following list?

• What challenges do you see if we want to add Hagar, June to the list if organized as an array of structures?

Acme, Sam(555) 898-2392

Dolan, Edith(555) 682-3104

Lanfrank, John(555) 718-4581

Mening, Stephanie(555) 382-7070

Zemann, Harold(555) 219-9912

Page 9: 1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: Must add malloc and free.

13

Simple Linked List Example-Telephone List

• First member is the individual’s name

• Second member is the telephone number

• Last member is the address of the next item

struct TeleType

{

char name[30];

char phoneNum[15];

struct TeleType *nextaddr;

};

TeleType name[]

phoneNum[]

nextaddr*

“Acme, Sam”

(555)898-2392

t1

&t2

Page 10: 1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: Must add malloc and free.

14

Linking The Data Using Addresses

“Acme, Sam”

(555)898-2392

t1

“Dolan, Edith”

(555)682-3104

t2

“Lanfrank, Jo”

(555)718-4581

t3

NULL

B Smith:

This example was used to show them how to walk through a list with a while loop. Elaborate in Sp05 on walking; more diagrams, pix.

B Smith:

This example was used to show them how to walk through a list with a while loop. Elaborate in Sp05 on walking; more diagrams, pix.

Page 11: 1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: Must add malloc and free.

15

List Implementation#include <stdio.h>

struct TeleType

{

char name[30];

char phoneNum[15];

struct TeleType *nextaddr;

};

int main()

{

struct TeleType t1 = {"Acme, Sam","(555) 898-2392"}; /* an instance */

struct TeleType t2 = {"Dolan, Edith","(555) 682-3104"};

struct TeleType t3 = {"Lanfrank, John","(555) 718-4581"};

struct TeleType *first; /* create a pointer to a structure */

first = &t1; /* store t1's address in first */

t1.nextaddr = &t2; /* store t2's address in t1.nextaddr */

t2.nextaddr = &t3; /* store t3's address in t2.nextaddr */

t3.nextaddr = NULL; /* store the NULL address in t3.nextaddr */

printf("\n%s \n%s \n%s",first->name,t1.nextaddr->name,t2.nextaddr->name);

return 0;

}

Page 12: 1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: Must add malloc and free.

16

Record Insertion

• Linked lists are especially useful when a record needs to be inserted How would you grow a predefined array?

• To insert the record for June Hagar into the list, what steps must be taken?

Page 13: 1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: Must add malloc and free.

17

List Item Insertion

“Acme, Sam”

(555)898-2392

t1 “Dolan, Edith”

(555)682-3104

“Lanfrank, Jo”

(555)718-4581

NULL

The next field must be changed to point to Hagar “Hagar, June”

(555)682-3104

This node must point to Lanfrank.

B Smith:

Stopped here on 11/1/04

B Smith:

Stopped here on 11/1/04

Page 14: 1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: Must add malloc and free.

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 15: 1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: Must add malloc and free.

19

List Item Deletion

• What might be the steps for removing a record from a linked list?

Page 16: 1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: Must add malloc and free.

20

Remove Dolan, Edith

“Acme, Sam”

(555)898-2392

t1

“Dolan, Edith”

(555)682-3104

“Lanfrank, Jo”

(555)718-4581

NULL

The next field must point to Lanfrank

t1.next = t1.next->next;

t1.next = t2.next;

Page 17: 1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: Must add malloc and free.

21

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