단일 체인 테이블 거품 정렬 (교환 노드)

4272 단어
코드는 다음과 같습니다.
#include
#include
using namespace std;
int num;
typedef struct list
{
    int data;
    struct list *next;
}Lnode,*linklist;
linklist Createlist(int n)//         
{
    linklist p, head,tail;
    head = (linklist)malloc(sizeof(Lnode));
    tail = NULL;
    head->next = tail;
    for (int i = 0; i < n; i++)
    {
        p = (linklist)malloc(sizeof(Lnode));
        cin >>p-> data;
        p->next = NULL;
        if (tail == NULL)
            head->next = p;
        else
            tail->next = p;
        tail=p;
    }
    return head;//        
}
void sortlist(linklist head)//     bubble sort
{
    linklist pre, p,tail;
    tail = NULL;
    while (head->next != tail)
    {
        pre = head;
        p = head->next;
        while (p->next!=tail)
        {
            if (p->data > p->next->data)
            {
                pre->next = p->next;
                p->next = pre->next->next;
                pre->next->next = p;
            }
            else
                p = p->next;
            pre = pre->next;
        }
        tail = p;
    }
}
int main()
{
    cin >> num;
    linklist head,x;
    head = Createlist(num);
    sortlist(head);
    x = head->next;
    while (x != NULL) 
    {
        cout << x->data;
        x = x->next;
    }
    cout << endl;
    return 0;
}

 
전재 대상:https://www.cnblogs.com/orion7/p/7227627.html

좋은 웹페이지 즐겨찾기