简单顺序表的增删改查实例:
#include <stdio.h> #define MAX 100 typedef struct { int length; int data[MAX]; }sqlist,*Psqlist; void initList(Psqlist p); void insertList(Psqlist p,int elem); void deleteList(Psqlist p,int locate); void displist(Psqlist p); void modiList(Psqlist p,int locate,int elem); int main() { sqlist list; Psqlist plist = &list; // 初始化 initList(plist); // 增 insertList(plist,12); insertList(plist,13); insertList(plist,14); insertList(plist,15); displist(plist); // 删 deleteList(plist,1); printf("\nAfter Delete\n"); displist(plist); // 改 modiList(plist,3,111); // 查 printf("\nAfter Modified\n"); displist(plist); } void initList(Psqlist p) { p->length = 0; for (int i = 0; i < 100; i ++) p->data[i] = 0; } void insertList(Psqlist p,int elem) { p->data[p->length] = elem; p->length ++; } void deleteList(Psqlist p,int locate) { if (locate > p->length || locate < 0) { printf("输入无效"); return; } for (int i = locate; i < p ->length-1; i++) p->data[i] = p->data[i+1]; p->data[p->length] = 0; p->length --; } void displist(Psqlist p) { for (int i = 0; i < p->length; i++) printf("%d\t",p->data[i]); printf("\n"); } void modiList(Psqlist p,int elem) { if (locate > p->length || locate < 0) { printf("输入无效"); return; } p->data[locate] = elem; }原文链接:https://www.f2er.com/datastructure/383116.html