Presentation is loading. Please wait.

Presentation is loading. Please wait.

Abstract Data Type Abstract Data Type as a design tool

Similar presentations


Presentation on theme: "Abstract Data Type Abstract Data Type as a design tool"— Presentation transcript:

1 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.

2 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.

3 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).

4 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

5 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

6 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

7 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)

8

9

10 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

11 Array-based Stack ADT Implementation

12 #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(); };

13 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;

14 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;

15 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];

16 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;

17 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; }

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

19 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

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


Download ppt "Abstract Data Type Abstract Data Type as a design tool"

Similar presentations


Ads by Google