CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

22
CSE1303 Part A CSE1303 Part A Data Structures and Data Structures and Algorithms Algorithms Lecture A7 – Nodes and Linked Lecture A7 – Nodes and Linked Structures Structures
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    220
  • download

    0

Transcript of CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

Page 1: CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

CSE1303 Part ACSE1303 Part AData Structures and AlgorithmsData Structures and Algorithms

Lecture A7 – Nodes and Linked Lecture A7 – Nodes and Linked StructuresStructures

Page 2: CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

2

Overview of TopicOverview of Topic

• Review List Implementations.

• Nodes.

• Linked Stacks.

• Linked Queues

• Linked Lists.

• Other List Operations

Today’s Lecture

Page 3: CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

3

ListsLists

151031 60 1 3 42

• Kept in alphabetical or numerical order• No spaces between elements• Simple lists are implemented in arrays

Page 4: CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

4

A List ADTA List ADT

• Initialize the list.• Determine whether the list is empty.• Determine whether the list is full.• Find the size of the list.• Insert an item anywhere in the list.• Delete an item anywhere in a list.• Go to a particular position in a list.

A sequence of elements together with these operations:

Lists need to be able to expand

Page 5: CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

5

Simple List ImplementationSimple List Implementation

1 3 10 15 21

6

Page 6: CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

6

Expansion and InsertionExpansion and Insertion

1 3 10 15 21

1 3 10 15 216

Copy old array, leave space for the new value

6

Simple List ImplementationSimple List Implementation

Page 7: CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

7

DisadvantagesDisadvantages

• Lots of memory needs to be allocated.

• Lots of copying needs to be done.

Page 8: CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

8

Linked List Implementation:Linked List Implementation:Using PointersUsing Pointers

3 15 1 21

0x2000 0x2008 0x2010 0x2018 0x2020

0x2020 0x2018 0x2000 NULL

start

10

0x2008

insert: 6

Page 9: CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

9

Method 1Method 1 start

3 15 1 21

0x2000 0x2008 0x2010 0x2018 0x2020

0x2020 0x2018 0x2000 NULL

10

0x2008

0x30F0 0x30F8 0x3100 0x3108 0x3110

newStart

0x3118

insert: 6

1 3

0x30F8 0x3100 0x3108 0x3110 0x3118 NULL

6 10 15 21

0x30F0

Copy old array, leave space for the new value

Page 10: CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

10

Method 2Method 2

3 15 1 21

0x2000 0x2008 0x2010 0x2018 0x2020

0x2020 0x2018 0x2000 NULL

start

10

0x2008

insert: 6

6newItem

0x30F0

Page 11: CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

11

Method 2Method 2

3 15 1 21

0x2000 0x2008 0x2010 0x2018 0x2020

0x30F0 0x2018 0x2000 NULL

start

10

0x2008

6newItem

0x30F0

0x2020

Page 12: CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

12

AdvantagesAdvantages

• Only a little amount of memory needs to be allocated.

• Only a little amount of copying needs to be done.

Page 13: CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

13

struct NodeRec{ float value; struct NodeRec* nextPtr;};

typedef struct NodeRec Node;

Node:

nextPtr:

value:

Page 14: CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

14

#ifndef NODEH#define NODEH

struct NodeRec{ float value; struct NodeRec* nextPtr;};

typedef struct NodeRec Node;

Node* makeNode(float item);

#endif

Page 15: CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

15

Make NodeMake Node

• To make a new node for an item– take enough bytes from the heap– remember its address in memory– put the item in that location– set “next” link to NULL– return the node’s address

Page 16: CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

Node* makeNode(float item){ Node* newNodePtr = (Node*)malloc(sizeof(Node));

if (newNodePtr == NULL) { fprintf(stderr, “Out of memory”); exit(1); } else { newNodePtr->value = item; newNodePtr->nextPtr = NULL; } return newNodePtr;}

16

Page 17: CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

#include <stdio.h>#include <stdlib.h>#include “node.h”

int main(){ float item; Node* headPtr = NULL; Node* newNodePtr = NULL; while (scanf(“%f”, &item) != EOF){ if (headPtr == NULL){ headPtr = makeNode(item);

} else {

newNodePtr = makeNode(item); newNodePtr->nextPtr = headPtr; headPtr = newNodePtr;}

}} 17

Insert nodes at the start of a listInsert nodes at the start of a list

Page 18: CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

18

0x30F0headPtr:

0x30F0

0x2124

0x2A40

1

0x2A40nextPtr:

value:

6

NULLnextPtr:

value:

3

0x2124nextPtr:

value:

Page 19: CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

19

Linked StructureLinked Structure

10x30F0

6

3 0x2124

0x2124

0x2A40

NULL

0x2A40 0x30F0headPtr:

Page 20: CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

20

Linked StructureLinked Structure

10x30F0

3 0x21240x2A40

0x2A40 0x30F0headPtr:

60x2124 NULL

Page 21: CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

21

Linked StructureLinked Structure

1

3

headPtr:

6

Page 22: CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

22

RevisionRevision• Node

• How to make a new Node

• Linked Structures

Revision: ReadingRevision: Reading• Kruse 4.5

PreparationPreparationNext lecture: Linked Stacks and Queues

• Read 3.1.6 in Kruse et al.