思维导图
顺序表的基本操作
头文件
#ifndef __SEQ_LIST_H__
#define __SEQ_LIST_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 7
typedef int datatype;
typedef struct seq_list
{
datatype data[MAX];
int len;
}seq_list,*seq_p;
seq_p seq_creat();
int empty_seq(seq_p S);
int full_seq(seq_p S);
void insert_head(seq_p S,datatype data);
void show_seq(seq_p S);
void insert_tail(seq_p S,datatype data);
void delete_head(seq_p S);
void zero(seq_p S);
void insert_location(seq_p S,datatype data,int n);
void free_seq(seq_p S);
#endif
主函数:
#include "seq_list.h"
int main(int argc, const char *argv[])
{
seq_p S=seq_creat();
empty_seq(S);
full_seq(S);
insert_head(S,50);
insert_head(S,12);
insert_head(S,45);
insert_head(S,78);
insert_head(S,66);
show_seq(S);
putchar(10);
insert_tail(S,100);
show_seq(S);
putchar(10);
delete_head(S);
show_seq(S);
putchar(10);
insert_location(S,666,2);
show_seq(S);
putchar(10);
zero(S);
show_seq(S);
putchar(10);
free_seq(S);
show_seq(S);
return 0;
}
自定义函数
#include "seq_list.h"
seq_p seq_creat()
{
seq_p S=(seq_p)malloc(sizeof(seq_list));
if(S==NULL)
{
printf("apply fail\n");
return NULL;
}
S->len=0;
bzero(S->data,sizeof(S->data));
return S;
}
int empty_seq(seq_p S)
{
if(S==NULL)
{
printf("parameter is void\n");
return -1;
}
return S->len==0?1:0;
}
int full_seq(seq_p S)
{
if(S==NULL)
{
printf("parameter is void\n");
return -1;
}
return S->len==MAX?1:0;
}
void insert_head(seq_p S,datatype data)
{
if(S==NULL)
{
printf("parameter is void\n");
return;
}
if(full_seq(S))
{
printf("the sequence is full\n");
return;
}
for(int i=S->len-1;i>=0;i--)
{
S->data[i+1]=S->data[i];
}
S->data[0]=data;
S->len++;
}
void show_seq(seq_p S)
{
if(S==NULL)
{
printf("parameter is void\n");
return;
}
if(empty_seq(S))
{
printf("the sequence is void\n");
return;
}
for(int i=0;i<S->len;i++)
printf("data[%d]=%d\n",i,S->data[i]);
}
void insert_tail(seq_p S,datatype data)
{
if(S==NULL)
{
printf("parameter is void\n");
return;
}
if(full_seq(S))
{
printf("the sequence is full\n");
return;
}
S->data[S->len]=data;
S->len++;
}
void delete_head(seq_p S)
{
if(S==NULL)
{
printf("parameter is void\n");
return;
}
if(empty_seq(S))
{
printf("the sequence is void\n");
return;
}
for(int i=0;i<S->len-1;i++)
{
S->data[i]=S->data[i+1];
}
S->len--;
}
void zero(seq_p S)
{
for(int i=0;i<S->len;i++)
S->data[i]=0;
}
void insert_location(seq_p S,datatype data,int n)
{
if(full_seq(S))
{
printf("the sequence is full\n");
return;
}
for(int i=S->len-1;i>=n;i--)
{
S->data[i+1]=S->data[i];
}
S->data[n]=data;
S->len++;
}
void free_seq(seq_p S)
{
free(S);
S=NULL;
}
实现结果