"Data Structure" Min
Header file SQlist.h
1 #ifndef sqlist_h_included2 #definesqlist_h_included3#include <stdio.h>4 #defineOK 15 #defineERROR-16 #defineTRUE 17 #defineFALSE 08 9 #defineMAXSIZE 100/* Storage space Initial allocation */Ten #defineListcreaty 10/* Allocation increment */ OnetypedefintElemtype;/*the Elemtype type is based on the actual situation and is assumed to be an int*/ AtypedefintStatus;/*Status is the type of the function, whose value is the function result status code, such as OK, etc.*/ - -typedefstruct the { -Elemtype *data;/*arrays, storing data elements*/ - intLength/*Linear table Current length*/ - intSize/*Linear table Capacity*/ + }sqlist; -Status Initlist (sqlist&); +Status Destorylist (sqlist&); A Status listlength (sqlist); at Status Listempty (sqlist); -Status Getelem (SqList,int, elemtype&); -Status insertsqlist (Sqlist&,int, elemtype); - Status Foundelem (sqlist, elemtype); -Status DeleteList (Sqlist&,int, elemtype&); - voidClearlist (sqlist&); in #endif
function implementation
1#include"SQlist.h"2#include <malloc.h>3#include <iostream>4 using namespacestd;5 /*Initialize sequential linear table*/6Status Initlist (sqlist&L)7 {8L.data = (elemtype*)malloc(sizeof(elemtype) *MAXSIZE);9 if(!l.data)Ten { One returnERROR; A } -L.size =MAXSIZE; -L.length =0; the returnOK; - } - /*destroying elements*/ -Status Destorylist (sqlist&L) + { - if(l.length) + { A Free(l.data); at } -L.data =NULL; -L.length =0; - returnOK; - } - /*Empty the linear table*/ in voidClearlist (sqlist&L) - { toL.length =0; + } - /*judged to be empty*/ the Status listempty (sqlist L) * { $ if(l.length)Panax Notoginseng { - return 0; the } + return 1; A } the /*inserts an element of position I*/ +Status Insertsqlist (Sqlist&l,intI, elemtype e) - { $ $ if(i<1|| I>l.length +1) - { -cout <<"NO"<<Endl; the //return ERROR; - }Wuyi if(L.length >l.size) the { -Elemtype *newbase = (elemtype*)realloc(L.data,sizeof(elemtype) * (l.size+MAXSIZE)); Wu if(!newbase) - { About returnERROR; $ } -L.data =newbase; -L.size + =MAXSIZE; - } A if(L.length! =0) + { theElemtype *q, *p; -Q = &l.data[i-1]; $p = &l.data[l.length-1]; the for(; p >= Q; p--) the { the* (P +1) = *p; the } -*q =e; inl.length++; the } the Else About { theL.data[l.length] =e; thel.length++; the } + returnOK; - } theStatus Getelem (SqList L,intI, elemtype&e)Bayi { the if(i<0&& i>l.length) the return 0; - Else - { theE = L.data[i-1]; the } the return 1; the } - Status listlength (sqlist L) the { the returnl.length; the }94 Status Foundelem (sqlist L, elemtype e) the { the inti; the for(i =0; i < l.length; i++)98 { About if(L.data[i] = =e) - {101 returni+1;102 }103 }104 return 0; the }106Status DeleteList (Sqlist&l,intI, elemtype&e)107 {108 if(i<1|| I>l.length)109 { the returnERROR;111 } theElemtype *p = &l.data[i-1];113Elemtype *q = l.data + L.length-1; theE = *p; the for(; p <= Q; p++) the {117* (P-1) = *p;118 }119l.length--; - returnOK;121}
Data structure-sequential implementation of linear tables