Review Records Dynamic Memory and Pointers Introduction to Linked Lists - PowerPoint PPT Presentation

About This Presentation
Title:

Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Description:

Dynamic Memory and Pointers Introduction to Linked Lists Review: Records Records Within Records There is nothing to prevent us from placing records inside of records ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 46
Provided by: ccGatech9
Category:

less

Transcript and Presenter's Notes

Title: Review Records Dynamic Memory and Pointers Introduction to Linked Lists


1
Review RecordsDynamic Memory and
PointersIntroduction to Linked Lists
Lecture 8
2
(No Transcript)
3
Review Records
4
Records Within Records
LB
  • There is nothing to prevent us from placing
    records inside of records (a field within a
    record)
  • Date_Type definesa record
  • day, month, year isoftype num
  • Endrecord
  • Student_Type definesa record
  • name isoftype string
  • gpa isoftype num
  • birth_day isoftype Date_Type
  • graduation_day isoftype Date_Type
  • endrecord

5
Record Within Records
  • Date_Type
  • Student_Type
  • bob isoftype Student_Type
  • bob.birth_day.month lt- 6

birth_day
graduation_day
6
Types vs. Variables
  • TYPE Definitions
  • Create templates for new kinds of variables
  • Do not create a variable no storage space is
    allocated
  • Have unlimited scope
  • VARIABLE Declarations
  • Actually create storage space
  • Have limited scope - only module containing the
    variable can see it
  • Must be based on an existing data type

7
Dynamic Memory and Pointers
8
Dynamic vs. Static
  • Static (fixed in size)
  • Sometimes we create data structures that are
    fixed and dont need to grow or shrink.
  • Dynamic (change in size)
  • Other times, we want the ability to increase and
    decrease the size of our data structures to
    accommodate changing needs.

9
Static Data
  • Static data is data declared ahead of time.
  • It is declared in a module (or main algorithm)
    and lives for as long as that module is active.
  • If we declare more static variables than we need,
    we waste space.
  • If we declare fewer static variables than we
    need, we are out of luck.
  • Often, real world problems mean that we dont
    know how many variables to declare, as the number
    needed will change over time.

10
Dynamic Data
  • Dynamic data refers to data structures which can
    grow and shrink to fit changing data
    requirements.
  • We can allocate (create) additional dynamic
    variables whenever we need them.
  • We can de-allocate (kill) dynamic variables
    whenever we are done with them.
  • A key advantage of dynamic data is that we can
    always have a exactly the number of variables
    required - no more, no less.
  • For example, with pointer variables to connect
    them, we can use dynamic data structures to
    create a chain of data structures called a linked
    list.

11
Note
LB
  • Dynamic data gives us more flexibility
  • Memory is still limited
  • But now we can use it where we need it
  • And we can determine that while the program is
    running

Examples? Printer Queues Airliners uh,
everything?
12
A View of Memory
LB
13
A List Example
  • We must maintain a list of data
  • Sometimes we want to use only a little memory
  • Sometimes we need to use more memory
  • Declaring variables in the standard way wont
    work here because we dont know how many
    variables to declare
  • We need a way to allocate and de-allocate data
    dynamically (i.e., on the fly)

14
The Stack
  • Recall the activation stack
  • The stack can expand, but as for the data
  • Each frame contains static (fixed size) data

The number of variables needed come from
the isoftype statements.
15
The Stack and Heap
LB
12
Heap
Main this_var that_var
my_num_ptr
7
4
Stack
  • The heap is memory not used by the stack
  • As stack grows, heap shrinks
  • Static variables live in the stack
  • Dynamic variables live in the heap

16
What?
LB
  • We know (sort of) how to get a pointer variable
  • my_num_ptr isoftype Ptr toa Num
  • But how do we get it to point at something?

17
The Built-In Function NEW()
  • Takes a type as a parameter
  • Allocates memory in the heap for the type
  • Returns a pointer to that memory
  • my_num_ptr lt- new(Num)
  • dynamic_string lt- new(String)
  • list_head lt- new(Node)

18
Accessing Dynamic Data via Pointers
43
Heap Dynamic
Main my_num_ptr
Stack Static
  • When we follow a pointer, we say that we
    dereference that pointer
  • The carat () means dereference the pointer
  • my_num_ptr means follow my_num_ptr to wherever
    it points
  • My_num_ptr lt- 43 is valid

19
Pointer Animation of Numbers
Ptr1 isoftype Ptr toa Num Ptr2 isoftype Ptr toa
Num Ptr1 lt- new(Num) Ptr1 lt- 5 Ptr2 lt-
Ptr1 Print(Ptr1, Ptr2) Ptr2 lt- 7 Print(Ptr1,
Ptr2)

5 5
5 5 7 7
Num
Ptr

Ptr1
5
7
Ptr
Ptr2
static
dynamic
20
  • A record to hold two items of data - a name and a
    SSN
  • Student definesa record
  • name isoftype String
  • SSN isoftype num
  • endrecord
  • And a pointer to a Student record
  • current isoftype ptr toa Student
  • current lt- new(Student)

21
Pointers and Records
Bob
current
123456789
static
dynamic
current
22
Pointers and Records
Bob
current
123456789
static
dynamic
current
23
Pointers and Records
Bob
current
123456789
static
dynamic
current.name lt- Bob
24
Pointers and Records
Bob
current
123456789
static
dynamic
current.SSN lt- 123456789
25
Whats the big deal
LB
  • We already knew about static data
  • Now we see we can allocate dynamic data but
  • Each piece of dynamic data seems to need a
    pointer variable and pointers seem to be static
  • So how can this give me flexibility

26
Questions?
27
Introduction to Linked Lists
28
Properties of Lists
  • We must maintain a list of data
  • Sometimes we want to use only a little memory
  • Sometimes we need to use more memory
  • Declaring variables in the standard way wont
    work here because we dont know how many
    variables to declare
  • We need a way to allocate and de-allocate data
    dynamically (i.e., on the fly)

29
Linked Lists Live in the Heap
Heap
Stack
Main this_var
list_head
4
  • The heap is memory not used by the stack
  • Dynamic variables live in the heap
  • We need a pointer variable to access our list in
    the heap

30
Linked Lists
  • With pointers, we can form a chain of data
    structures
  • List_Node definesa Record
  • data isoftype Num
  • next isoftype Ptr toa List_Node
  • endrecord //List_Node

4
17
42
31
Linked List Record Template
  • ltType Namegt definesa record
  • data isoftype lttypegt
  • next isoftype ptr toa ltType Namegt
  • endrecord
  • Example
  • Char_Node definesa record
  • data isoftype char
  • next isoftype ptr toa Char_Node
  • endrecord

32
Creating a Linked List Node
  • Node definesa record
  • data isoftype num
  • next isoftype ptr toa Node
  • endrecord
  • And a pointer to a Node record
  • current isoftype ptr toa Node
  • current lt- new(Node)

33
Pointers and Linked Lists
current
static
dynamic
current
34
Accessing the Data Field of a Node
42
current
static
dynamic
current.data lt- 42 current.next lt- NIL
35
Proper Data Abstraction
Vs.
36
Complex Data Records and Lists
LB
  • The examples so far have shown a single num
    variable as node data, but in reality there are
    usually more, as in
  • Node_Rec_Type definesa record
  • this_data isoftype Num
  • that_data isoftype Char
  • other_data isoftype Some_Rec_Type
  • next isoftype Ptr toa Node_Rec_Type
  • endrecord // Node_Rec_Type

pda(5)
37
A Better Approach with Higher Abstraction
  • One should separate the data from the structure
    that holds the data, as in
  • Node_Data_Type definesa Record
  • this_data isoftype Num
  • that_data isoftype Char
  • other_data isoftype Some_Rec_Type
  • endrecord // Node_Data_Type
  • Node_Record_Type definesa Record
  • data isoftype Node_Data_Type
  • next isoftype Ptr toa Node_Rec_Type
  • endrecord // Node_Record_Type

38
Creating a Pointer to the Heap
?
Main list_head
  • list_head isoftype ptr toa List_Node
  • Notice that list_head is not initialized and
    points to garbage.

39
Creating a New Node in the List
?
Main list_head
  • list_head lt- new(List_Node)

40
Filling in the Data Field
?
42
Main list_head
  • list_head.data lt- 42
  • The operator follows the pointer into the heap.

41
Creating a Second Node
?
42
Main list_head
  • list_head.data lt- 42
  • list_head.next lt- new(List_Node)
  • The . operator accesses a field of the record.

42
Cleanly Terminating the Linked List
42
91
Main list_head
  • list_head.next.data lt- 91
  • list_head.next.next lt- NIL
  • We terminate linked lists cleanly using NIL.

43
Deleting by Moving the Pointer
42
91
Main list_head
  • If there is nothing pointing to an area of memory
    in the heap, it is automatically deleted.
  • list_head lt- list_head.next

44
Questions?
45
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com