Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers and Linked Lists

Similar presentations


Presentation on theme: "Pointers and Linked Lists"— Presentation transcript:

1

2 Pointers and Linked Lists
Chapter 13 Pointers and Linked Lists

3 Overview 13.1 Nodes and Linked Lists 13.2 Stacks and Queues

4 13.2 Stacks and Queues

5 A Linked List Application
A stack is a data structure that retrieves data in the reverse order the data was stored If 'A', 'B', and then 'C' are placed in a stack, they will be removed in the order 'C', 'B', and then 'A' A stack is a last-in/first-out data structure like the stack of plates in a cafeteria; adding a plate pushes down the stack and the top plate is the first one removed Display 13.16

6 Program Example: A Stack Class
We will create a stack class to store characters Adding an item to a stack is pushing onto the stack Member function push will perform this task Removing an item from the stack is popping the the item off the stack Member function pop will perform this task contains the stack class interface Display 13.17

7 Using the stack Class demonstrates the use of the stack class
Display (1-2)

8 Function push The push function adds an item to the stack
It uses a parameter of the type stored in the stack void push(char the_symbol); Pushing an item onto the stack is precisely the same task accomplished by function head_insert of the linked list For a stack, a pointer named top is used instead of a pointer named head

9 Function pop The pop function returns the item that was at the top of the stack char pop( ); Before popping an item from a stack, pop checks that the stack is not empty pop stores the top item in a local variable result, and the item is "popped" by: top = top->link; A temporary pointer must point to the old top item so it can be "deleted" to prevent a memory leak pop then returns variable result

10 Empty Stack An empty stack is identified by setting the top pointer to NULL top = NULL;

11 The Copy Constructor Because the stack class uses a pointer and creates new nodes using new, a copy constructor is needed The copy constructor (a self-test exercise) must make a copy of each item in the stack and store the copies in a new stack Items in the new stack must be in the same position in the stack as in the original

12 The stack destructor Because function pop calls delete each time an item is popped off the stack, ~stack only needs to call pop until the stack is empty char next; while( ! empty ( ) ) { next = pop( ); }

13 stack Class Implementation
The stack class implementation is found in Display (1) Display (2)

14 A Queue A queue is a data structure that retrieves data in the same order the data was stored If 'A', 'B', and then 'C' are placed in a queue, they will be removed in the order ‘A', 'B', and then ‘C' A queue is a first-in/first-out data structure like the checkout line in a supermarket Display 13.20

15 Queue Class Implementation
The queue class is implemented in a manner similar to the stack except there are two pointers, one to track the front of the list, and one to track the back of the list Interface: Program using the Queue Class: Implementation: Display (1-2) Display (1-2) Display (1-3)

16 Section 13.2 Conclusion Can you
Give the definition of member function push? Create a definition for the stack class copy constructor? Know when to use a queue vs. a stack? Create a definition for the queue class copy constructor?

17 Chapter End

18 Display 13.1 Back Next

19 Display 13.2 Back Next

20 Display 13.3 Back Next

21 Display 13.4 Back Next

22 Display 13.5 Back Next

23 Display 13.6 Back Next

24 Display 13.7 Back Next

25 Display 13.8 Back Next

26 Display 13.9 Back Next

27 Display 13.10 Back Next

28 Display 13.11 Back Next

29 Display 13.12 Back Next

30 Display 13.13 Back Next

31 Display (1/2) Back Next

32 Display (2/2) Back Next

33 Display (1/3) Back Next

34 Display (2/3) Back Next

35 Display (3/3) Back Next

36 Display 13.16 Back Next

37 Display 13.17 Back Next

38 Display (1/2) Back Next

39 Display (2/2) Back Next

40 Display (1/2) Back Next

41 Display (2/2) Back Next

42 Display 13.20 Back Next

43 Display (1/2) Back Next

44 Display (2/2) Back Next

45 Display (1/2) Back Next

46 Display (2/2) Back Next

47 Display (1/3) Back Next

48 Display (2/3) Back Next

49 Display (3/3) Back Next


Download ppt "Pointers and Linked Lists"

Similar presentations


Ads by Google