삽입식 체인 테이블로 직접 선택 정렬 및 직접 삽입 정렬 예시

8844 단어
mysort()는 정렬을 직접 삽입하고 mysort2()는 정렬을 직접 선택한다.
직접 선택 정렬에 대해 나는 처음에 직접 교환한 결점의 데이터였다. 이것은 프로그램이 단순화되었지만 이것은 하나의 수조와 차이가 있습니까?그래서 나는 또 교환 결점을 바꿨다. 물론 이것은 더 짜증날 것이다. 하나는 4가지 다른 상황이 있다~~~~
[csharp] viewplain copy print ?
  • #include
  • #include
  • typedef structnode
  • {
  • int data;
  • struct node*next;
  • }NODE;
  • NODE * mycreate()
  • {
  • NODE * head = NULL;
  • returnhead;
  • }
  • NODE * myinsert(NODE *head,intdata)
  • {
  • NODE *q = head;
  • if(head ==NULL)
  • {
  • head = (NODE *)malloc(sizeof(NODE));
  • head->data = data;
  • head->next = NULL;
  • }
  • else
  • {
  • while(q->next !=NULL)
  • {
  • q = q->next;
  • }
  • NODE *newnode = (NODE *)malloc(sizeof(NODE));
  • newnode->data = data;
  • q->next = newnode;
  • newnode->next = NULL;
  • }
  • returnhead;
  • }
  • NODE * mysort(NODE *head)
  • {
  • NODE *newhead = mycreate();
  • NODE *q = head;
  • NODE * max;
  • NODE * tmp = NULL;
  • max = (NODE *)malloc(sizeof(NODE));
  • max->data = head->data;
  • NODE *maxpre = head;
  • while(head !=NULL)
  • {
  • max->data = head->data;
  • q = head;
  • while(q->next !=NULL)
  • {
  • if(q->next->data >max->data)
  • {
  • max->data = q->next->data;
  • tmp = q->next;
  • maxpre = q;
  • }
  • q = q->next;
  • }
  • newhead = myinsert(newhead,max->data);
  • if(tmp !=NULL)
  • {
  • maxpre->next = tmp->next;
  • if(tmp ==head)
  • {
  • head = head->next;
  • }
  • free(tmp);
  • tmp = NULL;
  • }
  • else
  • {
  • free(head);
  • head = NULL;
  • }
  • }
  • returnnewhead;
  • }
  • NODE * mysort2(NODE *head)
  • {
  • NODE *q = head;
  • NODE *last = head;
  • NODE *p = NULL;
  • NODE *max = NULL;
  • NODE *premax = NULL;
  • NODE *nextmax = NULL;
  • NODE *prelast = NULL;
  • NODE *lastnext = NULL;
  • int tmp;
  • while(last->next !=NULL)
  • {
  • p = last;
  • lastnext = last->next;
  • max = p;
  • while(p->next !=NULL)
  • {
  • if(p->next->data >max->data)
  • {
  • max = p->next;
  • premax = p;
  • }
  • p = p->next;
  • }
  • if(last ==head)
  • {
  • nextmax = max->next;
  • if(premax ==last)
  • {
  • last->next = nextmax;
  • max->next = last;
  • head = max;
  • }
  • else
  • {
  • premax->next = last;
  • head->next = max->next;
  • max->next = lastnext;
  • head = max;
  • }
  • last = head->next;
  • prelast = head;
  • }
  • else
  • {
  • nextmax = max->next;
  • prelast->next = max;
  • if(premax ==last)
  • {
  • max->next = last;
  • last->next = nextmax;
  • }
  • else
  • {
  • premax->next = last;
  • last->next = nextmax;
  • max->next = lastnext;
  • }
  • last = prelast->next;
  • prelast = last;
  • last = last->next;
  • }
  • }
  • returnhead;
  • }
  • void myprint(NODE*head)
  • {
  • if(head ==NULL)
  • {
  • printf("Empty !");
  • }
  • else
  • {
  • while(head->next !=NULL)
  • {
  • printf("%d\t",head->data);
  • head = head->next;
  • }
  • printf("%d",head->data);
  • }
  • }
  • intmain()
  • {
  • NODE *head = mycreate();
  • head = myinsert(head,1);
  • head = myinsert(head,2);
  • head = myinsert(head,3);
  • head = myinsert(head,4);
  • head = myinsert(head,5);
  • head = myinsert(head,6);

  • 4
  • printf(정렬 전:)
  • myprint(head);
  • head = mysort2(head);

  • 4
  • printf("정렬 후:")
  • myprint(head);
  • return 0;
  • }
  • #include 
    #include 
    
    typedef struct node 
    {
        int data;
        struct node *next;
    }NODE;
    
    NODE * mycreate()
    {
        NODE * head = NULL;
        return head;
    }
    
    NODE * myinsert(NODE *head,int data)
    {
        NODE *q = head;
        if(head == NULL)
        {
            head = (NODE *)malloc(sizeof(NODE));
            head->data = data;
            head->next = NULL;
        }
        else
        {
            while(q->next != NULL)
            {
                q = q->next;
            }
            NODE *newnode = (NODE *)malloc(sizeof(NODE));
            newnode->data = data;
            q->next = newnode;
            newnode->next = NULL;
        }
        return head;
    }
    
    NODE * mysort(NODE *head)
    {
        NODE *newhead = mycreate();
        NODE *q = head;
        NODE * max;
        NODE * tmp = NULL;
        max = (NODE *)malloc(sizeof(NODE));
        max->data = head->data;
        NODE *maxpre = head;
        while(head != NULL)
        {
                max->data = head->data;
                q = head;
            while(q->next != NULL)
            {
                if(q->next->data > max->data)
                {
                    max->data = q->next->data;
                    tmp = q->next;
                    maxpre = q;
                }
                q = q->next;
            }
            newhead = myinsert(newhead,max->data);
            if(tmp != NULL)
            {
                maxpre->next = tmp->next;
                if(tmp == head)
                {
                    head = head->next;
                }
                free(tmp);
                tmp = NULL;
            }
            else
            {
                free(head);
                head = NULL;
            }
        }
        return newhead;
    }
    
    NODE * mysort2(NODE *head)
    {
        NODE *q = head;
        NODE *last = head;
        NODE *p = NULL;
        NODE *max = NULL;
        NODE *premax = NULL;
        NODE *nextmax = NULL;
        NODE *prelast = NULL;
        NODE *lastnext = NULL;
        int tmp;
        while(last->next != NULL)
        {
            p = last;
            lastnext = last->next;
            max = p;
            while(p->next != NULL)
            {
                if(p->next->data > max->data)
                {
                    max = p->next;
                    premax = p;
                }
                p = p->next;
            }
            if(last == head)
            {
                    nextmax = max->next;
                    if(premax == last)
                    {
                        last->next = nextmax;
                        max->next = last;
                        head = max;  
                    }
                    else
                    {
                        premax->next = last;
                        head->next = max->next;
                        max->next = lastnext;
                        head = max;
                    }
                    last = head->next;
                    prelast = head;
            }
            else
            {
                nextmax = max->next;
                prelast->next = max;
                if(premax == last)
                {
                    max->next = last;
                    last->next = nextmax;
                }
                else
                {
                    premax->next = last;
                    last->next = nextmax;
                    max->next = lastnext;
    
                }
                last = prelast->next;
                prelast = last;
                last = last->next;
            }
    
        }
        return head;
    }
    
    void myprint(NODE *head)
    {
        if(head == NULL)
        {
            printf("Empty !
    "); } else { while(head->next != NULL) { printf("%d\t",head->data); head = head->next; } printf("%d
    ",head->data); } } int main() { NODE *head = mycreate(); head = myinsert(head,1); head = myinsert(head,2); head = myinsert(head,3); head = myinsert(head,4); head = myinsert(head,5); head = myinsert(head,6); printf(" :"); myprint(head); head = mysort2(head); printf(" :"); myprint(head); return 0; }

    좋은 웹페이지 즐겨찾기