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 @ 2007-09-05 15:00  emanlee  阅读(876)  评论(0编辑  收藏  举报