1. 程式人生 > >靜態陣列實現的順序表(C語言實現)

靜態陣列實現的順序表(C語言實現)

在寫順序表之前我們先來了解下相關的一些知識,以便我們後期能夠寫出更好的東西。

資料結構是相互之間存在一種或多種特定關係的資料元素的集合。根據資料元素之間關係的不同特性,通常有如下種基本結構:

集合:結構中的元素之間除了“同屬於一個集合”的關係外,別無其他的關係。如:廣義表。

線性結構:結構中的資料元素之間存在著一個對一個的關係。如:連結串列。

樹形結構:結構中的資料元素之間存在著一個對多個的關係。如二叉樹。

圖(網)狀結構:結構中的資料元素之間存在著多個對多個的關係。如:圖。

線上性結構中,根據儲存方式分為順序表、連結串列,根據對錶的操作限制,分為棧和佇列。

順序表的特徵是:在記憶體中佔用連續的儲存單元,可以簡單的理解為順序表就是陣列,知識根據需要,在實際應用中動態分配順序表用的記憶體單元,而陣列是在編譯的時候,預分配了指定大小的記憶體單元,順序表採用的是陣列,但是陣列有靜態陣列和動態陣列所以順序表有靜態順序表和動態順序表。

順序表的功能如下:

1、初始化

2、尾插

3、尾刪

4、頭插

5、頭刪

6、任意位置插入

7、任意位置刪除

8、讀取任意位置元素並返回元素值

9、更改任意位置元素值

10、查詢任意元素,並返回陣列的下標 

test.h

#define _CRT_SECURE_NO_WARNINGS
#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE  1000

typedef int DataType;//定義資料的型別;
typedef struct SeqList
{
	DataType arr[MAX_SIZE];
	int size;//順序表的長度
}SeqList;
void SeqListPrint(SeqList *seqlist);//列印順序表
void SeqListInit(SeqList *seqlist);//初始化順序表
void SeqListPushFront(SeqList*  seqlist, DataType value);//頭插
void SeqListPushBack(SeqList*  seqlist, DataType value);//尾插
void SeqListPopFront(SeqList* seqlist);//頭刪
void SeqListPopBack(SeqList*  seqlist);//尾刪
void SeqListErase(SeqList*  seqlist);//任意位置刪
void SeqListInsert(SeqList* seqlist, int pos, DataType value);//任意位置插入
DataType SeqListRead(SeqList* seqlist, int pos);//讀取任意位置元素,並返回元素值
void SeqListChange(SeqList* seqlist, int pos, DataType value);//更改任意位置元素值
DataType SeqListFindi(SeqList* seqlist, DataType value);//查詢任意位置陣列元素,並返回陣列下標



test.c

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include "test.h"

void SeqListPrint(SeqList* seqlist)//列印順序表中的陣列
{
	//printf(__FUNCTION__);
	if (seqlist == NULL){
		//__FUNCTION__功能是以程式設計方式從在當前執行的函式中得到函式名;
		return;
	}
	int i = 0;
	int len = 0;
	len = seqlist->size;
	for (i = 0; i < len; i++)
	{
		printf("%d ", seqlist->arr[i]);
	}
	printf("\n");
}
void SeqListInit(SeqList*  seqlist)//初始化順序表
{
	if (seqlist == NULL){
		printf(__FUNCTION__);
		return;
	}
	memset(seqlist->arr, 0, MAX_SIZE*sizeof(DataType));
	seqlist->size = 0;
}

void SeqListPushFront(SeqList*  seqlist, DataType value)//頭插
{
	if (seqlist == NULL){
		printf(__FUNCTION__);
		return;
	  }
	if (seqlist->size == MAX_SIZE){
		printf("順序表已滿\n");
       }
	int i = seqlist->size;
	for (; i > 0; i--)
	{
		seqlist->arr[i] = seqlist->arr[i -1];
	}
	seqlist->arr[0] = value;
	(seqlist->size)++;

}

void SeqListPopFront(SeqList* seqlist)//頭刪
{
	if (seqlist == NULL){
		printf(__FUNCTION__);
		return;
	}
	if (seqlist->size == 0){
		printf("結構體已經空了\n");
	}
	int i = 0;
	for (i = 1; i < seqlist->size; i++){
		seqlist->arr[i] = seqlist->arr[i - 1];
	}
	seqlist->size--;

}
void SeqListPushBack(SeqList*  seqlist, DataType value) //尾插
{
	//printf(__FUNCTION__);
	if (seqlist == NULL){
		printf("非法輸入\n");
		return;
	}
	if (seqlist->size == MAX_SIZE){
		printf("順序表已滿\n");
		return;
	}
	
	
	seqlist->arr[seqlist->size] = value;
	seqlist->size++;

}

void SeqListPopBack(SeqList*  seqlist)//尾刪
{
	if (seqlist == NULL){
		printf(__FUNCTION__);
		return;
	}
	if (seqlist->size == 0){
		printf("結構體已經空了\n");
	}
	seqlist->size--;
}
void SeqListInsert(SeqList* seqlist, int pos, DataType value)//任意位置插入
{
	if (seqlist == NULL){
		return;
	}
	if (pos > seqlist->size){
		return;
	}
	if (seqlist->size == MAX_SIZE){
		printf("順序表已滿\n");
	}
	int i = seqlist->size-1 ;
	for (; i>=pos-1; --i){
	seqlist->arr[i+1] = seqlist->arr[i];
	} 
	seqlist->arr[pos-1] = value;
	seqlist->size++;
	return;

}
void SeqListErase(SeqList*  seqlist,int pos)//任意位置刪
{
	if (seqlist == NULL){
		//printf(__FUNCTION__);
		return;
	}
	if (seqlist->size == 0){
		printf("順序表空了\n");
		return;
	}
	if (pos > seqlist->size){
		printf("位置錯誤\n");
		return;
	}
	int i =pos ;
	for ( ; i < seqlist->size; i++){
		seqlist->arr[i-1] = seqlist->arr[i];
	}
	  seqlist->size--;
}


DataType SeqListRead(SeqList* seqlist, int pos)//讀取任意位置元素,並返回元素值
{
	if (seqlist == NULL){
		return;
	}
	if (seqlist->size == 0){
		printf("結構體為空\n");
		return;
	}
	if ((pos >= seqlist->size) || (pos<0)){
		printf("沒有找到\n");
	}
	else
	printf("%d", seqlist->arr[pos]);
	return seqlist->arr[pos];
}

void SeqListChange(SeqList* seqlist, int pos, DataType value)//更改任意位置元素值
{
	if (seqlist == NULL){
		return;
	}
	if ((pos >= seqlist->size)||(pos<0)){
		printf("非法更改\n");
		return;
	}
	else
	printf("%d\n", seqlist->arr[pos]);
	seqlist->arr[pos] = value;
	return;
}
DataType SeqListFindi(SeqList* seqlist, int pos)//查詢任意位置陣列元素,並返回陣列下標
	{
		if (seqlist == NULL){
			return;
		}
		if (seqlist->size == 0){
			printf("結構體為空\n");
			return;
		}
		if ((pos >= seqlist->size) || (pos<0)){
			printf("訪問非法\n");
		}
		else{
			printf("%d", seqlist->arr[pos]);
		}
		     return  pos ;
}
			
	

main.c

#define _CRT_SECURE_NO_WARNINGS
#include "test.h"
void test1()//任意位置插入的測試
{
	SeqList seqlist;
	SeqListInit(&seqlist);
	SeqListPushBack(&seqlist, 1);
	SeqListPushBack(&seqlist, 2);
	SeqListPushBack(&seqlist, 3);
	SeqListPushBack(&seqlist, 4);
	SeqListPushBack(&seqlist, 5);
	SeqListPrint(&seqlist); 
	SeqListInsert(&seqlist, 3, 6);
	SeqListInsert(&seqlist, 1, 8);
	SeqListPrint(&seqlist);
	SeqListErase(&seqlist, 2);
	SeqListErase(&seqlist, 2);
	SeqListErase(&seqlist, 2);
	SeqListErase(&seqlist, 2);
	SeqListErase(&seqlist, 2);
	SeqListErase(&seqlist, 8);
	
	SeqListPrint(&seqlist);

}
void test2()//讀取任意位置元素,並返回元素值
{
	SeqList seqlist;
	SeqListInit(&seqlist);
	SeqListPushBack(&seqlist, 1);
	SeqListPushBack(&seqlist, 2);
	SeqListPushBack(&seqlist, 3);
	SeqListPushBack(&seqlist, 4);
	SeqListPushBack(&seqlist, 5);
	SeqListPrint(&seqlist);
	SeqListRead(&seqlist, -1);

}
void test3()//更改任意位置元素值
{
	SeqList seqlist;
	SeqListInit(&seqlist);
   SeqListPushBack(&seqlist, 2);
   SeqListPushBack(&seqlist, 3);
   SeqListPushBack(&seqlist, 4);
   SeqListPrint(&seqlist);
   SeqListChange(&seqlist, -1,8);
   SeqListPrint(&seqlist);

}
void test4(){
	//查詢任意位置陣列元素,並返回陣列下標
	SeqList seqlist;
	SeqListInit(&seqlist);
	SeqListPushBack(&seqlist, 4);
	SeqListPushBack(&seqlist, 5);
	SeqListPushBack(&seqlist, 6);
	SeqListPushBack(&seqlist, 7);
	SeqListPushBack(&seqlist, 8);
	SeqListPrint(&seqlist);
	SeqListFindi(&seqlist, 5);
}

void testseqlistpush()
{
	SeqList seqlist;
	SeqListInit(&seqlist);
	SeqListPushBack(&seqlist, 2);
	SeqListPushBack(&seqlist, 3);
	SeqListPushBack(&seqlist, 4);
	SeqListPrint(&seqlist);
	SeqListPushBack(&seqlist,8);//尾插
	SeqListPushBack(&seqlist,9);
	SeqListPushBack(&seqlist,10 );
	SeqListPrint(&seqlist);
	SeqListPopBack(&seqlist);  
	SeqListPopBack(&seqlist);
	SeqListPrint(&seqlist);
}

int main()
{
	 //SeqList seqlist;
	//SeqListInit(&MySqeList);//初始化順序表
	//SeqListPushFront(&MySqeList,1);//頭插
	//SeqListPushFront(&MySqeList, 2);//頭插
	//SeqListPushFront(&MySqeList, 3);//頭插
	//SeqListPushFront(&MySqeList,4);//頭插
	//SeqListPushFront(&MySqeList, 5);//頭插
	//SeqListPopFront(&MySqeList);//頭刪
	//SeqListPrint(&MySqeList);
    //SeqListPushBack(&seqlist,8);//尾插
	//SeqListPushBack(&seqlist,9);
	//SeqListPushBack(&seqlist,10 );
	//SeqListPopBack(&seqlist);
	//SeqListPopBack(&seqlist);
	//SeqListPrint(&seqlist);
	//testseqlistpush();
	//test1();
	//test2();
	//test3();
	test4();
	getchar();
	return 0;
}
簡單的順序表就寫到這了,後期會繼續完善的!!!!