c언어 양방향연결리스트에서 오름차순while문에대해 모르는 부분있어서 질문드립니다!
조회수 398회
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct {
int data;
struct Node *prev;
struct Node *next;
}Node;
Node *head;
Node *tail;
void addfirst(int data)
{
Node *node=(Node*)malloc(sizeof(Node));
node->data=data;
Node *cur=head->next;
while(cur->data<data&&cur!=tail)
{
cur=cur->next;
}
Node *prev=cur->prev;
cur->prev=node;
node->next=cur;
prev->next=node;
node->prev=prev;
}
void removenode()
{
Node *node=head->next;
head->next=node->next;
Node *prevnode=node->next;
prevnode->prev=head;
}
void showall()
{
Node *cur=head->next;
while(cur!=tail)
{
printf("%d ",cur->data);
cur=cur->next;
}
}
void freenode()
{
Node *cur=head->next;
while(cur!=tail)
{
Node *next=cur->nex
free(cur);
cur=next;
}
}
int main()
{
head=(Node*)malloc(sizeof(Node));
tail=(Node*)malloc(sizeof(Node));
head->next=tail;
head->prev=head;
tail->prev=head;
tail->next=tail;
addfirst(6);
addfirst(7);
addfirst(9);
addfirst(66);
removenode();
showall();
freenode();
}
addfirst
함수에서 오름차순정렬 코드를 저렇게 짠다고 하는데 코드에서 다른 부분은 모두 이해되지만 while(cur->data<data&&cur!=tail)
cur=cur->next
이 부분이 어떻게 오름차순을 만들어주는지 이해가 안돼네요.
어떻게 돌아가서 오름차순으로 정렬되는지 설명해주시면 정말 감사하겠습니다!
댓글 입력