实验1——顺序表例程

Description

实现顺序表的创建、插入、删除、查找

Input

第一行输入顺序表的实际长度n
第二行输入n个数据
第三行输入要插入的新数据和插入位置
第四行输入要删除的位置
第五行输入要查找的位置

Output

第一行输出创建后,顺序表内的所有数据,数据之间用空格隔开
第二行输出执行插入操作后,顺序表内的所有数据,数据之间用空格隔开
第三行输出执行删除操作后,顺序表内的所有数据,数据之间用空格隔开
第四行输出指定位置的数据

Sample Input

6
11 22 33 44 55 66
888 3
5
2

Sample Output

11 22 33 44 55 66 
11 22 888 33 44 55 66 
11 22 888 33 55 66 
22

HINT

第i个位置是指从首个元素开始数起的第i个位置,对应数组内下标为i-1的位置






#include<stdio.h> #define MAX 100 int SeqList[MAX],len; void Insert(int pos,int value){ int i; for(i=len;i>=pos;i--) SeqList[i+1]=SeqList[i]; SeqList[pos]=value; len++; } void Del(int pos){ int i; for(i=pos;i<len;i++) SeqList[i]=SeqList[i+1]; len--; } void Output(){ int i; for(i=1;i<=len;i++) printf("%d ",SeqList[i]); printf("\n"); } int main(){ int pos,i,value; scanf("%d",&len); for(i=1;i<=len;i++) scanf("%d",&SeqList[i]); Output(); scanf("%d%d",&value,&pos); Insert(pos,value); Output(); scanf("%d",&pos); Del(pos); Output(); scanf("%d",&pos); printf("%d\n",SeqList[pos]); return 0; }
原文地址:https://www.cnblogs.com/suiyun/p/2690676.html