23
7/31/2019 List and Pointers Lesson http://slidepdf.com/reader/full/list-and-pointers-lesson 1/23 List and Pointers

List and Pointers Lesson

Embed Size (px)

Citation preview

Page 1: List and Pointers Lesson

7/31/2019 List and Pointers Lesson

http://slidepdf.com/reader/full/list-and-pointers-lesson 1/23

List and Pointers

Page 2: List and Pointers Lesson

7/31/2019 List and Pointers Lesson

http://slidepdf.com/reader/full/list-and-pointers-lesson 2/23

• Programming languages can distinguish between different data

types such as:

 – Integers

 – Real numbers

 –

Strings• They also provide data structures such as:

 –  Arrays

 – Records

• OOPs have classes which are user-defined datatypes.

Introduction

Page 3: List and Pointers Lesson

7/31/2019 List and Pointers Lesson

http://slidepdf.com/reader/full/list-and-pointers-lesson 3/23

• Real-life problems require other structures to store data items called

lists.

•  A list that you might use in real life would be a shopping list.

• To define a list, we are going to view it as an abstract data type.

 –  A data type whose properties are specified independently of anyparticular programming language.

Introduction

Page 4: List and Pointers Lesson

7/31/2019 List and Pointers Lesson

http://slidepdf.com/reader/full/list-and-pointers-lesson 4/23

• Linear lists

 – Elements are stored in adjacent locations in memory

 – The compiler reserves the space for the array when it is declared

 – Therefore there is no way to increase or decrease the size of the array

in memory

• Linked lists

 –  Allocates memory space only during runtime, meaning that it is memory

efficient;

 –

Elements are known as nodes. – Each element of the list has an extra data field called a pointer that links

to the next node in the list.

Implementation

Page 5: List and Pointers Lesson

7/31/2019 List and Pointers Lesson

http://slidepdf.com/reader/full/list-and-pointers-lesson 5/23

•Operations that need to be performed on a list are: – Initialising the list

• Declare the array and initialise variable NumberOfElements 

• Declare the record and initialise start node

 – Inserting an element

• Stores the element in the next available space, then increment

NumberOfElements • Store new node directly after start node 

 – Deleting an element

• Find element to be deleted and move subsequent elements up a space, then

decrement NumberOfElements  

 – Finding an element

• Use linear search 

 – Getting the length of the list

• Output NumberOfElements  

 – Displaying the entire list

• Starting from the first element in the array, output each element until the end

of the array is reached

Introduction

Page 6: List and Pointers Lesson

7/31/2019 List and Pointers Lesson

http://slidepdf.com/reader/full/list-and-pointers-lesson 6/23

Linked lists

• Elements are known as nodes. 

• In linked lists, elements do not have to be stored adjacently in memory.

• Each element of the list has an extra data field called a pointer that links to

the next node in the list.

Page 7: List and Pointers Lesson

7/31/2019 List and Pointers Lesson

http://slidepdf.com/reader/full/list-and-pointers-lesson 7/23

A node

Data fieldPointer 

Bananas 21

Node address

Page 8: List and Pointers Lesson

7/31/2019 List and Pointers Lesson

http://slidepdf.com/reader/full/list-and-pointers-lesson 8/23

Inserting a node

Bananas 21

Start node

Carrots •2Start 1

Pears •4 2

NumberOfElements

Page 9: List and Pointers Lesson

7/31/2019 List and Pointers Lesson

http://slidepdf.com/reader/full/list-and-pointers-lesson 9/23

Inserting a node

Bananas 21 Carrots •2Start 4

Pears •4 2

NumberOfElements

Page 10: List and Pointers Lesson

7/31/2019 List and Pointers Lesson

http://slidepdf.com/reader/full/list-and-pointers-lesson 10/23

Inserting a node

Bananas 21 Carrots •2Start 4

Pears 14 2

NumberOfElements

Page 11: List and Pointers Lesson

7/31/2019 List and Pointers Lesson

http://slidepdf.com/reader/full/list-and-pointers-lesson 11/23

Inserting a node

Bananas 21

Carrots •2

Start 4 Pears 14

3

NumberOfElements

Page 12: List and Pointers Lesson

7/31/2019 List and Pointers Lesson

http://slidepdf.com/reader/full/list-and-pointers-lesson 12/23

Deleting a node

Bananas 21

Carrots •2

Start 4 Pears 14

3

NumberOfElements

Page 13: List and Pointers Lesson

7/31/2019 List and Pointers Lesson

http://slidepdf.com/reader/full/list-and-pointers-lesson 13/23

Deleting a node

Bananas 21

Carrots •2

Start 1 Pears 14

3

NumberOfElements

Page 14: List and Pointers Lesson

7/31/2019 List and Pointers Lesson

http://slidepdf.com/reader/full/list-and-pointers-lesson 14/23

Deleting a node

Bananas 21 Carrots 32Start 1

Pears •4 2

NumberOfElements

Page 15: List and Pointers Lesson

7/31/2019 List and Pointers Lesson

http://slidepdf.com/reader/full/list-and-pointers-lesson 15/23

Displaying the entire list

Bananas 21 Carrots 32Start 1

1

CurrentNode Output

Page 16: List and Pointers Lesson

7/31/2019 List and Pointers Lesson

http://slidepdf.com/reader/full/list-and-pointers-lesson 16/23

Displaying the entire list

Bananas 21 Carrots 32Start 1

1 Bananas

CurrentNode Output

Page 17: List and Pointers Lesson

7/31/2019 List and Pointers Lesson

http://slidepdf.com/reader/full/list-and-pointers-lesson 17/23

Displaying the entire list

Bananas 21 Carrots 32Start 1

2

CurrentNode Output

Page 18: List and Pointers Lesson

7/31/2019 List and Pointers Lesson

http://slidepdf.com/reader/full/list-and-pointers-lesson 18/23

Displaying the entire list

Bananas 21 Carrots 32Start 1

2 Carrots

CurrentNode Output

Page 19: List and Pointers Lesson

7/31/2019 List and Pointers Lesson

http://slidepdf.com/reader/full/list-and-pointers-lesson 19/23

Finding an element

Bananas 21 Carrots 32Start 1

1

CurrentNode Output

Page 20: List and Pointers Lesson

7/31/2019 List and Pointers Lesson

http://slidepdf.com/reader/full/list-and-pointers-lesson 20/23

Finding an element

Bananas 21 Carrots 32Start 1

1 Bananas

CurrentNode Output

Carrots

Input

= ?

Page 21: List and Pointers Lesson

7/31/2019 List and Pointers Lesson

http://slidepdf.com/reader/full/list-and-pointers-lesson 21/23

Finding an element

Bananas 21 Carrots 32Start 1

1 Bananas

CurrentNode Output

Carrots

Input

= ?

×

Page 22: List and Pointers Lesson

7/31/2019 List and Pointers Lesson

http://slidepdf.com/reader/full/list-and-pointers-lesson 22/23

Finding an element

Bananas 21 Carrots 32Start 1

2 Carrots

CurrentNode

Carrots

InputOutput

= ?

☺ 

Page 23: List and Pointers Lesson

7/31/2019 List and Pointers Lesson

http://slidepdf.com/reader/full/list-and-pointers-lesson 23/23

Free memory, the heap and pointers

• Many languages have a pointer type as a built-in data type.

 –  A variable that stores an address of a data value

• When a pointer variable is declared in a program, the compiler will

reserve a space in memory for the pointer but no space is reserved

for the values it can point to.

• The memory space is allocated when required at run time from a

pool of memory locations called the heap.

• This is known as dynamic allocation.