C數據結構 線性表順序表示及實現

頭文件

需要兩個頭文件:

  1. <stdio.h>
  2. <stdlib.h>
別名定義與宏定義
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1 //不可實行
#define OVERFLOW -2 //溢出
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10

typedef int Status;
typedef int ElemType;
抽象數據類型線性表定義
typedef struct {
	ElemType* elem;
	int length;
	int listsize;
}SqList;
基本操作與實現
初始化線性表

爲數據元素分配存儲空間,同時設置初始長度與表長容量

Status InitList_Sq(SqList& L) //構造一個空的線性表L
{
	L.elem = (int*)malloc(sizeof(int) * LIST_INIT_SIZE);
	if (!L.elem)
		exit(OVERFLOW);
	L.length = 0;
	L.listsize = LIST_INIT_SIZE;
	return OK;
}
銷燬線性表

free函數只把指針指向的內存空間釋放了,但是並沒有將指針的值賦爲NULL,指針仍然指向這塊內存。而程序判斷一個指針是否合法,通常都是使用if語句測試該指針是否爲NULL來判斷。

Status Destroy_Sq(SqList& L) //銷燬線性表L
{
	free(L.elem);
	L.elem = NULL;
	L.length = 0;
	L.listsize = 0;
	return OK;
}
判空
Status ListEmpty(SqList L) //判斷線性表是否爲空,若爲空表,則返回True,否則返回False
{
	return L.length == 0;
}
下標查找元素值
Status GetElem(SqList L,int i,ElemType &e) //用e返回L中第i個數據的元素
{
	if (i<0 || i>L.length + 1)
	{
		printf("該元素不存在");
		return ERROR;
	}	
	else
	{
		e = L.elem[i + 1];
		return OK;
	}
}
返回元素下標
Status LocateElem(SqList L, int i, ElemType e) //找到與e值相等第一個元素的位置並用i返回
{
	for (int n = 0; n <= L.length - 1; n++)
	{
		if (L.elem[n] == e)
		{
			i = n+1;
			break;
		}
	}
	return OK;
}
查找前驅元素
Status PriorElem(SqList L, ElemType cur_e, ElemType& pre_e) //若cur_e是L的數據元素,且不是第一個,則用pre_e返回它的前驅,否則操作失敗
{
	int num=0;//cur_e數組元素位置
	for (int i = 0; i <= L.length - 1; i++)
	{
		if (L.elem[i] == cur_e)
		{
			num = i+1;
			break;
		}
	}
	if (num <= 1 || num > L.length)
	{
		printf("操作失敗!");
		return ERROR;
	}
	else
	{
		pre_e = L.elem[num-2];
		return OK;
	}	
}
查找後繼元素
Status NextElem(SqList L, ElemType cur_e, ElemType& next_e) //若cur_e是L的數據元素,且不是最後一個,則用next_e返回它的後繼,否則操作失敗
{
	int num = 0;//cur_e數組元素位置
	for (int i = 0; i <= L.length - 1; i++)
	{
		if (L.elem[i] == cur_e)
		{
			num = i + 1;
			break;
		}
	}
	if (num < 1 || num >= L.length)
	{
		printf("操作失敗!");
		return ERROR;
	}
	else
	{
		next_e = L.elem[num];
		return OK;
	}
}
插入元素

當順序表已滿時,需要動態分配內存,這裏以十個整型變量內存爲例

Status ListInsert(SqList& L, int i, ElemType e) //在L中第i個位置之前插入新的數據元素e,L的長度加1
{
	if (i <= 1 || i >= L.length + 1)
	{
		printf("i值不合法!");
		return ERROR;
	}
	else
	{
		if (L.length >= L.listsize)
		{
			ElemType* newbase = (ElemType*)realloc(L.elem, (L.listsize + LISTINCREMENT) * sizeof(ElemType));
			if (!newbase)
				exit(OVERFLOW);
			L.elem = newbase;
			L.listsize += LISTINCREMENT;
		}
		L.elem[i - 1] = e;
		for (int n = L.length; n >= i; n--)
		{
			L.elem[n + 1] = L.elem[n];
		}
		L.length++;
		return OK;
	}
}
刪除元素
Status ListDelete(SqList& L, int i, ElemType &e) //刪除L的第i個數據元素,並用e返回其值,L的長度減1
{
	if (i < 1 || i >= L.length )
	{
		printf("不可實現!");
		return ERROR;
	}
	else
	{
		e = L.elem[i - 1];
		for (int n = i; n <= L.length - 1; n++)
		{
			L.elem[n - 1] = L.elem[n];
		}
	}
	L.length--;
	return OK;
}
順序表顯示
void ShowList(SqList L)
{
	printf("當前線性表長度爲:%d個int型數據\n", L.length);
	printf("線性表爲:");
	for (int i = 1; i <= L.length ; i++)
	{
		printf("%d ",L.elem[i - 1]);
	}
	printf(" 還有%d個整型容量!", L.listsize - L.length);
	printf("\n\n");
}
測試代碼
int main()
{
	SqList L;
	InitList_Sq(L);
	L.elem[0] = 1;L.elem[1] = 4;L.elem[2] = 2;L.elem[3] = 3;
	L.length = 4; 
	ShowList(L);

	int ele; //被刪除的元素
	ListDelete(L, 3, ele);
	ShowList(L);

	ListInsert(L, 3, 2);
	ShowList(L);

	int next; //後繼元素
	NextElem(L, 2, next);
	printf("2的後繼元素%d\n", next);

	int prior; //前驅元素
	PriorElem(L, 2, prior);
	printf("2的前驅元素%d", prior);

	for (int i = 2; i <= 98; i++)
	{
		ListInsert(L, i, i);
	}
	ShowList(L);
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章