顺序表所有操作

#include <stdio.h>
#include <string.h>

#define MAXSIZE 100 //顺序表最大长度 

typedef struct
{
	char key[15];
	char name[20];
	int age;
}DATA;

typedef struct //定义顺序表数据结构 
{
	DATA ListDate[MAXSIZE+1];//顺序表数组 
	int ListLen;//顺序表已存节点个数 
}SeqList;

void  SeqListInit(SeqList *L)//初始化顺序表 
{
	L->ListLen = 0;
}

int SeqListLength(SeqList *L)//得到顺序表长度 
{
	return L->ListLen;
}

int SeqListAdd(SeqList *L, DATA date)//添加元素 
{
	if(L->ListLen == (MAXSIZE + 1)) return 0;
	else
	{
		L->ListDate[L->ListLen] = date;
		L->ListLen++;
		return 1;
	}
}

int SeqListInsert(SeqList *L, int n, DATA date)//在n位置插入元素 
{
	if(L->ListLen == (MAXSIZE + 1)) return 0;
	else
	{
		if((n < 0) || (n > MAXSIZE))
		{
			return 0;
		}
		else
		{
			int i;
			for(i = L->ListLen; i > n; i--)
			{
				L->ListDate[i] = L->ListDate[i-1];
			}
			L->ListDate[n] = date;
			L->ListLen++;
			return 1;
		}

	}
}
int SeqListDelete(SeqList *L, int n)//删除位置n的元素 
{
		if((n < 0) || (n > MAXSIZE))
		{
			return 0;
		}
		else
		{
			int i;
			for(i = n; i < (L->ListLen-1); i++)
			{
				L->ListDate[i] = L->ListDate[i+1];
			}
			L->ListLen--;
			return 1;
		}
}
DATA *SeqListFindByNum(SeqList *L, int n)//查询n位置的元素是否存在 
{
	if(n < 0 || n > MAXSIZE) return 0;
	else return &(L->ListDate[n]);
}

int SeqListFindByCont(SeqList *L, char *key)//按关键字找 
{
	int i;
	for(i = 0; i < L->ListLen; i++)
	{
		if(strcmp(L->ListDate[i].key, key) == 0) return i;
	}
	return -1;
}

int SeqListAll(SeqList *L)//输出所有节点 
{
	int i;
	for(i = 0; i < L->ListLen; i++)
		printf("%s,%s,%d\n", L->ListDate[i].key, L->ListDate[i].name, L->ListDate[i].age);
}

int main()
{
	int i;
	SeqList L;
	DATA data,*data1;
	char key[15];
	
	SeqListInit(&L);
	printf("length=%d\n", SeqListLength(&L));
	
	do{
		printf("input:(sno, name, age)");
		fflush(stdin);
		scanf("%s%s%d", &data.key, &data.name, &data.age);
		if(data.age > 0)
		{
			//printf("**age= %d\n", data.age);
			
			if(!SeqListAdd(&L, data)) break;
		}
		else
		{
			break;
		}
	}while(1);
	
	SeqListAll(&L);

	data1 = SeqListFindByNum(&L, 2);
	i = SeqListFindByCont(&L, "11");
	printf("i=%d", i);
	
	
	return 0; 
	
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值