线性表_顺序表的基本操作

#include <stdio.h>
#include <stdlib.h>

typedef int ElementType;
#define maxSize 10
typedef struct LNode* SeList;
typedef struct LNode{
    ElementType data[maxSize];
    int last;
}LNode;

SeList InitialList();
int Find(SeList l, ElementType x);
void Insert(SeList l, ElementType x, int pos);
void Delete(SeList l, int pos);

SeList InitialList(){
    SeList l = (SeList*)malloc(sizeof(struct LNode));
    l->last = -1;
    return l;
}

int Find(SeList l, ElementType x){

    int i = 0;
    for(; i <= l->last && l->data[i] != x; i++)
        ;
    if(i > l->last) return -1;
    return i;
}

void Insert(SeList l, ElementType x, int pos){

    if(l->last >= maxSize - 1) return;
    if(pos < 1 || pos > l->last + 2) return;

    int i ;
    for(i = l->last; i >= pos - 1; i--)
        l->data[i + 1] = l->data[i];
    l->data[pos - 1] = x;
    l->last++;
}

void Delete(SeList l, int pos){

    if(l->last == -1) return;
    if(pos < 1 || pos > l->last + 1) return;

    for(int i = pos; i <= l->last; i++)
        l->data[i - 1] = l->data[i];
    l->last--;
}

猜你喜欢

转载自blog.csdn.net/hang981601026/article/details/82942551