實驗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; }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章