顺序表是在计算机内存中以数组的形式保存的线性表(linked list)。
顺序表是指用一组地址连续的存储单元依次存储数据元素的线性结构。线性表采用顺序存储的方式存储就称之为顺序表,顺序表是将表中的结点依次存放在计算机内存中一组地址连续的存储单元中。线性表采用指针链接的方式存储就称之为链表。
代码实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | #include <iostream> using namespace std; int sequence_list[1000]; void PrintList(int n){ cout<<"the new array is "; for (int i = 0; i < n; i++) { cout<<sequence_list[i]<<' '; } } void Insert(int n){ cout<<"please tell me the element and location:"; int a,b; cin>>a>>b; for (int i = n; i >=b; i--) { sequence_list[i]=sequence_list[i-1]; } sequence_list[b-1]=a; PrintList(n+1); } void Delete(int n){ cout<<"please tell me the element you want delete:"; int a; cin>>a; for (int i =a; i <n; i++) { sequence_list[i-1]=sequence_list[i]; } PrintList(n-1); } int main() { int n; cout<<"please input the length of array:"; cin>>n; cout<<"please input the element of array:"; for (int i = 0; i < n; i++) { cin>>sequence_list[i]; } cout<<"please choose what you want to do"<<endl; cout<<"1 insert a number"<<endl; cout<<"2 delete a number"<<endl; int x; cin>>x; switch(x){ case 1:Insert(n);break; case 2:Delete(n);break; default: cout<<"input again"; } return 0; } |