// LowQuikPoint.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
using namespace std;
typedef struct Node
{
struct Node * next;
int data;
}NODE;
void showList(NODE* L)
{
if (L == nullptr)
return;
NODE* s = L->next;
while (s!=nullptr)
{
cout << s->data << ",";
s = s->next;
}
}
void CreateList(NODE* &list)
{
int x = -1;
NODE* p = list;
if (nullptr == list) return;
cin >> x;
while (x != -1)
{
Node * n = new Node();
n->data = x;
p->next = n;
p = n;
cin >> x;
}
p->next = nullptr;
}
void DeleteList(Node * &header)
{
if (nullptr == header) return;
Node * list = header;
Node * p = header->next;
while (list!=nullptr)
{
delete list;
list = p;
if (p != nullptr)
p = p->next;
}
header = nullptr;
}
Node * bubblySort(Node * &header)
{
if (header == nullptr || header->next == nullptr) return header;
bool isChanger = true;
Node * p = nullptr;
//冒泡大的向后,如果最后只有一个无须再排;如果上一次发现不有交换,说明已经有序,无须再排;
while (p != header->next->next&& isChanger)
{
Node * q = header->next; isChanger = false;
for (; q->next != nullptr &&q->next != p; q = q->next)
{
if (q->data>q->next->data)
{
swap(q->data, q->next->data);
isChanger = true;
}
}
p = q;
}
return header;
}
int _tmain(int argc, _TCHAR* argv[])
{
Node * L = new Node();
CreateList(L);
showList(L);
cout << endl;
bubblySort(L);
showList(L);
cout << endl;
DeleteList(L);
showList(L);
return 0;
}