2008秋-计算机软件基础- 实验一 参考源程序
------------------------------------------------------------------
实验一 参考源程序
实验一 参考源程序
// 线性表的顺序存储(顺序表)
// 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;//循环变量
//向后移动元素,自右向左
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;//循环变量
//显示
printf("\n显示所有元素:\n");
for(i=0;i<=ListLength-1;i++)
printf(" %c ",List[i]);
printf("\n");
}
void main()
{
//在一维数组位置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
#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;//循环变量
//向后移动元素,自右向左
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;//循环变量
//显示
printf("\n显示所有元素:\n");
for(i=0;i<=ListLength-1;i++)
printf(" %c ",List[i]);
printf("\n");
}
void main()
{
//在一维数组位置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 */
/*计算机软件基础 教材 P79, ex2
题目:设线性表存放于整形数组a[arrayLength]的前几个分量
(a[0]-a[listLength-1])中,且递增有序。试写一算法,
将元素x插入线性表适当的位置,以保持线性表的有序性。*/
#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);
}
}
/*计算机软件基础 教材 P79, ex2
题目:设线性表存放于整形数组a[arrayLength]的前几个分量
(a[0]-a[listLength-1])中,且递增有序。试写一算法,
将元素x插入线性表适当的位置,以保持线性表的有序性。*/
#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);
}
}