建立长度为n的顺序表,在指定的数据元素item之前插入数据元素data。如果指定的数据元素item不存在,则将data插入到顺序表的尾端。(数据类型为整型)
第一行为顺序表的长度n;
第二行为顺序表中的数据元素;
第三行为指定的数据元素item;
第四行为要插入的数据元素data;
输出结果为顺序表中的数据元素。
--------------------------------------------------------------------
10
10 20 30 40 50 60 70 80 90 100
50
55
---------------------------------------------------------------------
10 20 30 40 55 50 60 70 80 90 100
--------------------------------------------------------------------
复制代码
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#include <stdio.h> #include <iostream> #include <string> using namespace std; struct node { int data[105]; int Length; }L; void Insert(node *L,int tar,int insert) { int i,j; for(i=0;i<L->Length;i++) { if(L->data[i]==tar) { for(j=L->Length-1;j>=i;j--) { L->data[j+1]=L->data[j]; } L->Length++; L->data[i]=insert; break; } } if(i==L->Length) { L->data[i]=insert; L->Length++; } } void Traverse() { for(int i=0;i<L.Length;i++) cout<<L.data[i]<<" "; //cout<<endl; } int main() { int num,tar,insert; int i; cin>>num; L.Length=num; for(i=0;i<num;i++) cin>>L.data[i]; cin>>tar>>insert; Insert(&L,tar,insert); Traverse(); return 0; }
最后
以上就是重要豌豆最近收集整理的关于【数据结构_顺序表_List_0943】顺序表插入操作的实践的全部内容,更多相关【数据结构_顺序表_List_0943】顺序表插入操作内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复