[JavaScript] 두 날짜간의 날수 리턴

Posted by MD워시퍼
2011. 1. 19. 17:09 Study
728x90
function getDaysBetweenBothDate(s_date, e_date) {
  s_date = removeFmt(s_date);
  e_date = removeFmt(e_date);
  if (s_date != "" && e_date != "") {
    if( parseFloat(s_date) <= parseFloat(e_date)) {
      daysS_date = calculate_total_days(s_date);
      daysE_date = calculate_total_days(e_date);

      days = daysE_date - daysS_date;
      return days;
    }
  }
  return "-1" ;
}

Double Linked List

Posted by MD워시퍼
2009. 7. 14. 10:01 Study
728x90
#include <stdio.h>
#include <stdlib.h>

typedef struct link {
    int key;
    struct link *next;
    struct link *pre;
} link_list;

link_list *head;
link_list *tail;

void insert_node(int count) {
    link_list *temp, *p = head;

    temp = (link_list *)malloc(sizeof(link_list));

    temp->key = count;

    while(p->next != tail && p->next->key < count) {
        p = p->next;
    }

    temp->next = p->next;
    temp->pre = p->next->pre;
    p->next->pre = temp;
    p->next = temp;


}

void delete_node(int key) {
 link_list *temp, *p;

 p = head;

 if(head->key == key) {
  temp = head;
  head = head->next;
 } else {
  while(true) {
   if(p->next->key == key) {
    temp = p->next;
    p->next = p->next->next;
    p->next->pre = p->next->pre->pre;
    break;
   } else if(p->next == NULL){
    printf("데이터가 존재하지 않습니다");
    return;
   } else {
    p = p->next;
   }
  }
 }
 free(temp);
}

void main() {
    int count;
    link_list *p;

    head = (link_list *)malloc(sizeof(link_list));
    tail = (link_list *)malloc(sizeof(link_list));

    head->next = tail;
    head->pre = NULL;
    tail->next = NULL;
    tail->pre = head;
    p = head;
   
    for(int i = 0 ; i < 50 ; i++) {
        count = (int)(rand()%100);
        insert_node(i);  // ①
    }
    delete_node(11);
    do {
        p = p->next;
        printf("%d ",p->key);
    }while(p->next != tail);

    do {
        p = p->pre;
        printf("%d ",p->key);
    }while(p->pre != head);

    printf("\n");
}

짝수가 먼저 출력이 되고, 홀수가 나중에 출력이 되는 Linked_List

Posted by MD워시퍼
2009. 7. 13. 13:33 Study
728x90
#include <stdio.h>
#include <stdlib.h>

typedef struct link {
    int key;
    struct link *next;
} link_list;

link_list *head;
link_list *tail;
int count = 1;

void insert_node() {
    link_list *temp, *p = head;

    temp = (link_list *)malloc(sizeof(link_list));

    temp->key = count++;

    switch(temp->key % 2) {
        case 0:
            while(p->next != tail && (p->next->key % 2) == 0) {
                p = p->next;
            }
           
            break;
        case 1:
            while(p->next != tail) {
                p = p->next;
            }
            break;
    }
    temp->next = p->next;
    p->next = temp;
}

void delete_node(int key) {
 link_list *temp, *p;

 p = head;

 if(head->key == key) {
  temp = head;
  head = head->next;
 } else {
  while(true) {
   if(p->next->key == key) {
    temp = p->next;
    p->next = p->next->next;
    break;
   } else if(p->next == NULL){
    printf("데이터가 존재하지 않습니다");
    return;
   } else {
    p = p->next;
   }
  }
 }
 free(temp);
}

void main() {
    link_list *pointer;

    head = (link_list *)malloc(sizeof(link_list));
    tail = (link_list *)malloc(sizeof(link_list));

    head->next = tail;
    tail->next = NULL;
   
    for(int i = 0 ; i < 100 ; i++) {
        insert_node();  // ①
    }

    do {
        head = head->next;
        printf("%d ",head->key);
    }while(head->next != tail);
}

LINKED LIST SOURCE

Posted by MD워시퍼
2009. 7. 11. 21:37 Study
728x90

/*                                                           */
/*  LINKED1.C  :   Simple Linked list example                */
/*                                                           */
/*                  Programmed By Lee jaekyu                 */
/*                                                           */

#include <stdio.h>

typedef struct _node
    {
    int key;
    struct _node *next;
    } node;

node *head, *tail;

void init_list(void)
    {
    head = (node*)malloc(sizeof(node));
    tail = (node*)malloc(sizeof(node));
    head->next = tail;
    tail->next = tail;
    }

int delete_next(node *t)
    {
    node *s;
    if (t->next == tail)
        return 0;  /* can't delete tail */
    s = t->next;
    t->next = t->next->next;
    free(s);
    return 1;
    }

node *insert_after(int k, node* t)
    {
    node *s;
    s = (node*)malloc(sizeof(node));
    s->key = k;
    s->next = t->next;
    t->next = s;
    return s;
    }

node *find_node(int k)
    {
    node *s;
    s = head->next;
    while (s->key != k && s != tail)
        s = s->next;
    return s;
    }

int delete_node(int k)
    {
    node *s;
    node *p;
    p = head;
    s = p->next;
    while (s->key != k && s != tail)
        {
        p = p->next;
        s = p->next;
        }
    if (s != tail)  /* if find */
        {
        p->next = s->next;
        free(s);
        return 1;
        }
    else
        return 0;
    }

node *insert_node(int t, int k)  /* before k, insert t */
    {
    node *s;
    node *p;
    node *r;
    p = head;
    s = p->next;
    while (s->key != k && s != tail)
        {
        p = p->next;
        s = p->next;
        }
    if (s != tail)  /* if find */
        {
        r = (node*)malloc(sizeof(node));
        r->key = t;
        p->next = r;
        r->next = s;
        }
    return p->next;
    }

node *ordered_insert(int k)
    {
    node *s;
    node *p;
    node *r;
    p = head;
    s = p->next;
    while (s->key <= k && s != tail)
        {
        p = p->next;
        s = p->next;
        }
    r = (node*)malloc(sizeof(node));
    r->key = k;
    p->next = r;
    r->next = s;
    return r;
    }

void print_list(node* t)
    {
    printf("\n");
    while (t != tail)
        {
        printf("%-8d", t->key);
        t = t->next;
        }
    }

node *delete_all(void)
    {
    node *s;
    node *t;
    t = head->next;
    while (t != tail)
        {
        s = t;
        t = t->next;
        free(s);
        }
    head->next = tail;
    return head;
    }

void main(void)
    {
    node *t;

    init_list();
    ordered_insert(10);
    ordered_insert(5);
    ordered_insert(8);
    ordered_insert(3);
    ordered_insert(1);
    ordered_insert(7);
    ordered_insert(8);

    printf("\nInitial Linked list is ");
    print_list(head->next);

    printf("\nFinding 4 is %ssuccessful", find_node(4) == tail ? "un" :
"");

    t = find_node(5);
    printf("\nFinding 5 is %ssuccessful", t == tail ? "un" : "");

    printf("\nInserting 9 after 5");
    insert_after(9, t);
    print_list(head->next);

    t = find_node(10);
    printf("\nDeleting next last node");
    delete_next(t);
    print_list(head->next);

    t = find_node(3);
    printf("\nDeleting next 3");
    delete_next(t);
    print_list(head->next);

    printf("\nInsert node 2 before 3");
    insert_node(2, 3);
    print_list(head->next);

    printf("\nDeleting node 2");
    if (!delete_node(2))
        printf("\n  deleting 2 is unsuccessful");
    print_list(head->next);

    printf("\nDeleting node 1");
    delete_node(1);
    print_list(head->next);

    printf("\nDeleting all node");
    delete_all();
    print_list(head->next);
    }

Linked_List 의 삽입과 삭제

Posted by MD워시퍼
2009. 7. 10. 18:46 Study
728x90

#include <stdio.h>
#include <stdlib.h>

typedef struct link {
    int key;
    struct link *next;
} link_list;

link_list *head;
link_list *tail;
int count = 0;
void insert_node() {
    link_list *temp, *p = head;

    temp = (link_list *)malloc(sizeof(link_list));

    temp->key = count++;

 while(p->next != tail) {
  p = p->next;
 }

 temp->next = p->next;
    p->next = temp;
}

void delete_node(int key) {
 link_list *temp, *p;

 p = head;

 if(head->key == key) {
  temp = head;
  head = head->next;
 } else {
  while(true) {
   if(p->next->key == key) {
    temp = p->next;
    p->next = p->next->next;
    break;
   } else if(p->next == NULL){
    printf("데이터가 존재하지 않습니다");
    return;
   } else {
    p = p->next;
   }
  }
 }
 free(temp);
}

void main() {
    link_list *pointer;

    head = (link_list *)malloc(sizeof(link_list));
    tail = (link_list *)malloc(sizeof(link_list));

    head->key = count++;
    tail->key = count++;
    head->next = tail;
    tail->next = NULL;

    insert_node();  // ①
    insert_node();  // ②
    insert_node();
    insert_node();
    delete_node(0);
    delete_node(3);

    do {
        printf("%d ",head->key);
        head = head->next;
    }while(head != NULL);
}