Selasa, 27 Februari 2018

Linked List Implementation 1


Linked List Implementation 1


I. Single Linked List

To create a list, we first need to define a node structure for the list.
Supposed we want to create a list of integers.

struct tnode {
  int value;
  struct tnode *next;
};

struct tnode *head = 0;

II. Single Linked List : Insert

To insert a new value, first we should dynamically allocate a new node and assign the value to it
and then connect it with the existing linked list.

Supposed we want to append the new node in front of the head.

struct tnode *node =
  (struct tnode*) malloc(sizeof(struct tnode));
node->value = x;
node->next  = head;
head = node;

III. Single Linked List : Delete

To delete a value, first we should find the location of node which store the value we want to 
delete, remove it, and connect the remaining linked list.

struct tnode *curr = head;

// if x is in head node
if ( head->value == x ) {
  head = head->next;
  free(curr);
}
// if x is in tail node
else if(tail->value == x){
  while(curr->next!=tail) curr = curr->next;
  free(tail); tail = curr;
  tail->next = NULL;
}
// if x is not in head node, find the location
else {
  while ( curr->next->value != x ) curr = curr->next;
  struct tnode *del = curr->next;
  curr->next = del->next;
  free(del);
}

III. Polynomial Representation

•Polynomial is given as 6x3 + 9x2 + 1
•Every individual term in a polynomial consists of two parts, a coefficient and a power
•Here, 6, 9, 7, and 1 are the coefficients of the terms that have 3, 2, 1, and 0 as their
 power respectively.
•Every term of a polynomial can be represented as a node of the linked list.
\
IV. Circular Single Linked List

In circular, last node contains a pointer to the first node. We can have a circular singly linked list 
as well as a circular doubly linked list. There is no storing of NULL values in the list.


V. Doubly Linked List

Doubly linked list or two-way linked list is a linked list data structure with two link, 
one that contain reference to the next data and one that contain reference to the 
previous data.
          struct tnode {
  int value;
  struct tnode *next;
  struct tnode *prev;
};

struct tnode *head = 0;
struct tnode *tail = 0;

VI. Doubly Linked List : Insert

Just like in a single linked list, first we should allocate the new node and assign the value
 to it, and then we connect the node with the existing linked list.

Supposed we want to append the new node behind the tail.

struct tnode *node =
  (struct tnode*) malloc(sizeof(struct tnode));
node->value = x;
node->next  = NULL;
node->prev  = tail;
tail->next  = node;
tail = node;

VII. Doubly Linked List : Delete

There are 4 conditions we should pay attention when deleting:
•The node to be deleted is the only node in linked list.
•The node to be deleted is head.
•The node to be deleted is tail.
•The node to be deleted is not head or tail.

1.  If the node to be deleted is the only node:
      free(head);
      head = NULL;
      tail = NULL;

2.If the node to be deleted is head:
      head = head->next;
      free(head->prev);
      head->prev = NULL;

3.If the node to be deleted is tail:
      tail = tail->prev;
      free(tail->next);
      tail->next = NULL;

4.If the node to be deleted is not in head or tail:

            struct tnode *curr = head;
     while ( curr->next->value != x ) curr = curr->next;
struct tnode *a   = curr;
struct tnode *del = curr->next;
struct tnode *b   = curr->next->next;
a->next = b;
b->prev = a;
free(del);

VIII. Circular Doubly Linked List

It is similar with circular single linked list, but total pointer in each node here is 
2 (two) pointers.




IX. Header Linked List

A header linked list is a special type of linked list which contains a header node at 
the beginning of the list. So, in a header linked list, START (L) will not point to the first 
node of the list but START (L) will contain the address of the header node.


Selasa, 20 Februari 2018

Array, Pointer and Introduction to Data Structure


Pointer, Array and Introduction 
to Data Structure

  I. Array

Array is a collection of similiar data elements. The data elements in an array always have the same data type (homogenous). The elements of the array are stored in consecutive  memory locations and are referenced by an index, which starts from index 0.

  • Array Declaration and Accessing Array
There are two types of array:
-One Dimensional Array
-Multi Dimensional Array


One Dimensional Array

Syntax : type name[size N];

An array of size N have indexes from 0 to N-1
Example:
Declaration:
 char arr[5];

Accessing  : 
arr[0]=H;
arr[1]=e;
arr[2]=l;
arr[3]=l;
arr[4]=o;


Multi Dimensional Array

In multi dimensional array, there is a limit of 255 indexes.

Syntax : type name[size1][size2]...[sizeN];

size1 ranged from index 0 to size-1
size2 ranged from index 0 to size-1, and so on
Example:

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;

       •Storing Array Values

1. Initialization
    Example : int marks[5] : {90,80,78,82,85};

2. Inputting Values
    Example : int i, marks[10];
                     for(i=0;i<10;i++){
                           scanf("%d",&marks[i]);
                     }

3. Assigning Values
    Example : int i, arr1[10], arr2[10];
                     for(i=0;i<10;i++){
                     arr2[i] = arr1[i]
                     }

     •Operations in Array

There are some operations that can be performed on arrays, such as :
-Transversal
-Insertion
-Searching
-Deletion
-Merging
-Sorting


II. Pointer

Pointer is a data type whose value refers to another value stored elsewhere in computer memory using its address.
There is two kinds of pointer, single and multiple pointer,
The single pointer is pointing towards a value using its memory address, while the multiple pointer  
is the pointer pointing the pointer before itself.

The two most important operators used with pointer type are:
• &  the address operator
• *   the dereferencing operator

Example

Declaration :
int x;           (x is an integer)
int *px;       (*px is a pointer to an integer)

If we say :
*px = &x;

That means *px returns the address value of x.
To assign the value of x we can declare:
x = 10
or
*px=10

III. Data Structure
A data structure is an arrangement of data, either in the computer’s memory or on the disk storage.

Types of Data Structure

     •Array
      A collection of similiar data elements which has a same data type

     •Linked List
      A very dynamic data structure in which the elements can be added to or deleted from anywhere
      at will

     •Queue
     A type of data structure which the element that was inserted first is the first one to be taken out.

      •Stacks
     Stacks can be represented as a linear array. Every stack has a variable TOP associated with it

      •Binary Trees
      A data structure which is defined as a collection of elements called the node
      Every node contains a left pointer, a right pointer, and a data element.

      •Data Type
       Data Type is a collection of objects and a set of operations that act on those objects.

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.