标签:amp ret insert clu enum print list arch 顺序
//静态顺序表操作
#include <stdio.h>
#define Maxsize 10
//定义
typedef struct{
int data[Maxsize];
int length;
}SqList;
void init(SqList&); //初始化
void insert(SqList& , int , int); //插入操作
int deletenum(SqList& , int , int&); //删除操作
int search(SqList , int); //按位查找操作
int searchnum(SqList , int);//按数值查找
int main(void){
SqList line;
init(line);
insert(line,1,1);
insert(line,2,2);
insert(line,3,3);
printf("顺序表中的数字是:");
for(int i = 0 ; i < line.length ; i ++){
printf("%d ",line.data[i]);
}
printf("\n");
int e = -1;
deletenum(line,1,e);
printf("删除的元素是:%d",e);
printf("\n");
int num1 = 1;
int result1 = search(line,num1);
printf("查找的第%d位数字是:%d",num1,result1);
printf("\n");
int num2 = 2;
int result2 = searchnum(line,2);
printf("查找的数字%d,存放在第%d位.",num2,result2);
return 0;
}
//初始化
void init(SqList &L){
for(int i = 0 ; i < Maxsize ; i ++){
L.data[i] = 0;
}
L.length = 0;
}
//插入
void insert(SqList &L , int m , int n){
for(int i = L.length ; i >= m ; i--){
L.data[i] = L.data[i-1];
}
L.data[m-1] = n;
L.length ++;
}
//删除
int deletenum(SqList &L , int m , int &n){
n = L.data[m-1];
for(int i = m ; i < L.length ; i++){
L.data[i-1] = L.data[i];
}
L.length --;
return n;
}
//按位查找
int search(SqList L , int m){
return L.data[m-1];
}
//按数值查找
int searchnum(SqList L , int n){
for(int i = 0 ; i < L.length ; i++){
if(L.data[i] == n){
return i+1;
}
}
return -1;
}
标签:amp ret insert clu enum print list arch 顺序
原文地址:https://www.cnblogs.com/Timesi/p/12378238.html