線性表
線性表是0個或多個類型相同的數據元素的集合,這些元素是有限的順序存儲的。
一個線性表有哪些具體的操作,在程序中的表現爲一組函數
- 創建線性表
- 銷燬線性表
- 清空線性表
- 將元素插入線性表
- 將元素從線性表中刪除
- 獲取線性表中某個位置的元素
- 獲取線性表的長度
線性表的順序存儲
一段地址連續的存儲單元依次存儲線性表的數據元素
插入數據:
在某個位置插入一個節點,我們需要檢索從插入位置起到尾部元素,從尾部元素開始依次向後移一個位置。
刪除數據:
刪除某個位置上的節點,如果需要返回這個節點,我們需要提前緩存先來,之後從這個節點的後一個節點開始依次向前移動一個位置。
順序存儲的優缺點:
優點:
無需爲線性表中的邏輯關係增加額外的空間
可以快速的獲取線性表中合法位置的元素
缺點:
需要提前確定線性表的容量
插入和刪除操作需要移動大量元素
//SeqList.h
#pragma once
typedef void SeqList;
typedef void SeqListNode;
//根據自定義容量創建並返回一個空的線性表
SeqList *SeqList_Creat(int capacity);
//銷燬一個線性表
void SeqList_Destroy(SeqList *list);
//將線性表中的元素清空,恢復到初始化狀態
void SeqList_Clear(SeqList *list);
//返回線性表中的元素個數
int SeqList_GetLength(SeqList *list);
//返回線性表中的容量
int SeqList_GetCapacity(SeqList *list);
//往線性表中的pos位置處插入一個行的元素(節點)
int SeqList_inster(SeqList *list, SeqListNode *node, int pos);
//獲取線性表中pos位置處的元素(節點),並返回
SeqListNode *SeqList_Get(SeqList *list, int pos);
//根據pos位置刪除線性表中的元素(節點)
SeqListNode *SeqList_Delete(SeqList *list, int pos);
//SeqList.c
#pragma warning(disable : 4996)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "SeqList.h"
typedef struct tag_SeqList
{
int capacity;
int length;
unsigned int **node;//int *node[]
}TSeqList;
//根據所給容量初始化線性表
SeqList *SeqList_Creat(int capacity)
{
TSeqList * temp = NULL;
if (capacity <= 0)
{
printf("SeqList_Creat err : capacity <= 0");
return NULL;
}
//分配結構體TSeqList的空間
temp = (TSeqList *)malloc(sizeof(TSeqList));
if (temp == NULL)
{
printf("SeqList_Creat err : temp = NULL");
return NULL;
}
memset(temp, 0, sizeof(TSeqList));
//根據capacity 的大小分配節點的空間
temp->node = (unsigned int **)malloc(sizeof(unsigned int **)*capacity);
if (temp->node == NULL)
{
printf("SeqList_Creat err : temp->node = NULL");
return NULL;
}
temp->capacity = capacity;
temp->length = 0;
return temp;
}
//刪除節點空間和結構體空間
void SeqList_Destroy(SeqList * list)
{
TSeqList *temp = NULL;
if (list == NULL)
{
printf("SeqList_Destroy err : list = NULL");
return;
}
temp = (TSeqList *)list;
if (temp->node != NULL)
{
free(temp->node);
}
free(temp);
temp->length = 0;
temp->capacity = 0;
printf("SeqList_Destroy finished \n");
}
//清空鏈表 回到初始化狀態
void SeqList_Clear(SeqList * list)
{
TSeqList *temp = NULL;
if (list == NULL)
{
printf("SeqList_Clear err : list = NULL");
return ;
}
temp = (TSeqList *)list;
temp->length = 0;
printf("SeqList_Destroy cleared \n");
}
//獲取鏈表長度
int SeqList_GetLength(SeqList * list)
{
TSeqList *temp = NULL;
if (list == NULL)
{
printf("SeqList_GetLength err : list = NULL");
return -1;
}
temp = (TSeqList *)list;
return temp->length;
}
//獲取鏈表容量
int SeqList_GetCapacity(SeqList * list)
{
TSeqList *temp = NULL;
if (list == NULL)
{
printf("SeqList_GetCapacity err : list = NULL");
return -1;
}
temp = (TSeqList *)list;
return temp->capacity;
}
//插入節點到線性表
int SeqList_inster(SeqList * list, SeqListNode * node, int pos)
{
TSeqList *temp = NULL;
int i = 0;
if (list == NULL || node == NULL || pos < 0)
{
printf("SeqList_inster err : list = NULL || node = NULL || pos < 0");
return -1;
}
temp = (TSeqList *)list;
//判斷插入的位置是否越界
if (pos > temp->length)
{
pos = temp->length;
}
//判斷線性表的節點是否已滿
if (temp->length == temp->capacity)
{
printf("SeqList_inster err : temp->length = temp->capacity");
return -2;
}
//要在線性表某個位置插入節點,那麼後面的節點應該向後移
//從最後一個節點開始,每個節點依次向後移動
for (i = temp->length; i > pos; i--)
{
temp->node[i] = temp->node[i - 1];
}
temp->node[pos] = node;
temp->length++;
return 0;
}
//根據位置獲得節點
SeqListNode * SeqList_Get(SeqList *list, int pos)
{
TSeqList *temp = NULL;
if (list == NULL || pos <0)
{
printf("SeqList_Get err : list = NULL || pos <0");
return NULL;
}
temp = (TSeqList *)list;
//判斷獲取的位置是否越界
if (pos > temp->length)
{
printf("SeqList_Get err : pos > temp->length");
return NULL;
}
return temp->node[pos];
}
SeqListNode * SeqList_Delete(SeqList * list, int pos)
{
TSeqList *temp = NULL;
SeqListNode * SLNode = NULL;
int i = 0;
if (list == NULL || pos < 0)
{
printf("SeqList_Delete err : list = NULL || pos <0");
return NULL;
}
temp = (TSeqList *)list;
//判斷獲取的位置是否越界
if (pos > temp->length)
{
printf("SeqList_Delete err : pos > temp->length");
return NULL;
}
//刪除之前先把要刪除的節點緩存下來
//從要刪除的位置開始,把後一個節點移動到前一個節點的位置
SLNode = temp->node[pos];
for (i = pos; i < temp->length; i++)
{
temp->node[i] = temp->node[i + 1];
}
temp->length--;
return SLNode;
}
//main.c
#pragma warning(disable : 4996)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "SeqList.h"
typedef struct tag_Teacher
{
int age;
char *name;
}Teacher;
int main(int argc, char** argv)
{
Teacher t1, t2, t3, t4, t5;
t1.age = 20;
t1.name = "zhangsan";
t2.age = 21;
t2.name = "lisi";
t3.age = 22;
t3.name = "wangwu";
t4.age = 23;
t4.name = "zhaoliu";
t5.age = 24;
t5.name = "wangqi";
SeqList *list = NULL;
int i, ret = 0;
list = SeqList_Creat(10);
if (list == NULL)
{
ret = -1;
printf("SeqList_Creat err : list = NULL");
return ret;
}
ret = SeqList_inster(list, (SeqListNode *)&t1, 0);
ret = SeqList_inster(list, (SeqListNode *)&t2, 0);
ret = SeqList_inster(list, (SeqListNode *)&t3, 0);
ret = SeqList_inster(list, (SeqListNode *)&t4, 0);
ret = SeqList_inster(list, (SeqListNode *)&t5, 0);
printf("SeqList capacity = %d\n", SeqList_GetCapacity(list));
printf("SeqList length = %d\n", SeqList_GetLength(list));
for (i = 0; i < SeqList_GetLength(list); i++)
{
Teacher *t = SeqList_Get(list, i);
printf("age = %d,name = %s\n", t->age, t->name);
}
SeqList_Delete(list, 3);
printf("SeqList_Delete \n");
for (i = 0; i < SeqList_GetLength(list); i++)
{
Teacher *t = SeqList_Get(list, i);
printf("age = %d,name = %s\n", t->age, t->name);
}
SeqList_Destroy(list);
system("pause");
return 0;
}