Computer Science 2 4-11-2014 Dynamics.

Slides:



Advertisements
Similar presentations
James Tam Linked Lists in Pascal Linked Lists In this section of notes you will learn how to create and manage a dynamic list.
Advertisements

Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Review C++ exception handling mechanism Try-throw-catch block How does it work What is exception specification? What if a exception is not caught?
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
Computer Science 2 Arrays of Records. First Record Program Input an unknown number of Team Names, School names, and Total scores. (While loop) –Output:
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
STACKS & QUEUES for CLASS XII ( C++).
Run-Time Environments Chapter 7
Data Structures Using C++ 2E
Review Array Array Elements Accessing array elements
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Pointers and Linked Lists
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Pointers and Linked Lists
Data Structure Interview Question and Answers
12 C Data Structures.
Stacks and Queues.
Pointers and Dynamic Variables
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Popping Items Off a Stack Lesson xx
Lesson Objectives Aims
Computer Science 2 Outline/Learning Objectives for the day:
How do you store a bunch of similar stuff?
Computer Science II First With files.
Computer Science II First With files.
Computer Science 2 Arrays of Records.
Continue on the Array of Records program
Computer Science 2 Arrays of Records.
Computer Science 2 Getting an unknown # of …. Into an array.
Computer Science Dynamics.
Computer Science Sorting.
Computer Science 1 1/19/2011 On the record (paper) write the following
Computer Science 2: Trees
Computer Science 2 Arrays of Records.
How do you store a bunch of similar stuff?
Deleting from a binary tree
CS 2 Records 2/22/2018.
Stacks and Queues.
Computer Science II Second With files.
Computer Science 2 More Trees.
Computer Science 1 while
Deleting from a binary tree
Computing Spans Given an an array X, the span S[i] of X[i] is
EE 312 Final Exam Review.
Welcome Back CS 2 2/4/2013 On the record (paper) write the following
Linked lists Prof. Noah Snavely CS1114
More Trees 5/9/2-017 Be able to dry run a program that uses trees
Continue on the Valentines program
Computer Science 2 Queue.
CSCS-200 Data Structure and Algorithms
Time to finish first stack program
Computer Science 2 Queue Day 2.
Computer Science 1 while
Dry Run Fix it Write a program
Computer Science
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
CS 2308 Final Exam Review.
LINEAR DATA STRUCTURES
Computer Science II First With files.
DATA STRUCTURES IN PYTHON
Chapter 14 Functions.
Presentation transcript:

Computer Science 2 4-11-2014 Dynamics

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.

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)

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?

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.

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)

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.

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!

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 get to information being pointed to.

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

Stacks Draw a stack using Top and Temp pointers How would you start a stack? (Initialize) How would you add to the stack?

Starting and Adding Start Adding (Pushing) Top :=nil; Make the memory Put stuff in it Connect to the top Update the top

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}

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;

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

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;

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

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

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.

Declarations Type Var pntrType = ^ rectype; Rectype = record End; Name:string; Age:integer; Phone:string; Next:pntrType; End; Var Top, temp: pntrtype;

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;

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

What do you recall about… Dynamics Pointer New Dispose Next ^ Stack queue

Warm-up Show the stack after completing the following Push: 10, 30, 15, 20 Pop two values Push: 90, 80, 100 Pop one value

How do you show the list? Temp:=top; While temp<>nil do Begin Writeln(temp^.name); Temp:=temp^.next; End;

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;

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.

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.

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

FixStack1.pas in the assignment folder. 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 in the assignment folder. It is supposed to put three values into a stack and then show them from the stack.

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.