您的位置:首页 > 理论基础 > 数据结构算法

线性表的顺序存储实现(陈越数据结构版)

2016-09-21 17:48 483 查看
//线性表的顺序存储实现(陈越数据结构版)
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
struct node{
int array[MAXSIZE];
int last;//最后一个元素的下标
};
struct node* Init(struct node * list)
{
list=(struct node*)malloc(sizeof(struct node));
list->last=-1;
return list;
}
void Delete(struct node* list,int i)
{
if(i<1||i>list->last+1){
printf("Wrong\n");
return;
}
for(int j=i;j<list->last;j++)
list->array[j-1]=list->array[j];
list->last--;
return;
}
void Insert(struct node* list,int i,int number)
{

if(list->last==MAXSIZE-1){
printf("Full\n");
return;
}
if(i<1||i>list->last+2){
printf("Wrong\n");
return;
}
for(int j=list->last;j>=i-1;j--)
list->array[j+1]=list->array[j];
list->array[i-1]=number;
list->last++;
return;

}
int Find(struct node* list,int number)
{
int i=0;
while(i<=list->last&&list->array[i]!=number)
i++;
if(i>list->last)
return 0;
else
return i;
}
int main()
{
struct node* list;
//初始化
list=Init(list);
//插入
Insert(list,1,2);//使插入的元素位于位置i,即下标为i-1处
//删除
Delete(list,1);//删除的元素位于位置i,即下标为i-1处
//查找
int location=Find(list,2);
if(location)
printf("%d\n",location);
else
printf("error\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: