两种方式实现顺序线性表
#include<stdio.h>
#include<stdlib.h>
#define INITSIZE 20
typedef int Elemtype;
typedef struct {
Elemtype* data;
Elemtype length;
}sqlist;
void initlist(sqlist* l);
int emptlist(sqlist* l);
void printlist(sqlist* l);
void addlist(sqlist* l, int a[], int n);
int insertlist(sqlist* l, int a, int b);
int deletelist(sqlist* l, int a, int* e);
int modify(sqlist* l, int a, int b);
int main() {
int a[5] = { 1,2,3,4,5 };
sqlist l1;
initlist(&l1);
if (emptlist(&l1))
{
printf("初始化成功\n");
}
addlist(&l1, a, 5);
printlist(&l1);
insertlist(&l1, 3, 9);
printlist(&l1);
int e;
deletelist(&l1, 4, &e);
printlist(&l1);
modify(&l1, 3, 8);
printlist(&l1);
system("pause");
return 0;
}
//初始化线性表
void initlist(sqlist* l) {
l->length = 0;
l->data = (Elemtype*)malloc(sizeof(Elemtype) * INITSIZE);
}
//判断线性表是否为空;
int emptlist(sqlist* l) {
if (l->length==0)
return 1;
else
return 0;
}
//遍历线性表
void printlist(sqlist* l) {
for (int i = 0; i < l->length; i++)
{
printf("%d", l->data[i]);
}
printf("\n");
}
//添加到线性表
void addlist(sqlist* l, int a[], int n) {
for (int i = 0; i < n; i++)
{
l->data[i] = a[i];
}
l->length = n;
}
//插入操作
int insertlist(sqlist* l, int a, int b) {
if (a > l->length||a<1) {
printf("超出要求范围");
return 0;
}
if (a < l->length) {
for (int i = l->length-1; i >= a-1; i--)
{
l->data[i + 1] = l->data[i];
}
l->data[a-1] = b;
l->length++;
return 1;
}
}
//删除操作;
int deletelist(sqlist* l, int a, int* e) {
if (a > l->length || a < 1) {
printf("超出要求范围");
return 0;
}
*e = l->data[a];
if (a < l->length) {
for (int i = a; i < l->length; i++) {
l->data[i - 1] = l->data[i];
}
l->length--;
return 1;
}
}
int modify(sqlist* l, int a, int b) {
if (a > l->length || a < 1) {
printf("超出要求范围\n");
return 0;
}
if (a<l->length)
{
l->data[a-1] = b;
printf("修改成功\n");
return 1;
}
}
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define MAXSIZE 50
typedef int Elemtype;
typedef struct {
Elemtype data[MAXSIZE];
int Maxsize, length;
}sqlist;
void initList(sqlist* l);
int createList(sqlist* l, int a[], int n);
int emptyList(sqlist* l);
int Length(sqlist* l);
void printList(sqlist* l);
int getList(sqlist* l, int k, int* ptr);
int insertList(sqlist* l, int k, int x);
int deleList(sqlist* l, int k, Elemtype* e);
int createList(sqlist* l, int a[], int n);
int main() {
Elemtype a[5] = { 1,2,3,4,5 };
sqlist list1;
initList(&list1);
if (emptyList(&list1))
{
printf("初始化成功\n");
}
printf("赋值12345并且遍历\n");
createList(&list1, a, 5);
printList(&list1);
printf("第三位插入9\n");
insertList(&list1, 3, 9);
printList(&list1);
int x;
if (deleList(&list1, 2, &x) == 1) {
printf("第2位被删除删除的值为%d\n", x);
printList(&list1);
}
system("pause");
return 0;
}
//初始化
void initList(sqlist *l) {
l->length = 0;
}
//创建表
int createList(sqlist* l, int a[], int n) {
if (n>MAXSIZE)
{
printf("空间不足\n");
return 0;
}
for (int k = 0; k < n; k++)
{
l->data[k] = a[k];
}
l->length = n;
return 1;
}
//判断表是否为空
int emptyList(sqlist* l) {
if (l->length==0)
{
return 1;
}
else {
return 0;
}
}
//求顺序表长度
int Length(sqlist* l) {
return l->length;
}
//遍历操作
void printList(sqlist* l) {
for (int i = 0; i < l->length; i++)
{
printf("%d", l->data[i]);
}
printf("\n");
}
//按位置查找
int getList(sqlist* l, int k, int* ptr) {
if (k<1||k>l->length)
{
printf("查找位置非法\n");
return 0;
}
else {
*ptr = l->data[k];
return 1;
}
}
//插入操作
int insertList(sqlist* l, int k, int x) {
if (l->length > MAXSIZE) {
printf("溢出错误");
return 0;
}
if (k<1 || k>l->length) {
printf("超出界限");
return 0;
}
for (int t=l->length-1; t >= k-1; t--)
{
l->data[t + 1] = l->data[t];
}
l->data[k - 1] = x;
l->length++;
return 1;
}
//删除
int deleList(sqlist* l, int k,Elemtype* e) {
if (l->length <0) {
printf("溢出错误");
return 0;
}
if (k<1 || k>l->length) {
printf("超出界限");
return 0;
}
*e = l->data[k - 1];
if (k<l->length)
{
for (int i = k; i < l->length ; i++)
{
l->data[i-1] = l->data[i];
}
}
l->length--;
return 1;
}