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

Similar documents
Chapter 2. Stack & Queues. M hiwa ahmad aziz

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

Data Structures G5029

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.

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

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

Basic Data Structures

Associate Professor Dr. Raed Ibraheem Hamed

ASSIGNMENT NO 13. Objectives: To learn and understand concept of Inheritance in Java

Stacks and Queues. Chapter Stacks

Basic Data Structures 1 / 24

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

B.Sc (Computer Science) Object Oriented Programming with Java and Data Structures Lab Programs- Data Structures

CSE 143 SAMPLE MIDTERM

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

Data Abstraction and Specification of ADTs

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

Cosc 241 Programming and Problem Solving Lecture 9 (26/3/18) Collections and ADTs

CT 229 Object-Oriented Programming Continued

Outline and Reading. The Stack ADT ( 2.1.1) Applications of Stacks ( 2.1.1) Array-based implementation ( 2.1.1) Growable array-based stack ( 1.

A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list.

CS24 Week 4 Lecture 2

Stack Implementation

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

Data Structures and Object-Oriented Design III. Spring 2014 Carola Wenk

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

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Stacks. Access to other items in the stack is not allowed A LIFO (Last In First Out) data structure

Application of Stack (Backtracking)

Advanced Java Concepts Unit 3: Stacks and Queues

1. Stack Implementation Using 1D Array


Complexity, General. Standard approach: count the number of primitive operations executed.

Stacks Fall 2018 Margaret Reid-Miller

Outline and Reading. The Stack ADT ( 4.2.1) Applications of Stacks ( 4.2.3) Array-based implementation ( 4.2.2) Growable array-based stack.

CMPT 225. Lecture 9 Stack

Data Structures And Algorithms

CSCD 326 Data Structures I Stacks

Arrays and Linked Lists

Data Structures and Algorithms, Winter term 2018 Practice Assignment 3

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Abstract Data Types. Abstract Data Types

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

CS350: Data Structures Stacks

Lecture Data Structure Stack

Spring 2018 Discussion 6: February 19, 2018

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

final int a = 10; for(int i=0;i<a;i+=2) { for(int j=i;j<a;j++) { System.out.print("*"); } System.out.println(); }

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

Stack and Its Implementation

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

Program 20: //Design an Applet program to handle Mouse Events. import java.awt.*; import java.applet.*; import java.awt.event.*;

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

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Outline. Example stack Version 1 -- int stack with fixed array Version 2 -- int stack with flexible array Version 3 -- with interface

Linked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list }

csci 210: Data Structures Stacks and Queues

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

CSE 143 SAMPLE MIDTERM SOLUTION

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

UNIT-2 Stack & Queue

Lecture 4. The Java Collections Framework

CS 216 Exam 1 Fall SOLUTION

Chettinad College of Engineering & Technology Department of Information Technology Internal Examination II (Answer Key)

Implementing Dynamic Data Structures

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

What can go wrong in a Java program while running?

Interfaces & Generics

CSC 273 Data Structures

Announcements Queues. Queues

Stacks. Lecture6: Stacks. Stack Operations. Stack Interface in Java

Darshan Institute of Engineering & Technology for Diploma Studies Unit 3

Stacks and Queues as Collections

Midterm Exam 2 CS 455, Spring 2011

ITEC2620 Introduction to Data Structures

CMSC 132: Object-Oriented Programming II. Stack and Queue

Csci 102: Sample Exam

CMPS 390 Data Structures

C Sc 227 Practice Test 2 Section Leader Your Name 100pts. a. 1D array b. PriorityList<E> c. ArrayPriorityList<E>

Stacks and queues. CSCI 135: Fundamentals of Computer Science Keith Vertanen.

Discussion. Type 08/12/2016. Language and Type. Type Checking Subtypes Type and Polymorphism Inheritance and Polymorphism

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

CSC148 Week 2. Larry Zhang

Stacks. Gaddis 18.1, Molly A. O'Neil CS 2308 :: Spring 2016

DM537 Object-Oriented Programming. Peter Schneider-Kamp.

ITI Introduction to Computing II

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Stacks and Queues. Fundamentals of Computer Science.

Name Section Number. CS210 Exam #2 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

CSC148H Week 3. Sadia Sharmin. May 24, /20

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

Do not turn to the next page until the start of the exam.

DataInputStream dis=new DataInputStream(System.in);

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

Odds and Ends. Think about doing research Programming grading. Assignment 3 due Tuesday

Java generics. CSCI 136: Fundamentals of Computer Science II Keith Vertanen

CS 302 ALGORITHMS AND DATA STRUCTURES

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

Transcription:

Write a program to implement stack or any other data structure in Java ASSIGNMENT NO 15 Title: Demonstrate implementation of data structure in Java Objectives: To learn implementation of data structure in Java. Problem Statement: Write a program to implement stack or any other data structure in Java. Outcomes: Students will be able to learn and understand implementation of stack in Java. Hardware requirements: Any CPU with Pentium Processor or similar, 256 MB RAM or more, 1 GB Hard Disk or more. Software requirements: 64 bit Linux/Windows Operating System, JDK 1.4 or later Theory: In computer science, a data structure is a particular way of organizing data in a computer so that it can be used efficiently. Data structures can implement one or more particular abstract data types (ADT), which specify the operations that can be performed on a data structure and the computational complexity of those operations. In comparison, a data structure is a concrete implementation of the specification provided by an ADT. Different kinds of data structures are suited to different kinds of applications, and some are highly specialized to specific tasks. The implementation of a data structure usually requires writing a set of procedures that create and manipulate instances of that structure. The efficiency of a data structure cannot be analyzed separately from those operations. Following abstract data types are available: 1. Container 2. List 3. Associative array 4. Multimap 5. Set 6. Bag 7. Multiset 8. Stack

9. Queue 10. Double-ended queue 11. Priority queue 12. Tree 13. Graph Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO (Last In First Out) or FILO (First In Last Out). A stack is a container of objects that are inserted and removed according to the last-in-first-out (LIFO) principle. Objects can be inserted at any time, but only the last (the most-recently inserted) object can be removed. Inserting an item is known as pushing onto the stack. Popping off the stack is synonymous with removing an item. The Stack Abstract Data Type: A stack is an abstract data type (ADT) that supports two main methods: - push(o): Inserts object o onto top of stack o Input: Object; o Output: none pop(): Removes the top object of stack and returns it; if stack is empty an error occurs. o Input: none; o Output: Object The following support methods should also be defined peek(): Get the topmost object of stack o Input : none; o Output : Object; size(): Returns the number of objects in stack o Input: none; o Output: integer isempty(): Return a boolean indicating if stack is empty. o Input: none; o Output: boolean

top(): return the top object of the stack, without removing it; if the stack is empty an error occurs. o Input: none; o Output: Object A Stack Interface in Java: While, the stack data structure is a built-in class of Java s java.util package, it is possible, and sometimes preferable to define your own specific one, There are many real life examples of stack. Consider the simple example of plates stacked over one another in canteen. The plate which is at the top is the first one to be removed, i.e. the plate which has been placed at the bottommost position remains in the stack for the longest period of time. So, it can be simply seen to follow LIFO/FILO order. Implementation: There are two ways to implement a stack: Using array Using linked list An Array-Based Stack: Create a stack using an array by specifying a maximum size N for our stack, e.g. N = 1,000. The stack consists of an N-element array S and an integer variable t, the index of the top element in array S.

Array indices start at 0, so we initialize t to -1 Pseudo-code Algorithm size(): return t +1 Algorithm isempty(): return (t Algorithm push(o): if size() = N then throw a StackFullException t t + 1 S[t] o Algorithm pop(): if isempty() then throw a StackEmptyException e S[t] S[t] null t t-1 return e Conclusion: In this assignment, we have studied and demonstrated implementation of integer stack and its operations in Java. Roll No. Name Student of Date of Performance Date Assessment of Grade Sign of Student Sign Faculty of Program class StackDS private int top; private int elements[]; private int size; StackDS() top=-1; elements =new int[1]; size=1; StackDS(int n) top=-1; elements=new int[n]; size=n; void push(int num) if(top==(size-1)) System.out.println("Stack Full"); else elements[++top]=num;

int pop() if(top<0) System.out.println("Stack Empty"); return -1; else return elements[top--]; int peek() return elements[top]; void print() System.out.println("Elements in stack are"); for(int i=top;i>=0;i--) System.out.print(elements[i]+"\t"); System.out.println(); public class StackDemo public static void main(string args[]) System.out.println("Enter size of Stack"); int size=integer.parseint(system.console().readline()); StackDS stack=new StackDS(size); do System.out.println("\n\n*********MENU*************"); System.out.println("1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit\nEnter your choice:\n"); int choice=integer.parseint(system.console().readline()); switch(choice) case 1: System.out.println("Enter number to push on stack"); int elm=integer.parseint(system.console().readline()); stack.push(elm); break; case 2: System.out.println("Popped Element is "+stack.pop()); break; case 3:

System.out.println("Top Element is "+stack.peek()); break; case 4: stack.print(); break; case 5: System.exit(0); default: System.out.println("Wrong choice"); while(true);

OUTPUT: