數據結構—線性表之順序表的操作

數據結構中,線性表之順序表的操作,包括初始化、創建順序表、插入,刪除,查找
#include <stdio.h>
#include <stdlib.h>

/**
  * @brief 線性表的順序存儲結構,在存、讀數據時不管是哪個位置,時間複雜度都爲O(1)
  *        而插入或刪除時,時間複雜度爲O(n)
  *        比較適合元素個數不太變化,而更多是存取數據的應用
  */
//順序表的結構定義
#define MAXSIZE 10

typedef struct {
	int data[MAXSIZE];	//數組長度是存放線性表的存儲空間的長度,存儲分配後這個量是不變的
	int length;			//線性表的長度是線性表中數據元素的個數,隨插入和刪除操作,量會變化
}Sqlist;

/**
  * @brief 初始化順序表
  */
void initList(Sqlist *L)
{
	int i;
	for ( i = 0; i < MAXSIZE; ++i ) {
		L->data[i] = 0;
	}
	L->length = 0;
}
/**
  * @brief 創建順序表
  */
void createList(Sqlist *L)
{
	int i;
	L->length = 5;	//線性表中數據元素的個數爲5

	int element[5] = { 1, 2, 3, 4, 5 };
	for ( i = 0; i < L->length; ++i ) {
		L->data[i] = element[i];	//把數組元素依次賦值給 線性表
	}
}
/**
  * @brief 獲取元素操作
  */
int getElement(Sqlist *L, int i, int *e)
{
	if ( L->length == 0 || i < 1 || i > L->length ) 
		return 0;
	*e = L->data[i - 1];
	return 1;
}

/**
  * @brief 在L中第i個位置之前插入新的數據元素e,L的長度加1
  */
int insertList(Sqlist *L, int i, int e)//因爲L本身要發生改變,這裏用引用型
{
	int j;
	if ( L->length == MAXSIZE || i < 1 || i > L->length + 1 )
		return 0;
	if ( i <= L->length ) {//若插入的數據位置不在表尾
		for ( j = L->length; j >= i; --j )
			L->data[j] = L->data[j - 1];
	}
	//printf("enter!\n");
	L->data[i - 1] = e;
	L->length++;
	return 1;
}

/**
  * @brief 刪除L中第i個數據元素,並用e返回其值,L的長度減1
  */
int deleteList(Sqlist *L, int i, int *e)
{
	int j;
	if ( i < 1 || i > L->length )
		return 0;
	if ( i <= L->length ) {//若刪除的數據位置不在表尾
		for ( j = i - 1; j <= L->length - 1; ++j ) {
			L->data[j] = L->data[j + 1];
		}
	}
	*e = L->data[i - 1];
	L->length--;
	return 1;
}
/**
  * @brief 找出順序表裏面與給定參數相同的元素的位置
  * @param e爲要找的數據元素
  */
int locatePos(Sqlist *L, int e)
{
	int i;
	for ( i = 0; i < L->length; ++i ) {

		if ( L->data[i] == e )
			return i + 1;
		if ( i == L->length - 1 ) {
			printf("can't find!\n");
			return -1;
		}
	}
}
/**
  * @brief 打印輸出順序表裏面的數據
  */
void printList(Sqlist *L)
{
	int i; 
	for ( i = 0; i < L->length; ++i ) {
		printf("%d ", L->data[i]);
	}
	printf("\n");
}

int main()
{
	int choice = 0;
	int postion = 0;
	int element = 0;
	int retval = 0;
	
	Sqlist *L = (Sqlist *)malloc(sizeof(Sqlist));

	printf("1.init\n");
	printf("2.creat\n");
	printf("3.insert\n");
	printf("4.delete\n");
	printf("5.locate_value\n");
	printf("6.locate_pos\n");
	//printf("7.print\n");
	printf("0.exit\n");

	while ( 1 ) {
		printf("please input your choice:");
		scanf("%d", &choice);

		switch ( choice ) {
		case 1 : 
			initList(L); 
			printList(L); 
			//printf("enter!");
			break;
		case 2 : 
			createList(L); 
			printList(L); 
			break;
		case 3 :
			printf("the postion and element of insert:\n");
			scanf("%d %d", &postion, &element);
			insertList(L, postion, element); 
			printList(L);
			break;
		case 4 :
			printf("the postion of delete:\n");
			scanf("%d", &postion);
			deleteList(L, postion, &element); 
			//printf("the element of delete:%d", element);
			printList(L);
			break;
		case 5 :
			printf("the postion of searching elment:\n");
			scanf("%d", &postion);
			getElement(L, postion, &element); 
			printf("the element of search=%d\n", element);
			//printList(L);
			break;
		case 6 :
			printf("the element of find:");
			scanf("%d", &element);
			retval = locatePos(L, element);
			printf("the postion of find element:%d\n", retval);
			break;
		//case 7 : printList(); break;
		case 0 : exit(0);
		}
	}
	free(L);
}

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