새로운 테크닉

언어2015. 9. 5. 15:36

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#pragma warning(disable:4996)
struct node
{
 int data;
 struct node *next;
};
int size = 0;

struct node *head = NULL;
struct node *tail = NULL;
struct node *temp = NULL;

//함수 원형----------------------------------------------------------------
//노드에 갯수                                                                 
int sizereturn(struct node **head);                                        
//마지막에     노드 추가                  
void insertLast(struct node **head, struct node **tail);
//처음에        노드 추가
void insertFirst(struct node **head, struct node **tail);
//임시 위치에 노드 추가
void insertRand(struct node **head, struct node **tail);
//데이터 삭제
void insertdelete(struct node **head, struct node **tail);
//데이터 출력
void printfall(struct node *head);
//---------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////
int main(void)
{
 int sel = 0;
 printf("\n기능선택 : 1. 맨뒤에 노드추가, 2. 맨앞에 노드추가, 3. 특정부분 노드추가, 4. 노드 삭제, 5. 노드 출력, 6. 노드 탐색, 7. 사이즈 출력  // 999. 종료\n");
 while (1)
 {
  printf("선택:");
  scanf_s("%d", &sel);
  if (sel == 1)
  {
   //맨뒤에 노드 추가
   insertLast(&head, &tail);
  }
  if (sel == 2)
  {
   //맨앞에 노드 추가
   insertFirst(&head, &tail);
  }
  if (sel == 3)
  {
   //특정부분 노드 추가
   insertRand(&head, &tail);
  }
  if (sel == 4)
  {
   //노드 삭제
   insertdelete(&head, &tail);
  }
  if (sel == 5)
  {
   //노드 출력
   printfall(head);
  }
  if (sel == 7)
  {
   //사이즈 출력
   printf("%d\n", sizereturn(&head));
  }
  if (sel == 999)
  {
   break;
  }
 }
  return 0;
}
//(1)---------------------------------------------------------------------------------
int sizereturn(struct node **head)
{
 int count = 0;
 if ((*head) == NULL)
 {
  return 0;
 }
 else
 {
  while ((*head)->next != NULL)
  {
   count++;
   (*head) = (*head)->next;
  }
  return count + 1;
 }
}
//(1)---------------------------------------------------------------------------------

//(2)---------------------------------------------------------------------------------
void insertLast(struct node **head, struct node **tail)
{
 struct node* n = NULL;
 n = (struct node*)malloc(sizeof(struct node));
 if (n == NULL)
 {
  printf("동적할당 실패!!!\n");
  return;//종료
 }
 else//(n != NULL)
 {
  memset(n, 0, sizeof(struct node));
  size++;//<------------------------------ 데이터 추가
  printf(" 노드에 추가할 값:  ");
  scanf_s("%d", &n->data);
  if ((*head) == NULL)
  {
   (*head) = n;
   (*tail) = n;
  }
  else
  {
   (*tail)->next = n;
   (*tail) = n;
   (*tail)->next = NULL;
  }
 }
}
//(2)---------------------------------------------------------------------------------

//(3)---------------------------------------------------------------------------------
void insertFirst(struct node **head, struct node **tail)
{
 struct node *n;
 n = (struct node*)malloc(sizeof(struct node));
 if (n == NULL)
 {
  printf(" 동적할당 실패!!\n ");
  return;//종료
 }
 else//(n != NULL)
 {
  memset(n, 0, sizeof(struct node));
  size++;//<------------------------------ 데이터 추가
  printf(" 노드에 추가할 값:   ");
  scanf_s("%d", &n->data);
  if ((*head) == NULL)
  {
   (*head) = n;
   (*tail) = n;
  }
  else
  {
   n->next = *head;
   (*head) = n;
  }
 }
}
//(3)---------------------------------------------------------------------------------

//(4)---------------------------------------------------------------------------------
void insertRand(struct node **head, struct node **tail)
{
 struct node* n = NULL;//<-------------------새로운 노드
 struct node* temp = NULL;
 int position         = 0;
 int temp_position = 0;
 
 printf("추가할 위치 입력:   ");
 scanf_s("%d", &position);
 // || or : operator
 if ((position > size-1) || (position < 0))
 {
  printf("추가할 수 없는 위치 입니다.\n");
  return;//종료
 }
 else// ((position <= size-1) || (position >= 0))
 {
  n = (struct node*)malloc(sizeof(struct node));
  if (n == NULL)
  {
   printf("동적할당 실패!!\n");
   return;
  }//n!=NULL
  else
  {
   memset(n, 0, sizeof(struct node));
   //처음 위치에 삽입
   if (position == 0)
   {
    insertFirst(head, tail);
   }
   //마지막 위치에 삽입
   else if (position == size-1)
   {
    insertLast(head, tail);
   }
   //내가 임의로 결정한 위치
   else
   {
    temp = (*head);
    while (temp_position != position)
    {
     temp = temp->next;
     temp_position;
    }
    temp->next = n;
    printf("삽입할 데이터 입력!!!:    ");
    scanf_s("%d", &n->data);
    (*tail) = n;
    (*tail)->next = NULL;
   }
  }
 }
 
}
//(4)---------------------------------------------------------------------------------
//(5)---------------------------------------------------------------------------------
void insertdelete(struct node **head, struct node **tail)
{
   struct node *i, *pre = NULL;
   int data = 0;
   printf("삭제할 숫자 데이터를 입력하세요:  ");
   scanf_s("%d", &data);
   for (i = *head; i != NULL;)
   {
      if (i->data == data)
   {
          if (*head == i)
          {
    *head = i->next;
             free(i);
             i = *head;
          }
          else
          {
             pre->next = i->next;
             free(i);
             i = pre->next;
          }
       }
       else
       {
          pre = i;
          i = i->next;
        }
   }
}
//(5)---------------------------------------------------------------------------------
//(6)---------------------------------------------------------------------------------
void printfall(struct node *head)//전체 출력
{
    struct node *i;
    for (i = head; i != NULL; i = i->next)
    {
       printf("%d ", i->data);
    }
    printf("\n");
}
//(6)---------------------------------------------------------------------------------

 

 

'언어' 카테고리의 다른 글

double_linked_list  (0) 2015.09.07
네이버 지식인 답변  (0) 2015.09.06
str1[i] - '0'  (0) 2015.09.04
네이버 지식인 - 2 = 2015년 08월 30일 (가위바위보 c++)  (0) 2015.08.30
naver 지식인 - 1 (2015/ 08/ 30)  (0) 2015.08.30