Á¤¼ºÈÆ
    Å×½ºÆ® Áß



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

#define TRUE 1
#define FALSE 0

typedef int element;
typedef struct ListNode {
 element data;
 struct ListNode *link;
} ListNode;

typedef struct {
 ListNode *head;   // Çìµå Æ÷ÀÎÅÍ
 int length;  // ³ëµåÀÇ °³¼ö
} ListType;

void init(ListType *list)
{
 list->head = NULL;
 list->length = 0;
}

ListType list1;

void insert_node(ListNode **phead, ListNode *p, ListNode *new_node)
{
 if (*phead == NULL) { // °ø¹é¸®½ºÆ®ÀÎ °æ¿ì
  new_node->link = NULL;
  *phead = new_node;
 }
 else if (p == NULL) { // p°¡ NULLÀ̸é ù¹ø° ³ëµå·Î »ðÀÔ
  new_node->link = *phead;
  *phead = new_node;
 }
 else {    // p ´ÙÀ½¿¡ »ðÀÔ
  new_node->link = p->link;
  p->link = new_node;
 }
}
void remove_node(ListNode **phead, ListNode *p, ListNode *removed)
{
 if (p == NULL)
  *phead = (*phead)->link;
 else
  p->link = removed->link;
 free(removed);
}

void insert_first(ListNode **phead, ListNode *node)
{
 if (*phead == NULL) {
  *phead = node;
  node->link = node;
 }
 else {
  node->link = (*phead)->link;
  (*phead)->link = node;
  *phead = node;
 }
}

void insert_last(ListNode **phead, ListNode *node)
{
 if (*phead == NULL) {
  *phead = node;
  node->link = node;
 }
 else {
  node->link = (*phead)->link;
  (*phead)->link = node;
  
 }
}

void error(char *str)
{
 printf("%s\n", str);
 exit(1);
}

int is_empty(ListType *list)
{
 if (list->head == NULL) return 1;
 else return 0;
}

int get_length(ListType *list)
{
 return list->length;
}

ListNode *get_node_at(ListType *list, int pos)
{
 int i;
 ListNode *tmp_node = list->head;
 if (pos < 0) return NULL;
 for (i = 0; i < pos; i++)
  tmp_node = tmp_node->link;
 return tmp_node;
}
// ÁÖ¾îÁø À§Ä¡¿¡ µ¥ÀÌÅ͸¦ »ðÀÔÇÑ´Ù.
void add(ListType *list, int position, element data)
{
 ListNode *p;
 if ((position >= 0) && (position <= list->length)) {
  ListNode*node = (ListNode *)malloc(sizeof(ListNode));
  if (node == NULL) error("¸Þ¸ð¸® ÇÒ´ç¿¡·¯");
  node->data = data;
  p = get_node_at(list, position - 1);
  insert_node(&(list->head), p, node);
  list->length++;
 }
}
void delete(ListType *list, int pos)
{
 if (!is_empty(list) && (pos >= 0) && (pos < list->length)) {
  ListNode *p = get_node_at(list, pos - 1);
  remove_node(&(list->head), p, (p != NULL) ? p->link : NULL);
  list->length--;
 }
}

element get_entry(ListType *list, int pos)
{
 ListNode *p;
 if (pos >= list->length) error("À§Ä¡ ¿À·ù");
 p = get_node_at(list, pos);
 return p->data;
}

void display(ListType *list)
{
 int i;
 ListNode *node = list->head;
 printf("( ");
 for (i = 0; i < list->length; i++) {
  printf("%d ", node->data);
  node = node->link;
 }
 printf(" )\n");
}

int is_in_list(ListType *list, element item)
{
 ListNode *p;
 p = list->head;  // Çìµå Æ÷ÀÎÅÍ¿¡¼­ºÎÅÍ ½ÃÀÛÇÑ´Ù.
 while ((p != NULL)) {
  // ³ëµåÀÇ µ¥ÀÌÅÍ°¡ itemÀ̸é
  if (p->data == item)
   break;
  p = p->link;
 }
 if (p == NULL) return FALSE;
 else return TRUE;
}
void add_last(ListType *list, element data)
{
 ListNode *newnode;
 newnode = (ListNode *)malloc(sizeof(ListNode));
 newnode->data = data;
 if (newnode == (ListNode *)NULL) error("¸Þ¸ð¸® ¿À·ù");
 insert_last(&(list->head), newnode);
 list->length++;
}
void add_first(ListType *list, element data)
{
 ListNode *newnode;
 newnode = (ListNode *)malloc(sizeof(ListNode));
 newnode->data = data;
 if (newnode == (ListNode *)NULL) error("¸Þ¸ð¸® ¿À·ù");
 insert_first(&(list->head), newnode);
 list->length++;
}
int main()
{
 ListType list1;
 init(&list1);
 add(&list1, 0, 20);
 add_last(&list1, 30);
 printf("here:\n");
 display(&list1);
 add_first(&list1, 10);
 add_last(&list1, 40);
 // list1 = (10, 20, 30, 40)
 display(&list1);
 // list1 = (10, 20, 30)
 delete(&list1, 3);
 display(&list1);
 // list1 = (20, 30)
 delete(&list1, 0);
 display(&list1);
 printf("%s\n", is_in_list(&list1, 20) == TRUE ? "¼º°ø" : "½ÇÆÐ");
 printf("%d\n", get_entry(&list1, 0));
}

  µî·ÏÀÏ : 2018-11-08 [19:17] Á¶È¸ : 1723 ´Ù¿î : 0   
 
¡â ÀÌÀü±Û±³Àç ÇÁ·Î±×·¥ Âü°í ´Ù¿î·Îµå ¾ÈµÉ ¶§
¡ä ´ÙÀ½±Û(¿¹ºñ 1) ¹®ÀÚ¿­ ó¸® ÇÔ¼ö
ÀڷᱸÁ¶ ½Ç½À°Ô½ÃÆÇ
¹øÈ£ ¨Ï Á¦ ¸ñ À̸§ Á¶È¸ µî·ÏÀÏ
±³Àç ÇÁ·Î±×·¥ Âü°í ´Ù¿î·Îµå ¾ÈµÉ ¶§
68 (ÀÓ½Ã) ÀÌÁß Æ÷ÀÎÅÍÀÇ ÀÌÇØ ½Ç½À Á¤¼ºÈÆ 300 11-21
67 (ÀÓ½Ã) ´Ü¼ø¿¬°á ¸®½ºÆ® insert_node ÁýÁß ºÐ¼® ½Ç½À Á¤¼ºÈÆ 404 11-21
66 ±³Àç ÇÁ·Î±×·¥ Âü°í ´Ù¿î·Îµå ¾ÈµÉ ¶§ ½Ç½À Á¤¼ºÈÆ 767 10-03
65 Å×½ºÆ® Áß ½Ç½À Á¤¼ºÈÆ 1723 11-08
64 (¿¹ºñ 1) ¹®ÀÚ¿­ ó¸® ÇÔ¼ö ½Ç½À Á¤¼ºÈÆ 2728 05-14
63 ¦¦❶ ÈùÆ®1 - strlen ±¸Çö ¿¹) °è¼Ó Á¤¼ºÈÆ 2003 06-05
62 ¦¦❶ l½Ç½À ÇÁ·Î±×·¥ ¿¹) ºñ°ø°³ °è¼Ó Á¤¼ºÈÆ 170 05-14
61 (½Ç½À 12) ·Î¶Ç Á¶ÇÕ »ý¼º±â ½Ç½À Á¤¼ºÈÆ 3274 05-26
60 ¦¦❶ ½Ç½À ÇÁ·Î±×·¥ ¿¹) ºñ°ø°³ °è¼Ó Á¤¼ºÈÆ 94 05-26
59 (½Ç½À 11) ½Ã¹Ä·¹ÀÌ¼Ç ½Ç½À Á¤¼ºÈÆ 8891 05-23
58 ¦¦❶ Ãß°¡ 1)ÀÇ °á°úÈ­¸é ¿¹ °è¼Ó Á¤¼ºÈÆ 1873 06-03
57    ¦¦❷ Ãß°¡ 2)ÀÇ °á°úÈ­¸é ¿¹ °è¼Ó Á¤¼ºÈÆ 1690 06-03
56 ¦¦❶ ½Ç½À ÇÁ·Î±×·¥ ¿¹) ºñ°ø°³ °è¼Ó Á¤¼ºÈÆ 303 05-23
55    ¦¦❷ ½Ç½À ÇÁ·Î±×·¥ ¿¹2) ºñ°ø°³ °è¼Ó Á¤¼ºÈÆ 147 06-03
54 (½Ç½À 10) ¹Ì·Îã±â ½Ç½À Á¤¼ºÈÆ 5601 05-15

[1][2][3][4][5]