链表实现冒泡排序算法

 

 

// TEST2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include
#include


int main(int argc, char* argv[])
{
	int a = 0, i = 0;
	int *arr = NULL;
	struct Node{
		int num;
		struct Node* next;
	};
	struct Node *Head, *pTmp, *pPre, *tHead, *pLast;

	while (1 == scanf("%d",&a)){
		pTmp = (struct Node *)malloc(sizeof(struct Node));
		if(pTmp == NULL){
			return 0;
		}
		pTmp->num = a;
		pTmp->next = NULL;
		if(i++ == 0){
			Head = pTmp;
			pPre = pTmp;
		}else{
			pPre->next = pTmp;
			pPre = pTmp;
		}
	}

	for(int j=0; jnext;  pTmp != NULL;  pPre = pTmp, pTmp = pTmp->next ){	
			if(pPre->num > pTmp->num){
			int nTmp = pPre->num;
			pPre->num = pTmp->num;
			pTmp->num = nTmp;		
			}			
		}
			
	}
	

	pTmp = Head;
	while (pTmp != NULL){
		printf("%d ",pTmp->num);
		pTmp = pTmp->next;
	}
	printf("\n");
	
	getchar();
	return 0;
}


 

你可能感兴趣的:(【LeetCode,OJ】,数据结构与算法)