SLinkList.h头文件 #ifndef SLINKLIST_H_H #define MAX_SIZE 100 #define OK 0 #define ERROR -1 typedef int type; typedef struct SNode { type elem; int cur; }SLinkList[MAX_SIZE]; int init_slinkList(SLinkList); int malloc_node(SLinkList); void free_node(SLinkList, int); int print_slinkList(SLinkList); int create_slinkList(SLinkList,int*); int insert_slinkList(SLinkList, int, type); int delete_slinkList(SLinkList, type); #endif SLinkList.cpp文件 #include<stdio.h> #include<stdlib.h> #include "SLinkList.h" int init_slinkList(SLinkList slist) { for(int i = 0 ; i < MAX_SIZE - 1 ; i++) slist[i].cur = i + 1; slist[MAX_SIZE - 1].cur = 0; return OK; } int malloc_node(SLinkList slist) { int i = slist[0].cur; if(slist[0].cur) slist[0].cur = slist[i].cur; return i; } void free_node(SLinkList slist, int i) { slist[i].cur = slist[0].cur; slist[0].cur = i; } int print_slinkList(SLinkList slist) { int tmp = 1; while(slist[tmp].cur != 0) { tmp = slist[tmp].cur; printf("%d ",slist[tmp].elem); } printf("/n"); return OK; } int create_slinkList(SLinkList slist, int *head) { type num; printf("input num: "); scanf("%d",&num); int r; *head = malloc_node(slist); //malloc head node slist[*head].elem = 0; r = *head; while(num != 0) { int pos = malloc_node(slist); slist[pos].elem = num; slist[r].cur = pos; r = pos; slist[*head].elem++; scanf("%d",&num); } slist[r].cur = 0; return OK; } int insert_slinkList(SLinkList slist, int pos, type e) { if(pos < 0 || pos > slist[1].elem) return ERROR; int tmp = 1; int tmp1; while((pos--) > 0) { tmp1 = tmp; tmp = slist[tmp].cur; } int i = malloc_node(slist); slist[i].elem = e; slist[i].cur = tmp; slist[tmp1].cur = i; slist[1].elem++; return OK; } int delete_slinkList(SLinkList slist, type e) { if(0 == slist[1].elem) return ERROR; int i = 1; while(slist[slist[i].cur].elem != e && i != 0) { i = slist[i].cur; } if(0 == i) return ERROR; slist[i].cur = slist[i + 1].cur; free_node(slist,i + 1); slist[1].elem--; return OK; }