#include <stdio.h>
#include <stdlib.h>
#define MAX 100
typedef struct seqlist
{
int data[MAX];
int last;
}seqlist;
seqlist* initSeqList()
{
seqlist* seq = (seqlist*)malloc(sizeof(seqlist));
if (seq == NULL)
{
perror("init malloc error!\n");
return NULL;
}
int last = -1;
return seq;
}
int isFull(seqlist* list)
{
return list->last == MAX - 1;
}
void insertSeqList(seqlist* list, int position, int data)
{
if (position < 0 || position > list->last + 1 || isFull(list))
{
printf("位置错误!\n");
}
position -= 1;
for (int i = list->last; i >= position; i--)
{
list->data[i + 1] = list->data[i];
}
list->data[position] = data;
list->last++;
}
void delete(seqlist* list, int position)
{
if (position < 0 || position > list->last)
{
printf("位置错误!\n");
}
position -= 1;
for (int i = position + 1; i <= list->last; i++)
{
list->data[i - 1] = list->data[i];
}
list->last--;
}
void modifyList(seqlist* list, int position, int data)
{
if (position < 0 || position > list->last)
{
printf("位置错误!\n");
}
position -= 1;
list->data[position] = data;
}
void printList(seqlist* list)
{
for (int i = 0; i < list->last; i++)
{
printf("%d ", list->data[i]);
}
printf("\n");
}
void noneList(seqlist* list)
{
list->last = -1;
}
int searchData(seqlist* list, int data)
{
for (int i = 0; i < list->last; i++)
{
if (list->data[i] == data)
{
return i + 1;
}
}
printf("没找到!\n");
}
int main(int argc, char const *argv[])
{
seqlist* list = initSeqList();
insertSeqList(list, 1, 1);
insertSeqList(list, 2, 3);
insertSeqList(list, 1, 5);
insertSeqList(list, 4, 7);
insertSeqList(list, 3, 9);
printList(list);
delete(list, 1);
delete(list, 4);
printList(list);
modifyList(list, 3, 8);
printList(list);
printList(list);
printf("%d在位置:%d\n", 8, searchData(list, 8));
return 0;
}
