Presentation is loading. Please wait.

Presentation is loading. Please wait.

Abstract Data Type 1.

Similar presentations


Presentation on theme: "Abstract Data Type 1."— Presentation transcript:

1 Abstract Data Type 1

2 Why ADT To abstract is to leave out unimportant parts of the information keeping the more important parts. In an ADT we leave out details of implementation. So, programmers can concentrate more on the problem solving. An ADT should be implemented such that the programmers who use the type do not need to know the details of how the values are represented and how the operations are implemented. High level languages often provide built in ADTs. the C++ STL (Standard Template Library) the Java standard library 2

3 Data structure, ADT, and Program
Operation1 Operation2 I n t e r f a c e Result of operation Data Structure Operation3 Program Request to perform operation Etc. ADT operations 3

4 An ADT Example: Complex Numbers
Specification/Definition Domain: {a + b i| a, b are real numbers} Rules Association of values i2 = -1 Operations Addition (a + b i) + (c + d i) = (a+c) + (b+d) i Multiplication (a + b i) * (c + d i) = (ac-bd) + (ad+bc) i 4

5 Complex Numbers conti. /* Representation 1 */ class Complex {
private double re, im; // Constructor public Complex(double x, double y) { re = x; im = y; } // Accessors double re () { return re;} double im () { return im;} }; 5

6 Complex Numbers conti. /* Representation 2 */ class Complex {
private double vals[2]; // Constructor public Complex(double x, double y) { vals = new double[2]; vals[0] = x; vals[1] = y; } // Accessors double re () { return vals[0];} double im () { return vals[1];} }; 6

7 Complex Numbers conti. /* Implementation of operations */
Complex add(Complex C1, Complex C2) { return Complex(C1.re()+C2.re(), C1.im()+C2.im()); } Complex mul(Complex C1, Complex C2) { double r1, r2, i1, i2; r1 = C1.re(); i1 = C1.im(); r2 = C2.re(); i2 = C2.im(); return Complex( r1*r2-i1*i2, r1*i2 + r2*i1); 7

8 Methods for representing values of data structures
Array based There are better algorithms for sorting and searching. Reference based/pointer based/Linked/Chaining Insertion and removal of elements can be done faster. 8

9 Array based VS Reference based
Last/size Reference based Last First

10 List ADT A collection of objects in a specific order and having the same type. delete insert first current last Primary operations: insertBefore(e) – Add an element e before the current position. insertAfter(e) – Add an element e after the current position. remove(e) – Element e is removed from the list. current() – Returns the current element. size() – Returns the number of elements on the list. forward() – Move the current position forward one position. backword() - Move the current position backward one position. resetCurrent – Reset the current position at the first element. 10

11 Array Implementation List ADT
frist=0 current size capacity Representation: public class List { private int size=0, current=-1, capacity=default ; private Object L[]; public List(int maxSize) { capacity = maxSize; L = new Object[capacity]; }

12 Array Implementation List ADT conti.
public int size() { return size; } public Object current() { if (current>=0) return L[current]; else return null;} public void insertBefore(Object element) { if (size==capacity) return; if (size>0) for (int i=size-1; i>=current;--i) L[i+1] = L[i]; else current = 0; L[current] = element; size++; } public void forward() { if (size>0 && current<size-1) current++;} 9 size 8 7 6 5 element current 4 3 2 1

13 Linked Implementation List ADT
first Data Next null Element current Representation: Singly linked list. Drawback: Can only process elements in onedirection. public class List { private int size=0; private Node current, first ; }

14 Representation of nodes
public class Node { private Object Data = null; private Node Next = null; public Node(Object Element) { Data = Element; } public void setNext(Node N) { Next = N; } public Node getNext() { return Next; } public Object getElement() { return Data; } public void setElement(Object Element) { Data = Element; } } Data Next Element 14

15 Linked Implementation List ADT conti.
public int size() { return size; } public Object current() { if (current!=null) return current.getElement(); else return null; } public void insertAfter(Object e) { Node nxt = current==null ? null : current.getNext(); Node tmp = new Node(e); if (current!=null) current.setNext(tmp); else current = first = tmp; tmp.setNext(nxt); size++; public void forward() { Node tmp = current.getNext(); if (tmp!=null) current = tmp; 1 2 3 e tmp 1 2 3 current nxt

16 Linked Implementation List ADT conti.
public void remove(Object e) { Node cur = first; Node prv = null; while(cur!=null && cur.getElement()!=e) { prv = cur; cur = cur.getNext();} if (cur==null) return; Node nxt = cur.getNext(); if (prv!=null) prv.setNext(nxt); else first = nxt; current = nxt; if (current==null && prv!=null) current = prv; size--; } prv current nxt

17 Representation of nodes revisit
public class Node { public Object Data = null; public Node Next = null; } Data Next Element 17

18 Linked Implementation revisit conti.
public int size() { return size; } public Object current() { if (current!=null) return current.Data; else return null; } public void insertAfter(Object e) { Node nxt = current==null ? null : current.Next; Node tmp = new Node(); tmp.Data = e; if (current!=null) current.Next = tmp; else current = first = tmp; tmp.Next = nxt; size++; public void forward() { Node tmp = current.Next; if (tmp!=null) current = tmp; 1 2 3 e tmp 1 2 3 current nxt

19 Linked Implementation revisit conti.
public void remove(Object e) { Node cur = first; Node prv = null; while(cur!=null && cur.Data!=e) { prv = cur; cur = cur.Next;} if (cur==null) return; Node nxt = cur.Next; if (prv!=null) prv.Next = nxt; else first = nxt; current = nxt; if (current==null && prv!=null) current = prv; size--; } prv current nxt

20 Doubly Linked Implementation List ADT
head tail Prev Data Next null null Element current Representation: Doubly linked list. Drawback: Waste more memory space on node references. public class List { private int size=0,; private DLNode current, Head, Tail ; }

21 Representation of doubly linked nodes
public class DLNode { public Object Data = null; public DLNode Prev= null, Next=null; } Prev Data Next Element 21

22 Doubly Linked Implementation List ADT conti.
public void insertBefore(Object e) { DLNode prv = current==null ? Tail : current.Prev; DLNode tmp = new DLNode(); tmp.Data = e; if (prv!=null) prv.Next = tmp; else Head = tmp; tmp.Prev = prv; tmp.Next = current; if (current!=null) current.Prev = tmp; else Tail = tmp; size++; } 1 2 3 4 5 1 e tmp 3 4 2 5 prv current

23 DoublyLinked Implementation List ADT conti.
public void remove() { if (current==null) return; DLNode prv = current.Prev; DLNode nxt = current.Next; if (prv!=null) prv.Next = nxt; else head = nxt; if (nxt!=null) nxt.Prev = prv; else tail = prv; current = nxt; if (current==null && prv!=null) current = prv; size--; } prv current next

24 Homework Assume that class Node is defined as in slide 17. Draw a picture to show the effect of the following statements. b = a.Next; c = b.Next; b.Data = c.Next.Data; c.Next = a; a 1 2 3 4


Download ppt "Abstract Data Type 1."

Similar presentations


Ads by Google