Abstract Data Type Abstract Data Type as a design tool

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues.
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.
Stacks and Queues. 2 Stack and Queue ADT’s You should understand How are they different philosophically from arrays What are they used for How do you.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
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 CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof.
Ali Abdul Karem Habib Kufa University / mathematics & Science Of Computer.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
Stacks and Queues Introduction to Computing Science and Programming I.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Data Structures & Algorithms
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
Lecture No.05 Data Structures Dr. Sohail Aslam.  Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
CSCE 3110 Data Structures & Algorithm Analysis
STACKS & QUEUES for CLASS XII ( C++).
Review Array Array Elements Accessing array elements
Stacks.
Queues.
UNIT II Queue.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
COSC160: Data Structures: Lists and Queues
QUEUES.
Chapter 15 Lists Objectives
Stacks.
Stacks and Queues.
Queues Queues Queues.
STACKS AND QUEUES UNIT 2 DS THROUGH C++.
Stacks.
Stack and Queue APURBO DATTA.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Stack and Queue.
Stacks A stack is a data structure that is similar in spirit to a pile of cafeteria trays. Think about the trays in the dining halls: when the dining staff.
Stacks and Queues.
Building Java Programs
CMSC 341 Lecture 5 Stacks, Queues
Principles of Computing – UFCFA3-30-1
CSCE 3110 Data Structures & Algorithm Analysis
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks.
Queues.
Stack and Queues Stack implementation using Array
UNIT-I Topics to be covere d 1.Introduction to data structures.
CSC 143 Stacks [Chapter 6].
Stacks and Queues 1.
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Visit for more Learning Resources
Stacks CS-240 Dick Steflik.
Stacks, Queues, and Deques
Abstract Data Types Stacks CSCI 240
Lecture 9: Stack and Queue
Presentation transcript:

Abstract Data Type Abstract Data Type as a design tool Concerns only on the important concept or model No concern on implementation details. Stack & Queue is an example of ADT An array is not ADT.

What is the difference? Stack & Queue vs. Array Arrays are data storage structures while stacks and queues are specialized data structures and used as programmer’s tools. Stack – a container that allows push and pop Queue - a container that allows enqueue and dequeue No concern on implementation details. In an array any item can be accessed, while in these data structures access is restricted. They are more abstract than arrays.

stack Allows access to only the last item inserted. A stack is an ordered list in which all insertions and deletions are made at one end called the top. Stack principle: LAST IN FIRST OUT = LIFO In stack always the last item to be put in to the stack is the first item to be removed. So stack is a Last In First Out or LIFO data structure. Push and Pop are the operations that are provided for insertion of an element in to the stack and the removal of an element from the stack. Allows access to only the last item inserted. An item is inserted or removed from the stack from one end called the “top” of the stack. This mechanism is called Last-In-First-Out (LIFO).

Last In First Out B A C B A D C B A E D C B A D C B A top top top top

Example Consider an empty stack created. 17 19 12 12 12 4 4 4 4 4 4 4 Consider an empty stack created. Consider the following sequence of operations Push(4), Push(19), pop(), push(12), push(17), pop(), pop(). The resulting stack is shown in the figure. Data Structures Week 4

Stack ADT Stack is a collection of elements in which insertion and deletion of elements is done by one end called top Operations Push: By this operation one can push elements onto the stack. Before performing push, we should check whether stack is full or not Pop: By this operation one can remove the elements from the stack. Before performing pop, we should check whether stack is empty or not Implementation: Array based Linked list based

Basic Stack Operations The stack concept is introduced and three basic stack operations are discussed. Push (add item to stack) Pop (remove top item from stack)

Array-based Stack Implementation Allocate an array of pre-defined size Maximum N elements in stack Bottom stack element stored at element 0 last index in the array is the top Increment top when one element is pushed, decrement after pop

Array-based Stack ADT Implementation

#include<iostream. h> #include<conio #include<iostream.h> #include<conio.h> #define MAX 5 class STACK_CLASS { private: struct stack int s[MAX]; int top; }st; public: STACK_CLASS() st.top=-1; } void push(int data); int pop(); int stfull(); int stempty(); void display(); };

void STACK_CLASS::push(int data) { st. top++; st. s[st void STACK_CLASS::push(int data) { st.top++; st.s[st.top]=data; } int STACK_CLASS::pop() int data; data=st.s[st.top]; st.top--; return data;

int STACK_CLASS::stempty() { if(st.top==-1) return 1; } else return 0; int STACK_CLASS::stfull() { if((st.top) >= (MAX-1)) return 1; } else return 0; int STACK_CLASS::stempty() { if(st.top==-1) return 1; } else return 0;

void STACK_CLASS::display() { int i; if(stempty()) cout<<"\n stack is empty"; } else for(i=st.top; i>=0; i--) cout<<"\n"<<st.s[i];

int main() { clrscr(); int data, choice; char ans; STACK_CLASS obj; do cout<<"\n Main menu"; cout<<"\n 1. push"; cout<<"\n 2. pop"; cout<<"\n 3. display"; cout<<"\n 4. exit"; cout<<"\n enter your choice"; cin>>choice;

switch(choice) { case 1: cout<<"\n enter the element to be pushed"; cin>>data; if(obj.stfull()) { cout<<"\n stack is full (overflow)“; } else { obj.push(data); } break; case 2: if(obj.stempty()) { cout<<"\n empty stack (underflow)"; } { data = obj.pop(); cout<<"\n popped element is"<<data; } case 3: obj.display(); case 4: return 0; }

cout<<"\n Do you want to continue cout<<"\n Do you want to continue?"; cin>>ans; }while(ans == 'y'); getch(); return 0; }

Stack Applications Real life Pile of books Plate trays More applications related to computer science Program execution stack (read more from your text) Evaluating expressions

Algorithm Analysis push O(?) pop O(?) isEmpty O(?) isFull O(?) What if top is stored at the beginning of the array?