标签:c语言 顺序表 静态顺序表的头插、头删
#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; }
本文出自 “C语言100-200素数” 博客,请务必保留此出处http://10740184.blog.51cto.com/10730184/1743136
标签:c语言 顺序表 静态顺序表的头插、头删
原文地址:http://10740184.blog.51cto.com/10730184/1743136