#ifndef _seqlist_h_#define_seqlist_h_typedefvoidSeqlist;typedefvoidSeqlistnode; Seqlist* Seqlist_create (intcapacity);voidSeqlist_destroy (seqlist*list);voidSeqlist_clear (seqlist*list);intSeqlist_length (seqlist*list);intSeqlist_capacity (seqlist*list);intSeqlist_insert (seqlist* list, seqlistnode* node,intPOS); Seqlistnode* Seqlist_get (seqlist* list,intPOS); Seqlistnode* Seqlist_delete (seqlist* list,intPOS);#endif
#include <stdio.h> #include <malloc.h> #include "SeqList.h" typedef unsigned int tseqlistnode;typedef struct _tag_seqlist{int capacity; int length; tseqlistnode* node;} Tseqlist; seqlist* seqlist_create (int capacity)//O (1) {tseqlist* ret = NULL; if (capacity >= 0) {ret = (tseqlist*) malloc (sizeof (tseqlist) + sizeof (tseqlistnode) * capacity); } if (ret! = NULL) {ret->capacity = capacity; ret->length = 0; Ret->node = (tseqlistnode*) (ret + 1); } return ret;} void Seqlist_destroy (seqlist* list)//O (1) {free (list);} void Seqlist_clear (seqlist* list)//O (1) {tseqlist* sList = (tseqlist*) list; if (sList! = NULL) {slist->length = 0; }}int seqlist_length (seqlist* list)//O (1) {tseqlist* sList = (tseqlist*) list; int ret =-1; if (sList! = NULL) {ret = slist->length; } return ret;} int seqlist_capacity (seqlist* list)//O (1) {TSeqlist* sList = (tseqlist*) list; int ret =-1; if (sList! = NULL) {ret = slist->capacity; } return ret;} int Seqlist_insert (seqlist* list, seqlistnode* node, int pos)//O (n) {tseqlist* sList = (tseqlist*) list; int ret = (sList! = NULL); int i = 0; RET = ret && (slist->length + 1 <= slist->capacity); RET = ret && (0 <= POS); if (ret) {if (pos >= slist->length) {pos = slist->length; } for (i=slist->length; i>pos; i--) {Slist->node[i] = slist->node[i-1]; } Slist->node[i] = (tseqlistnode) node; slist->length++; } return ret;} seqlistnode* seqlist_get (seqlist* list, int pos)//O (1) {tseqlist* sList = (tseqlist*) list; seqlistnode* ret = NULL; if ((sList! = NULL) && (0 <= Pos) && (POS < slist->length)) {ret = (Seqlistnode*) (Slist->node[pos]); } return ret;} seqlistnode* seqlist_delete (seqlist* list, int pos)//O (n) {tseqlist* sList = (tseqlist*) list; seqlistnode* ret = seqlist_get (list, POS); int i = 0; if (ret! = NULL) {for (i=pos+1; i<slist->length; i++) {slist->node[i-1] = Slist-> ; node[i]; } slist->length--; } return ret;}}
#include <stdio.h>#include<stdlib.h>#include"SeqList.h"/*Run this program using the console Pauser or add your own getch, System ("pause") or input loop*/intMainintargcChar*argv[]) {Seqlist* List = Seqlist_create (5); inti =0; intj =1; intK =2; intx =3; inty =4; intz =5; intindex =0; Seqlist_insert (list,&i,0); Seqlist_insert (list,&j,0); Seqlist_insert (list,&k,0); Seqlist_insert (list,&x,0); Seqlist_insert (list,&y,0); Seqlist_insert (list,&z,0); for(index=0; Index<seqlist_length (list); index++) { int* p = (int*) Seqlist_get (list, index); printf ("%d\n", *p); } printf ("\ n"); while(Seqlist_length (list) >0 ) { int* p = (int*) Seqlist_delete (list,0); printf ("%d\n", *p); } Seqlist_destroy (list); return 0;}
Implementation of linear table