Sieve public class sieve { public static void main(String args[]) {

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Stacks, Queues, and Linked Lists
Stack & Queues COP 3502.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues.
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stack and Queue COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Stacks, Queues, and Deques
CMPT 225 Stacks.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Implementing Stacks Using Arrays CSC 1401: Introduction to Programming with Java Week 14 – Lecture 1 Wanda M. Kunkle.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
Definition Stack is an ordered collection of data items in which access is possible only at one end (called the top of the stack). Stacks are known.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Object-Oriented Programming Simple Stack Implementation.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
One-dimensional arrays and strings: Chapter 6, Slide 1 The concept of array - an extension of the basic model of memory:
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Data Structures Evolution of algorithms for an array-based stack, queue, and linked-list. Evolved data structures used to evolve solutions to problems.
Queues Another Linear ADT Copyright © 2009 Curt Hill.
Stacks and Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
CS Week 8 Jim Williams, PhD.
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
An Introduction to Java – Part I, language basics
ADT list.
Stacks and Queues 1.
class PrintOnetoTen { public static void main(String args[]) {
Abstract Data Type Abstract Data Type as a design tool
F II 3. Classes and Objects Objectives
UNIT-II.
Stacks CS-240 Dick Steflik.
Circular Queues: Implemented using Arrays
Presentation transcript:

Sieve public class sieve { public static void main(String args[]) { int max = 100; boolean prime[] = new boolean[max+1]; int n,m; // initialize the array (entries 0,1 unused) for (n = 2; n <= max; n++) { prime[n] = true; } // cross out multiples of uncrossed numbers if (prime[n]) { for (m = 2*n; m <= max; m += n) prime[m] = false; // print out primes: uncrossed numbers System.out.print(n+" "); System.out.println();

Queue class Queue { char q[]; // this array holds the queue int putloc, getloc; // the put and get indices Queue(int size) { q = new char[size+1]; // allocate memory for queue putloc = getloc = 0; } boolean isEmpty(){ if (getloc == putloc) return true; else return false; boolean isFull(){ if (putloc == q.length-1) return true;

// put a characer into the queue void put(char ch) { if (isFull()) { System.out.println(" -- Queue is full."); return; } putloc++; q[putloc] = ch; // get a character from the queue char get() { if (isEmpty()){ System.out.println(" -- Queue is empty."); return (char) 0; getloc++; return q[getloc];

// Demonstrate the Queue class. class QueueDemo { public static void main(String args[]) { Queue bigQ = new Queue(100); Queue smallQ = new Queue(4); char ch; int i; System.out.println("Using bigQ to store the alphabet."); // put some numbers into bigQ for(i=0; i < 26; i++) bigQ.put((char) ('A' + i)); // retrieve and display elements from bigQ System.out.print("Contents of bigQ: "); for(i=0; i < 26; i++) { ch = bigQ.get(); if(ch != (char) 0) System.out.print(ch); } System.out.println("\n");

System.out.println("Using smallQ to generate erros."); // Now, use smallQ to generate some errors for(i=0; i < 5; i++) { System.out.print("Attempting to store " + (char) ('Z' - i)); smallQ.put((char) ('Z' - i)); System.out.println(); } // more errors on smallQ System.out.print("Contents of smallQ: "); ch = smallQ.get(); if(ch != (char) 0) System.out.print(ch);

Stack class Stack { int st[]; // this array holds the stack int sp; // the stack pointer index Stack(int size) { st = new int[size+1]; // allocate memory for stack sp = 0; } boolean isFull(){ if (sp == (st.length-1)) return true; else return false; boolean isEmpty(){ if (sp == 0) return true;

void push(int n) { if(isFull()) { System.out.println(" -- Stack is full."); return; } sp++; st[sp] = n; int pop() { int top; if(isEmpty()) { System.out.println(" -- Stack is empty."); return -1000; top = st[sp]; sp--; return top;

// Demonstrate the Stack class. class StackDemo { public static void main(String args[]) { Stack st1 = new Stack(5); int i,n; System.out.println("Using stack st1 to store numbers."); // put some numbers into st1 for(i=1; i < 7; i++) st1.push(i); // pop and display elements from st1 System.out.println("Contents of st1: "); for(i=1; i < 7; i++) { n = st1.pop(); if (n != -1000) System.out.println(n); }