
Data Structure

This illustrates a data structure, a means of storing data within a program.

You may see other such structures in the Data Structures category.


Task

Create a compound data type:

  Point(x,y) 



A compound data type is one that holds multiple independent values.



Related task

   Enumeration


See also

 Array
 Associative array: Creation, Iteration
 Collections
 Compound data type
 Doubly-linked list: Definition, Element definition, Element insertion, List Traversal, Element Removal
 Linked list
 Queue: Definition, Usage
 Set
 Singly-linked list: Element definition, Element insertion, List Traversal, Element Removal
 Stack


 
# Lightweight JS objects (with CS sugar).
point =
  x: 5
  y: 3
 
console.log point.x, point.y # 5 3
 
# Heavier OO style
class Point
  constructor: (@x, @y) ->
  distance_from: (p2) ->
    dx = p2.x - @x
    dy = p2.y - @y
    Math.sqrt dx*dx + dy*dy
 
p1 = new Point(1, 6)
p2 = new Point(6, 18)
console.log p1 # { x: 1, y: 6 }
console.log p1.distance_from # [Function]
console.log p1.distance_from p2 # 13
 