Download presentation
Presentation is loading. Please wait.
1
Computer Science 2 Dynamics
2
Learning Objectives Be able to read a program that uses dynamic data structures Be able to fix a program that uses dynamic data structures Be able to write a program that uses dynamic data structures. Be able to add to and remove from a stack.
3
What can you use when you have…
A large group of similar information? You want to use the information more than once. You don’t know how many items there will be You want fast access (too fast for files)
4
Or What can you use to link different pieces of information together? Like relationships, passageways from one room to another, … Why can’t we use the ^ for exponents. How can you model a line of people, items, components,… where you make a variable when you add someone to the line, and free up space when a person is served?
5
Pointers/Dynamics Lets you dynamically (while the program is running) create variables. Lets you link/connect variables in non-linear ways. Lets you dynamically erase variables when you are finished with them.
6
Pointer (Definition) A dynamic data structure that allows the program to create, link and dispose memory locations. Dynamic: Creating and disposing while the program is running. Data Structure: A place to save information. (We’ll be using records)
7
Declaring the data structure
Type pointertype = ^ NameRecType; NameRecType = record name:string; next:pointertype; {This is how the data is connected} end; Var top, temp:pointertype; These work a lot like file pointers.
8
Commands New(PointerVariable); Dispose(PointerVariable);
Allocates memory and points PointerVariable to it. Dispose(PointerVariable); Disposes the information where PointerVariable is pointing. There must be something to dispose in order to dispose it. Otherwise Crash!
9
Other pointer stuff nil ^
A pointer constant. It is used to initialize pointers and check if you are at the end of a list (Like EOF in files.) ^ Used to access the information being pointed to.
10
How do you link them together?
top Linked lists Stacks Models hiring/firing Lines (Queues) Circularly linked lists Binary trees Sparse Arrays nil A B C D
11
Stacks Draw a stack using Top and Temp pointers
How would you start a stack? (Initialize) How would you add to the stack?
12
Starting and Adding Start Adding (Pushing) Top :=nil; Make the memory
Put stuff in it Connect to the top Update the top
13
To code Begin Top:=nil;{Start/Initialize the list}
New(temp);{Creates memory} Writeln(‘Please enter name’); Readln(temp^.name); {Put stuff in} Temp^.next:=top; {Connect to top} Top:=temp; {Update top}
14
Adding a bunch to the stack?
Begin Top:=nil;{Start/Initialize the list} Writeln(‘Please enter name -1 to quit’); Readln(shuttle); while shuttle<> ‘-1’ do begin New(temp); Temp^.name:=shuttle; Temp^.next:=top; {Connect to top} Top:=temp; {Update top} End; Type pointertype = ^ NameRecType; NameRecType = record name:string; next:pointertype; end; Var top, temp:pointertype; shuttle:string;
15
Remove from a stack (Pop)
How can you do it and not lose the stack? Point to the person to be fired. Update the top dispose
16
Pop Temp:=top; Top:=top^.next Dispose(temp);
What could go wrong with the above code? How can you fix it? If top<>nil then begin Temp:=top; Top:=top^.next Dispose(temp); End;
17
Quick review Declarations Nil New() Dispose() Pop Push
Declare pointer type, then the record it will point to. Nil Like eof New() Allocates memory Dispose() Disposes the info where pointer is pointing Pop Remove from a stack Push Add to a stack
18
Stack Define a stack. Show a stack after the following actions
Push: 5, 8, 15, 31 Pop: 2 values Push: 4, 26, 12 Pop: One value
19
Warm-up Write the declarations (Type and Var) for a stack that will hold the following Name, Age, Phone Number Write the code for the following Initialize the stack Add one value to the stack Remove one value from the stack.
20
Declarations Type Var pntrType = ^ rectype; Rectype = record End;
Name:string; Age:integer; Phone:string; Next:pntrType; End; Var Top, temp: pntrtype;
21
Begin Top:= nil Writeln(‘Please enter your name’); New(temp);
readln(temp^.name); Temp^.next:= top; Top:= temp; //Remove If top<> nil then Temp:= top; Top:= top^.next; Dispose(temp); End;
22
Quick review Declarations Nil New() Dispose() Pop Push
Declare pointer type, then the record it will point to. Nil Like eof New() Allocates memory Dispose() Disposes the info where pointer is pointing Pop Remove from a stack Push Add to a stack
23
What do you recall about…
Dynamics Pointer New Dispose Next ^ Stack queue
24
Warm-up Show the stack after completing the following
Push: 10, 30, 15, 20 Pop two values Push: 90, 80, 100 Pop one value
25
How do you show the list? Temp:=top; While temp<>nil do Begin
Writeln(temp^.name); Temp:=temp^.next; End;
26
How do you delete the list?
Dispose(top); {What’s wrong with this?} Temp:=top; While temp<>nil do Begin Top:=temp^.next; Dispose(temp); End;
27
Don’t do this Writeln(top);
Dispose a pointer that is pointing to nothing Leave information lost in space… Keep something pointing to it, so you can find it.
28
Review Dynamics/pointers: Let you create and dispose memory on the fly. Uses the ^ to point Stack: Last in First Out (LIFO) Nil is like the end of file.
29
Dry Run procedure c(var top:ptrtype); var temp:ptrtype; begin
Pascal 2: Pointer Worksheet #2 program DryRunPointers; type ptrtype = ^rectype; rectype = record score:integer; next:ptrtype; end; var top:ptrtype; procedure a(var top:ptrtype); temp:ptrtype; count:integer; begin for count:= 1 to 4 do new(temp); temp^.score:= 3*count; Temp^.next:= top; top:=temp; procedure b(top:ptrtype); while top<> nil do writeln(top^.score); top:=top^.next; procedure c(var top:ptrtype); var temp:ptrtype; begin temp:=top; top:=top^.next; dispose(temp); end; procedure d(var top:ptrtype); while temp^.next<> nil do temp:=temp^.next; temp^.next:=top; begin {Of the main body}; top:=nil; a(top); b(top); c(top); d(top); End. Dry Run
30
FixStack1.pas on the class website.
program fixstaque; type recType = record number:integer; end; pointertype = *rectype; var top:integer; temp:pointertype; begin {Add a number into the stack} new(temp); new(temp2); new(temp3); temp^.1:= 5; temp2:= 8; temp 3 = 14; for top:= 1 to 3 do writeln(top); top:= top+1; end. Fix the following FixStack1.pas on the class website. It is supposed to put three values into a stack and then show them from the stack.
31
Program options Create, show and delete a stack of names and ages using the following main body (You write the procedures) Top:=nil; Create(top);{Adds an unknown # of people} Show(top); {Shows the list} Delete(top); {Deletes the entire list} Like the first option, but add a loop and a menu for each of the options. Like the first option, but show the information in reverse order. Write a recursive show procedure.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.