数据结构---顺序表

思维导图

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);
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值