數據結構之動態順序表(C實現)
阿新 • • 發佈:2017-10-27
int 隊列 destroy element 類型 for str ttr def
線性表有2種,分為順序表和鏈表。
順序表: 采用順序存儲方式,在一組地址連續的存儲空間上存儲數據元素的線性表(長度固定)
鏈表: 有3種,單鏈表、雙向鏈表、循環鏈表(長度不固定)
seqList.h
#ifndef SEQLIST_H #define SEQLIST_H #include <stdio.h> #include <malloc.h> typedef enum { OK=0, //正確 ERROR=1, //出錯 TRUE=2, //為真 FALSE=3 //為假 }status; typedef int ElemType; //宏定義隊列的數據類型 #define initSize 20 //線性表存儲空間的初始大小 //順序表的結構 typedef struct { //靜態順序表:使用動態分配的指針方式 ElemType *data; //順序表的數據元素指針,指向存放數據元素存儲空間的基地址 int length; //順序表的長度 int maxSize; //順序表的最大容量 }seqList; //創建順序表 pSeqList-順序表對象指針 elemSize-線性表的初始存儲容量 status initList(seqList *pSeqList,int elemSize);//銷毀順序表 void destroyList(seqList *pSeqList); //清空順序表: 將長度清零即可 status clearList(seqList *pSeqList); //判斷順序表是否為空 status isEmpityList(seqList *pSeqList); //在第i個位置上插入新元素,即在當前線性表的第i-1和第i個元素之間插入 //將原先的第i個元素以及後面所有元素都後移一個位置 status enList(seqList *pSeqList, int i, ElemType element); //刪除元素,即將位置為i的元素刪除,從i+1開始及以後的元素後移status deList(seqList *pSeqList,int i,ElemType *element); //遍歷隊列 void listTraverse(seqList *pSeqList); #endif // SEQLIST_H
seqList.c
#include "seqlist.h" /********************************************* * 線性表有2種,分為順序表和鏈表。 * 順序表: 采用順序存儲方式,在一組地址連續的存儲空間上存儲數據元素的線性表(長度固定) * 鏈表: 有3種,單鏈表、雙向鏈表、循環鏈表(長度不固定) *********************************************/ //創建順序表 pSeqList-順序表對象指針 elemSize-線性表的初始存儲容量 status initList(seqList *pSeqList,int elemSize) { //給順序表的 pSeqList->data = (ElemType *)malloc(sizeof(ElemType)*elemSize); if(!pSeqList->data) { printf("pSeqList->data malloc error!\n"); return ERROR; } pSeqList->length = 0; //最開始,順序表的元素個數為0,即長度為0 pSeqList->maxSize = elemSize; return OK; } //銷毀順序表 void destroyList(seqList *pSeqList) { free(pSeqList->data); pSeqList->data = NULL; } //清空順序表: 將長度清零即可 status clearList(seqList *pSeqList) { if(!pSeqList->data) //檢驗順序表的數據元素指針是否為空 { pSeqList->length = 0; //將長度清零 } return OK; } //判斷順序表是否為空 status isEmpityList(seqList *pSeqList) { if(pSeqList->length==0) return TRUE; return FALSE; } //在第i個位置上插入新元素,即在當前線性表的第i-1和第i個元素之間插入 //將原先的第i個元素以及後面所有元素都後移一個位置 (i=0,即刪除第一個元素) status enList(seqList *pSeqList,int i,ElemType element) { int len = pSeqList->length; //獲得線性表的長度 //i的範圍限制為0-len+1(原先長度為len,現在為len+1) if(i<0 || i>len+1) { printf("i is false!\n"); return ERROR; } //將原先的第i個元素以及後面所有元素都後移一個位置 for(int j=len-1;j>=i;j--) { pSeqList->data[j+1] = pSeqList->data[j]; } pSeqList->data[i] = element; //在i的位置上,下標為i-1,插入新元素element pSeqList->length++; //線性表的長度+1 return OK; } //刪除元素,即將位置為i的元素刪除,從i+1開始及以後的元素後移 status deList(seqList *pSeqList,int i,ElemType *element) { int len = pSeqList->length; //獲得線性表的長度 //i的範圍限制為0-len+1(原先長度為len,現在為len+1) if(i<0 || i>len+1) { printf("i is false!\n"); return ERROR; } *element = pSeqList->data[i]; //將被刪元素之後的元素逐個前移 for(int j=i+1;j<len;j++) { pSeqList->data[j-1] = pSeqList->data[j]; } pSeqList->length--; //線性表的長度-1 return OK; } //遍歷隊列 void listTraverse(seqList *pSeqList) { //如果隊列為空 if(isEmpityList(pSeqList)==TRUE) { printf("\nqueue is NULL!\n"); } printf("listTraverse: "); for(int i=0;i<pSeqList->length;i++) { printf("%d ", pSeqList->data[i]); } printf("\n"); }
main.c
/********************************************* * C實現順序表 2017/10/26 by nieXianFeng *********************************************/ #include <stdio.h> #include "seqlist.h" int main() { int value; //用於保存刪除的元素 seqList *pSeqList = (seqList*)malloc(sizeof(seqList)); if(!pSeqList) //檢測是否申請失敗 { printf("pSeqList malloc error!\n"); return -1; } //調用初始化隊列的函數 initList(pSeqList,initSize); //調用出隊函數 enList(pSeqList,0,1); enList(pSeqList,1,2); enList(pSeqList,2,3); //調用遍歷隊列的函數 listTraverse(pSeqList); //調用出隊函數 if(deList(pSeqList,1,&value)==OK) { printf("出隊一次,元素為:%d\n", value); } //調用遍歷隊列的函數 listTraverse(pSeqList); return 0; }
數據結構之動態順序表(C實現)