SlideShare une entreprise Scribd logo
1  sur  10
CSE 373
Data Structures and Algorithms
Lecture 1: Introduction; ADTs; Stacks; Eclipse
Course objectives
 Learn basic data structures and algorithms
 data structures – how data is organized
 algorithms – unambiguous sequence of steps to compute
something
 algorithm analysis – determining how long an algorithm
will take to solve a problem
 Become a better software developer
 "Data Structures + Algorithms = Programs"
-- Niklaus Wirth, author of Pascal language
2
Abstract Data Types
 abstract data type (ADT): A specification of a collection
of data and the operations that can be performed on it.
 Describes what a collection does, not how it does it
 Described in Java with interfaces (e.g., List, Map, Set)
 Separate from implementation
 ADTs can be implemented in multiple ways by classes:
 ArrayList and LinkedList implement List
 HashSet and TreeSet implement Set
 LinkedList , ArrayDeque, etc. implement Queue
 Java messed up on Stack—there's no Stack interface, just a
class.
3
List ADT
 An ordered collection the form A0, A1, ..., AN-1, where N is the
size of the list
 Operations described in Java's List interface (subset):
 ArrayList and LinkedList are implementations
4
add(elt, index) inserts the element at the specified position
in the list
remove(index) removes the element at the specified position
get(index) returns the element at the specified position
set(index, elt) replaces the element at the specified position
with the specified element
contains(elt) returns true if the list contains the element
size() returns the number of elements in the list
Stack ADT
5
 stack: a list with the restriction that insertions/deletions
can only be performed at the top/end of the list
 Last-In, First-Out ("LIFO")
 The elements are stored in order of insertion,
but we do not think of them as having indexes.
 The client can only add/remove/examine
the last element added (the "top").
 basic stack operations:
 push: Add an element to the top.
 pop: Remove the top element.
 peek: Examine the top element.
Applications of Stacks
 Programming languages:
 method calls are placed onto a stack (call=push,
return=pop)
 Matching up related pairs of things:
 find out whether a string is a palindrome
 examine a file to see if its braces { } and other operators
match
 Sophisticated algorithms:
 searching through a maze with "backtracking"
 many programs use an "undo stack" of previous
operations
6
method3
return var
local vars
parameters
method2
return var
local vars
parameters
method1
return var
local vars
parameters
Class Stack
7
Stack<Integer> s = new Stack<Integer>();
s.push(42);
s.push(-3);
s.push(17); // bottom [42, -3, 17] top
System.out.println(s.pop()); // 17
Stack<E>() constructs a new stack with elements of type E
push(value) places given value on top of stack
pop() removes top value from stack and returns it;
throws EmptyStackException if stack is empty
peek() returns top value from stack without removing it;
throws EmptyStackException if stack is empty
size() returns number of elements in stack
isEmpty() returns true if stack has no elements
Stack limitations/idioms
 Remember: You can’t loop over a stack like you do
a list.
Stack<Integer> s = new Stack<Integer>();
...
for (int i = 0; i < s.size(); i++) {
do something with s.get(i);
}
 Instead, you pull contents out of the stack to view
them.
 Idiom: Remove each element until the stack is empty.
while (!s.isEmpty()) {
do something with s.pop();
}
8
Exercise
9
 Write a method symbolsBalanced that accepts a
String as a parameter and returns whether or not
the parentheses and the curly brackets in that
String are balanced as they would have to be in a
valid Java program.
 Use a Stack to solve this problem.
Eclipse concepts
10
 workspace: a collection of projects
 stored as a directory
 project: a Java program
 must have your files in a project in order to be able to
compile, debug and run them
 by default stored in a directory in your workspace
 perspective: a view of your current project using a
set of pre-laid-out windows and menus
 Java perspective
 debugging perspective

Contenu connexe

Similaire à 01-intro_stacks.ppt

stack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxstack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptx
HusnainNaqvi2
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringDS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and Engineering
RAJASEKHARV8
 
Stack linked list
Stack linked listStack linked list
Stack linked list
bhargav0077
 
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docxAssg 14 C++ Standard Template Library (STL)(Extra Credit .docx
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
festockton
 

Similaire à 01-intro_stacks.ppt (20)

Collections
CollectionsCollections
Collections
 
Collections and its types in C# (with examples)
Collections and its types in C# (with examples)Collections and its types in C# (with examples)
Collections and its types in C# (with examples)
 
Data structures in c#
Data structures in c#Data structures in c#
Data structures in c#
 
stack.ppt
stack.pptstack.ppt
stack.ppt
 
Lec2
Lec2Lec2
Lec2
 
DS UNIT 1.pdf
DS UNIT 1.pdfDS UNIT 1.pdf
DS UNIT 1.pdf
 
DS UNIT 1.pdf
DS UNIT 1.pdfDS UNIT 1.pdf
DS UNIT 1.pdf
 
Lab Manual-OOP.pdf
Lab Manual-OOP.pdfLab Manual-OOP.pdf
Lab Manual-OOP.pdf
 
Array properties
Array propertiesArray properties
Array properties
 
stack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxstack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptx
 
TSAT Presentation1.pptx
TSAT Presentation1.pptxTSAT Presentation1.pptx
TSAT Presentation1.pptx
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringDS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and Engineering
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
Array list(1)
Array list(1)Array list(1)
Array list(1)
 
Lec3
Lec3Lec3
Lec3
 
Unit i(dsc++)
Unit i(dsc++)Unit i(dsc++)
Unit i(dsc++)
 
Data Structure In C#
Data Structure In C#Data Structure In C#
Data Structure In C#
 
Collections generic
Collections genericCollections generic
Collections generic
 
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docxAssg 14 C++ Standard Template Library (STL)(Extra Credit .docx
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
 
7 stacksqueues
7 stacksqueues7 stacksqueues
7 stacksqueues
 

Dernier

Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 

Dernier (20)

Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 

01-intro_stacks.ppt

  • 1. CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse
  • 2. Course objectives  Learn basic data structures and algorithms  data structures – how data is organized  algorithms – unambiguous sequence of steps to compute something  algorithm analysis – determining how long an algorithm will take to solve a problem  Become a better software developer  "Data Structures + Algorithms = Programs" -- Niklaus Wirth, author of Pascal language 2
  • 3. Abstract Data Types  abstract data type (ADT): A specification of a collection of data and the operations that can be performed on it.  Describes what a collection does, not how it does it  Described in Java with interfaces (e.g., List, Map, Set)  Separate from implementation  ADTs can be implemented in multiple ways by classes:  ArrayList and LinkedList implement List  HashSet and TreeSet implement Set  LinkedList , ArrayDeque, etc. implement Queue  Java messed up on Stack—there's no Stack interface, just a class. 3
  • 4. List ADT  An ordered collection the form A0, A1, ..., AN-1, where N is the size of the list  Operations described in Java's List interface (subset):  ArrayList and LinkedList are implementations 4 add(elt, index) inserts the element at the specified position in the list remove(index) removes the element at the specified position get(index) returns the element at the specified position set(index, elt) replaces the element at the specified position with the specified element contains(elt) returns true if the list contains the element size() returns the number of elements in the list
  • 5. Stack ADT 5  stack: a list with the restriction that insertions/deletions can only be performed at the top/end of the list  Last-In, First-Out ("LIFO")  The elements are stored in order of insertion, but we do not think of them as having indexes.  The client can only add/remove/examine the last element added (the "top").  basic stack operations:  push: Add an element to the top.  pop: Remove the top element.  peek: Examine the top element.
  • 6. Applications of Stacks  Programming languages:  method calls are placed onto a stack (call=push, return=pop)  Matching up related pairs of things:  find out whether a string is a palindrome  examine a file to see if its braces { } and other operators match  Sophisticated algorithms:  searching through a maze with "backtracking"  many programs use an "undo stack" of previous operations 6 method3 return var local vars parameters method2 return var local vars parameters method1 return var local vars parameters
  • 7. Class Stack 7 Stack<Integer> s = new Stack<Integer>(); s.push(42); s.push(-3); s.push(17); // bottom [42, -3, 17] top System.out.println(s.pop()); // 17 Stack<E>() constructs a new stack with elements of type E push(value) places given value on top of stack pop() removes top value from stack and returns it; throws EmptyStackException if stack is empty peek() returns top value from stack without removing it; throws EmptyStackException if stack is empty size() returns number of elements in stack isEmpty() returns true if stack has no elements
  • 8. Stack limitations/idioms  Remember: You can’t loop over a stack like you do a list. Stack<Integer> s = new Stack<Integer>(); ... for (int i = 0; i < s.size(); i++) { do something with s.get(i); }  Instead, you pull contents out of the stack to view them.  Idiom: Remove each element until the stack is empty. while (!s.isEmpty()) { do something with s.pop(); } 8
  • 9. Exercise 9  Write a method symbolsBalanced that accepts a String as a parameter and returns whether or not the parentheses and the curly brackets in that String are balanced as they would have to be in a valid Java program.  Use a Stack to solve this problem.
  • 10. Eclipse concepts 10  workspace: a collection of projects  stored as a directory  project: a Java program  must have your files in a project in order to be able to compile, debug and run them  by default stored in a directory in your workspace  perspective: a view of your current project using a set of pre-laid-out windows and menus  Java perspective  debugging perspective