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);
}
}
分类:
[18] 数据结构与算法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY