2008秋-计算机软件基础- 实验一 参考源程序
生活随笔
收集整理的這篇文章主要介紹了
2008秋-计算机软件基础- 实验一 参考源程序
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
------------------------------------------------------------------
實驗一 參考源程序
// 線性表的順序存儲(順序表)
// Author: Eman Lee
#include<stdio.h>
void?SearchMultipleLocation(char?List[],int?*ListLength,char?key)
{
????int?i;
????for(i=0;i<*ListLength;i++)
????{
?????if(List[i]==key)
?????????printf("\nSearch?%c,?found,?location:?%d?\n",key,i);
????}
}?
int?Search(char?List[],int?*ListLength,char?key)
{
????int?i;
????for(i=0;i<*ListLength;i++)
????{
?????if(List[i]==key)
????????????return?i;
????}
????return?-1;
}?
void?SeqListDelete(char?list[],int?*?listLength,int?i)
{
????/*刪除順序表中下標為i的元素,使得表長減1。?*/
??int?j?;
??if(*listLength==0)
??????printf("the?list?is?empty!\n");
??else
??????if?(i<0?||?i>*listLength-1)
?????????printf("the?position?is?invalid!\n");
??????else
?????????{
???????????for?(j=i+1;j<=*listLength-1;j++)
????????????????list[j-1]=list[j];?????/*元素前移*/
???????????(*listLength)--;???????????/*表長減1*/
???????????printf("\n刪除順序表中下標為%d的元素\n",i);
??????????}
}
void?SeqListInsert(char?List[],int?Location,
???????????????????char?E,int?*ListLength)
{???//ListLength=&length
????int?i;//循環(huán)變量
????//向后移動元素,自右向左
????for(i=*ListLength-1;i>=Location;i--)
????{
????????List[i+1]=List[i];
????}
????List[Location]=E;//插入元素
????(*ListLength)++;//表長加一
????printf("\n在%d處插入元素%c\n",Location,E);
????//PrintSeqList(List,*ListLength);
}
void?PrintSeqList(char?List[],int?ListLength)
{
???int?i;//循環(huán)變量
???//顯示
???printf("\n顯示所有元素:\n");
????for(i=0;i<=ListLength-1;i++)
????????printf("?%c?",List[i]);
????printf("\n");
}
void?main()
{
//在一維數(shù)組位置Location處插入E
????char?SeqList[10]={'0','1','3','2','3','4','5'};
????int?location;
????int?length=7;//表長為6
????PrintSeqList(SeqList,length);
????SeqListInsert(SeqList,5,'7',&length);//在位置5處插入7
????PrintSeqList(SeqList,length);//顯示
????SeqListDelete(SeqList,&length,0);
????PrintSeqList(SeqList,length);//顯示
????SearchMultipleLocation(SeqList,&length,'3');
????location=Search(SeqList,&length,'2');
????if(location>=0)
????????printf("found?'2',loc:?%d\n",location);
????else
????????printf("search?'2',not?found\n?");
}
/*計算機軟件基礎(chǔ)?教材?P79,?ex2
題目:設(shè)線性表存放于整形數(shù)組a[arrayLength]的前幾個分量
(a[0]-a[listLength-1])中,且遞增有序。試寫一算法,
將元素x插入線性表適當?shù)奈恢?#xff0c;以保持線性表的有序性。*/
#include<stdio.h>
void?show(int?a[],int?listLength)
{
?int?i;
?printf("\n顯示所有元素:");
?for(i=0;i<listLength;i++)
?????printf("?%d?",a[i]);
}
int?insert(int?a[],int?arrayLength,int?*ListLength,int?x)
{
??int?i,j;
??if(*ListLength==arrayLength)
????????return?0;/*fail*/
??printf("\n插入元素%d.",x);
??for(i=0;i<*ListLength;i++)
??{
???if(a[i]>=x)/*search?successfully*/
???{
????for(j=*ListLength;j>i;j--)
???????a[j]=a[j-1];/*move*/
????a[i]=x;
??? (*ListLength)++;
????return?1;/*success*/
???}
??
??}
??(*ListLength)++;
???a[i]=x;
??return?1;/*success*/
}
void?main()
{
????int?array[100]={?1,3,5,7,9};
????int?listLength=5;
????int????i;
????show(array,listLength);
????for(i=0;i<11;i++)
????{
????????insert(array,100,&listLength,i);
????????show(array,listLength);
????}
}
實驗一 參考源程序
// 線性表的順序存儲(順序表)
// Author: Eman Lee
#include<stdio.h>
void?SearchMultipleLocation(char?List[],int?*ListLength,char?key)
{
????int?i;
????for(i=0;i<*ListLength;i++)
????{
?????if(List[i]==key)
?????????printf("\nSearch?%c,?found,?location:?%d?\n",key,i);
????}
}?
int?Search(char?List[],int?*ListLength,char?key)
{
????int?i;
????for(i=0;i<*ListLength;i++)
????{
?????if(List[i]==key)
????????????return?i;
????}
????return?-1;
}?
void?SeqListDelete(char?list[],int?*?listLength,int?i)
{
????/*刪除順序表中下標為i的元素,使得表長減1。?*/
??int?j?;
??if(*listLength==0)
??????printf("the?list?is?empty!\n");
??else
??????if?(i<0?||?i>*listLength-1)
?????????printf("the?position?is?invalid!\n");
??????else
?????????{
???????????for?(j=i+1;j<=*listLength-1;j++)
????????????????list[j-1]=list[j];?????/*元素前移*/
???????????(*listLength)--;???????????/*表長減1*/
???????????printf("\n刪除順序表中下標為%d的元素\n",i);
??????????}
}
void?SeqListInsert(char?List[],int?Location,
???????????????????char?E,int?*ListLength)
{???//ListLength=&length
????int?i;//循環(huán)變量
????//向后移動元素,自右向左
????for(i=*ListLength-1;i>=Location;i--)
????{
????????List[i+1]=List[i];
????}
????List[Location]=E;//插入元素
????(*ListLength)++;//表長加一
????printf("\n在%d處插入元素%c\n",Location,E);
????//PrintSeqList(List,*ListLength);
}
void?PrintSeqList(char?List[],int?ListLength)
{
???int?i;//循環(huán)變量
???//顯示
???printf("\n顯示所有元素:\n");
????for(i=0;i<=ListLength-1;i++)
????????printf("?%c?",List[i]);
????printf("\n");
}
void?main()
{
//在一維數(shù)組位置Location處插入E
????char?SeqList[10]={'0','1','3','2','3','4','5'};
????int?location;
????int?length=7;//表長為6
????PrintSeqList(SeqList,length);
????SeqListInsert(SeqList,5,'7',&length);//在位置5處插入7
????PrintSeqList(SeqList,length);//顯示
????SeqListDelete(SeqList,&length,0);
????PrintSeqList(SeqList,length);//顯示
????SearchMultipleLocation(SeqList,&length,'3');
????location=Search(SeqList,&length,'2');
????if(location>=0)
????????printf("found?'2',loc:?%d\n",location);
????else
????????printf("search?'2',not?found\n?");
}
?
/*??Author: Eman Lee *//*計算機軟件基礎(chǔ)?教材?P79,?ex2
題目:設(shè)線性表存放于整形數(shù)組a[arrayLength]的前幾個分量
(a[0]-a[listLength-1])中,且遞增有序。試寫一算法,
將元素x插入線性表適當?shù)奈恢?#xff0c;以保持線性表的有序性。*/
#include<stdio.h>
void?show(int?a[],int?listLength)
{
?int?i;
?printf("\n顯示所有元素:");
?for(i=0;i<listLength;i++)
?????printf("?%d?",a[i]);
}
int?insert(int?a[],int?arrayLength,int?*ListLength,int?x)
{
??int?i,j;
??if(*ListLength==arrayLength)
????????return?0;/*fail*/
??printf("\n插入元素%d.",x);
??for(i=0;i<*ListLength;i++)
??{
???if(a[i]>=x)/*search?successfully*/
???{
????for(j=*ListLength;j>i;j--)
???????a[j]=a[j-1];/*move*/
????a[i]=x;
??? (*ListLength)++;
????return?1;/*success*/
???}
??
??}
??(*ListLength)++;
???a[i]=x;
??return?1;/*success*/
}
void?main()
{
????int?array[100]={?1,3,5,7,9};
????int?listLength=5;
????int????i;
????show(array,listLength);
????for(i=0;i<11;i++)
????{
????????insert(array,100,&listLength,i);
????????show(array,listLength);
????}
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/emanlee/archive/2007/10/16/926716.html
總結(jié)
以上是生活随笔為你收集整理的2008秋-计算机软件基础- 实验一 参考源程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。