Lecture 04 Components of Programming Languages, Part III

Slides:



Advertisements
Similar presentations
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Advertisements

David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
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.
S-1 University of Washington Computer Programming I Lecture 18: Structures © 2000 UW CSE.
Grade 12 Computer Studies HG
1 Data Abstractions Shyh-Kang Jeng Department of Electrical Engineering/ Graduate Institute of Communication Engineering National Taiwan University.
Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2.
Introduction to Data Structures Fall 2008 Dr. David A. Gaitros
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11. The Struct Data Type.
Lecture 26: Structures / struct s/. 2 Lecture Contents: t Basics of structs t Struct type definition ( struct reserved word) t Struct type definition.
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Lecture 23: Pointers. 2 Lecture Contents: t Pointers and addresses t Pointers and function arguments t Pointers and arrays t Pointer arrays t Demo programs.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
CNG 140 C Programming (Lecture set 12) Spring Chapter 13 Dynamic Data Structures.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
Structures CSE 2031 Fall March Basics of Structures (6.1) struct point { int x; int y; }; keyword struct introduces a structure declaration.
1 Structures & Unions. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc)
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
C Structures and Memory Allocation
(to be given after OOP lectures)
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Type Checking, and Scopes
Data Structure Interview Question and Answers
Programming Abstractions
CS150 Introduction to Computer Science 1
Chapter 8: Data Abstractions
Stacks Chapter 4.
CSCE 210 Data Structures and Algorithms
CMSC 341 Lecture 5 Stacks, Queues
Implementing Subprograms
Lecture 9 Structure 1. Concepts of structure Pointers of structures
1. Structures 2. Abstract Data Types
Implementing Subprograms
Chapter 8: Data Abstractions
Introduction to Data Structures
Introduction to Programming
Classes and Objects.
CS111 Computer Programming
Chapter 16 Linked Structures
Structures CSE 2031 Fall February 2019.
Pointers & Dynamic Data Structures
Java Programming Language
Course Overview PART I: overview material PART II: inside a compiler
Lec17 Structs.
Chapter 9: Pointers and String
Structures CSE 2031 Fall April 2019.
Structures CSE 2031 Fall May 2019.
Dynamic allocation (continued)
Structure (i.e. struct) An structure creates a user defined data type
Structures EECS July 2019.
C Structures and Memory Allocation
Implementing Subprograms
Structures, Unions, and Enumerations
Presentation transcript:

Lecture 04 Components of Programming Languages, Part III COS220 Concepts of PLs AUBG, COS dept Lecture 04 Components of Programming Languages, Part III (Selected Data Structures) Reference: R.Sebesta, Chapters 5, 6 8/22/2018 Assoc. Prof. Stoyan Bonev

Assoc. Prof. Stoyan Bonev Lecture Contents: Structures as opposed to Arrays Presentation in C++, VB, C#, Java List Abstract Data Entities Stack Queue Deque Binary Tree 8/22/2018 Assoc. Prof. Stoyan Bonev

Structures as opposed to Arrays Array: a collection of data items of the same type. Simple data type: Data type used to store a single value. Structure: A collection of simple variables. Arrays are data structures to store a collection of data items of the same type. Structures as opposed to arrays, are data structures to store collections of related data items that have different types. The individual component of a structure is called a member. 8/22/2018 Assoc. Prof. Stoyan Bonev

Assoc. Prof. Stoyan Bonev Structures in C++ l 8/22/2018 Assoc. Prof. Stoyan Bonev

Structure definition (declaration) structure specifier – no storage occupying sample struct Person { char name[20];// string name; char gender; int age; float grade; }; 8/22/2018 Assoc. Prof. Stoyan Bonev

Structure definition (declaration) OR 8/22/2018 Assoc. Prof. Stoyan Bonev

Structure definition (declaration) Applying typedef reserved word typedef struct { char name[20];// string name; char gender; int age; float grade; } Person; 8/22/2018 Assoc. Prof. Stoyan Bonev

Structure definition (declaration) structure specifier – applying struct reserved word. struct Date { int day, month, year; }; Template only which does not occupy storage /memory cells/. 8/22/2018 Assoc. Prof. Stoyan Bonev

Referencing a structure type How to define a structure as a variable to be a scalar, an array or a pointer? Date a, b; Date c[20]; Date *ptr1; 8/22/2018 Assoc. Prof. Stoyan Bonev

Referencing a structure type How to define a structure as a variable to be a scalar, an array or a pointer? Person John, Mary; Person ClsCos220a[23]; Person *ptr2; 8/22/2018 Assoc. Prof. Stoyan Bonev

Initializing structure variables Date today = { 25, 09, 2014 }; Person peter = {“Miller”,‘m’, 19, 3.48 }; 8/22/2018 Assoc. Prof. Stoyan Bonev

Accessing structure members Direct component selection operator . (period) a.day=26; b.month=11; c[10].year=2014; Indirect component selection operator ─> ptr1 = &a; ptr1─>month = 3; 8/22/2018 Assoc. Prof. Stoyan Bonev

Structures within structures struct Dimension { int feet; float inches; }; struct Room { Dimension length, width; }; Room kitchen; kitchen.length.feet = 12; kitchen.length.inches = 2.4; kitchen.width.feet = 10; kitchen.width.inches = 1.8; 8/22/2018 Assoc. Prof. Stoyan Bonev

Functions returning structures struct Point { int x; int y; }; Point makepoint( int xpar, int ypar) { Point temp; temp.x = xpar; temp.y = ypar; return temp; } void main() { Point a, b; a = makepoint(0,0); b = makepoint(350, 200); … 8/22/2018 Assoc. Prof. Stoyan Bonev

Passing structure variables as arguments to functions struct Point { int x; int y; }; struct Rect { Point pt1, pt2; }; bool isPtinRect( Point p, Rect r) { return p.x>=r.pt1.x && p.x<=r.pt2.x && p.y>=r.pt1.y && p.y<=r.pt2.y ; } 8/22/2018 Assoc. Prof. Stoyan Bonev

Functions with structures as arguments returning structures struct Point { int x; int y; }; Point addpoint( Point p1, Point p2) { Point temp; // alternate version temp.x = p1.x + p2.x; // p1.x = p1.x + p1.y; temp.y = p1.y + p2.y; // p1.y += p2.y; return temp; // return p1; } void main() { Point a, b, c; a=makepoint(0,0); b=makepoint(40, 20); c=addpoint(b,makepoint(20, 40)); . . . } 8/22/2018 Assoc. Prof. Stoyan Bonev

Parallel arrays and arrays of structures Problem: data base for COS220students Solution based on parallel arrays: Alternate solution: based on array of structures: 8/22/2018 Assoc. Prof. Stoyan Bonev

Parallel arrays and arrays of structures Problem: data base for COS220 students Solution based on parallel arrays: char *name[23];int id[23];double gpa[23]; Three parallel arrays because data items with same subscript pertain the same student name[I] = “Peter”; id[I] = 0100245678; gpa[I] = 3.68; 8/22/2018 Assoc. Prof. Stoyan Bonev

Parallel arrays and arrays of structures Problem: data base for COS220 students Alternate solution: based on array of structures: struct Stud{char *name; int id; double gpa;}; Stud ar[23]; . . . ar[I].name = “Peter”; ar[I].id = 0100245678; ar[I].gpa = 3.68; 8/22/2018 Assoc. Prof. Stoyan Bonev

Pointers to structures struct Person { char *name, gender; int age;}; Person Mary, family[5], *ps; for (ps=family; ps<family+5; ps++) // or for (ps=&family[0]; ps<=&family[4]; ps++) { (*ps).name = … or ps->name = … (*ps).gender = … or ps->gender = … (*ps).age = … or ps->age = … } 8/22/2018 Assoc. Prof. Stoyan Bonev

Assoc. Prof. Stoyan Bonev Struct Employee // Definition of struct employee struct employee { int id; string name; char gender; int numDepend; money rate; money totWages; }; 8/22/2018 Assoc. Prof. Stoyan Bonev

Assoc. Prof. Stoyan Bonev Employee variables // Define two struct employee variables employee organist, janitor; 8/22/2018 Assoc. Prof. Stoyan Bonev

Assoc. Prof. Stoyan Bonev Anonymous structure // Definition of anonymous structure struct { int id; string name; char gender; int numDepend; money rate; money totWages; } organist, janitor; 8/22/2018 Assoc. Prof. Stoyan Bonev

9.8 Structs as Operands and Arguments How to do arithmetic and other operations using structs Process entire struct using programmer defined functions Often better to pass an entire structure rather than individual elements 8/22/2018 Assoc. Prof. Stoyan Bonev

Struct Copy or Assignment struct copies Given employee structure employee organist, janitor; organist = janitor; 8/22/2018 Assoc. Prof. Stoyan Bonev

Assoc. Prof. Stoyan Bonev Structures in VB l 8/22/2018 Assoc. Prof. Stoyan Bonev

Structure definition (declaration) structure specifier Structure Person Dim name as String Dim gender As Char Dim age As Integer Dim grade As Single End Structure 8/22/2018 Assoc. Prof. Stoyan Bonev 27

The structure template The structure should be globally defined. It is recommended The structure template to be typed outside any Sub or Function body. Globally defined structure template makes it visible in all Subs/Functions that follow the definition. 8/22/2018 Assoc. Prof. Stoyan Bonev

Accessing structure members Structure Person Dim name As String,gender As Char, _ age As Integer, grade As Single End Structure Direct component selection operator . (period) Dim John, Mary As Person John.name=“Petersen” :Mary.name=“Brown” John.gender=‘m’ :Mary.gender=‘f’ John.age=19 :Mary.age=21 John.grade=3.45 :Mary.grade=3.87 8/22/2018 Assoc. Prof. Stoyan Bonev

Accessing structure members Structure Person Dim name As String, gender As Char, _ age As Integer, grade As Single End Structure Direct component selection operator . (period) Dim p(10) As Person p(5).name = "stoyan" p(5).gender = ‘m’ p(5).age = 59 p(5).grade = 3.55 Console.WriteLine("{0} {1} {2} {3}",_ p(5).name, p(5).gender, p(5).age, p(5).grade) 8/22/2018 Assoc. Prof. Stoyan Bonev

Functions returning structures Structure Point Dim x, y As Integer End Structure Sub Main() Dim a, b As Point a = makepoint(0, 0) b = makepoint(350, 200) Console.WriteLine("Point a coordinates are: {0} , {1}", a.x, a.y) Console.WriteLine("Point b coordinates are: {0} , {1}", b.x, b.y) Console.ReadLine() End Sub Function makepoint(ByVal px As Integer, ByVal py As Integer) As Point Dim temp As Point temp.x = px temp.y = py Return temp End Function 8/22/2018 Assoc. Prof. Stoyan Bonev

Functions with structures as arguments returning structures Structure Point Dim x, y As Integer End Structure Sub Main() Dim a, b, c As Point a = makepoint(20, 30) : b = makepoint(350, 200) c = addpoints(a, b) Console.WriteLine("Point c coordinates are: {0} , {1}", c.x, c.y) Console.ReadLine() End Sub Function makepoint(ByVal px As Integer, ByVal py As Integer) As Point Dim temp As Point temp.x = px : temp.y = py : Return temp End Function Function addpoints(ByVal p1 As Point, ByVal p2 As Point) As Point temp.x = p1.x + p2.x temp.y = p1.y + p2.y Return temp 8/22/2018 Assoc. Prof. Stoyan Bonev

Assoc. Prof. Stoyan Bonev Structure Employee ‘ Definition of structure employee Structure employee Dim id As Integer Dim name, gender As String Dim Salary As Single End Structure ‘ Definition of two employee variables Dim organist, janitor As employee 8/22/2018 Assoc. Prof. Stoyan Bonev

Accessing Members of a structure Members are accessed using the member access operator, a period . For structure variable s and member variable m to access m you would use the following: Console.Writeline(“{0}”, s.m) 8/22/2018 Assoc. Prof. Stoyan Bonev

Accessing Members of a structure organist.id = 1234 organist.name = “Noel Goddard” organist.gender = ‘F’ organist.salary = 500.6 organist.salary = organist.salary + 100 organist.salary += 50 8/22/2018 Assoc. Prof. Stoyan Bonev

Structures as Operands and Arguments How to do arithmetic and other operations using structures Process entire structure using programmer defined functions Often better to pass an entire structure rather than individual elements 8/22/2018 Assoc. Prof. Stoyan Bonev

Structure Copy or Assignment structure copies Given employee structure Dim organist, janitor As employee organist = janitor 8/22/2018 Assoc. Prof. Stoyan Bonev

Structures within structures Structure Dimension Dim feet As Integer, inches As Double End Structure Structure Room Dim length, width As Dimension Dim kitchen As Room kitchen.length.feet = 12 kitchen.length.inches = 2.4 kitchen.width.feet = 10 kitchen.width.inches = 1.8 8/22/2018 Assoc. Prof. Stoyan Bonev

Assoc. Prof. Stoyan Bonev Example Structure FullName Dim firstName As String Dim lastName As String End Structure Structure Student Dim name As FullName Dim credits() As Integer Structure FullName contained, or nested, inside Student 8/22/2018 Assoc. Prof. Stoyan Bonev

Assoc. Prof. Stoyan Bonev Private Sub btnGet_Click(...) Handles btnGet.Click Dim numYears, i As Integer Dim person As Student … person.name.firstName = InputBox("First Name:") person.name.lastName = InputBox("Second Name:") End Sub 8/22/2018 Assoc. Prof. Stoyan Bonev

Passing structure variables as arguments to functions Structure Point Dim x, y As Integer End Structure Structure Rect Dim pt1, pt2 As Point Function isPtInRect( p As Point, r As Rect) As Boolean Return p.x >= r.pt1.x AND p.x <= r.pt2.x AND _ p.y >= r.pt1.y AND p.y <= r.pt2.y End Function 8/22/2018 Assoc. Prof. Stoyan Bonev

Parallel arrays and arrays of structures Problem: data base for COS220 students Solution based on simple scalar variables Solution based on parallel arrays Alternate solution: array of structures 8/22/2018 Assoc. Prof. Stoyan Bonev

Parallel arrays and arrays of structures Problem: data base for COS220 students Suppose that you want to evaluate the exam grades for 20 students and to display the names of the students whose scores are above average. Using simple variables would mean declaring 20 variables for the students’ names and 20 variables for the students’ ids and 20 variables for the students’ scores. Very tedious! Dim student1 As String, id1 As Integer, score1 As Double Dim student2 As String, id2 As Integer, score2 As Double . . . Dim student20 As String, id20 As Integer, score20 As Double 8/22/2018 Assoc. Prof. Stoyan Bonev

Parallel arrays and arrays of structures Problem: data base for COS220 students Much easier with arrays – one array variable for the names, and one array variable for the ids, and one array variable for the scores. Dim name(19) As String Dim id(19) As Integer Dim score(19) As Double Much more convenient with array of structures 8/22/2018 Assoc. Prof. Stoyan Bonev

Parallel arrays and arrays of structures Problem: data base for COS220 students Solution based on parallel arrays: Dim name(19) As String Dim id(19) As Integer Dim score(19) As Double Three parallel arrays because data items with same subscript pertain the same student name(I) = “Peter” id(I) = 0100245678 score(I) = 3.68 8/22/2018 Assoc. Prof. Stoyan Bonev

Parallel arrays and arrays of structures Problem: data base for COS220 students Alternate solution: array of structures: Structure Student Dim name As String, id As Integer Dim score As Double End Structure Dim ar(19) As Student . . . ar(I).name = “Peter” ar(I).id = 0100245678 ar(I).score = 3.68 8/22/2018 Assoc. Prof. Stoyan Bonev

Assoc. Prof. Stoyan Bonev Structures in C# l 8/22/2018 Assoc. Prof. Stoyan Bonev

how to declare and use structs public struct DayOfYear { public int day; public int month; public int year; public DayOfYear(int a, int b, int c) day = a; month = b; year = c; } 8/22/2018 Assoc. Prof. Stoyan Bonev

how to declare and use structs DayOfYear today = new DayOfYear(24, 9, 2014); DayOfYear day1; DayOfYear daylast; daylast = new DayOfYear(); Console.WriteLine(); Console.WriteLine("{0} {1} {2} {0} {1} {2}", today.day, today.month, today.year); 8/22/2018 Assoc. Prof. Stoyan Bonev

how to declare and use structs Structs vs. Classes Structs may seem similar to classes, but there are important differences that you should be aware of. First of all, classes are reference types and structs are value types. By using structs, you can create objects that behave like the built-in types and enjoy their benefits as well. Heap or Stack? When you call the New operator on a class, it will be allocated on the heap. However, when you instantiate a struct, it gets created on the stack. 8/22/2018 Assoc. Prof. Stoyan Bonev

how to declare and use structs This example declares a struct with three members: a property, a method, and a private field. It creates an instance of the struct and puts it to use. 8/22/2018 Assoc. Prof. Stoyan Bonev

how to declare and use structs using System; struct SimpleStruct { private int xval; public int X get return xval; } set if (value < 100) xval = value; public void DisplayX() Console.WriteLine("The stored value is: {0}", xval); class TestClass public static void Main() SimpleStruct ss = new SimpleStruct(); ss.X = 5; ss.DisplayX(); 8/22/2018 Assoc. Prof. Stoyan Bonev

Assoc. Prof. Stoyan Bonev Structures in Java Not supported in Java! 8/22/2018 Assoc. Prof. Stoyan Bonev

Introduction to abstract data structures Arrays and Lists are basic fundamental data structured entities. Stack, Queue, Deque, Binary Tree are abstract data structured entities. Arrays and Lists serve as building elements to create abstract data structures. 8/22/2018 Assoc. Prof. Stoyan Bonev

Assoc. Prof. Stoyan Bonev Data Structures: List 8/22/2018 Assoc. Prof. Stoyan Bonev

Assoc. Prof. Stoyan Bonev Data Structures: List List or a linked list is a data structure that consists of a sequence of data records such that in each record or node there is a field that contains a reference (i.e., a link) to the next record in the sequence. A linked list whose nodes contain two fields: an integer value and a link to the next node Linked lists as well as arrays provide an easy implementation for important abstract data structures, including stacks, queues, associative arrays 8/22/2018 Assoc. Prof. Stoyan Bonev

Data Structures: Stack 8/22/2018 Assoc. Prof. Stoyan Bonev

Data Structures: Stack stack is a last in, first out (LIFO) data structure. A stack is characterized by only two operations: push and pop. The push operation adds an item to the top of the stack,. The pop operation removes an item from the top of the stack, and returns this value to the caller. 8/22/2018 Assoc. Prof. Stoyan Bonev

Data Structures: Queue 8/22/2018 Assoc. Prof. Stoyan Bonev

Data Structures: Queue A queue is a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. 8/22/2018 Assoc. Prof. Stoyan Bonev

Data Structures: Queue A queue A priority queue 8/22/2018 Assoc. Prof. Stoyan Bonev

Data Structures: Deque 8/22/2018 Assoc. Prof. Stoyan Bonev

Data Structures: Deque double-ended queue (or shortly deque, pronounced deck) is a data structure that implements a queue for which elements can only be added to or removed from the front (head) or back (tail). It is also often called a head-tail linked list. 8/22/2018 Assoc. Prof. Stoyan Bonev

Data Structures: Binary Tree 8/22/2018 Assoc. Prof. Stoyan Bonev

Data Structures: Binary Tree binary tree is a data structure in which each node has at most two child nodes, usually distinguished as "left" and "right". Nodes with children are parent nodes. Outside the tree, there is often a reference to the "root" node (the ancestor of all nodes) 8/22/2018 Assoc. Prof. Stoyan Bonev

Data Structures: Binary Tree Traverse strategies: Top-Down Root Top-Down traverse to Left sub tree Top-Down traverse to Right sub tree Mixed Mixed traverse to Left sub tree Mixed traverse to Right sub tree Bottom-Up Bottom-Up traverse to Left sub tree Bottom-Up traverse to Right sub tree 8/22/2018 Assoc. Prof. Stoyan Bonev

Thank You for Your attention