Pert ke 1 - Pointer, Array and Introduction to Data Structure - 2101680123 - Milliady Kusnadi The Bu Hiap



Pointer, Array and
Introduction to Data Structure

A.   Array
         A collection of similar data elements
         These data elements have the same data type (homogenous)
         The elements of the array are stored in consecutive memory locations and are referenced by an index
         Array index starts from zero

*Array Declaration & Accessing Array
Declaration:
      int arr[5];
Accessing:
      arr[0] = 7;
      arr[1] = 2;
      arr[2] = 13;
      arr[3] = 13;
      arr[4] = 13;
*Array Declaration & Accessing Array
Two Dimensional Array
Declaration:
      int arr[3][6];
Accessing:
      arr[0][2] = 2;
      arr[2][1] = 9;
      arr[1][5] = 13;
      arr[2][4] = 10;
Syntax:
          type name[size1][size2];
      The first index of array ranged from 0 to size1 – 1.
      The second index of array ranged from 0 to size2 – 1.
*Array Declaration & Accessing Array
Multi Dimensional Array
Declaration:
      int arr[4][3][7][10];
Accessing:
      arr[0][2][2][9]= 2;
      arr[2][1][6][0]= 9;
      arr[3][0][0][6]= 13;
      arr[2][1][3][8]= 10;
Syntax:
type ame[size1][size2][size3][...];
      The first index of array ranged
from 0 to size1 – 1.
      The second index of array ranged
from 0 to size2 – 1.
      The third index of array ranged
from 0 to size3 – 1.
      and so on.
*Storing Array Values
         Initialization of Arrays
          Example:    int marks[5] = {90, 82, 78, 95, 88};
         Inputting Values
          Example:    int i, marks[10];
                             for (i=0; i<10; i++)
                                      scanf(“%d”, &marks[i]);
         Assigning Values
          Example:    int i, arr1[10], arr2[10];
                             for(i=0; i<10; i++)
                                      arr2[i] = arr1[i];



*Operations in Array
There are a number of operations that can be performed
on arrays. They are:
         Traversal
         Insertion
         Searching
         Deletion
         Merging
         Sorting
B.   Pointer
Pointer is a data type whose value refers to another value stored 
elsewhere in computer memory using its address.
The two most important operators used with pointer type are:
 & the address operator
 * the dereferencing operator
If we have the declaration:
     int x;
     int *px;
then x is an integer and px is a pointer to an integer. If we say:
     px  = &x;
then &x returns the address of x and assigns it as the value of px.
To assign a value of x we can say
     x = 10;
     *pi = 10;
What is the output of this program?
int a  = 10;
int *p = &a;
printf( “%d\n”, *p );
a  = 17;
*p = 20;
printf( “%d\n”, a );
C.   Data Structure
      A data structure is an arrangement of data, either in the computer’s memory or on the disk storage.
      Some common examples of data structures include:
     Arrays
     Linked lists
     Queues
     Stacks
     Binary trees
     Hash tables
*Types of Data Structure
      Arrays
     A collection of similar data elements
     Data elements have the same data type
·           Linked Lists
     A very dynamic data structure in which the elements can be added to or deleted from anywhere at will
     Each element is called a node
·           Queue
     The element that was inserted first is the first one to be taken out
     The elements in a queue are added at one end called the rear and removed from the other end called the front
·           Stacks
     Stacks can be represented as a linear array
     Every stack has a variable TOP associated with it
     LIFO (Last In First Out) / FILO (First In Last Out)
     Binary Trees
     A data structure which is defined as a collection of elements called the nodes
     Every node contains a left pointer, a right pointer, and a data element
D.   Data Type
      Data Type is a collection of objects and a set of operations that act on those objects.
      For example, the data type int consists of:
         objects                  : 0, +1, -1, +2, -2, etc
         operations  : +, -, *, /, %, etc
      Example of predefined data types are int, char, float.
*Abstract Data Type
      Abstract Data Type (ADT) is a data type that is organized in such a way that the specification of the objects and the specification of the operations on the objects is separated from the representation of the objects and the implementation of the operations.
      C/C++ has a concept called class and struct which assist the programmer in implementing abstract data type.
*Example of ADT
      Supposed we want to create an ADT of natural number which has integer number as objects and several functions as operation.
      structure Number is
          objects        : an integer x
          functions     :
                   bool is_zero()       if ( x == 0 ) return TRUE else return FALSE
                   bool equal(y)        if ( x == y ) return TRUE else return FALSE
                   void set(y)  x = y
                   void add(y) x = x + y
                   int get ()                return x
E.   Summary
      Pointer is a data type whose value refers to another value stored elsewhere in computer memory using its address
      Array is a collection of similar data elements
      Array index starts from zero
      A data structure is an arrangement of data, either in the computer’s memory or on the disk storage
      Some common examples of data structures include: arrays, linked lists, queues, stacks, binary trees, hash tables
      Abstract Data Type (ADT) is a data type that is organized in such a way that the specification of the objects and the specification of the operations on the objects is separated from the representation of the objects and the implementation of the operations

Komentar