2.3按位置操作
1)按照位置插入数据
void Insert_seqlist_single(Seqlist* sq,int arr_sub,int num){if(sq->pos==N ){ //判断顺序列表是否为满printf("error");return;}else if(arr_sub<0||arr_sub>sq->pos){printf("error");return;}else{for(int i=sq->pos-1;i>=arr_sub;i--){sq->data[i+1]=sq->data[i];}sq->data[arr_sub]=num;}sq->pos++;
}
2)按照位置删除数据
void Delete_seqlist_single(Seqlist* sq,int arr_sub){if(sq->pos==0){printf("error");return;}else if(arr_sub<0 || arr_sub>=sq->pos ){printf("error");return;}else{for(int i=arr_sub;i<sq->pos;i++){sq->data[i]=sq->data[i+1];}}sq->pos--;
}
3)按照位置修改数据
void reorder_list(Seqlist* sq,int arr_sub,int num){if(sq->pos==0){printf("error\n");}else if(arr_sub<0 || arr_sub>=sq->pos){printf("error\n");}else {sq->data[arr_sub]=num;}
}
4)按照位置查找数据
void Iterate_seqlist_single(Seqlist* sq,int arr_sub){if(sq->pos==0){printf("error");}else if(arr_sub<0 || arr_sub>=sq->pos){printf("error\n");}else {printf("%d\n",sq->data[arr_sub]);}
}
2.4按数据操作
1)按照数据查找数据
int Iterate_seqlist_arr_sub(Seqlist* sq, int num) {for (int i = 0; i < sq->pos; i++) {if (sq->data[i] == num) {return i;}}
}
2)按照数据修改数据
#include <stdio.h>
#include <stdlib.h>
#define N 10typedef struct{int data[N];int pos;
}Seqlist;
//创建堆空间
Seqlist* establish(void){Seqlist* sq= malloc(sizeof (Seqlist));if(sq==NULL){printf("error");}sq->pos=0;return sq;
}
//堆空间释放
void free(void *ptr);
//顺序表(增:插入数据)
void Insert_seqlist(Seqlist* sq,int num){if(sq->pos==N){ //判断顺序列表是否为满printf("error");}else{sq->data[sq->pos]=num;}sq->pos++;//return ;
}
//查:顺序表 (遍历顺序表)
void Iterate_seqlist(Seqlist* sq){for(int i=0;i<sq->pos;i++){printf("%d ",sq->data[i]);}putchar(10);
}
void reorder_list_data(Seqlist* sq,int num1,int num2){if(sq->pos==0){printf("error");}else {for(int i=0;i<sq->pos;i++){if(sq->data[i]==num1){sq->data[i]=num2;}}}
}
int main(void){Seqlist* sq=establish();Insert_seqlist(sq,1);Insert_seqlist(sq,2);Insert_seqlist(sq,2);Insert_seqlist(sq,3);Insert_seqlist(sq,1);Iterate_seqlist(sq);reorder_list_data(sq,1,60);Iterate_seqlist(sq);free(sq);return 0;
}
2.5删除顺序表
void Insert_seqlist(Seqlist* sq,int num){if(sq->pos==0){ //判断顺序列表是否为满printf("error");}else{sq->pos=0;}
}
顺序表去重
删除重复数据(将先出现的数据与后面的数据对比,如果有重复的将后面的数据删除)
提示:可以直接使用按位置删除的函数。两层循环
顺序表中原有:1221134324534541
122134324534541
顺序表中存储:12345
提示:先找到重复数据的位置,按位置删除。
#include <stdio.h>
#include <stdlib.h>
#define N 10typedef struct{int data[N];int pos;
}Seqlist;
//创建堆空间
Seqlist* establish(void){Seqlist* sq= malloc(sizeof (Seqlist));if(sq==NULL){printf("error");}sq->pos=0;return sq;
}
//堆空间释放
void free(void *ptr);
//顺序表(增:插入数据)
void Insert_seqlist(Seqlist* sq,int num){if(sq->pos==N){ //判断顺序列表是否为满printf("error");}else{sq->data[sq->pos]=num;}sq->pos++;//return ;
}
//查:顺序表 (遍历顺序表)
void Iterate_seqlist(Seqlist* sq){for(int i=0;i<sq->pos;i++){printf("%d ",sq->data[i]);}putchar(10);
}
//按照位置删除
void Delete_seqlist_single(Seqlist* sq,int arr_sub){if(sq->pos==0){printf("error");return;}else if(arr_sub<0 || arr_sub>=sq->pos ){printf("error");return;}else{for(int i=arr_sub;i<sq->pos;i++){sq->data[i]=sq->data[i+1];}}sq->pos--;
}
//去重
void Deletion_duplicates(Seqlist* sq){for(int i=0;i<sq->pos;i++){for(int j=i+1;j<sq->pos;j++){if(sq->data[i]==sq->data[j]){Delete_seqlist_single(sq,j);}}}
}
int main(void){Seqlist* sq=establish();Insert_seqlist(sq,1);Insert_seqlist(sq,2);Insert_seqlist(sq,2);Insert_seqlist(sq,3);Insert_seqlist(sq,5);Insert_seqlist(sq,1);Insert_seqlist(sq,3);Insert_seqlist(sq,4);Insert_seqlist(sq,4);Insert_seqlist(sq,5);Deletion_duplicates(sq);Iterate_seqlist(sq);free(sq);return 0;
}