List.h //------------------------------------------------ // List structure definition. //================================================ #ifndef _LIST_H_ #define _LIST_H_ #include <iostream> using namespace std; #include <malloc.h> // malloc() ... #include <process.h> // exit() ... typedef int ElemType; #define LIST_INIT_SIZE 10 // List's initiate size #define LIST_INCREMENT 2 // list's increment memory size typedef struct{ ElemType *item; // store space base adress int iLength; // current length int iListSize; // Current malloc memory size }sqList; bool InitList(sqList *L); bool DestroyList(sqList *L); bool ClearList(sqList *L); bool ListEmpty(sqList L); int ListLength(sqList L); bool GetElement(sqList L, int iPos, ElemType *e); bool ListInsert(sqList *L, int iPos, ElemType e); bool ListDelete(sqList *L, int iPos, ElemType *e); bool ListTraverse(sqList L, void (*vi) (ElemType *)); void Visit(ElemType *e); #endif //_LIST_H_ List.cpp #include "list.h" /* Parameter : sqList * * Return : true or false * Description : Construct a list */ bool InitList(sqList *L) { L->item = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType)); if (!L->item) { exit(-2); // overflow } L->iLength = 0; // L->iListSize = LIST_INIT_SIZE; return true; } // End of InitList /* Parameter : sqList *, int , ElemType * Return : true or false * Description : Insert an Element to destinated position of the list */ bool ListInsert(sqList *L, int iPos, ElemType e) { ElemType *newBase, *q, *p; if (iPos<1 || iPos>L->iLength+1) { return false; // iPos is illegal } if (L->iLength >= L->iListSize) { // current store space is full newBase = (ElemType *)realloc(L->item, (L->iListSize + LIST_INCREMENT) * sizeof(ElemType) ); if (!newBase) { exit(-2); // overflow } L->item = newBase; L->iListSize += LIST_INCREMENT; } q = L->item + iPos - 1; // q is the position where to insert for (p = L->item + L->iLength - 1; p >= q; --p) { *(p + 1) = *p; } *q = e; // insert the element L->iLength ++; // list length add one return true; } // End of ListInsert /* * Description : Output the element */ void Visit(ElemType *e) { cout<<"Element is : "<<*e<<endl; } /* Parameter : sqList, void(*vi) (ElemType *) * Return : true or false * Description : visit the element of the list */ bool ListTraverse(sqList L, void (*vi) (ElemType *)) { ElemType *p; int i; p = L.item; for (i = 1; i <= L.iLength; ++i) { vi(p++); } return true; } /* Parameter : sqList * Return : list's length * Description : Get list length */ int ListLength(sqList L) { return L.iLength; } // End of ListLength /* Parameter : sqList, int, ElemType * Return : true or false * Description : Get the element of destination position */ bool GetElement(sqList L, int iPos, ElemType *e) { ElemType *pe = L.item; if (iPos < 0 || iPos > L.iLength) { cout<<"wrong position!"<<endl; return false; } *e = *(pe + iPos - 1); // get the element directly return true; } // End of GetElement /* Parameter : sqList& , int , ElemType *e * Return : ture of false * Description : delete the element and return its value in element */ bool ListDelete(sqList *L, int iPos, ElemType *e) { ElemType *pe = L->item; if (iPos < 0 || iPos > L->iLength) { cout<<"wrong position!"<<endl; return false; } *e = *(pe + iPos - 1); // store the element of position for (int i = iPos; i < L->iLength; ++i) { *(pe + i -1) = *(pe + i); } L->iLength--; return true; } // End of ListDelete /* Parameter : sqList * Return : true or false * Description : decide the list is empty or not */ bool ListEmpty(sqList L) { if (!L.iLength) return true; else return false; } // End of ListEmpty Main.cpp //----------------------------------------------------------------------------- // Copyright (c) 2009 eryar All rights reserved. // // File : Main.cpp // Date : 2009-05-11 22:49 // Author : eryar@163.com // // Description: // // // //============================================================================= #include "list.h" int main(int argc, char *argv[]) { sqList list; ElemType element; if (InitList(&list)) { cout<<"Initiate list successed!"<<endl; } if (ListEmpty(list)) { cout<<"list now is empty!"<<endl; } for (int i=0; i<9; ++i) { ListInsert(&list, i, rand() % 101); //random 100 number and insert to the list } ListTraverse(list, Visit); if (!ListEmpty(list)) { cout<<"list now is not empty!"<<endl; } cout<<"List length is : "<<ListLength(list)<<endl; cout<<"Third element is : "; if (GetElement(list, 3, &element)) { Visit(&element); } if (ListDelete(&list, 3, &element) ) { cout<<"After delete 3rd element : "; Visit(&element); cout<<"After delete 2nd element : "; ListDelete(&list, 2, &element); Visit(&element); } ListTraverse(list, Visit); return 0; }