Download presentation
Presentation is loading. Please wait.
1
CS2468: Data Structures and Data Management
Lecturer: Lusheng Wang Office: B6422 Phone: TA (Assignment Marking and Tutorial) Chao SHEN , Phone: Zicheng ZHAO Welcome to ask questions at ANY time. The course Website: Canvas Some Java Source code: The course Website: Text Book: Michael T. Goodrich and Roberto Tamassia, Data Structure and Algorithms in Java, John Wiley & Sons, Inc. 5th Edition Stacks
2
Topics to be covered Analysis of Algorithms Data Structures
worst case time and space complexity Data Structures stack, queue, linked list, tree, priority queue, heap, hash, and graph, Searching algorithms binary and AVL search trees; Sorting algorithms merge sort, quick sort, bucket sort and radix sort; (Reduce some contents) Graph data structure, depth first search and breadth first search. (add some interesting contents). shortest path. Stacks
3
Why This Course? You will be able to evaluate the quality of a program (Analysis of Algorithms: Running time and memory space ) You will be able to write fast programs You will be able to solve new problems You will be able to give non-trivial methods to solve problems. (Your algorithm (program) will be faster than others.) Stacks
4
Course Evaluations Course work: 30% Final Exam: 70% Course Work:
Three assignments, 15% One quiz or lab test 3% One mid term exam 12% Stacks
5
OBTL: Course Intended Learning Outcomes
1.Describe the functionality of a data structure as an abstract data type; 2.Implement an abstract data type in a programming language; 3.Implement and test data structures for common structures; 4.Select an appropriate data structure from a given set of structures to solve a given problem; 5.Design and implement data storage management with simple file structures. Will be tested in quiz or assignment or midterm. For each item, you have to get 40% to pass.
6
Pre-requisites: CS2360 Java Programming /or Not known java?
Spend the rest of 8-10 days to study Java. Read textbook p1-p53 Design a class with two integers C1 and C2 and a method f() to calculate the average value of c1 and c2. If you cannot do that, you should worry… Stacks
7
Data Structures Algorithms
A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes. Data stored operation on data Algorithms an effective method expressed as a finite list of well-defined instructions for calculating a function. Algorithms are used for calculation, data processing, and automated reasoning. In simple words an algorithm is a step-by-step procedure for calculations. Stacks
8
How to describe algorithm?
Nature languages Chinese, English, etc. Not accurate. Pseudo-code Codes close to computer languages Omits details that are not essential for human understanding Intended for human reading rather than machine reading Programs C /C++ programs, Java programs. Pseudo-code is preferred Allow a well-trained programmer to be able to implement. Allow an expert to be able to analyze the running time. Stacks
9
An Example of an Algorithm
Algorithm sorting(X, n) Input array X of n integers Output array X sorted in a non-decreasing order for i 0 to n 1 do for j i+1 to n-1 do if (X[i]>X[j]) then { s=X[i]; X[i]=X[j]; X[j]=s; } return X // after i-th round, the i-th smallest number is at i-th position. Algorithm sorting(X, n) Input array X of n integers Output array X sorted in a non-decreasing order for i from 0 to n 1 do for j from i+1 to n-1 do if X[i] is larger than X[j] swap(X[i], X[j]) return X // after i-th round, the i-th smallest number is at i-th position. Stacks
10
Variables, Primitives A variable has its value Examples
A type is associated, E.g., int, boolean, float, long, etc. A value is assigned to the variable The value is stored in the memory The size of the value is determined umbigously; 64 bit machine, int 8 bytes, double 8 bytes, etc Examples int x; int y=50; char c, z=‘A’; Stacks
11
Objects (extension of variables)
An object has both a state and behavior. State defines the object, Behavior defines what the object does. What is the size of an object in the memory?
12
Class Class defines the type of an object
the kinds of operations that it performs. A Java class uses variables to define data fields and methods to define behaviors. A class provides a special type of methods, known as constructors, which are invoked to construct objects from the class.
13
Classes
14
Class Diagram
15
State/attributes—variables Operation/action – methods
© 2003 Brooks/Cole Publishing / Thomson Learning™ State/attributes—variables Operation/action – methods Figure 2.3: Class specification of Person
16
Figure 2.7: Providing methods to access the attributes of Person
© 2003 Brooks/Cole Publishing / Thomson Learning™ Figure 2.7: Providing methods to access the attributes of Person
17
Figure 2.8: An Instance fred of the Person class
© 2003 Brooks/Cole Publishing / Thomson Learning™ Figure 2.8: An Instance fred of the Person class
18
Figure 2.9: Using the access methods to set fred’s attributes
© 2003 Brooks/Cole Publishing / Thomson Learning™ Figure 2.9: Using the access methods to set fred’s attributes
19
Constructors Circle() { } Circle(double newRadius) {
radius = newRadius; Constructors are a special kind of methods that are invoked to construct objects.
20
Constructors, cont. A constructor with no parameters is referred to as a no-arg constructor. Constructors must have the same name as the class itself. Constructors do not have a return type—not even void. Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects.
21
Creating Objects Using Constructors
new ClassName(); Example: new Circle(); new Circle(5.0);
22
Declaring/Creating Objects in a Single Step
ClassName objectRefVar = new ClassName(); Example: Circle myCircle = new Circle(); Assign object reference Create an object
23
Accessing Objects Referencing the object’s data: objectRefVar.data
e.g., myCircle.radius Invoking the object’s method: objectRefVar.methodName(arguments) e.g., myCircle.getArea() Show a complete program C7-TestCircle1.html
24
Part-B1 Stacks (p198-p213)
25
Abstract Data Types (ADTs)
an abstraction of a data structure An ADT specifies: Data stored Operations on the data Error conditions associated with operations e.g. : ADT modeling a students record The data stored are Student name, id, as1, as2,as3, exam The operations supported are int averageAs(as1,as2,as3) Int finalMark(as1, as2,as3, exam) ) Error conditions: Calculate the final mark for absent student Stacks
26
The Stack ADT (§5.1) Auxiliary stack operations:
object top() returns the last inserted element without removing it integer size() returns the number of elements stored boolean isEmpty() indicates whether no elements are stored The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in first-out (LIFO) scheme Main stack operations: push(object) inserts an element object pop() removes and returns the last inserted element Stacks
27
Applications of Stacks
Direct applications Undo sequence in a text editor Chain of method calls in the Java Virtual Machine Indirect applications Auxiliary data structure for algorithms Component of other data structures Stacks
28
Parentheses Matching An expression, i.e.,[(2+3)*x+5]*2.
Each “(”, “{”, or “[” must be paired with a matching “)”, “}”, or “[” correct: ( )(( )){([( )])} ((( )(( )){([( )])})) incorrect )(( )){([( )])}( ({[ ])} ([)]) ( Stacks
29
Examples Picture is from Stacks
30
Examples ( ) ( ( ) ) { ( [ ( ) ] ) } ( ( ( ( { ( { [ ( { ( [ { [ ( { (
( ) ( ( ) ) { ( [ ( ) ] ) } ( ( ( ( { ( { [ ( { ( [ { [ ( { ( { {
31
Examples ( ) ( ( ] ) { ( [ ( ) ] ) } Mismatch!!! ( ( ( (
32
Examples ( ) ( ( ) ) { ( [ ) ] ) } Mismatch!!! ( ( ( ( { ( { [ ( { ( {
33
Examples ( ) ( ( ) ) { ( [ Mismatch!!! ( ( ( ( { ( { [ ( {
( ) ( ( ) ) { ( [ Mismatch!!! (stack is non-empty) ( ( ( ( { ( { [ ( {
34
Examples ( ) ( ( ) ) ) Mismatch!!! (pop empty stack) ( ( ( (
35
Parentheses Matching Algorithm
Algorithm ParenMatch(X,n): Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match Let S be an empty stack for i=0 to n-1 do if X[i] is an opening grouping symbol then S.push(X[i]) else if X[i] is a closing grouping symbol then if S.isEmpty() then return false {nothing to match with} if S.pop() does not match the type of X[i] then return false {wrong type} return true {every symbol matched} else return false {some symbols were never matched} Stacks
36
Parentheses Matching Example 1
Input: () (() [()]) i X[i] Operation Stack Output ( Push ( 1 ) Pop ( Test if ( and X[i] match? YES 2 3 (( 4 Test if ( and X[i] match? YES 5 [ Push [ ([ Stacks
37
Parentheses Matching Example 1
Input: () (() [()]) i X[i] Operation Stack Output 6 ( Push ( ([( 7 ) Pop ( Test if ( and X[i] match? YES ([ 8 ] Pop [ Test if [ and X[i] match? YES 9 Pop ( Test if ( and X[i] match? YES Test if stack is Empty? YES TRUE Stacks
38
Parentheses Matching Example 2
Input: ( () [] ]() i X[i] Operation Stack Output ( Push ( 1 (( 2 ) Pop ( Test if ( and X[i] match? YES 3 [ Push [ ([ 4 ] Pop [ Test if [ and X[i] match? YES 5 Pop ( Test if ( and X[i] match ? NO FASLE Stacks
39
Stack Interface in Java
public interface Stack { public int size(); public boolean isEmpty(); public Object top() throws EmptyStackException; public void push(Object o); public Object pop() throws EmptyStackException; } Java interface corresponding to our Stack ADT Requires the definition of class EmptyStackException Stacks
40
Exceptions Attempting the execution of an operation of ADT may sometimes cause an error condition, called an exception Exceptions are said to be “thrown” by an operation that cannot be executed In the Stack ADT, operations pop and top cannot be performed if the stack is empty Attempting the execution of pop or top on an empty stack throws an EmptyStackException Stacks
41
Array-based Stack (Implementation)
Algorithm size() return t + 1 Algorithm pop() if isEmpty() then throw EmptyStackException else t t 1 return S[t + 1] A simple way of implementing the Stack ADT uses an array We add elements from left to right A variable t keeps track of the index of the top element … S 1 2 t Stacks
42
Array-based Stack (cont.)
The array storing the stack elements may become full A push operation will then throw a FullStackException Limitation of the array-based implementation Not intrinsic to the Stack ADT Algorithm push(o) if t = S.length 1 then throw FullStackException else t t + 1 S[t] o S 1 2 t … Stacks
43
Array-based Stack (Cont.)
Algorithm isEmpty() if t<0 then return true else return false Algorithm top() if isEmpty() then throw EmptyStackException return S[t ] A Stack might be empty top returns the element at the top of the Stack, but does not remove the top element. When the Stack is empty, an error occurs. … S 1 2 t Stacks
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.