˳Ðò±íÊÇÔÚ¼ÆËã»úÄÚ´æÖÐÒÔÊý×éµÄÐÎʽ±£´æµÄÏßÐÔ±í£¬ÊÇÖ¸ÓÃÒ»×éµØÖ·Á¬ÐøµÄ´æ´¢µ¥ÔªÒÀ´Î´æ´¢Êý¾ÝÔªËصÄÏßÐԽṹ¡£ÏßÐÔ±í²ÉÓÃ˳Ðò´æ´¢µÄ·½Ê½´æ´¢¾Í³Æ֮Ϊ˳Ðò±í¡£Ë³Ðò±íÊǽ«±íÖеĽáµãÒÀ´Î´æ·ÅÔÚ¼ÆËã»úÄÚ´æÖÐÒ»×éµØÖ·Á¬ÐøµÄ´æ´¢µ¥ÔªÖС£
¼´¿ÉÒÔ˵˳Ðò´æ´¢½á¹¹¾ÍÊÇÓÃÒ»×éµØÖ·Á¬ÐøµÄ´æ´¢µ¥ÔªÒÀ´Î´æ´¢¸ÃÏßÐÔ±íÖеĸ÷¸öÔªËØ¡£ÓÉÓÚ±íÖи÷¸öÔªËؾßÓÐÏàͬµÄÊôÐÔ£¬ËùÒÔÕ¼ÓõĴ洢¿Õ¼äÏàͬ¡£ ÏßÐÔ±í°´Á´Ê½´æ´¢Ê±£¬Ã¿¸öÊý¾ÝÔªËØ (½áµã)µÄ´æ´¢°üÀ¨Êý¾ÝÇøºÍÖ¸ÕëÇøÁ½¸ö²¿·Ö¡£
Êý¾ÝÇø´æ·Å½áµã±¾ÉíµÄÊý¾Ý£¬Ö¸ÕëÇø´æ·ÅÆäºó¼ÌÔªËصĵØÖ·Ö»ÒªÖªµÀ¸ÃÏßÐÔ±íµÄÆðʼµØÖ·±íÖеĸ÷¸öÔªËؾͿÉͨ¹ýÆä¼äµÄÁ´½Ó¹ØϵÖð²½ÕÒµ½ ÓÅȱµã ˳Ðò´æ´¢ÐèÒª¿ª±ÙÒ»¸ö¶¨³¤µÄ¿Õ¼ä£¬¶ÁдËٶȿ죬ȱµã²»¿ÉÀ©³äÈÝÁ¿(Èç¹ûÒªÀ©³äÐèÒª¿ª±ÙÒ»¸öеÄ×ã¹»´óµÄ¿Õ¼ä°ÑÔÀ´µÄÊý¾ÝÖØд½øÈ¥) Á´Ê½´æ´¢ÎÞÐèµ£ÐÄÈÝÁ¿ÎÊÌ⣬¶ÁдËÙ¶ÈÏà¶ÔÂýЩ£¬ÓÉÓÚÒª´æ´¢ÏÂÒ»¸öÊý¾ÝµÄµØÖ·ËùÒÔÐèÒªµÄ´æ´¢¿Õ¼ä±È˳Ðò´æ´¢´ó¡£
list.h
#ifndef _LIST_H_ #define _LIST_H_
#define LIST_SIZE 10
typedef int data_t;
typedef struct LIST { data_t data[LIST_SIZE]; data_t iNum; }List;
typedef enum LIST_OP { LIST_ERR = -1, LIST_OK }LIST_ENUM_OP;
List* CreatList(); void DestroyList(List*plist); int InsertList(List*plist,data_t tdata,int offset); int DeleteList(List*plist,data_t* pdata,int offset); int UpdateList(List*plist,data_t olddata,data_t newdata); int FindList(List * plist,data_t data); void ShowList(List*plist);
#endif // _LIST_H_ |
list.c
#include <stdio.h> #include <string.h> #include <stdlib.h> #include "list.h"
/********************************************* º¯ÊýÃû£ºCreatList() º¯Êý¹¦ÄÜ£º´´½¨Ë³Ðò±í ×÷Õߣºxxxx ÈÕÆÚ£ºxxxx Ð޸ģº **********************************************/ List* CreatList() { List * plist = malloc(sizeof(List)); if(NULL == plist) { printf("malloc error\n"); return plist; } memset(plist,0,sizeof(List));
return plist; }
/********************************************* º¯ÊýÃû£ºDestroyList(List*plist) º¯Êý¹¦ÄÜ£ºÏú»Ù˳Ðò±í ×÷Õߣºxxxxxx ÈÕÆÚ£ºxxxxx Ð޸ģº **********************************************/ void DestroyList(List* plist) { if(NULL == plist) { return ; } free(plist); plist = NULL; return ; }
/***************************************************** º¯ÊýÃû£ºInsertList(List*plist,data_t tdata,int offset) º¯Êý¹¦ÄÜ£ºÏú»Ù˳Ðò±í ×÷Õߣºxxxxx ÈÕÆÚ£ºxxxxx Ð޸ģº ***********************************************************/ int InsertList(List*plist,data_t tdata,int offset) { if((NULL == plist)||(offset< 0)||(offset>plist->iNum)||(plist->iNum==LIST_SIZE)) { printf("InsertList not error\n"); return LIST_ERR; } int MoveCount = plist->iNum-offset; int i = plist->iNum-1; for(;MoveCount>0;MoveCount--) { plist->data[i+1] = plist->data[i]; i--; } plist->data[offset] = tdata; plist->iNum++; return LIST_OK; }
/******************************************************* º¯ÊýÃû:DeleteList(List*plist,data_t* pdata,int offset) º¯Êý¹¦ÄÜ£ºÏú»Ù˳Ðò±í ×÷Õߣºxxxxxx ÈÕÆÚ£ºxxxxxxx Ð޸ģº ***********************************************************/ int DeleteList(List*plist,data_t* pdata,int offset) { if((NULL == plist)||(NULL == pdata)||(offset<0)||(offset>= plist->iNum)||(0==plist->iNum)) { printf("delete is error\n"); return LIST_ERR; } int i = offset; *pdata = plist->data[offset]; for(;i<plist->iNum-1;i++) { plist->data[i] = plist->data[i+1]; } plist->iNum--; return LIST_OK; }
/***************************************************************** º¯ÊýÃû£ºUpdateList(List*plist,data_t olddata,data_t newdata) º¯Êý¹¦ÄÜ£º¸üÐÂÊý¾Ý ×÷Õߣºxxxxx ÈÕÆÚ£ºxxxx Ð޸ģº ******************************************************************/ int UpdateList(List*plist,data_t olddata,data_t newdata) { if(NULL == plist) { printf("update is error\n"); return LIST_ERR; } int i; for(i=0;i<plist->iNum;i++) { if(olddata == plist->data[i]) { plist->data[i] = newdata; } } return LIST_OK; }
/********************************************* º¯ÊýÃû£ºFindList(List * plist,data_t data); º¯Êý¹¦ÄÜ£ºÏú»Ù˳Ðò±í ×÷Õߣºxxxxx ÈÕÆÚ£ºxxxxx Ð޸ģº **********************************************/ int FindList(List * plist,data_t data) { if(NULL == plist) { printf("find list error\n"); return LIST_ERR; } int i; for(i= 0;i<plist->iNum;i++) { if(data == plist->data[i]) { return i; } } printf("not find\n"); return LIST_OK; }
/********************************************* º¯ÊýÃû£ºShowList(List*plist); º¯Êý¹¦ÄÜ£ºÏú»Ù˳Ðò±í ×÷Õߣºxxxxx ÈÕÆÚ£ºxxxxx Ð޸ģº **********************************************/ void ShowList(List*plist) { if(NULL == plist) { return ; } int i; for(i = 0;i<plist->iNum;i++) { printf(" %d",plist->data[i]);
} printf("\n"); return LIST_OK; }
|
main.c
#include <stdio.h> #include "list.h"
int main () { List* plist = CreatList(); if(NULL == plist) { printf("creat error\n"); return -1; } int i,ret; for(i= 0;i<LIST_SIZE;i++) { ret = InsertList(plist,i,i); if(ret == LIST_ERR) { break; } } ShowList(plist); data_t num = -1; DeleteList(plist,&num,1); printf("the delete is %d\n",num); ShowList(plist);
ret = FindList(plist,6); printf("the findlist num %d\n",ret);
UpdateList(plist,8,88); ShowList(plist); DestroyList(plist); plist = NULL; return 0; } |