Abstract Data Type 1.

Slides:



Advertisements
Similar presentations
1 - Recursion on linked lists Lecture 7 ADS2 Lecture 7.
Advertisements

Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Linked Lists Chapter 4.
CS 367 – Introduction to Data Structures
Data Structures ADT List
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Lecture 5 Sept 12, 2011 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists.
Linked Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Chapter 17 Linked List.
Complex Structures Nested Structures Self referential structures A structure may have Data variables Internal structures/unions Pointer links Function.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Chapter 5 Linked Lists II
CS2006- Data Structures I Chapter 5 Linked Lists III.
Sequences1 Vectors Positions Lists General Sequences Bubble Sort Algorithm.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
Complex Structures Nested Structures Self referential structures A structure may have Data variables Internal structures/unions Pointer links Function.
CSCS-200 Data Structure and Algorithms Lecture
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
One implementation of the LIST ADT Insert new node before current and new node becomes current (assume new node created) node newNode = new node; head.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Linked List, Stacks Queues
Linked List ADT used to store information in a list
Lecture 6 of Computer Science II
Elementary Data Structures
Cpt S 122 – Data Structures Abstract Data Types
G64ADS Advanced Data Structures
Data Structure Dr. Mohamed Khafagy.
Doubly Linked List Review - We are writing this code
UNIT-3 LINKED LIST.
Data Structures and Algorithms
Chapter 4 Linked Lists.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Algorithm for deleting a node from a singly linked list
Priority Queue.
Linked List Sudeshna Sarkar.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked node problem 3 What set of statements turns this picture:
Linked Lists: Implementation of Queue & Deque
Data Structures ADT List
Data Structures ADT List
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Lecture 7: Linked List Basics reading: 16.2
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
ADT list.
Doubly Linked List Implementation
Data Structures and Algorithms
Stacks and Queues 1.
Linked Lists The items in a linked list data structure are linked to one another An item in a linked list references its successor A linked list is able.
Data Structures ADT List
Linked Lists & Iterators
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Data Structures & Algorithms
Building Java Programs
Doubly Linked List Implementation
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

Abstract Data Type 1

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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