数据结构之线性表的顺序存储

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

#define maxnum 100
typedef int ElementType;
struct Lnode{
ElementType array[maxnum];
int last;
};
typedef struct Lnode * List;
List makeEmpty(){
List ptr=(List)malloc (sizeof(struct Lnode));
ptr->last=-1;
return ptr;
}
int find(ElementType e,List ptr){
for(int i=0;i<=ptr->last;i++){
if(e==ptr->array[i])
return i;
}
return -1;
}
void insert(ElementType e,int n,List ptr){
if(ptr->last==maxnum-1){
printf("表已满");
return;
}
if(n<0||n>ptr->last+2){
printf("输入有误");
return;
}
for(int i=ptr->last+1;i>n;i-- ){
ptr->array[i]=ptr->array[i-1];
}
ptr->array[n]=e;
ptr->last++;
}
void delete(int n,List ptr){
if(n<0||n>ptr->last){
printf("输入有误");
return;
}
for(int i=n;i<ptr->last;i++){
ptr->array[i]=ptr->array[i+1];
}
ptr->last--;

}
int main(int argc, char *argv[]) {
List list=makeEmpty();
insert(5,0,list);
insert(10,0,list);
printf("%d\n",list->last);
printf("%d\n",find(10,list));
return 0;
}

猜你喜欢

转载自www.cnblogs.com/hsiaolung/p/9250035.html