Data Structures. BSc in Computer Science University of New York, Tirana. Assoc. Prof. Marenglen Biba 1-1

Similar documents
CS350: Data Structures Stacks

Lecture 2: Stacks and Queues

Distributed Systems Recitation 1. Tamim Jabban

Write a program to implement stack or any other data structure in Java ASSIGNMENT NO 15

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Distributed Systems Recitation 1. Tamim Jabban

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh

CSE 143 SAMPLE MIDTERM

Lecture 2: Implementing ADTs

Midterm Exam 2 CS 455, Spring 2011

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

Day 6. COMP1006/1406 Summer M. Jason Hinek Carleton University

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

CMPT 225. Lecture 9 Stack

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid. Fall 2018

CS 302 ALGORITHMS AND DATA STRUCTURES

Data Abstraction and Specification of ADTs

Algorithms & Datastructures Laboratory Exercise Sheet 1

What are Generics? e.g. Generics, Generic Programming, Generic Types, Generic Methods

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Largest Online Community of VU Students

Data Structures and Algorithms

Associate Professor Dr. Raed Ibraheem Hamed

CS 10, Fall 2015, Professor Prasad Jayanti

Lecture 4. The Java Collections Framework

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

csci 210: Data Structures Stacks and Queues

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

Chapter 4 Defining Classes I

EECS168 Exam 3 Review

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.

Introduction to Programming System Design CSCI 455x (4 Units)

CE 221 Data Structures and Algorithms

Apply to be a Meiklejohn! tinyurl.com/meikapply

Stacks and Queues. Chapter Stacks

Stacks Fall 2018 Margaret Reid-Miller

Review: Trees Binary Search Trees Sets in Java Collections API CS1102S: Data Structures and Algorithms 05 A: Trees II

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

CSE 143 SAMPLE MIDTERM SOLUTION

CSCI 136 Data Structures & Advanced Programming. Lecture 13 Fall 2018 Instructors: Bill 2

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

Introduction to Computer Science II (ITI 1121) Midterm Examination

CS 216 Exam 1 Fall SOLUTION

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

Building Java Programs

CSE 332: Data Structures. Spring 2016 Richard Anderson Lecture 1

Chapter 5. ADTs Stack and Queue

Computer Science 210: Data Structures

Stack and Its Implementation

CSCA48 Summer 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh University of Toronto Scarborough

Why Use Generics? Generic types Generic methods Bounded type parameters Generics, Inheritance, and Subtypes Wildcard types

The Java Collections Framework. Chapters 7.5

CSIS 10B Lab 2 Bags and Stacks

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Data Structure (CS301)

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

DATA STRUCTURES CHAPTER 1

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

Data Structures and Algorithms Winter Semester

== isn t always equal?

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

PROBLEM 1 : (Vocabulary: 8 points) For each of the words/phrases below, circle the denition that is the best description as it pertains in the context

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

CSC 1052 Algorithms & Data Structures II: Queues

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving

Interfaces & Generics

Collections Chapter 12. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

San José State University Computer Science Department CS49J, Section 3, Programming in Java, Fall 2015

OOP- 5 Stacks Individual Assignment 35 Points

Basic Data Structures 1 / 24

CSE 143, Winter 2010 Midterm Exam Wednesday February 17, 2010

(3-2) Basics of a Stack. Instructor - Andrew S. O Fallon CptS 122 (January 26, 2018) Washington State University

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CMSC Introduction to Algorithms Spring 2012 Lecture 7

Introduction to Programming Using Java (98-388)

Week 6. Data structures

Today s lecture. CS 314 fall 01 C++ 1, page 1

JAVA MOCK TEST JAVA MOCK TEST II

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

CHAPTER 7 OBJECTS AND CLASSES

Chapter 18: Stacks And Queues

CS24 Week 4 Lecture 2

ADTs, Arrays, Linked Lists

CE221 Programming in C++ Part 1 Introduction

CSCI387 Data Structure Fall. Aug. 21, 2012 Sung Hee Park Computer Science Virginia State University

ABSTRACT DATA TYPES (ADTS) COMP1927 Computing 2 16x1 Sedgewick Chapter 4

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Data Structures G5029

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

1. Stack Implementation Using 1D Array

Introduction to Computer Science II (ITI 1121) Midterm Examination

Transcription:

Data Structures BSc in Computer Science University of New York, Tirana Assoc. Prof. Marenglen Biba 1-1

General info Course : Data Structures (3 credit hours) Instructor : Assoc. Prof. Marenglen Biba Office : Faculty building 2 nd floor Office Hours : Wednesday 12-14 or by appointment Phone : 42273056 / ext. 112 E-mail : marenglenbiba@unyt.edu.al Course page : http://www.marenglenbiba.net/ds/ Course Location and Time Laboratory Room LAB3, Monday 14-17. 1-2

Course description This course couples work on program design, analysis, and verification with an introduction to the study of data structures. Data structures capture common ways to store and manipulate data, and they are important in the construction of sophisticated computer programs. Upon completion of this course, students should be able to: work on almost all widely used data structures. 1-3

Course Outcomes Upon course completion, students will have demonstrated the ability to do the following: understand the advantages of data structures as useful abstractions in programming understand the concept of a linked list structure, recite different implementations of linked lists. understand the concept of a LIFO structure; implement and use stacks understand the concept of a FIFO structure; implement and use queues understand the concept of the tree data structure; implement and use trees in applications understand the concept of hash tables; implement and use them effectively understand the concept of graphs; implement and use effectively understand algorithm analysis and evaluate performance 1-4

Required Readings Data Structures and Problem Solving Using Java, 4/E. Mark A. Weiss. Addison-Wesley, 2010. ISBN-10: 0321541405. 1-5

Content of the Course Introduction to Data Structures Algorithm analysis Linked Lists Stacks Queues Trees Hash tables Graphs Priority Queues 1-6

Assumptions for this Class Programming in Java 1-7

Grading Policy Project 40% Midterm 30% Final 30% 1-8

Start studying now Recommendations Do not be shy! Ask any questions that you might have. Every questions makes you a good candidate. The professor is a container of knowledge and the goal is to get most of him, thus come and talk. Respect the deadlines Respect the appointments Try to study from more than one source, Internet is great! If you have any problems come and talk with me in advance so that we can find an appropriate solution GOOD LUCK! 1-9

Outline Stacks Stack: Implementation with Array Stack: Implementation with Linked List 1-10

Chapter 16 Stacks

ADT and Classes Abstract data type A type whose implementation is hidden from the rest of the system Class: An abstraction in the context of object-oriented languages A class encapsulates state and behavior 1-12

ADTs An ADT represents an abstract way to store data. This is different from normal data types, such as int or char. ADTs offer interesting ways to store data, depending on what the use of the data is for. 1-13

Linear Data Structures The defining property of a list is that the elements are organized linearly, that is, every element has one element immediately before it and another immediately after it (except, of course, the elements at the beginning and end of the list). 1-14

Stack The stack is an example of a constrained linear data structure. In a stack, the elements are ordered from most recently added (the top) to the least recently added (the bottom). All insertions and deletions are performed at the top of the stack. You use the push operation to insert an element onto the stack and the pop operation to remove the topmost stack element. 1-15

Stack: LIFO mode of operation The constraints on insertion and deletion produce the last in, first out (LIFO) behavior that characterizes a stack. Although the stack data structure is narrowly defined, it is used so extensively by systems software that support for a primitive stack is one of the basic elements of most computer architectures. 1-16

The first ADT: The Stack 1-17

Stack Two implementations: Based on Array Based on Linked List 1-18

Constructor Stack ( ) Precondition: None. Postcondition: creates an empty stack and allocates enough memory for a stack of default capacity. 1-19

Methods in the Interface void push ( Object newelement ) Precondition: newelement is not null. Postcondition: Inserts newelement onto the top of a stack. 1-20

Methods in the Interface pop ( ) Precondition: Stack is not empty. Postcondition: Removes the most recently added (top) element from a stack. 1-21

Methods in the Interface void makeempty ( ) Precondition: None. Postcondition: Removes all the elements in a stack. 1-22

Methods in the Interface boolean isempty ( ) Precondition: None. Postcondition: Returns true if a stack is empty. Otherwise, returns false. 1-23

Methods in the Interface Object top ( ) Precondition: Stack is not empty. Postcondition: Get the most recently inserted item in the stack. 1-24

Methods in the Interface Object topandpop ( ) Precondition: Stack is not empty. Postcondition: Removes the most recently added (top) element from a stack and returns it. 1-25

Method: doublearray Internal method to extend the stack in case it is full. 1-26

Array Implementation of Stack 1-27

Before we start implementation: generic programming in Java Generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. 1-28

Non generic class: Box public class Box { private Object object; public void set(object object) { this.object = object; } public Object get() { return object; } } 1-29

Generic class: Box public class Box<T> { // T stands for "Type" private T t; public void set(t t) { this.t = t; } public T get() { return t; } } 1-30

Invoking and Instantiating a Generic Type To reference the generic Box class from within your code, you must perform a generic type invocation, which replaces T with some concrete value, such as Integer: Box<Integer> integerbox; 1-31

AnyType: Generic programming Array that contains the elements Index of top of stack 1-32

Constructor 1-33

isempty() and makeempty() 1-34

Push( ) 1-35

Top( ) and Pop( ) 1-36

topandpop( ) 1-37

doublearray( ) private void doublearray( ) { AnyType [ ] newarray; } newarray = (AnyType []) new Object[ thearray.length * 2 ]; for( int i = 0; i < thearray.length; i++ ) newarray[ i ] = thearray[ i ]; thearray = newarray; 1-38

Linked List Implementation of Stack 1-39

Linked List concept This is the node containing two elements: the pointer/reference to next node and the object 1-40

Reference to top of stack Element of the node Reference to next node 1-41

Push( ) and Pop( ) 1-42

Top( ) and topandpop( ) 1-43

Stack: An Implementation with ArrayList 1-44

1-45

Running times of the two implementations Both the array and linked list versions run in constant time per operation. The array version is likely to be faster if an accurate estimation of capacity is performed. If an additional constructor is provided to specify the initial capacity and the estimate is correct, no doubling is performed. 1-46

Readings Book Chapter 16 1-47

Lab Exercises Add to the ADT for both implementations that we have defined the following methods: ShowElements: shows all the elements in the stack ShowInverse: show the elements in inverse order. New constructor which specifies size of stack (for the array implementation) as parameter. Clone: replicate a stack in another stack. Swap: exchange the two topmost items on the stack. Test all these in a testing class 1-48