#include <stdio.h>

#include <stdlib.h>


typedef int Element;

typedef struct LinkedNode {

   Element data;

   struct LinkedNode* link;

} Node;


void init_list(Node* head) { head = NULL; }

int is_empty(Node* head) { return head == NULL; }


Node* get_entry(Node* head,int pos) {

   Node* p = head;

   int i;

   for (i = 0; i < pos; i++, p = p->link)

      if (p == NULL)return NULL;


   return p;

}


int size(Node* head) {


   Node* p;

   int count = 0;

   for (p = head; p != NULL; p = p->link)

      count++;

   return count;

}


Node* find(Node* head, Element val) {

   Node* p;

   for (p = head; p != NULL; p = p->link)

      if (p->data == val) return p;

   return NULL;

}


void replace(Node* head, int pos, Element val) {

   Node* node = get_entry(head,pos);

   if (node != NULL)

      node->data = val;

}


void insert_next(Node *prev, Node *node) {

   if (node != NULL) {

      node->link = prev->link;

      prev->link = node;

   }

}


void insert(Node* head, int pos, Element e) {

   Node *new_node, *prev;


   new_node = (Node*)malloc(sizeof(Node));

   new_node->data = e;

   new_node->link = NULL;


   if (pos == 0) {


      new_node->link = head;

      head = new_node;

   }

   else {

      prev = get_entry(head, pos - 1);

      if (prev != NULL)

         insert_next(prev, new_node);

      else free(new_node);

   }

}


Node* remove_next(Node *prev) {

   Node* removed = prev->link;

   if (removed != NULL)

      prev->link = removed->link;

   return removed;

}


void delete(Node* head, int pos) {

   Node* prev, *removed;


   if (pos == 0 && is_empty(head) == 0) {

      removed = head;

      head = head->link;

      free(removed);

   }

   else {

      prev = get_entry(head,pos - 1);

      if (prev != NULL) {

         removed = remove_next(prev);

         free(removed);

      }

   }

}


void clear_list(Node* head) {

   while (is_empty(head) == 0)

      delete(head,0);

}


void print_list(Node* head, char* msg)

{

   Node* p;

   printf("%s[%2d]: ", msg, size(head));

   for (p = head; p != NULL; p = p->link)

      printf("%2d ", p->data);

   printf("\n");

}


void node_sum(Node* head) {


   Node* p;

   int sum = 0;

   int count = 1;

   printf("노드의 개수 %d \n", size(head));

   for (p = head; p != NULL; p = p->link)

   {


      printf("노드# %d 데이터:%d \n", count, p->data);

      sum += p->data;

      count++;

   }

   printf("연결 리스트의 데이터 합:%d \n", sum);


}


void find_count(Node* head)

{

   Node* p;

   int count = 1;

   int find;

   int find_count = 0;

   printf("노드의 개수 %d \n", size(head));

   for (p = head; p != NULL; p = p->link)

   {


      printf("노드# %d 데이터:%d \n", count, p->data);

      count++;

   }

   printf("탐색할 값을 입력하십시오:");

   scanf_s("%d", &find);


   for (p = head; p != NULL; p = p->link) {

      if (p->data == find)

         find_count++;

   }


   printf("%d는 연결 리스트에서 %d번 나타납니다", find, find_count);

}



Node *reverse(Node *list)

{

   

   Node *p, *q, *r;

   p = list;

   q = NULL;

   while (p != NULL) {

      r = q;

      q = p;

      p = p->link;

      q->link = r;

   }


   return q;

}


Node *concat(Node *head1, Node *head2)

{

    Node *p;

    if(head1 == NULL) return head2;

    else if(head2 == NULL) return head1;

    else{

        p = head1;

        while(p->link!=NULL){

            p = p->link;

        }

        p->link = head2;

        return head1;

    }

}



void main()

{

Node *list1 = NULL, *list2 = NULL;


init_list(list1);

insert(list1, 0, 1);

insert(list1, 1, 2);

insert(list1, 2, 3);

insert(list1, 3, 4);

insert(list1, 4, 5);

print_list(list1,"연결리스트 1");

}



-------------------------------------------------


이거 이중포인터를 써야하나 insert가 왜 안되지?

3시간째 씨름중인데 어딜 수정해야할지 모르겠다 슬슬 감은 오는거같은데