연결리스트 숫자를 넣었을때 오름차순으로 정렬하기

조회수 1648회

/* void insert(int x)

  1. 공백 list일 경우
  2. 앞에 삽입
  3. 중간에 삽입 void delete(int x)
  4. 앞을 삭제
  5. 중간 삭제
  6. 없는 data 삭제 main {insert(3) 1 4 5 */

include

include

typedef struct node *nodeptr;

typedef struct node{ int data; nodeptr link; } node;

nodeptr head = NULL;

void Linsert(int x) { nodeptr head = (nodeptr)malloc(sizeof(node)); nodeptr newpy = (nodeptr)malloc(sizeof(node));//newpy에 struct Node 만큼 동적할 nodeptr curr; nodeptr prev; if(head == NULL){ newpy -> link = NULL; newpy -> data =x;

} else{

while(curr!=NULL){ if(curr->data <= x){ if(curr->link == NULL){ newpy ->link = NULL; newpy ->data = x; curr->link = newpy; } else if(curr->link->data > x){ newpy -> link = curr -> link; newpy->data = x; curr->link = newpy; } else{ newpy -> link = curr -> link; newpy -> data = x; curr -> link = newpy;

   }
   curr = curr->link;
}

}

} }

void printList() { nodeptr curr; curr=head; while(curr != NULL) { printf("%d",curr->data); curr=curr->link; } printf("\n"); }

int main() { Linsert(3); //Linsert(1); //Linsert(4); //Linsert(5); printList();

     return 0;

}

  • 슬슬 과제철이 도래하나 봅니다...물론 과제를 질문할 수 있습니다만 이런식은 아닙니다. 해보고 잘 안되는 부분을 구체적으로 질문하시기 바랍니다. 정영훈 2019.4.5 00:24

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)