一、線性表:零個或多個數據元素的有限序列。 ios
二、線性表的順序存儲結構:用一段連續的存儲單元依次存放線性表的數據元素。這種結構在獲取節點的值的時候很是簡單,可是在插入數據元素或者刪除數據元素時,前面和後面的元素都得作相應的移動,很是浪費時間。這種結構比較審覈元素個數固定,不會輕易進行數據增長或者刪除的狀況。 spa
下面是針對這種結構的插入刪除方法: code
#filename:list.h #define MAXSIZE 30 typedef int ElemType; typedef struct{ ElemType data[MAXSIZE]; int length; }SqList,* pSqList; #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 typedef int Status; Status InsertElem(pSqList L,int i,ElemType e); Status DeleteElem(pSqList L,int i,ElemType & e); void PrintList(pSqList L);
#filename:list.cpp Status InsertElem(pSqList L,int i,ElemType e){ if(L->length == MAXSIZE){ return ERROR; } if(i < 1 || i > L->length+1){ return ERROR; } if(i <= L->length){ for(int k = L->length -1; k >= i-1; k--){ L->data[k+1] = L->data[k]; } } L->data[i-1] = e; L->length ++; return OK; } void PrintList(pSqList L){ for(int i = 0; i < L->length; ++i){ cout << L->data[i] << endl; } } Status DeleteElem(pSqList L,int i,ElemType & e){ if(L->length == 0){ return ERROR; } if(i < 1 || i > L->length){ return ERROR; } e = L->data[i-1]; if(i < L->length){ for(int j = i-1; j < L->length; ++j){ L->data[j] = L->data[j+1]; } } L->length--; return OK; }
#filename:main.cpp #include <iostream> using namespace std; #include "list.h" #include "list.cpp" int main(){ pSqList list = new SqList; int e; list->length = 0; cout << InsertElem(list,1,11) << endl; cout << InsertElem(list,2,12) << endl; cout << InsertElem(list,1,13) << endl; cout << DeleteElem(list,2,e) << endl; PrintList(list); return 0; }
三、線性表的鏈式存儲結構: io