...在一个有序序列中,输入一个数,将该述插入在正确的位置,使得插入后的...
发布网友
发布时间:2024-10-04 17:50
我来回答
共3个回答
热心网友
时间:2024-10-04 20:53
用单链表写了个,你参考下吧~~~
#include <stdio.h>
#include <stdlib.h>
typedef struct sDataList{
int data;
struct sDataList *pNext;
}sDataList;
sDataList *pHead = NULL;
sDataList *pTail = NULL;
void CreateDataList( int data )
{
sDataList *current = malloc(sizeof(sDataList));
current->data = data;
current->pNext = NULL;
if (pHead == NULL){
pHead = current;
}
else{
pTail->pNext = current;
}
pTail = current;
}
void InsertData(int data)
{
int i;
sDataList *current = malloc(sizeof(sDataList));
sDataList *tmpBlock,*preBlock;
current->data = data;
current->pNext = NULL;
if (pHead->data >= data){
current->pNext = pHead;
pHead = current;
}
else if (pTail->data <= data){
pTail->pNext = current;
pTail = current;
}
else{
preBlock = pHead;
tmpBlock = preBlock->pNext;
while (tmpBlock){
if (tmpBlock->data >= data){
preBlock->pNext = current;
current->pNext = tmpBlock;
break;
}
preBlock = preBlock->pNext;
tmpBlock = tmpBlock->pNext;
}
}
}
void main()
{
int iTime = 0,inputData;
sDataList *current;
printf("输入-1退出有序序列的输入\n");
printf("请输入有序序列:\n");
while (1){
scanf("%d",&iTime);
if (iTime != -1){
CreateDataList(iTime);
}
else{
break;
}
}
printf("请输入要插入的数据 : ");
scanf("%d",&inputData);
InsertData(inputData);
current = pHead;
while (current){
printf("%d\n",current->data);
current = current->pNext;
}
}
热心网友
时间:2024-10-04 20:57
这是插入排序算法,网上代码太多了.
热心网友
时间:2024-10-04 20:50
next;
p->rnext=s;
s->lnext=p;
s->rnext=ptmp;
ptmp->lnext=s;
由于没有具体的结构体,所以只能凭自己想象了……