Stack ADT & Modularity 2 implementations of the Stack abstract data type: Array Linked List Program design: modularity, abstraction and information hiding.

Slides:



Advertisements
Similar presentations
Lilian Blot Announcements Teaching Evaluation Form week 9 practical session Formative Assessment week 10 during usual practical sessions group 1 Friday.
Advertisements

Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
COMPSCI 105 S Principles of Computer Science 12 Abstract Data Type.
1 Abstract Data Types. Objectives To appreciate the concept and purpose of abstract data types, or ADTs To understand both the abstract behavior and the.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Design The goal is to design a modular solution, using the techniques of: Decomposition Abstraction Encapsulation In Object Oriented Programming this is.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Ch4: Software Architecture and Design. 1 Modules: Interface vs. Implementation (contd..)  Interface design considerations:
CS Winter 2011 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing.
Topic 3 The Stack ADT.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Chapter 3 Introduction to Collections – Stacks Modified
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Chapter 19: Program Design Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 19 Program Design.
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Introduction to Design (and Zen) CpSc 372: Introduction to Software Engineering Jason O. Hallstrom Authorship Disclaimer. These.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Chapter 10, Slide 1 ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:  process abstraction  data abstraction Both provide:  information.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington Last updated: 2/17/
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.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
Program Design Chapter 19 Copyright © 2008 W. W. Norton & Company.
Stack ADT (Abstract Data Type) N …
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
UNIT II Queue.
Lists, Stacks and Queues in C
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Stacks and Queues Chapter 4.
Abstract Data Types and Encapsulation Concepts
ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:
Data Structures and Algorithms
Abstract Data Types and Stacks
Chapter 12: Data Structures
Hassan Khosravi / Geoffrey Tien
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Stack and Queue APURBO DATTA.
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
CSCE 3110 Data Structures & Algorithm Analysis
CSC215 Homework Homework 11 Due date: Dec 19, 2016.
Abstract Data Types (ADTs)
Lecture 9 Concepts of Programming Languages
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Preconditions, Postconditions & Assertions
5.4 Additional Stack Applications
Abstract Data Types and Encapsulation Concepts
Chapter 13 Collections.
Programming Linked Lists.
Data Structures and Algorithms
Introduction to Data Structure
Stack.
Programming Languages and Paradigms
Data Abstraction: The Walls
Lists CMSC 202, Version 4/02.
Abstract Data Types Stacks CSCI 240
Lecture 9 Concepts of Programming Languages
Abstract Data Types (ADTs)
Abstract Data Types and Stacks
Presentation transcript:

Stack ADT & Modularity 2 implementations of the Stack abstract data type: Array Linked List Program design: modularity, abstraction and information hiding

What is a Stack? stack: abstract data type that represents a collection of elements access only allowed at one point of the structure, typically called the top of the stack access to the most recently added item only like a stack of plates in a cafeteria – last in, first out (LIFO) new elements/plates placed on top the top plate/element is always the first to be removed Fundamental operations push: add item to stack pop: remove top item from stack top: get top item without removing it isEmpty like a stack of physical items, e.g., books, plates, etc. ADT – data collection described in terms of behavior/operations, not implementation https://en.wikipedia.org/wiki/Stack_(abstract_data_type)

Abstract Data Type (ADT) Type with public interface & hidden implementation User only knows the available operations In C: public interface contains function prototypes in a header file Implementation in separate .c file: function definitions Client code can use type to declare variables & use provided operations Implementation of ADT can change with no impact on client or change in interface

Stack ADT Stack.h: #include<stdbool.h> typedef struct stackType *Stack; Stack create(); // create new empty stack void destroy(Stack s); // remove stack and all its elements bool isEmpty(Stack s); // return true if stack is empty, false otherwise bool isFull(Stack s); // return true if stack is full, false otherwise void push(Stack s, int n); // add n to top of stack int pop(Stack s); // remove and return top stack element

Stack ADT Client code, stackEx.c: #include<stdio.h> #include "Stack.h" int main() { Stack s1, s2; int n; s1 = create(); s2 = create(); push(s1, 1); push(s1, 2); push(s1, 3); while(!isEmpty(s1)) { n = pop(s1); push(s2, n); } destroy(s1); while(!isEmpty(s2)) { printf("Popped from s2: %i\n", pop(s2)); destroy(s2); If stack implemented correctly, output is: Popped from s2: 1 Popped from s2: 2 Popped from s2: 3

Stack ADT: Array Implementation Stack.c: #include<stdio.h> #include<stdlib.h> #include "Stack.h" #define STACK_SIZE 100 typedef struct stackType { int contents[STACK_SIZE]; int top; } stackType; Stack create() { Stack s = malloc(sizeof(stackType)); if(s == NULL) exit(EXIT_FAILURE); stop = 0; return s; } void destroy(Stack s) { free(s); } bool isEmpty(Stack s) { return stop == 0; bool isFull(Stack s) { return stop == STACK_SIZE; void push(Stack s, int n) { if(isFull(s)) exit(EXIT_FAILURE); scontents[stop] = n; stop++; Exercise: int pop(Stack s) { int n = s->contents[s->top-1]; s->top--; return n; }

Stack ADT: Linked List Implementation Stack.c, version 2: #include<stdio.h> #include<stlib.h> #include "Stack.h" struct node { int data; struct node *next; }; struct stackType { struct node *top; Stack create() { Stack s = malloc(sizeof(struct stackType)); if(s == NULL) exit(EXIT_FAILURE); stop = NULL; return s; } void destroy(Stack s) { while(!isEmpty(s)) pop(s); free(s); } bool isEmpty(Stack s) { return stop == NULL; bool isFull(Stack s) { return false;

Stack ADT: Linked List Implementation void push(Stack s, int n) { struct node *newNode = malloc(sizeof(struct node)); if(newNode == NULL) exit(EXIT_FAILURE); newNodedata = n; newNodenext = stop; stop = newNode; } int pop(Stack s) { struct node *oldTop; if(isEmpty(s)) exit(EXIT_FAILURE); oldTop = stop; int n = oldTopdata; stop = oldTopnext; free(oldTop); return n;

Program Design

Introduction Most full-featured programs are at least 100,000 lines long. Although C wasn’t designed for writing large programs, many large programs have been written in C. Writing large programs is quite different from writing small ones. Issues that are important when writing large programs: Style Documentation Maintenance Design Your program design should make programs readable and maintainable. We've been building very small programs. We can keep all the details in our head at once. Real application programs are much larger – they are too large and complex to hold all the details in our heads at once. They are written by multiple software developers, also. We need techniques for dealing with this complexity. One solution: modular programming: the code is composed of different code modules that are developed separately. A developer can take on a small piece of the system, and design and implement it without having to understand the rest of the program. The code modules are brought together to make a working program.

Modules Can view a program as a number of independent modules Module: collection of services (functions) – some are made available to other parts of the program Each module has: interface: describes available services header file containing prototypes, made available to clients implementation: source code file – contains implementation of module's functions

Modules in C The C library is a collection of modules Each header in the library serves as interface to a module: <stdio.h>: interface to module containing I/O functions <string.h>: interface to module containing string-handling functions Advantages of dividing program into modules: abstraction – a distancing between ideas and details. We can use modules without knowing how they work. reusability – a module can be reused in other programs. maintainability – a bug is contained to a single module, making it easier to find and fix. Abstraction in an IPod: you understand its external behavior (buttons, screen). You don't understand its inner details, and you don't need to, in order to use an iPod.

Modular Design Decisions to consider during modular design: What should the program's modules be? Which services should each module provide? How should the modules be interrelated? Want modules to have two properties: high cohesion: a module has a well-defined purpose. All elements of module are closely related to each other. low coupling: modules should be as independent of each other as possible. loosely coupled modules make it easier to modify the program Types of Modules: libraries: collection of related functions. E.g., <string.h> abstract data types: a type whose representation is hidden data pool: a collection of related variables and constants Often just a header file in C. E.g., <float.h>, <limits.h>

Information Hiding A well-designed module often keeps some information secret from clients Clients of our Stack module don't need to know how the Stack is implemented (array, linked list, etc.) Information hiding: concealing information from clients of a module Advantages: Security: Clients don't know how a module's data is stored so they can't corrupt it. We control the client's access to data through the operations we provide. Flexibility: Changing the implementation is easy because the client isn't dependent on a particular implementation.