思维导图
stutable.h
#ifndef _STUTABLE_H__
#define _STUTABLE_H__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 30
typedef char dataType;
typedef struct student{
dataType data[MAX];
int len;
}stu,*stuList;
stuList create_stu();//chuang jian biao
int full(stuList p);//pan man
int null(stuList p);//pan kong
int addstu(stuList p,dataType t);
void selectStu(stuList p);//bian li
int insertStu(stuList p,int index,dataType t);
int delStu(stuList p,int index);
int updateStu(stuList p,int index,dataType t);
int findStu(stuList p,dataType t);
int remStu(stuList p);
void my_free(stuList p);
#endif
stutable.c
#include"stutable.h"
stuList create_stu(){
stuList p = (stuList)malloc(sizeof(stu));
if(p == NULL){
printf("table creation failure");
}
p->len == 0;
memset(p->data,0,sizeof(p->data));
return p;
}
int full(stuList p){
if(p == NULL){
printf("expression illegality");
}
return p->len == MAX;
}
int null(stuList p){
if(p == NULL){
printf("expression illegality");
}
return p->len == 0;
}
int addstu(stuList p,dataType t){
if(p == NULL || full(p)){
printf("expression illegality");
return 0;
}
p->data[p->len]=t;
p->len++;
return 1;
}
void selectStu(stuList p){
if(p == NULL || null(p)){
printf("expression illegality");
}
for(int i=0;i<p->len;i++){
printf("%c ",p->data[i]);
}
putchar(10);
}
int insertStu(stuList p,int index,dataType t){
if(p == NULL || full(p) || index <= 0 || index >= MAX){
printf("error");
return -1;
}
index -= 1;
for(int i=0;i<p->len;i++){
p->data[p->len-i] = p->data[p->len-1-i];
}
p->data[index] = t;
p->len++;
return 1;
}
int delStu(stuList p,int index){
if(p == NULL || null(p) || index <= 0 || index >= MAX){
printf("error\n");
return 0;
}
index -= 1;
for(int i=index;i<p->len;i++){
p->data[i]=p->data[i+1];
}
p->len--;
return 1;
}
int updateStu(stuList p,int index,dataType t){
if(p == NULL || null(p) || index <= 0 || index >= p->len){
printf("error\n");
return -1;
}
index -= 1;
p->data[index] = t;
return 0;
}
int findStu(stuList p,dataType t){
if(p == NULL || null(p)){
printf("error\n");
return 0;
}
int flag = 0;
for(int i=0;i<p->len;i++){
if(p->data[i] == t){
flag = 1;
return i;
}
}
if(flag == 0){
printf("search failure\n ");
}
}
int remStu(stuList p){
if(p == NULL || null(p)){
printf("error");
return -1;
}
for(int i=0;i<p->len;i++){
for(int j=i+1;j<p->len;j++){
if(p->data[i] == p->data[j]){
delStu(p,j+1);
j--;
}
}
}
return 1;
}
void my_free(stuList p){
if(p == NULL){
printf("error\n");
}
free(p);
printf("free p success\n");
p=NULL;
}
main.c
#include"stutable.h"
int main(){
stuList p = create_stu();
addstu(p,'a');
addstu(p,'b');
addstu(p,'c');
addstu(p,'d');
addstu(p,'a');
selectStu(p);
insertStu(p,2,'z');
insertStu(p,1,'x');
insertStu(p,3,'a');
selectStu(p);
delStu(p,3);
selectStu(p);
updateStu(p,4,'h');
selectStu(p);
remStu(p);
selectStu(p);
my_free(p);
}