#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<assert.h> #include<string.h> #define MAXSIZE 1000 typedef int DateType; typedef struct SeqList { DateType arr[MAXSIZE]; size_t size; }SeqList; //打印静态顺序表 void PrintSeqList(SeqList *Seq) { assert(Seq); if (Seq->size == 0) { printf("静态顺序表当前为空!\n"); return; } for (int index = 0; index < Seq->size; index++) { printf("%d-> ", Seq->arr[index]); } } //初始化 void init(SeqList *Seq) { assert(Seq); memset(Seq->arr, 0, sizeof(DateType)* MAXSIZE); Seq->size = 0; } //头插 void PushHead(SeqList *Seq, DateType x) { assert(Seq); /*Seq->size = 0;*/ if (Seq->size >= MAXSIZE) { printf("静态顺序表当前已满,无法插入!\n"); return; } Seq->size++; for (int index = Seq->size - 1; index >= 0; index--) { Seq->arr[index + 1] = Seq->arr[index]; } Seq->arr[0] = x; } //头删 void PopHead(SeqList *Seq) { if (Seq->size >= MAXSIZE) { printf("静态顺序表当前已满,无法插入!\n"); return; } for (int index = 0; index < Seq->size; index++) { Seq->arr[index] = Seq->arr[index + 1]; } Seq->size--; } void Test2(SeqList *Seq) { init(Seq); PushBack(Seq, 0); PushBack(Seq, 1); PushBack(Seq, 2); PushBack(Seq, 3); PushBack(Seq, 4); PushBack(Seq, 5); PrintSeqList(Seq); printf("\n"); PushHead(Seq, 10); PrintSeqList(Seq); printf("\n"); PopHead(Seq); PrintSeqList(Seq); printf("\n"); } int main() { SeqList Seq; Seq.size = 0; Test2(&Seq); system("pause"); return 0; }
时间: 2024-10-09 19:49:36