연결리스트 숫자를 넣었을때 오름차순으로 정렬하기
조회수 1648회
/* void insert(int x)
- 공백 list일 경우
- 앞에 삽입
- 중간에 삽입 void delete(int x)
- 앞을 삭제
- 중간 삭제
- 없는 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;
}
댓글 입력