生活随笔
收集整理的這篇文章主要介紹了
                                
【数据结构基础】-线性表的顺序实现(数组实现)基本操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
 
                                
                            
                            
                            2019.10.10
 【數據結構-線性表的順序結構】
 
基本操作:初始化,判斷是否空表,清空表,獲取表中的第i個元素,查找元素,插入元素,刪除元素,獲取表的元素個數。
 
抽象數據類型:
 
 
#include <stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>//數據結構-線性表的順序表示和實現#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0#define MAXSIZE 100  //定義線性表的最大長度typedef  int Status;	//Status是函數的類型,其值是函數的結果狀態碼
typedef int ElemType;	//順序表存儲元素類型,考慮到可移植性,若改變存儲類型,只需要修改這一處,默認為int類型typedef struct MyStruct
{ElemType data[MAXSIZE];	//存儲空間地址int length;		//當前長度
}List;//初始化操作,建立一個空的線性表
Status InitList(List &L);//打印線性表的所有元素
void PrintList(List L);//若線性表為空,則返回true,否則返回false
bool ListEmpty(List L);//將線性表清空
Status ClearList(List &L);//將線性表中地i個元素值返回給e
ElemType GetElem(List L, ElemType i, ElemType *e);//在線性表中查找與給定值e相等的元素,如果查找成功,返回該元素在表中的序號表示成功;否則,返回0表示失敗
int LocateElem(List L, ElemType e);//在線性表第i個位置插入新元素e
bool InsertList(List &L, ElemType i, ElemType e);//刪除線性表中第i個元素,并用e返回其值
Status ListDelete(List &L, ElemType i, ElemType &e);//返回線性表L的元素的個數
int ListLength(List L);int main() {List list1, list2;//初始化ListInitList(list1);//判斷list是否為空if (ListEmpty(list1)){printf("該線性表為空\n");}else {printf("該線性表不為空\n");}//給線性表插入元素printf("給線性表插入元素...");InsertList(list1, 1, 1);InsertList(list1, 2, 2);InsertList(list1, 3, 3);//打印線性表中的元素PrintList(list1);//查找1在線性表中的序號printf("整數1在線性表中的序號: %d\n", LocateElem(list1, 1));//返回線性表中的元素個數printf("list1線性表中元素個數為:%d\n", ListLength(list1));//刪除線性表中的元素int num;ListDelete(list1, 1, num);//打印線性表中的元素PrintList(list1);//返回線性表中的元素個數printf("list1線性表中元素個數為:%d\n", ListLength(list1));system("pause");return 0;
}//初始化操作,建立一個空的線性表
Status InitList(List &L) {L.length = 0;return OK;
}//打印線性表的所有元素
void PrintList(List L){if (L.data == NULL) {exit(0);}printf("該線性表的元素為: ");for (int i = 0; i < L.length ; i++){printf("%d ", L.data[i]);}printf("\n");
}//若線性表為空,則返回true,否則返回false
bool ListEmpty(List L){if (L.data == NULL){return false;}else{return true;}
}//將線性表清空
Status ClearList(List &L){if (L.data != NULL){free(L.data);}return OK;
}//將線性表中地i個元素值返回給e
ElemType GetElem(List L, ElemType i, ElemType *e){if (i<0 && i>L.length){	//輸入的i有誤return ERROR;}*e = L.data[i - 1];return OK;
}//在線性表中查找與給定值e相等的元素,如果查找成功,返回該元素在表中的序號表示成功;否則,返回0表示失敗
int LocateElem(List L, ElemType e){for (int i = 0; i < L.length; i++){if (L.data[i] == e) {return i + 1;		//注意:返回的是該元素在線性表的序號,并非下標!}else{return 0;}}
}//在線性表第i個位置插入新元素e
bool InsertList(List &L, int i, ElemType e)
{if (i<1 || i>L.length + 1)		//判斷i的范圍是否有效{printf("位置無效!\n");return false;}if (L.length >= MAXSIZE)		//當前存儲空間已滿,不能插入{printf("當前存儲空間已滿!!!\n");return false;}//表中最后一個元素的下標是length-1,再后一位是length			for (int j = L.length; j >= i; j--)L.data[j] = L.data[j - 1];L.data[i - 1] = e;				//在位置i處放入eL.length++;					//線性表長度加1return true;
}//刪除線性表中第i個元素,并用e返回其值
Status ListDelete(List &L, ElemType i, ElemType &e){if (i<0 || i>L.length){		//輸入的序號錯誤!return ERROR;}e = L.data[i - 1];		//將被刪除的元素賦值給e,通過e返回其值for (int j = i; j <= L.length - 1; j++) {L.data[j - 1] = L.data[j];}L.length--;return OK;
}//返回線性表L的元素的個數
int ListLength(List L){return L.length;
}
                            
總結
                            
                                以上是生活随笔為你收集整理的【数据结构基础】-线性表的顺序实现(数组实现)基本操作的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                            
                                如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。