Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Dale Roberts, Lecturer

Slides:



Advertisements
Similar presentations
Linked Lists CS-212 Dick Steflik. Linked Lists A sequential collection of information Can be unordered; i.e. in no specific order Can be ordered; may.
Advertisements

Stacks, Queues, and Linked Lists
CSCE 3110 Data Structures & Algorithm Analysis
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
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.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
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 Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
CS Data Structures II Review COSC 2006 April 14, 2017
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Chapter 4.
CS Data Structures Chapter 4 Lists. Dynamically Linked Stacks and Queues (1/8)  When several stacks and queues coexisted, there was no efficient.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Queues.
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 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Introduction to Data Structure
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
CHAPTER 3 Lists, Stacks, and Queues §1 Abstract Data Type (ADT) 【 Definition 】 Data Type = { Objects }  { Operations } 〖 Example 〗 int = { 0,  1, 
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
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’
Data structures Abstract data types Java classes for Data structures and ADTs.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
CSCE 3110 Data Structures & Algorithm Analysis More on lists. Circular lists. Doubly linked lists.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Dale Roberts, Lecturer Data Structure.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
UNIT II Queue.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
CC 215 Data Structures Queue ADT
Abstract Data Types Polynomials CSCI 240
CSCE 3110 Data Structures & Algorithm Analysis
Stacks and Queues.
Abstract Data Types Sparse Matrices CSCI 240
Stack and Queue APURBO DATTA.
EEE2108: Programming for Engineers Chapter 4. Linked Lists
Introduction to Data Structure
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Data Structures Chapter 4: Linked Lists.
Abstract Data Types Stacks CSCI 240
Data Structures & Programming
Presentation transcript:

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Dale Roberts, Lecturer

Dale Roberts Abstract Data Type Defined Sedgewick Definition 4.1: An abstract data type (ADT) is a data type (a set of values and a collection of operations on those values) that is accessed only through an interface. We refer to a program that uses an ADT as a client, and a program that specifies the data type as an implementation. The key distinction that makes a data type abstract is the word only. Client programs never access data except through the operations provided in the interface. We say that the interface is opaque. The client cannot see the implementation through the interface. This is also called encapsulation.

Dale Roberts Point ADT Interface // point.h class POINT { private: private: // Implementation-dependent code // Implementation-dependent code public: public: POINT(); POINT(); float distance(POINT) const; float distance(POINT) const; }; }; We can use different implementations (point.cpp) that have the same interface without changing any code in the client programs that use the ADT.

Dale Roberts Stack model A stack is a list with the restriction that insertion & deletion can be performed only at the end (or top) of the list. Only the top node is accessible: New nodes can be added and removed only at the top Similar to a pile of dishes Last-in, first-out (LIFO) Bottom of stack indicated by a link member to NULL Constrained version of a linked list push Adds a new node to the top of the stack pop Removes a node from the top Stores the popped value Returns true if pop was successful A stack can be empty, “pop” from an empty stack is an error A stack can never be full (assuming infinite memory) top S4 S3 S2 S1 top  Push (4)  Pop (4) top Pushdown-stack

Dale Roberts Sample stack linked-list implementation (C) struct list { int data struct list *next; } typedef struct list node; typedef node *link; Push link push(link stack, int value) { link newnode; newnode = (link)malloc(sizeof(node)); newnode->data = value; newnode->next = stack; stack = newnode; return(stack);}Pop link pop(link stack, int *valuePtr) { link top; top = stack; stack = stack->next; *valuePtr = top->data; free(top);return(stack);} stack top stack top stack newnode stack newnode valuePtr 4

Dale Roberts Pushdown-stack ADT interface template template class STACK { private: private: // Implementation-dependent code // Implementation-dependent code public: public: STACK(int); STACK(int); int empty() const; int empty() const; void push(Item item); void push(Item item); Item pop(); Item pop(); }; };

Dale Roberts Array implementation of pushdown stack template template class STACK { private: private: Item *s; int N; Item *s; int N; public: public: STACK(int maxN) STACK(int maxN) { s = new Item[maxN]; N = 0; } { s = new Item[maxN]; N = 0; } int empty() const int empty() const { return N == 0; } { return N == 0; } void push(Item item) void push(Item item) { s[N++] = item; } { s[N++] = item; } Item pop() Item pop() { return s[--N]; } { return s[--N]; } }; };

Dale Roberts Linked-list implementation of a pushdown stack template template class STACK { private: private: struct node struct node { Item item; node* next; { Item item; node* next; node(Item x, node* t) node(Item x, node* t) { item = x; next = t; } { item = x; next = t; } }; }; typedef node *link; typedef node *link; link head; link head; public: public: STACK(int) STACK(int) { head = 0; } { head = 0; } int empty() const int empty() const { return head == 0; } { return head == 0; } void push(Item x) void push(Item x) { head = new node(x, head); } { head = new node(x, head); } Item pop() Item pop() { Item v = head->item; link t = head->next; { Item v = head->item; link t = head->next; delete head; head = t; return v; } delete head; head = t; return v; } }; }; We can implement the push and pop operations for the pushdown stack ADT in constant time, using either arrays or linked lists.

Dale Roberts Queues Queue Similar to a supermarket checkout line First-in, first-out (FIFO) Nodes are removed only from the head Nodes are inserted only at the tail Insert and remove operations Enqueue (insert) and dequeue (remove) Queue Model queue is a list, with insertion done only at one end and deletion done at the other end. enqueue: insert an element at the end of the queue dequeue: delete (and return) the element at the start of the queue first in first out model (FIFO) Linked list implementation of queues operating as a list constant time for enqueue & dequeue (keeping pointer to both the head and tail of the list)

Dale Roberts Sample linked-list implementation (C) enqueue link enqueue(link queue, int value) { link newnode; newnode = (link)malloc(sizeof(node)); newnode->data = value; newnode->next = NULL; if (queue!=NULL) { queue->next = newnode; queue = queue->next; }else queue = newnode; return(queue);}dequeue link dequeue(link queue, int *valuePtr) { link dequeuenode; dequeuenode = queue; *valuePtr = dequeuenode ->data; queue = queue->next; free(dequeuenode);return(queue);} newnodequeue

Dale Roberts FIFO queue ADT interface template template class QUEUE { private: private: // Implementation-dependent code // Implementation-dependent code public: public: QUEUE(int); QUEUE(int); int empty(); int empty(); void put(Item); void put(Item); Item get(); Item get(); }; };

Dale Roberts FIFO queue linked-list implementation template template class QUEUE { private: private: struct node struct node { Item item; node* next; { Item item; node* next; node(Item x) node(Item x) { item = x; next = 0; } { item = x; next = 0; } }; }; typedef node *link; typedef node *link; link head, tail; link head, tail; public: public: QUEUE(int) QUEUE(int) { head = 0; } { head = 0; } int empty() const int empty() const { return head == 0; } { return head == 0; } void put(Item x) void put(Item x) { link t = tail; { link t = tail; tail = new node(x); tail = new node(x); if (head == 0) if (head == 0) head = tail; head = tail; else t->next = tail; else t->next = tail; } Item get() Item get() { Item v = head->item; link t = head->next; { Item v = head->item; link t = head->next; delete head; head = t; return v; } delete head; head = t; return v; } }; };

Dale Roberts FIFO queue array implementation template template class QUEUE { private: private: Item *q; int N, head, tail; Item *q; int N, head, tail; public: public: QUEUE(int maxN) QUEUE(int maxN) { q = new Item[maxN+1]; { q = new Item[maxN+1]; N = maxN+1; head = N; tail = 0; } N = maxN+1; head = N; tail = 0; } int empty() const int empty() const { return head % N == tail; } { return head % N == tail; } void put(Item item) void put(Item item) { q[tail++] = item; tail = tail % N; } { q[tail++] = item; tail = tail % N; } Item get() Item get() { head = head % N; return q[head++]; } { head = head % N; return q[head++]; } }; }; We can implement get and put operations for the FIFO queue ADT in constant time, using either arrays or linked-lists. If head = tail, then empty; if put would make them equal, then full. Array is 1 larger to allow checks.

Dale Roberts DYNAMICALLY LINKED STACKS AND QUEUES   NULL    top element link (a) Linked Stack   NULL    front element link (b) Linked queue rear *Figure 4.10: Linked Stack and queue (p.147)

Dale Roberts Represent n stacks #define MAX_STACKS 10 /* maximum number of stacks */ typedef struct { int key; /* other fields */ } element; typedef struct stack *stack_pointer; typedef struct stack { element item; stack_pointer link; }; stack_pointer top[MAX_STACKS];

Dale Roberts Represent n queues #define MAX_QUEUES 10 /* maximum number of queues */ typedef struct queue *queue_pointer; typedef struct queue { element item; queue_pointer link; }; queue_pointer front[MAX_QUEUE], rear[MAX_QUEUES];

Dale Roberts First-class ADT Sedgewick Definition 4.4: A first-class data type is one for which we can have potentially many different instances, and which we can assign to variables which we declare to hold the instances. Our prior examples provided a capability to use a single instance of a particular generalized stack or queue, and to achieve the important objective or hiding the data structure (or encapsulating the data) from the client. This situation is analogous to having a program that manipulates only one integer. Our Fraction ADT is a first-class ADT.

Dale Roberts First-class Queue ADT template template class QUEUE { private: private: // Implementation-dependent code // Implementation-dependent code public: public: QUEUE(int); QUEUE(int); QUEUE(const QUEUE&); QUEUE(const QUEUE&); QUEUE& operator=(const QUEUE&); QUEUE& operator=(const QUEUE&); ~QUEUE(); ~QUEUE(); int empty() const; int empty() const; void put(Item); void put(Item); Item get(); Item get(); }; }; Notice how each and every interface operations now includes a references to a particular Q. We can create as many queues as we need.

Dale Roberts First-class ADT interface for polynomials template template class POLY { private: private: // Implementation-dependent code // Implementation-dependent code public: public: POLY (Number, int); POLY (Number, int); float eval(float) const; float eval(float) const; friend POLY operator+(POLY &, POLY &); friend POLY operator+(POLY &, POLY &); friend POLY operator*(POLY &, POLY &); friend POLY operator*(POLY &, POLY &); }; };

Dale Roberts typedef struct poly_node *poly_pointer; typedef struct poly_node { int coef; int expon; poly_pointer link; }; poly_pointer a, b, c; coef expon link Representation Linked-list implementation of polynomials

Dale Roberts Examples a b null

Dale Roberts Adding Polynomials a b d a->expon == b->expon a b d a->expon expon -3 10

Dale Roberts Adding Polynomials (Continued) a b a->expon > b->expon d 2 8

Dale Roberts Circularly Linked Lists ptr avail... avail temp circular list vs. chain

Dale Roberts      avail temp ptr     NULL avail (1) (2) Deleting a polynomial in contant time using a circular linked-list and an available list.

Dale Roberts Head Node a a Zero polynomial Represent polynomial as circular list. (1) zero (2) others

Dale Roberts Sparse Matrices               Sparse matrices have a large number of zero elements in proportion to the number of nonzero elements. Storing in n x n array will have a lot of wasted space. Alternative: circular lists storing only nonzero elements.

Dale Roberts matrix_pointer mread(void); void mwrite(matrix_pointer node); void merase(matrix_pointer *node); matrix_pointer madd( matrix_pointer a, matrix_pointer b) And other matrix operations like sub, multiply, etc. It is typical to typedef matrix_pointer Matrix. #include “Matrix.h” In code: Matrix a, b, c; c = madd(a, b); Matrix ADT (C)

Dale Roberts down head right next head node down entry right value rowcol tag a ij i j entry node a ij # of head nodes = # of rows + # of columns tag is used to tell entry, head and a ij nodes part Sparse Matrix Nodes

Dale Roberts Circular linked list Linked Representation for Matrix

Dale Roberts Sparse Matrix Data Structure #define MAX_SIZE 50 /* size of largest matrix */ typedef enum {head, entry} tagfield; typedef struct matrix_node *matrix_pointer; typedef struct entry_node { int row; int col; int value; }; typedef struct matrix_node { matrix_pointer down; matrix_pointer right; tagfield tag; union { matrix_pointer next; entry_node entry; } u; }; matrix_pointer hdnode[MAX_SIZE]; #define MAX_SIZE 50 /* size of largest matrix */ typedef enum {head, entry} tagfield; typedef struct matrix_node *matrix_pointer; typedef struct entry_node { int row; int col; int value; }; typedef struct matrix_node { matrix_pointer down; matrix_pointer right; tagfield tag; union { matrix_pointer next; entry_node entry; } u; }; matrix_pointer hdnode[MAX_SIZE];

Dale Roberts Try creating your data for this matrix

Dale Roberts Acknowledgements All of this code is from Horowitz, Sahni, and Anderson-Freed, Fundamentals of Data Structures in C. Some slides were originally developed by Chen, Hsin-His.