Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Dale Roberts, Lecturer"— Presentation transcript:

1 Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Dale Roberts, Lecturer IUPUIdroberts@cs.iupui.edu

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

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

4 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 4 1 3 6  1 3 6 Push (4)  Pop (4) 1 3 6 top Pushdown-stack

5 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 2 3 1 4 stack top 2 3 1 4 stack newnode 2 3 1 4stack newnode 2 3 1 4 valuePtr 4

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

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

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

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

10 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

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

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

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

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

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

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

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

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

19 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 &); }; };

20 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

21 Dale Roberts Examples 3 14 2 8 1 0 a 8 14 -3 10 10 6 b null

22 Dale Roberts Adding Polynomials 3 14 2 8 1 0 a 8 14 -3 10 10 6 b 11 14 d a->expon == b->expon 3 14 2 8 1 0 a 8 14 -3 10 10 6 b 11 14 d a->expon expon -3 10

23 Dale Roberts Adding Polynomials (Continued) 3 14 2 8 1 0 a 8 14 -3 10 10 6 b 11 14 a->expon > b->expon -3 10 d 2 8

24 Dale Roberts Circularly Linked Lists 3 14 2 8 1 0 ptr avail... avail temp circular list vs. chain

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

26 Dale Roberts Head Node 3 14 2 81 0 a a Zero polynomial Represent polynomial as circular list. (1) zero (2) others

27 Dale Roberts Sparse Matrices               15000 0040 00512 01100 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.

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

29 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

30 Dale Roberts 4 4 1 0 12 2 1 -4 0 2 11 3 3 -15 1 1 5 Circular linked list Linked Representation for Matrix

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

32 Dale Roberts Try creating your data for this matrix

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


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

Similar presentations


Ads by Google