1 #include <stdio.h>
2 #include <stdlib.h>
3
4 /*此处是顺序线性表数据结构定义*/
5 typedef int DataType;
6 struct seqList
7 {//有3个数据成员
8 int MAXNUM;//用于记录顺序线性表中能存放的最大元素个数的 整型 MAXNUM
9 int curNum;//用于存放顺序线性表中数据元素的个数 整型 curNum
10 DataType *element;//用于存放顺序线性表数据元素的连续空间的起始地址
11 };
12
13 typedef struct seqList *PseqList;
14 //第一关
15 PseqList createNullList_seq(int m)
16 {//此处填写代码,创建一个空的顺序线性表,能存放的最大元素个数为 m
17 //若m=0,则返回NULL
18 if(m==0){
19 return NULL;
20 }
21 PseqList pse = (PseqList)malloc(sizeof(struct seqList));
22 if(pse!=NULL){
23 pse->element = (DataType*)malloc(sizeof(DataType)*m);
24 }
25 if(pse->element){
26 pse->MAXNUM = m;
27 pse->curNum=0;
28 return pse;
29 }
30
31 }
32
33 //第二关
34 int isFullList_seq(PseqList L)
35 {
36 //判断顺序线性表是否已满,若已满,返回值为1,否则返回值为0
37 return L->curNum==L->MAXNUM;
38
39
40 }
41
42
43 int insertP_seq(PseqList L , int p ,int x)
44 {// 在线性表L中下标为p的位置插入数据元素x,若下标p非法或线性表已满无法插入数据,返回0;
45 //插入成功返回值为1
46 //如果线性表满了, 还需输"list is full"的提示
47 //如果插入位置非法,需输出提示"position is illegel"
48 if(p<0||p>L->curNum){
49 printf("position is illegel");
50 return 0;
51 }
52 if(L->curNum>=L->MAXNUM){
53 printf("list is full");
54 return 0;
55 }
56 for(int i = L->curNum - 1;i>=p;i--){
57 L->element[i+1]=L->element[i];
58 }
59 L->element[p]=x;
60 L->curNum++;
61 return 1;
62 }
63
64 int insertPre_seq(PseqList L , int p ,int x)
65 {
66 // 在线性表L中下标为p的位置的前面插入数据元素x,若下标p非法或线性表已满无法插入数据,
67 //返回0;插入成功返回值为1
68 //提示:直接调用insertP函数实现即可
69 if(insertP_seq(L,p-1,x)){
70 return 1;
71 }else{
72 return 0;
73 }
74
75
76 }
77
78 int insertPost_seq(PseqList L , int p ,int x)
79 {
80 // 在线性表L中下标为p的位置的后面插入数据元素x,若下标p非法或线性表已满无法插入数据,返回0;插入成功返回值为1
81 //提示:直接调用insertP函数实现即可
82 if(insertP_seq(L,p+1,x)){
83 return 1;
84 }else{
85 return 0;
86 }
87
88 }
89
90 void printList_seq(PseqList L)
91 {//逐个输出线性表的元素,相邻的两个数据元素之间以一个空格为分隔符隔开
92 for(int i=0;i<L->curNum;i++){
93 printf("%d ",L->element[i]);
94 }
95 }
96
97 //第三关
98 int destroyList_seq(PseqList L)
99 {
100 //返回值为销毁的线性表中现有数据元素的个数,若待销毁的线性表不存在,则返回0
101 if(L==NULL){
102 return 0;
103 }
104 int ans=L->curNum;
105 free(L);
106 return ans;
107 }
108
109 //第四关
110 int locate_seq(PseqList L,int x)
111 {//在顺序表L中查找给定值x首次出现的位置,若不存在给定值,则返回-1
112 for(int i=0;i<L->curNum;i++){
113 if(L->element[i]==x){
114 return i;
115 }
116 }
117 return -1;
118 }
119
120 DataType locatePos_seq(PseqList L,int pos)
121 {// 在顺序表L中查找指定位置pos处的数据元素,若位置非法,则返回第0个数据元素
122 if(pos<0||pos>L->curNum){
123 return L->element[0];
124 }
125 return L->element[pos];
126 }
127
128 //第五关
129 int deletePos_seq(PseqList L,int pos)
130 {//在顺序表L中删除与下标pos处的数据元素,若pos非法,则返回-1;否则返回1
131 if(pos<0||pos>L->curNum-1){
132 return -1;
133 }
134 for(int i=pos;i<L->curNum-1;i++){
135 L->element[i]=L->element[i+1];
136 }
137 L->curNum=L->curNum-1;
138 return 1;
139 }
140
141 int delete_seq(PseqList L,int x)
142 {//在顺序表L中删除与参数x值相同的数据元素,返回删除数据元素的个数
143 //可以使用之前已完成的操作
144 int cnt = 0;
145 for(int i=0;i<=L->curNum;i++){
146 if(L->element[i]==x){
147 cnt++;
148
149 for(int j=i;j<L->curNum-1;j++){
150 L->element[j]=L->element[j+1];
151 }
152 L->curNum=L->curNum-1;
153 }
154 }
155 return cnt;
156
157 }
158
159
160 //第六关
161 void replace_seq(PseqList L,int x,int y)
162 {//将顺序表L中值为x的数据元素替换为y
163 for(int i=0;i<L->curNum;i++){
164 if(L->element[i]==x){
165
166 L->element[i]=y;
167 }
168 }
169
170 }
171
172 void delDuplicate_seq(PseqList L)
173 {//移除线性表中的所有重复元素;不要使用额外的数组空间,必须在原地修改输入数组 并在使用 O(1) 额外空间的条件下完成
174 //使用常规删除即可,已修改测试用例
175 for(int i=0;i<L->curNum-1;i++){
176 for(int j=i+1;j<L->curNum;j++){
177 if(L->element[j]==L->element[i]){
178 for(int k=j;k<L->curNum-1;k++){
179 L->element[k]=L->element[k+1];
180 }
181 L->curNum=L->curNum-1;
182
183 }
184 }
185 for(int k=i;k<L->curNum-1;k++){
186 L->element[k]=L->element[k+1];
187 }
188
189 L->curNum=L->curNum-1;
190 }
191 }