79
1 Working with Pointers Working with Pointers An exercise in destroying your computer

Working with Pointers

  • Upload
    rhett

  • View
    32

  • Download
    3

Embed Size (px)

DESCRIPTION

Working with Pointers. An exercise in destroying your computer. What is this?. Your worst nightmare! Comes from pointer misuse. Let’s look at Memory! Blue is memory address, Black is value. 1 -4717. 2 -901. 3 76. 4 -0. 5 98131. 6 -1038. 7 -554. 8 7462. 9 312. 11 3619. 10 - PowerPoint PPT Presentation

Citation preview

Page 1: Working with Pointers

1

Working with PointersWorking with Pointers

An exercise in destroying your computer

Page 2: Working with Pointers

2

What is this?What is this?

• Your worst nightmare!• Comes from pointer misuse

Page 3: Working with Pointers

3

Let’s look at Memory!Let’s look at Memory!Blue is memory address, Black is value

1 -4717

2-901

3 76

4-0

5 98131

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 -4717

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 4: Working with Pointers

4

Declare an intDeclare an intint myInt;int myInt;

1 -4717

2-901

3 76

4-0

5 98131

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 myInt-4717

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 5: Working with Pointers

5

What’ve we done?What’ve we done?

• By declaring the int, we’ve taken up just enough memory to hold an int

• We don’t know where in memory (the address) that it’s located

• Computer picks “at random”• What value is at that memory

location?• Can we print that value out?

– The value –4717 would print! (garbage)

Page 6: Working with Pointers

6

Copy 42 into that Section of Copy 42 into that Section of MemoryMemory

myInt = 42;myInt = 42;1 -4717

2-901

3 76

4-0

5 98131

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 myInt 26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

42

Page 7: Working with Pointers

7

PointersPointers

• Allow us to get to the address of where information is located

• Similar to call forwarding– Ask the pointer where to go– Go there for the information

• To create a pointer, we use the *• Follows format of <data type> <name>;• Example: int* ptr;

Page 8: Working with Pointers

8

Declare an int pointerDeclare an int pointerint* ptr;int* ptr;

1 -4717

2-901

3 76

4-0

5 ptr98131

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 myInt42

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 9: Working with Pointers

9

Now what have we done?Now what have we done?

• Created a new variable of type ptr that points to an int

• Notice that we haven’t initialized the pointer to “point” to myInt yet

• What if we print the pointer out?

Page 10: Working with Pointers

10

cout << ptr;cout << ptr;(prints out value of ptr: 98131)

1 -4717

2-901

3 76

4-0

5 ptr98131

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 myInt42

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 11: Working with Pointers

11

ProblemProblem

• How do we get address of myInt so ptr can point to it?

• Remember, we can still access the value of myInt directlyint someInt = myInt;

• We really need the pointer to store the address of where myInt is located

• We do not need to store the value of myInt for the pointer (just the address)

Page 12: Working with Pointers

12

The & operatorThe & operator

• Use the & operator to get the address of where the variable is in memory

• What would the following statement print to the screen?

cout << &myInt << endl;

Page 13: Working with Pointers

13

What would happen?What would happen?cout << &myInt;cout << &myInt;

1 -4717

2-901

3 76

4-0

5 ptr98131

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 myInt42

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 14: Working with Pointers

14

Getting the Pointer to PointGetting the Pointer to Point

• We now need “ptr” to point to myInt

• Code:ptr = &myInt;

ptr is a pointer,so it expects anaddress to be assigned to it

Here, we get the address of wheremyInt is stored in memory and copythat value into “ptr”

Page 15: Working with Pointers

15

BeforeBefore

1 -4717

2-901

3 76

4-0

5 ptr98131

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 myInt42

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 16: Working with Pointers

16

AfterAfterptr = &myInt;ptr = &myInt;

1 -4717

2-901

3 76

4-0

5 ptr25

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 myInt42

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 17: Working with Pointers

17

What would this do?What would this do?ptr = myInt;ptr = myInt;

1 -4717

2-901

3 76

4-0

5 ptr98186

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 myInt42

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 18: Working with Pointers

18

Wrong AddressWrong Addressptr = myInt;ptr = myInt;

1 -4717

2-901

3 76

4-0

5 ptr42

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 myInt42

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 19: Working with Pointers

19

Tricky Screens of Death!Tricky Screens of Death!

• Last thing to learn is how to “de-reference” a pointer

• This means “how to follow the pointer”• Unfortunately, we use the * operator as

well• Example:

cout << *ptr << endl;

Follow wherever ptr is pointing to and print that value out.

Page 20: Working with Pointers

20

Follow the Pointer and Print it Follow the Pointer and Print it OutOut

cout << *ptr << endl;cout << *ptr << endl;1 -4717

2-901

3 76

4-0

5 ptr25

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 myInt42

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 21: Working with Pointers

21

Another ExampleAnother ExampleBlue is memory address, Black is value, Red is variable name

1 -4717

2-901

3 76

4-0

5 98131

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 -4717

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 22: Working with Pointers

22

Declare a PointerDeclare a Pointerint *ptr;int *ptr;

1 -4717

2-901

3 76

4-0

5 98131

6 -1038

7 -554

8 7462

9 312

10 ptr -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 -4717

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 23: Working with Pointers

23

What would happen?What would happen?cout << *ptr << endl;cout << *ptr << endl;

1 -4717

2-901

3 76

4-0

5 98131

6 -1038

7 -554

8 7462

9 312

10 ptr -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 -4717

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 24: Working with Pointers

24

Blue Screen of Death!Blue Screen of Death!

Page 25: Working with Pointers

25

• Because parameter passing only passes a copy so the function can’t change main’s variables!

void cannotChange (int x) {x = 6;cout << x << endl;

}void main ( ) {

int myInt = 17;cannotChange (myInt);cout << myInt << endl;

}

Why do I need Pointers?Why do I need Pointers?

0 1 2

3 4 5

6 7 8

-2 91 571

-2991 0 -33

41 61 -1

Page 26: Working with Pointers

26

• Because parameter passing only passes a copy so the function can’t change main’s variables!

void cannotChange (int x) {x = 6;cout << x << endl;

}void main ( ) {

int myInt = 17;cannotChange (myInt);cout << myInt << endl;

}

Declare myIntDeclare myInt

0 1 2

3 4 5

6 7 8

-2 91 571

-2991 0 -33

41 17 -1

myInt

Page 27: Working with Pointers

27

• Because parameter passing only passes a copy so the function can’t change main’s variables!

void cannotChange (int x) {x = 6;cout << x << endl;

}void main ( ) {

int myInt = 17;cannotChange (myInt);cout << myInt << endl;

}

Call the functionCall the function

0 1 2

3 4 5

6 7 8

-2 91 571

-2991 0 -33

41 17 -1

myInt

Page 28: Working with Pointers

28

• Because parameter passing only passes a copy so the function can’t change main’s variables!

void cannotChange (int x) {x = 6;cout << x << endl;

}void main ( ) {

int myInt = 17;cannotChange (myInt);cout << myInt << endl;

}

Here’s where the Copy is Here’s where the Copy is MadeMade

0 1 2

3 4 5

6 7 8

-2 17 571

-2991 0 -33

41 17 -1

myInt

x

Page 29: Working with Pointers

29

• Because parameter passing only passes a copy so the function can’t change main’s variables!

void cannotChange (int x) {x = 6;cout << x << endl;

}void main ( ) {

int myInt = 17;cannotChange (myInt);cout << myInt << endl;

}

Changing Only Local CopyChanging Only Local Copy

0 1 2

3 4 5

6 7 8

-2 6 571

-2991 0 -33

41 17 -1

myInt

x

Page 30: Working with Pointers

30

• Because parameter passing only passes a copy so the function can’t change main’s variables!void cannotChange (int x) {

x = 6;cout << x << endl;

}void main ( ) {

int myInt = 17;cannotChange (myInt);cout << myInt << endl;

}

Print Out Local Copy (6)Print Out Local Copy (6)

0 1 2

3 4 5

6 7 8

-2 6 571

-2991 0 -33

41 17 -1

myInt

x

Page 31: Working with Pointers

31

• Because parameter passing only passes a copy so the function can’t change main’s variables!

void cannotChange (int x) {x = 6;cout << x << endl;

}void main ( ) {

int myInt = 17;cannotChange (myInt);cout << myInt << endl;

}

Return to Main (print 17)Return to Main (print 17)(x is gone and leaves garbage)(x is gone and leaves garbage)

0 1 2

3 4 5

6 7 8

-2 6 571

-2991 0 -33

41 17 -1

myInt

Page 32: Working with Pointers

32

void canChange (int* x) {*x = 6;cout << *x << endl;

}void main ( ) {

int myInt = 17;int* ptr = &myInt;canChange (ptr);cout << myInt << endl;

}

Now with PointersNow with Pointers

0 1 2

3 4 5

6 7 8

-2 6 571

-2991 0 -33

41 412 -1

Page 33: Working with Pointers

33

void canChange (int* x) {*x = 6;cout << *x << endl;

}void main ( ) {

int myInt = 17;int* ptr = &myInt;canChange (ptr);cout << myInt << endl;

}

Declare myIntDeclare myInt

0 1 2

3 4 5

6 7 8

-2 6 571

-2991 0 -33

41 17 -1

myInt

Page 34: Working with Pointers

34

void canChange (int* x) {*x = 6;cout << *x << endl;

}void main ( ) {

int myInt = 17;int* ptr = &myInt;canChange (ptr);cout << myInt << endl;

}

Declare a Pointer to myIntDeclare a Pointer to myInt

0 1 2

3 4 5

6 7 8

7 6 571

-2991 0 -33

41 17 -1

myInt

ptr

Page 35: Working with Pointers

35

void canChange (int* x) {*x = 6;cout << *x << endl;

}void main ( ) {

int myInt = 17;int* ptr = &myInt;canChange (ptr);cout << myInt << endl;

}

Pass a Copy of ptrPass a Copy of ptr

0 1 2

3 4 5

6 7 8

7 6 571

-2991 0 -33

41 17 -1

myInt

ptr

Page 36: Working with Pointers

36

void canChange (int* x) {*x = 6;cout << *x << endl;

}void main ( ) {

int myInt = 17;int* ptr = &myInt;canChange (ptr);cout << myInt << endl;

}

Pass a Copy of ptrPass a Copy of ptr

0 1 2

3 4 5

6 7 8

7 6 7

-2991 0 -33

41 17 -1

myInt

ptr x

Page 37: Working with Pointers

37

void canChange (int* x) {*x = 6;cout << *x << endl;

}void main ( ) {

int myInt = 17;int* ptr = &myInt;canChange (ptr);cout << myInt << endl;

}

Change Whatever x is Pointing Change Whatever x is Pointing tootoo

0 1 2

3 4 5

6 7 8

7 6 7

-2991 0 -33

41 17 -1

myInt

ptr x

Page 38: Working with Pointers

38

void canChange (int* x) {*x = 6;cout << *x << endl;

}void main ( ) {

int myInt = 17;int* ptr = &myInt;canChange (ptr);cout << myInt << endl;

}

Change Whatever x is Pointing Change Whatever x is Pointing tootoo

0 1 2

3 4 5

6 7 8

7 6 7

-2991 0 -33

41 6 -1

myInt

ptr x

Page 39: Working with Pointers

39

void canChange (int* x) {*x = 6;cout << *x << endl;

}void main ( ) {

int myInt = 17;int* ptr = &myInt;canChange (ptr);cout << myInt << endl;

}

Follow x and Print it Out (6)Follow x and Print it Out (6)

0 1 2

3 4 5

6 7 8

7 6 7

-2991 0 -33

41 6 -1

myInt

ptr x

Page 40: Working with Pointers

40

void canChange (int* x) {*x = 6;cout << *x << endl;

}void main ( ) {

int myInt = 17;int* ptr = &myInt;canChange (ptr);cout << myInt << endl;

}

See the Change in main (6 See the Change in main (6 also)also)

0 1 2

3 4 5

6 7 8

7 6 7

-2991 0 -33

41 6 -1

myInt

ptr x

Page 41: Working with Pointers

41

void canChange (int* x) {*x = 6;cout << *x << endl;

}void main ( ) {

int myInt = 17;int* ptr = &myInt;canChange (ptr);cout << myInt << endl;

}

Interesting NoteInteresting Note

At this point, these two statements print out the same thing!

cout << *ptr << endl;cout << myInt << endl

So do these!

cout << ptr << endl;cout << &myInt << endl;WHY?

Page 42: Working with Pointers

42

Summary of PointersSummary of Pointers

• To understand pointers, you need to understand memory

• The & is the secret to it all!• Create and de-reference with *• Passing a pointer to a function

can make changes to the main

Page 43: Working with Pointers

Lists, Linked Lists, Stacks,Lists, Linked Lists, Stacks,and Queuesand Queues

Page 44: Working with Pointers

• A List is a collection of data elements in an array structure. From any given element in the array, the previous and next items can be obtained by moving to adjacent memory locations.

• A Linked List is a collection of data elements where the location of each element in memory is only recorded by a pointer from a previous element. The data elements may happen to be adjacent in memory, but you cannot rely on this.

What the is a Linked List?What the is a Linked List?

Page 45: Working with Pointers

What the is a Linked List?What the is a Linked List?

• List : (Array of 5 elements : simple integers)

• Linked List : (5 elements - all simple integers)

275569510122

22 101 695 55 27

Page 46: Working with Pointers

– Are just a sequence of data linked together in memory.– Each element in the list is made up of data plus a

pointer to the next element in the list.– Node is the common name used for this data plus the

associated pointer to the next item in the list.– However, many kilo-bytes of data : names, addresses,

student-ids, etc - could be stored at each Node.– A chain is only as strong as its weakest link. – Similarly, if something erased or corrupted a Node in

the middle of the list, then Nodes after this will NOT be found !

What the is a Linked List?What the is a Linked List?

Page 47: Working with Pointers

Queues : FIFO Vs LIFOQueues : FIFO Vs LIFO

• Queues can be of two main types :– FIFO - First In First Out

•The first item into the queue is the first to be processed or output.

•Example : Supermarket checkout queue.

– LIFO queue - Last In First Out.•The last item into the queue is the first

one out.•Example : Stack of paper - where you put

paper on top of each other and can only get to a page in the stack by removing each page on top of it in turn.

Page 48: Working with Pointers

Linked Lists - All are LIFO in Linked Lists - All are LIFO in this Lecturethis Lecture

• We are going to build all Linked Lists in this lecture on a LIFO queue, but we could have made them all FIFO instead.

• Exercise : As an exercise that will help you understand queues and Linked Lists, you can convert these examples to FIFO.

Page 49: Working with Pointers

Building a Basic Linked ListBuilding a Basic Linked List

#include <iostream.h> // for cin and cout.#include <mem.h> // for NULL.#include <stdio.h> // getchar

// Define the type of data to be stored at each nodetypedef float ListElement;

struct Node{ ListElement Value; // Data stored at this Node Node* Next; // Pointer to Next Node in the List};

Page 50: Working with Pointers

Basic Linked List - only 4 Basic Linked List - only 4 FunctionsFunctions

class Linked_List{ private: Node *First;

public: Linked_List(); // Constructor // Add Node into the start of a Linked List void Insert_Node (ListElement In_Value);};

Page 51: Working with Pointers

Basic Linked List - DiagramBasic Linked List - Diagram

• Once we have setup the class member functions and created a main() program to use our Linked List class, our Linked List could be visualized as :

Value 3

Next

FirstNULL

Value 1

Next

Value 2

Next

Page 52: Working with Pointers

Basic Linked List - Basic Linked List - ConstructorConstructor

Linked_List::Linked_List()// Constructor{ // Create an empty list First = NULL;}

Page 53: Working with Pointers

Basic Linked List - Insert Basic Linked List - Insert NodeNode

void Linked_List::Insert_Node (ListElement In_Value)

// Add Node into the start of a Linked List.

{

Node *New_Node = new Node;

// Store the value of the node.

New_Node->Value = In_Value;

// The Next Node in the list is currently the First Node.

New_Node->Next = First;

// Make our new node the new First Node.

First = New_Node;

}

Page 54: Working with Pointers

Basic Linked List - Basic Linked List - Overloaded Output << Overloaded Output <<

OperatorOperatorostream& operator << (ostream& out, const Linked_List& My_List)

{

Node *Curr_Node;

Curr_Node = My_List.First;

// Loop through all nodes in the list.

while (Curr_Node != NULL)

{

out << "\n" << Curr_Node->Value; // Display each Node's Value

Curr_Node = Curr_Node->Next; // Point at the next node in the list

}

out << endl;

return out;

}

Page 55: Working with Pointers

Basic Linked List - simple Main Basic Linked List - simple Main programprogram

void main(){ Linked_List My_List; // Define a Linked List

My_List.Insert_Node (1.1); // Add in a bunch of nodes My_List.Insert_Node (2.2); My_List.Insert_Node (3.3);

cout << "\n" << My_List; // Display the list}

Page 56: Working with Pointers

Basic Linked List - What really Basic Linked List - What really is happening?is happening?

Declaring the Linked List : Linked_List My_List; Would simply set First to NULL, so we have a pointer

pointing at the null memory address.Inserting the first node : My_List.Insert_Node

(1.1);

Inserting the second node :My_List.Insert_Node (2.2);1.1

NextFirst NULL

2.2

Next

First NULL1.1

Next

Page 57: Working with Pointers

Linked Lists - Find All Nodes Linked Lists - Find All Nodes with a particular Valuewith a particular Value

void Linked_List::Find_Nodes_With_Value (ListElement In_Value)

// Find ALL Nodes in a Linked List with a particular value stored in them.

{ // Allocate a working Node.

Node *Curr_Node = First; int Node_Count = 0; // Keep track of the number of Nodes found.

// Skip through the Linked List and list all nodes we find with the required value.

cout << "\nNodes with value of " << In_Value << " : ";

Page 58: Working with Pointers

Linked Lists - Find All Nodes Linked Lists - Find All Nodes with a particular Valuewith a particular Value

while (Curr_Node != NULL) { Node_Count++;

if (Curr_Node->Value == In_Value) cout << Node_Count << ", ";

Curr_Node = Curr_Node->Next; } if (Node_Count == 0) cout << "No Nodes Found !!";

cout << endl;}

Page 59: Working with Pointers

Linked Lists - Find All Nodes Linked Lists - Find All Nodes with a particular Valuewith a particular Value

• Example Usage :

Find_Nodes_With_Value (3.3);

• will display all Nodes in the linked list with a value of 3.3.

• If none are found, a “None Found” message will be displayed.

Page 60: Working with Pointers

Linked ListsLinked ListsDelete a Node from the ListDelete a Node from the List

• We will now look at a function to delete a particular node in the list.

• Remember: – When we insert a new node, it is our new

FIRST node.– We have a LIFO queue - Last In First Out.– So, when we delete node number 1, we are

actually deleting the first node in the list, which is the node most recently added to the list!

Page 61: Working with Pointers

Linked ListsLinked ListsDelete a Node from the List Delete a Node from the List

void Linked_List::Delete_Node (unsigned int Node_Num)// Delete a Node from a Linked List.{ // Allocate Del and Prev Nodes. Node *Del_Node = First; Node *Prev_Node = First;

// Skip through the Linked List to get to the required node. for (int i = 1; i < Node_Num; i++) { if (Del_Node->Next == NULL) // Stop when there are no more

nodes. break; Prev_Node = Del_Node; Del_Node = Del_Node->Next; }

Page 62: Working with Pointers

Linked ListsLinked ListsDelete a Node from the ListDelete a Node from the List

// Make sure we are deleting a valid Node. if (Node_Num > 0) { if (Node_Num == 1) // If we are deleting the first node, make First point at the second // node in the list. First = Del_Node->Next; else // Point the previous node to the node after the one to be

deleted. Prev_Node->Next = Del_Node->Next;

// Delete the required Node. delete Del_Node; }}

Page 63: Working with Pointers

Linked ListsLinked ListsDelete a Node from the ListDelete a Node from the List

Delete_Node (0)

will delete nothing. There is not a 0’th node number in the list.

Delete_Node (1)

will delete the first node in the list. (i.e. the last node inserted into the list, since nodes are inserted at the start of the list by the Insert function. Remember : LIFO!

Delete_Node (2)

will delete the second node. (i.e. the second last node inserted into the list).

Page 64: Working with Pointers

Linked ListsLinked ListsDelete a Node from the ListDelete a Node from the List

Delete_Node (50)

will delete the last node if there are 50 nodes in the list. (i.e. the very first node inserted into the list).

Delete_Node (99)

when there are only 50 nodes in the list will delete the last node in the list. (i.e. the very first node inserted into the list).

Page 65: Working with Pointers

Some Points on Linked ListsSome Points on Linked Lists

• When working with pointer data structures (such as Linked Lists) it is important that you use diagrams when developing or interpreting code, even experienced code writers make logic errors.

• Be very careful when inserting or deleting nodes so that links between nodes are not broken.

• It is crucial that you ensure your code works with all nodes - especially the first and last nodes in the list.

Page 66: Working with Pointers

Generic Linked List ClassGeneric Linked List Class

• Let’s start of with an extremely basic class - called Animals.

• Then, we will include this class in a Liked List class, and use the member functions of our Animals class to do any of the input / output for Animals.

• In this way, by changing a single line of code in our Linked List class, we can make our Linked List work with any kind of data in any kind of class.

Page 67: Working with Pointers

Generic Linked List - Animals Generic Linked List - Animals Class HeaderClass Header

class Animal{ private:

float Height;char Name[30];int Age;

public: Animal(); // Constructor.};

Page 68: Working with Pointers

Generic Linked List - Animals Generic Linked List - Animals Class Member FunctionsClass Member Functions

Animal::Animal() // Constructor.{ Anim_Height = 0; Anim_Name [0] = '\0'; Anim_Age = 0;}

Page 69: Working with Pointers

Generic Linked List - Basic Generic Linked List - Basic Linked List Class HeaderLinked List Class Header

typedef Animal List_Element;struct Node{ List_Element Value; // We are now storing Animals data Node* Next;};

class Linked_List{ private: Node *First; public: Linked_List(); // Constructor. void Insert_Node ();};

Page 70: Working with Pointers

Generic Linked List - Basic Generic Linked List - Basic Linked List Class Member Linked List Class Member

FunctionsFunctions

Linked_List::Linked_List() // Constructor. (Same as earlier in Lecture)

{ // Create an empty list. First = NULL;}

Page 71: Working with Pointers

Generic Linked List - Basic Generic Linked List - Basic Linked List Class Member Linked List Class Member

FunctionsFunctionsvoid Linked_List::Insert_Node () // Add Node into the start of a

Linked List{ // Allocate New Node. Node *New_Node = new Node;

// Get and store the value of the new node. // The Next Node in the list is the current First Node. New_Node->Next = First;

// Make our new node the new First Node. First = New_Node;}

Page 72: Working with Pointers

Generic Linked List - Main Generic Linked List - Main ProgramProgram

void main(){ Linked_List My_List; // Define a Linked List - of Animals

My_List.Insert_Node (); // insert an item in the list cout << "\n\n" << My_List; // Display the list}

Page 73: Working with Pointers

Classes of ClassesClasses of Classes

• We have a class (Animals) which has private data and various member functions.

• Then, we have included this in a Linked List class, with its own private data and its own member functions.

• When C++ notices that we are manipulating the Linked List, then the Linked Lists functions are called.

• If any of these Linked List functions manipulate any Animals data, then the Animals functions are called automatically by C++.

Page 74: Working with Pointers

• In exactly this way, classes can be combined and extremely complex systems can be built, and at each level there are member functions doing their job on the private data of the class they belong to.

• Some of the most complex human creations are built like this, for example :

–Operating Systems–Games–Business Applications–Network Software

Classes of ClassesClasses of Classes

Page 75: Working with Pointers

Very Brief Introduction toVery Brief Introduction to Stacks and Queues Stacks and Queues

• Stacks and Queues are both special types of data structures or ADTs that may be implemented as linked lists or using an array. These ADTs are very useful for many different type of programming problems.

• A stack’s data :o is always added (pushed) onto the front of

the list / array, and, o removed (popped) from the front of the list /

array.• This type of data structure is called a Last In

First Out (LIFO) structure. That is the last item added is the first removed.

Page 76: Working with Pointers

Stack as a linked listStack as a linked list

Data

Next

97

Data

Next

84

Data

Next

55

Null

Stack Top

Data Pushed on to store Poped off to retrieve

To get to a particular number in the stack, we would need to keep removing values off the top of the stack until we got to the required value.

Page 77: Working with Pointers

• In contrast, a queue’s data :

– is always added onto the end of the list, and,

–removed from the front of the list.

• This type of data structure is called a First In First Out (FIFO) structure.

• Just like a queue at a super-market checkout or a bank

QueuesQueues

Page 78: Working with Pointers

Queue as a linked listQueue as a linked list

Queue Front Pointer

Data Next

573 Null 573 -29 616

Queue Rear Pointer

To get to a particular number in the queue, we would need to keep removing values off the queue (i.e. to the left) until we got to the required value.

Page 79: Working with Pointers

79

End of LectureEnd of Lecture

• Next time, lists continued…