一、问题描述
存储数据不仅数组可以,链表也行。那么如何用链表实现冒泡排序呢?
我们需要把数据存储在链表中,然后调用排序函数就可以了。但必须要注意链表与数组的不同点,链表没有下标,要想访问数据域必须通过节点来访问。
二、代码实现
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct Node
{
ElemType pData;
Node* pNext;
}Node,*pList;
void Init(pList* ihead)
{
*ihead = (pList)malloc(sizeof(Node));
if (*ihead == NULL)exit(0);
(*ihead)->pNext = NULL;
(*ihead)->pData = 0;
}
void Insert(pList head, ElemType val)
{
pList pCur = head;
for (int i = 0; i < head->pData; ++i)
{
pCur = pCur->pNext;
}
pList newNode = (pList)malloc(sizeo