简单的顺序表

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
//01构建顺序表结构。
typedef int data_t;
typedef struct seqlist{
	data_t data[SIZE];//记录表元素 
	data_t last;//表下标 
}seqlist;
//02创建表头以及初始化
seqlist* create_seqlist()
{  
    seqlist* head = (seqlist*)malloc(sizeof(seqlist));
    if(NULL == head)
    {
    	return NULL;
	}
    head->last = -1;
    /
    memset(head->data,0,sizeof(head->data)); 
    /
    
	return head;
}
//03求表长
int get_seqlist_len(seqlist* head)
{
	if(NULL == head)
    {
    	return -1;
	}
	return head->last+1;
	
}
//04判断是否为空
int get_seqlist_empty(seqlist* head)
{
	if(NULL == head)
    {
    	return -1;
	}
	return ((head->last == -1)?1:0); //1为空,0为非空 
}
//06判断是否为满
int get_seqlist_full(seqlist* head)
{
	if(NULL == head)
    {
    	return -1;
	}
	return ((head->last+1 == SIZE)?1:0);//1为满,0为空 
}
//07按位置插入元素
int insert_by_pos_seqlist(seqlist* head,int pos,int value)
{
	if(NULL == head)
    {
    	return -1;
	}
	///
	if(get_seqlist_full(head) == 1)//判是否为满 
	 {
	 	return -1;
	 }
	 if(pos < 0 || pos > head->last+1)//判断输入的pos是否有效 
	 {
	 	return -1;
	 }
	 int i;
	 for(i = head->last+1;i > pos;i--)//移动的次数 
	 {
	 	head->data[i]=head->data[i-1];//移动 
	 }
	///
	head->data[pos] = value;
	head->last++;
	return 0;
}
//08打印各个元素
void prn(seqlist* head)
{
	int len = get_seqlist_len(head);
	int i;
	for(i = 0;i < len;i++)
	{
		printf("%-3d",head->data[i]);
	}
	printf("\n");
}
//09按位置删除
int delete_by_pos_seqlist(seqlist* head,int pos)
{
	if(NULL == head)
	{
		return -1;
	}
	if(get_seqlist_full(head) == 1)
	{
		return -1;
	}
	 if(pos < 0 || pos > head->last+1)//判断输入的pos是否有效 
	{
	return -1;
	}
	int i;
	for(i = pos;i < head->last;i++)//移动位置 
	{
	head->data[i] = head->data[i+1];
    }
    head->last--;
    return 0;
 } 
 //10按位置查询
 int get_pos_seqlist(seqlist* head,int pos)
 {
 	if(NULL == head)
	{
		return -1;
	}
	if(get_seqlist_full(head) == 1)
	{
		return -1;
	}
	 if(pos < 0 || pos > head->last+1)//判断输入的pos是否有效 
	{
	return -1;
	}
	
	return head->data[pos];
  } 
//11按位置修改
int change_by_pos_seqlist(seqlist* head,int pos,int new_value)
{
	if(NULL == head)
	{
		return -1;
	}
	if(get_seqlist_full(head) == 1)
	{
		return -1;
	}
	 if(pos < 0 || pos > head->last+1)//判断输入的pos是否有效 
	{
	return -1;
	}
	
	return head->data[pos] = new_value;
	
 } 
 //12按值查找,返回下标 
 int find_by_data_seqlist(seqlist* head,data_t value)
 {
	if(NULL == head)
	{
		return -1;
	}
	if(get_seqlist_full(head) == 1)
	{
		return -1;
	}
 	int i;
 	for(i = 0;i< head->last+1;i++)//0-5
	 {
	 	if(head->data[i] == value)
	 	{
	 		return i;
	    }
	  } 
 	return -1;
 }
 //13按值删除
 int delete_by_data_seqlist(seqlist* head,data_t value)
 {
 	if(NULL == head)
 	{
 		return -1;
	 }
	 if(get_seqlist_empty(head) == 1)
	 {
	 	return -1;
	 }
	 int pos = find_by_data_seqlist(head,value);
	 delete_by_pos_seqlist(head,pos);
	 
  } 
 //14按值修改 
  int change_by_data_seqlist(seqlist* head,data_t old_value,data_t new_value)
  {
  	if(NULL == head)
 	{
 		return -1;
	}
	 if(get_seqlist_empty(head) == 1)
	{
	 	return -1;
	}
	
	int pos = find_by_data_seqlist(head,old_value);
	change_by_pos_seqlist(head,pos,new_value);
  }
  
//主函数 
int main()
{
	seqlist* head = create_seqlist();
	if(NULL == head)
    {
    	return -1;
	}
	int i;
	for(i = 0;i < 5;i++)
	{
	insert_by_pos_seqlist(head,i,i+1);
	}
	//03
	printf("表的值为:\n");
	prn(head);
	int len = get_seqlist_len(head);
	printf("表长为:%d\n",len);
	//07 
	printf("2 的位置插入 77:\n"); 
	insert_by_pos_seqlist(head,2,77);
	prn(head);
	//09
	printf("删除 3 位置的值:\n");
	delete_by_pos_seqlist(head,3);
	prn(head);
	//10
	int pos = get_pos_seqlist(head,2);
	printf("2 位置表中的值为:%d\n",pos);
	//11
	printf("删除表中1的值:\n");
	delete_by_data_seqlist(head,1);
	prn(head);
	//12
	printf("2在表中的位置:%d\n",find_by_data_seqlist(head,2));
	//13
	printf("3位置改为77:\n");
	change_by_pos_seqlist(head,3,77);
	prn(head);
	return 0;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值