上面既有排序,又有合并,不想要合并的話,把跟這個函數(shù)有關(guān)的函數(shù),和調(diào)用刪除就行了!!!
讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:域名注冊、網(wǎng)絡(luò)空間、營銷軟件、網(wǎng)站建設(shè)、鄂托克前網(wǎng)站維護、網(wǎng)站推廣。
#includestdio.h
#includestdlib.h
struct LNode //創(chuàng)建鏈表結(jié)點
{ int data;
struct LNode *next;
};
struct LNode *creat() //鏈表創(chuàng)建函數(shù)
{ struct LNode *head,*p,*rear;
int x,n;
head=(struct LNode *)malloc(sizeof(struct LNode));//鏈表頭指針
rear=head;
puts("輸入鏈表元素,以0結(jié)束:");
scanf("%d",x); //鏈表元素輸入
while(x) //鏈表節(jié)點鏈接
{ p=(struct LNode *)malloc(sizeof(struct LNode));
p-data=x; rear-next=p;
rear=p; scanf("%d",x);
}
rear-next=NULL;
return head-next; //返回鏈表中第一個有值結(jié)點地址
}
struct LNode *Merger_Linklist( struct LNode *L1,struct LNode *L2 ) //鏈表合并函數(shù)
{ struct LNode *p;
for(p=L1;p-next!=NULL;p=p-next) //查找鏈表L1的尾結(jié)點地址
{ if(p-next==NULL)
break;
}
p-next =L2; //將鏈表L2鏈接在L1之后
return L1;
}
struct LNode *Rand(struct LNode *L) //鏈表排序函數(shù)
{ struct LNode *Cur; //當(dāng)前節(jié)點
struct LNode *Next; //遍歷未排序節(jié)點
struct LNode *min; //指向未排序節(jié)點中最小節(jié)點
int temp;
for(Cur = L; Cur-next!= NULL; Cur = Cur-next)
//從頭節(jié)點的下一個節(jié)點開始,一直到倒數(shù)第二個節(jié)點
{ min = Cur;
for(Next = Cur-next; Next != NULL; Next = Next-next)
{ if(min-data Next-data)
{min = Next;}
}
temp = Cur-data; //鏈表數(shù)值交換
Cur-data = min-data;
min-data = temp;
}
return L;
}
void print(struct LNode *head,char a) //鏈表打印函數(shù)
{ struct LNode *p;
p=head;
printf("%c = ( ",a);
while( p )
{ printf("%d ",p-data);
p=p-next;
}
puts(" )\n");
}
struct LNode *DeleteC_Linklist( struct LNode *L )//刪除鏈表重復(fù)數(shù)
{ int key,had;
struct LNode *h,*p,*r,*q;
for(p=L;p-next!=NULL;p=p-next)
{ had = p-data;
for(r=p,q=p-next;q!=NULL;)
{ key = q-data;
if( key==had )
{ r-next=q-next; h=q;
q=q-next; free( h );
}
else
{ q=q-next; r=r-next;
}
}
}
return L;
}
void main () //主函數(shù)
{ struct LNode *L1,*L2,*L3;
char a='A',b='B',c='C',d='D';
L1 = creat(); //創(chuàng)建鏈表L1
Rand(L1); //對L1排序
puts("\n創(chuàng)建的鏈表經(jīng)排序后為:");
print(L1,a);
L2 = creat(); //創(chuàng)建鏈表L2
Rand(L2); //對L2排序
puts("\n創(chuàng)建的鏈表經(jīng)排序后為:");
print(L2,b);
L3=Merger_Linklist( L1,L2 ); //合并鏈表L1和L2
Rand(L3); //對合并后的鏈表排序
puts("合并后的鏈表為:");
print(L3,c);
DeleteC_Linklist( L3 );
printf("刪除重復(fù)數(shù)后的鏈表為:\n");
print(L3,d); //打印刪除后的鏈表
}
#includestdio.h
#define?N?100
void?paixu(int?*,?int?*,?int);//聲明函數(shù)
int?main()
{
int?a[N],c[N];
int?i,n=0;
printf("請輸入n個整數(shù):");
for(i=0;;i++)
{
scanf("%d",a[i]);
n++;
if(getchar()=='\n')
break;
}
paixu(a,?c,?n);//調(diào)用函數(shù),數(shù)組只需給出數(shù)組名
return?0;
}
void?paixu(int?a[N],int?c[N],?int?n)
{
int?i,j,s=0;
for(i=0;in;i++)
{
c[i]=0;
}
for(i=0;in;i++)
for(j=i+1;jn;j++)
{
if(a[i]==a[j])
c[i]=1;
}
for(i=0;in;i++)
{
s=1;
for(j=0;jn;j++)
{
if(c[j]==0a[i]a[j])
{
s++;
}
}
printf("%d?",s);
}
}
以下是一個可能的實現(xiàn),包括insertX函數(shù)和主函數(shù)示例:
#include stdio.h
int insertX(int* pa, int n, int x) {
int i, j;
// 找到插入位置
for (i = 0; i n; i++) {
if (pa[i] x) {
break;
}
}
// 將插入位置后的元素后移
for (j = n; j i; j--) {
pa[j] = pa[j - 1];
}
// 插入元素
pa[i] = x;
// 返回插入后數(shù)組的長度
return n + 1;
}
int main() {
int n, x;
printf("請輸入有序數(shù)列的長度n:");
scanf("%d", n);
int a[n];
printf("請輸入%d個有序整數(shù):\n", n);
for (int i = 0; i n; i++) {
scanf("%d", a[i]);
}
printf("請輸入要插入的整數(shù)x:");
scanf("%d", x);
n = insertX(a, n, x);
printf("插入后的有序整數(shù)為:\n");
for (int i = 0; i n; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
在上述代碼中,我們定義了一個insertX函數(shù)來實現(xiàn)將一個整數(shù)x插入到一個有序數(shù)組中的功能。該函數(shù)的參數(shù)包括一個指向數(shù)組首地址的指針pa,數(shù)組的長度n,以及要插入的整數(shù)x。函數(shù)的具體實現(xiàn)過程如下:
遍歷數(shù)組,找到插入位置,即第一個大于x的元素的位置i;
將插入位置后的元素后移一位;
在插入位置處插入x;
返回插入后數(shù)組的長度n+1。
在主函數(shù)中,我們先輸入有序數(shù)列的長度n和n個有序整數(shù),然后輸入要插入的整數(shù)x。接著調(diào)用insertX函數(shù)將x插入到數(shù)組中,并輸出插入后的有序整數(shù)序列。
需要注意的是,上述代碼并沒有對輸入的數(shù)據(jù)進行范圍檢查,如果輸入的數(shù)據(jù)不符合要求,程序可能會出現(xiàn)錯誤。因此,在實際使用中應(yīng)該添加相應(yīng)的數(shù)據(jù)檢查和錯誤處理機制。
#includestdio.h
#includestdlib.h
#includelimits.h
#define datelimit 1000 + 1 + 1
#define n 5
#define m 5 //datelimit 是最大的數(shù)組數(shù) 注意別開爆 n和m是a、b兩隊列的元素數(shù)用的時候修改后面的數(shù)字就可以了
int main()
{
int a[datelimit],b[datelimit],c[datelimit * 2];
int i,j,head;
int inf = INT_MAX;
for(i = 1 ;i = n;i++)
3scanf("%d",a[i]);
for(i = 1 ;i = m;i++)
scanf("%d",b[i]);
a[n + 1] = inf ;
b[m + 1] = inf ; //處理a或b的隊列已經(jīng)出完
i = 1;
j = 1;
head = 1;
while(i = n + 1 j = m + 1)
{
if(a[i] b[j])
{
c[head] = b[j];
head ++;
j ++;
}
else
{
c[head] = a[i];
head ++;
i ++;
}
} //進入c隊列
for(i = 1 ;i = n + m;i++)
printf("%d ",c[i]);
system("pause"); //最好刪去這一句 以免使用時出錯
return 0;
}