❶ 用c語言實現建立順序表 並查找最小的元素
//給你個完整的
#include <malloc.h>
#include<stdlib.h>
#include <stdio.h>
typedef struct Lnode
{int data;
struct Lnode *next;
}*Linklist,Lnode;
void CreatList(Lnode *L) /*建立鏈表函數*/
{ Lnode *p;
int value;
L->next=NULL;
while (1) /*當輸入非0數值時*/
{scanf( "%d",&value);
if (value==NULL)
return;
p=(Lnode *)malloc(sizeof(Lnode)); /*建立P鏈表*/
p->data=value;
p->next=L->next; /*把後輸入的插到前面*/
L->next=p;
}
}
void paixu(Lnode *L)
{
Linklist r,q,small;int temp;
for(r=L->next;r->next!=NULL;r=r->next)
{small=r;
for(q=r->next;q;q=q->next) /*找到鏈表中最小元素*/
if(q->data<small->data)
small=q;
if(small!=r)
{temp=r->data;
r->data=small->data; /*把最小的數值換到P指針所指的位置數值上(原P指針的next指向不變)*/
small->data=temp; /*把原先p指針所指位置的數值填入被置換出的最小元素位置*/
}
}
printf("正在對新鏈表進行排序……\n");
}
void PrintList(Lnode *L) /*列印鏈表函數*/
{
Lnode *p=L->next; /*帶頭節點,把指針置於第一個數*/
if(p==0)
{printf("鏈表為空");}
else
printf("鏈表為:");
{while(p)
{
printf("%d>>", p->data);
p=p->next;
}
}
printf("\n");
}
inter(Lnode *L,int i)
{
Lnode *k=L->next;
while(k)
{if (k->data==i)
return 1;
k=k->next;
}
return 0;
}
int ListInsert_L(Lnode *L, int i, int e)
{
Lnode *p=L->next;
Lnode *s;
int j=0;
while (p && j<i-1) {p=p->next; ++j;}
if (!p || j>i-1) return 0;
s=(Lnode *)malloc(sizeof(Lnode));
s->data=e;
s->next=p->next;
p->next=s;
return 1;
}
int ListDelete_L(Lnode *L,int i)
{
Lnode *p=L->next;
int j=0;
Lnode *q;
while (p->next && j<i-1) {p=p->next; ++j;} /*找出第i節點,並令p指向其前趨*/
if (!p->next || j>i-1) return 0;
q=p->next;
p->next=q->next;
free(q);
return 1;
}
main()
{
int sign,sign1,signa,signb,signc,i,x,ca,cb,cc;
int choice=1;
Lnode *A,*B,*C,*D,*E,*L,*p,*q,*n,*m;
A=(Lnode *)malloc(sizeof(Lnode));
B=(Lnode *)malloc(sizeof(Lnode));
C=(Lnode *)malloc(sizeof(Lnode));
D=(Lnode *)malloc(sizeof(Lnode));
E=(Lnode *)malloc(sizeof(Lnode));
printf("\t 《瀟的數據結構課程設計——單鏈表的級別操作》\n\n");
while (choice)
{
printf("\t 請選擇您想進行的操作(按任意鍵退出程序):\n 1:對A鏈表操作 \n 2:對B鏈表操作 \n 3:兩鏈表運算 \n \n 您的選擇是:");
scanf("%d",&sign1);
if (sign1==1)
{L=A;
ca=1;
while (ca)
{printf("\t 請選擇對A鏈表進行的操作:\n 1:建立鏈表 \n 2:對鏈表排序 \n 3:在鏈表中插入元素 \n 4:在鏈表中刪除元素 \n 5:返回上一級菜單 \n 您的選擇是:");
scanf("%d",&signa);
switch(signa)
{
case 1:
printf("\n請輸入鏈表元素(輸入0結束)\n");
CreatList(A);
PrintList(A);
break;
case 2:
printf("對A鏈表進行排序,結果為:\n ");
paixu(A);
PrintList(A);
break;
case 3:
printf("請輸入想要插入的位置及插入的數值(以逗號分隔): ");
scanf("%d,%d",&i,&x);
if (ListInsert_L(L,i,x)==1)
{printf("修改成功!目前A鏈表為:\n");
PrintList(L);}
else
printf("警告!您輸入的插入位置超過鏈表長度。 \n");
break;
case 4:
printf("請輸入想要刪除的元素位置: ");
scanf("%d",&i);
if (ListDelete_L(L,i)==1)
{printf("刪除元素成功!目前A鏈表為:\n");
PrintList(L);}
else
printf("警告!您輸入的刪除位置超過鏈表長度。 \n");
break;
case 5:
ca=0;
break;
default:
printf("警告!只能選擇1-5。 \n");
break;
}
}
}
else if (sign1==2)
{L=B;
cb=1;
while (cb)
{printf("\t 請選擇對B鏈表進行的操作:\n 1:建立鏈表 \n 2:對鏈表排序 \n 3:在鏈表中插入元素 \n 4:在鏈表中刪除元素 \n 5:返回上一級菜單 \n 您的選擇是:");
scanf("%d",&signb);
switch(signb)
{
case 1:
printf("\n請輸入鏈表元素(輸入0結束)\n");
CreatList(B);
PrintList(B);
break;
case 2:
printf("對B鏈表進行排序,結果為:\n ");
paixu(B);
PrintList(B);
break;
case 3:
printf("請輸入想要插入的位置及插入的數值(以逗號分隔): ");
scanf("%d,%d",&i,&x);
if (ListInsert_L(L,i,x)==1)
{printf("修改成功!目前B鏈表為:\n");
PrintList(L);}
else
printf("警告!您輸入的插入位置超過鏈表長度。 \n");
break;
case 4:
printf("請輸入想要刪除的元素位置: ");
scanf("%d",&i);
if (ListDelete_L(L,i)==1)
{printf("刪除元素成功!目前B鏈表為:\n");
PrintList(L);}
else
printf("警告!您輸入的刪除位置超過鏈表長度。 \n");
break;
case 5:
cb=0;
break;
default:
printf("警告!只能選擇1-5。 \n");
break;
}
}
}
else if (sign1==3)
{cc=1;
while (cc)
{printf("\t 請選擇操作的名稱:\n 1:顯示當前的A、B鏈表 \n 2:進行差運算 \n 3:進行交運算 \n 4:進行並運算 \n 5:返回上一級菜單 \n 您的選擇是:");
scanf("%d",&signc);
switch(signc)
{
case 1:
printf(" \n 當前A");
PrintList(A);
printf(" \n 當前B");
PrintList(B);
break;
case 2:
p=B->next;
while(p)
{if (!inter(A,p->data))
{m=(Lnode *)malloc(sizeof(Lnode));
m->data=p->data;
m->next=C->next;
C->next=m;
}
p=p->next;
}
printf(" \n 進行差運算,結果為:\n");
PrintList(C);
C=(Lnode *)malloc(sizeof(Lnode)); /*必須再分配一次地址空間以用來把原鏈表清空,否則每次運行都會使鏈表元素增加*/
break;
case 3:
p=B->next;
while(p)
{if (inter(A,p->data))
{q=(Lnode *)malloc(sizeof(Lnode));
q->data=p->data;
q->next=D->next;
D->next=q;
}
p=p->next;
}
printf(" \n 進行交運算,結果為:\n");
PrintList(D);
D=(Lnode *)malloc(sizeof(Lnode));
break;
case 4:
*E=*A;
p=B->next;
while(p)
{if (!inter(E,p->data))
{n=(Lnode *)malloc(sizeof(Lnode));
n->data=p->data;
n->next=E->next;
E->next=n;
}
p=p->next;
}
printf(" \n 進行並運算,結果為:\n");
PrintList(E);
E=(Lnode *)malloc(sizeof(Lnode));
break;
case 5:
cc=0;
break;
default:
printf("警告!只能選擇1-5。 \n");
break;
}
}
}
else
{
printf("謝謝使用,請按任意鍵退出!\n");
break;
}
}
return 0;
}
❷ 大神,在C語言中怎麼在數組中刪除最大和最小的元素
先找到最大元素和最小元素下標。 然後把後續的依次前移即可。
比如
intdel_max_min(int*a,intn)
{
intmaxi,mini,i,j;
maxi=mini=0;
for(i=1;i<n;i++)
if(a[maxi]<a[i])maxi=i;
elseif(a[mini]>a[i])mini=i;
for(i=j=0;i<n;i++)
if(i!=maxi&&i!=mini)
a[j++]=a[i];
returnj;
}
返回的是刪除後 a中元素個數。
❸ 什麼是組成c語言函數的最小元素
常說的是組成C語言的最小元素是函數
沒聽說過組成函數的最小元素
如果一定要說的話,那就是表達式了
❹ C語言中求任一一維數組中的最大元素和最小元素,怎麼編寫
#include <stdio.h>void main( )
{
double a[10],max,min;//輸入幾個數可以自己決定。
int i;
for(i=0;i<10;i++)
scanf("%lf",&a[i]);
max=min=a[0];
for(i=1;i<10;i++)
{
if(max<a[i]) max=a[i];
if(min>a[i]) min=a[i];
}
printf("max=%lf,min=%lf\n",max,min);
}
❺ C語言求出一維數組中最小元素的值和下標號
你的程序沒錯誤,只是你輸入錯誤,空格和回車都被視為一次輸入結束,都好不能作為輸入結束符。你那裡for循環要輸入10次,必須用回車或空格表示輸入一次結束。
除非你改為
scanf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",&a[0],&a[1],&a[2],&a[3],&a[4],&a[5],&a[6],&a[7],&a[8],&a[9]);
就可以這樣輸入:1,2,3,4,5,6,7,8,9,0
❻ C語言中 求數組中的最大元素和最小元素
int mian(void)
main 函數名字寫錯,,,
❼ c語言作業:從鍵盤輸入一個字元串a,找出其中的最大元素和最小元素
#include<stdio.h>
intmain()
{
charstr[200];
scanf("%s",str);
charmin,max;
intminid,maxid;
inti=0;
min=max=str[0];
maxid=minid=0;
while(str[i]!='