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

Post on 30-Mar-2015

213 views 1 download

Tags:

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

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

6

Overview

• Linked Lists

• Lists vs Arrays

• List Insertion

• List Deletion

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

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.

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

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.

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

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

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

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.

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;

}

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?

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

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

19

List Item Deletion

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

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;

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