# Data Structures Simple data type –int, char, long Reference data type –Integer, String, Composite data type == Data Structure –Collection of elements.

## Presentation on theme: "Data Structures Simple data type –int, char, long Reference data type –Integer, String, Composite data type == Data Structure –Collection of elements."— Presentation transcript:

Data Structures Simple data type –int, char, long Reference data type –Integer, String, Composite data type == Data Structure –Collection of elements of possibly different types –Class

Linear Data Structures Elements have an order Each element has a predecessor and a successor One element is first, and one is last Examples: –List –Stack –Queue

Hierarchical Data Structures Elements relate in 1:many relationships –Trees One root at the base of the structure One or more leaves most distant from the root 0, 1, or many internal nodes (neither root, nor leaf) Each node has: –a unique predecessor, and –0, 1, or many successors

Graph Data Structure Elements may have many:many relationships with other elements No constraints on numbers of predecessors or successors

Arrays [0] [1] [2] [3] [4] [5] [6] [7] Addr. of a[2] Addr. of a[0] + 2*element-size What’s not to like? 8 advantages: Create one of any size while the program is running. Efficient; only overhead is stored length. Constant-time element access.

Lists An ordered collection of nodes Node contains: –Information (“data”) –Identity of the successor (“next”) –Identity of the predecessor (“previous”)

List Operations/Methods first() last() next() previous() add() / add( pos ) remove() / remove( pos ) append() get() / get( pos ) set() / set( pos ) exists() /contains() /isOnList() size()

From Prof. Heliotis, 2006 Linked List Starts empty; grows as things are added  no wasted space. Insertion / removal from front in constant time… advantages:

From Prof. Heliotis, 2006 Link Nodes, Basic Types class LinkNode { int value; LinkNode next; LinkNode( int v ) { value = v; } // other methods... }; 7 new LinkNode( 7 )

From Prof. Heliotis, 2006 Link Nodes, Generic Contents class LinkNode { E value; LinkNode next; LinkNode( E v ) { value = v; } // other methods... }; new LinkNode( obj ) HeartObj

From Prof. Heliotis, 2006 Insert insertAfter( a, c ); a c b X

From Prof. Heliotis, 2006 Deletion remove( c ); a c b Where is a?

From Prof. Heliotis, 2006 Deletion, Take 2 removeNodeFollowing( a ); a c b X

From Prof. Heliotis, 2006 Doubly Linked Lists (code) class DLinkNode { int value; DLinkNode previous, next; DLinkNode( int v ) { value = v; } // other methods... };

From Prof. Heliotis, 2006 Doubly Linked Lists Now it is possible to directly delete a link node!