2008秋季-计算机软件基础-线性表的顺序存储(顺序表)

引例:在一维数组中插入和删除元素
//在一维数组中插入和删除元素
//2008-8-31
#include<stdio.h>
void main()
{
//在一维数组位置Location处插入E
    int List[10]={0,1,2,3,4,5};
    
int ListLength=6;//表长
    int E=6;//被插入的元素
    int i;//循环变量
    int Location=5;//插入位置,下标
    
//向后移动元素,自右向左
    for(i=ListLength-1;i>=Location;i--)
    {
        List[i
+1]=List[i];
    }
    List[Location]
=E;//插入元素
    ListLength++;//表长加一
    
//显示
    for(i=0;i<=ListLength-1;i++)
        printf(
" %d ",List[i]);

//在一维数组中删除元素
/*
    int List[10]={0,1,2,3,4,5};
    int ListLength=6;
    int i;
    int Location=2;
    for(i=Location;i<=ListLength-1;i++)
    {
        List[i]=List[i+1];
    }
    ListLength--;
    for(i=0;i<=ListLength-1;i++)
        printf(" %d ",List[i]);
*/
}

线性表的顺序存储(顺序表)
//线性表的顺序存储(顺序表)
//2008-8-31
#include<stdio.h>
void PrintSeqList(int List[],int ListLength);
void SeqListInsert(int List[],int Location,
                   
int E,int *ListLength);
void main()
{
//在一维数组位置Location处插入E
    int SeqList[10]={0,1,2,3,4,5};
    
int length=6;//表长为6
     
//PrintSeqList(SeqList,6);
    SeqListInsert(SeqList,5,7,&length);//在位置5处插入7
    PrintSeqList(SeqList,length);//显示
}
void SeqListInsert(int List[],int Location,
                   
int E,int *ListLength)
{   
//ListLength=&length
    int i;//循环变量
    
//向后移动元素,自右向左
    for(i=*ListLength-1;i>=Location;i--)
    {
        List[i
+1]=List[i];
    }
    List[Location]
=E;//插入元素
    (*ListLength)++;//表长加一
    PrintSeqList(List,*ListLength);
}

void PrintSeqList(int List[],int ListLength)
{
   
int i;//循环变量
   
//显示
    for(i=0;i<=ListLength-1;i++)
        printf(
" %d ",List[i]);
}



 

posted @   emanlee  阅读(879)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· 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
点击右上角即可分享
微信分享提示