#include <stdio.h>
#include <malloc.h>
#define LIST_MAX_LENGTH 10
/**
* Linear List of integers. The key is data.
*/
typedef struct SequentialList
{
int actualLength;
int data[LIST_MAX_LENGTH];//The maximum Length is fixed.
} *SequentialListPtr;
/**
* Output the memory for the List.
*/
void outputList(SequentialListPtr paraList) {
for(int i = 0; i < paraList->actualLength; i++) {
printf("%d",paraList->data[i]);
}//of for i
printf("\r\n");
}//of OutputList
/**
* output the memeory for the list.
*/
void outputMemory(SequentialListPtr paraListPtr){
printf("The address of the structure: %ld\r\n",paraListPtr);
printf("The address of the actualLength: %ld\r\n",¶ListPtr->actualLength);
printf("The address of the data: %ld\r\n",¶ListPtr->data);
printf("The address of actual data: %ld\r\n",¶ListPtr->data[0]);
printf("The address of the second data: %ld\r\n",¶ListPtr->data[1]);
}//of outputMemory
/**
* Initialize a sequential list.No error checking for this function.
* @param paraListPtr The pointer to the list. It must be a pointer to change the i.
* @param paraValues An int array storing all element.
*/
SequentialListPtr sequentialListInit(int paraData[], int paraLength){
SequentialListPtr resultPtr = (SequentialListPtr)malloc(sizeof(struct SequentialList));
for(int i = 0;i<paraLength; i ++)
{
resultPtr->data[i] = paraData[i];
}//of for i
resultPtr->actualLength = paraLength;
return resultPtr;
}//of sequentialListInit
/**
* Insert an element into a sequential linear list.
* @param paraListPtr The pointer to the list. It must be a pointer to change the list.
* @param paraPosition The position ,e.g,0 stands for inserting at the fist position.
* @param paraValue The value to be inserted.
*/
void sequentialListInsert(SequentialListPtr paraListPtr,int paraPosition,int paraValue ){
//Step 1. Space check.
if(paraListPtr->actualLength >= LIST_MAX_LENGTH){
printf("Cannot insert element: list full.\r\n");
return;
}//of if
//Step 2.Position check.
if(paraPosition < 0)
{
printf("Cannot insert element: negative position unsupported.");
return;
}//of if
if(paraPosition > paraListPtr->actualLength)
{
printf("Cannot insert element: the position %d is bigger than the list length %d.\r\n",paraPosition,paraListPtr->actualLength);
return;
}//of if
//Step 3.Move the remaining part.
for (int i=paraListPtr->actualLength; i>paraPosition;i--)
{
paraListPtr->data[i]=paraListPtr->data[i-1];
}//of for i
//Step 4.Insert.
paraListPtr->data[paraPosition]=paraValue;
//Step 5.Update the length.
paraListPtr->actualLength ++;
}//of sequentialListInsert
/**
* Test the insert function.
*/
void sequentialInsertTest(){
int i;
int tempArray[5] = {3,5,2,7,4};
printf("----sequentialInsertTest begins.----\r\n");
//Initialize.
SequentialListPtr tempList = sequentialListInit(tempArray,5);
printf("After initialization,the list is: ");
outputList(tempList);
//Insert to the first.
printf("Now insert to the first,the list is: ");
sequentialListInsert(tempList,0,8);
outputList(tempList);
//Insert to the last.
printf("Now insert to the last ,the list is: ");
sequentialListInsert(tempList,6,9);
outputList(tempList);
//Insert beyond the tail.
printf("Now insert beyond the tail.\r\n");
sequentialListInsert(tempList,8,9);
printf("The list is : ");
outputList(tempList);
//Insert to position 3.
for(i=0;i<5;i++)
{
printf("Inserting %d.\r\n",(i + 10));
sequentialListInsert(tempList,0,(i + 10));
outputList(tempList);
}//of for i
printf("---- sequentialInsertTest ends. ----\r\n");
}//of sequentialInsertTest
/**
* Delete an element from a sequential linear list.
* @param paraListPtr The pointer to the list.It must be a pointer to change the list.
* @param paraPosition The position,e.g.,0 stands fir inserting at the first position.
* @return The deleted value.
*/
int sequentialListDeleted(SequentialListPtr paraListPtr,int paraPosition){
//Step 1.Position check.
if(paraPosition <0){
printf("Invalid position: %d.\r\n",paraPosition);
return -1;
}//of if
if(paraPosition >=paraListPtr->actualLength){
printf("Cannot delete element: the position %d is beyond the list length %d.\r\n",paraPosition,paraListPtr->actualLength);
return -1;
}//of if
//Step 2.Move the remaining part.
int resultValue = paraListPtr->data[paraPosition];
for(int i=paraPosition;i<paraListPtr->actualLength;i++)
{
paraListPtr->data[i]=paraListPtr->data[i+1];
}//of for i
//Step 3. Update the length.
paraListPtr->actualLength--;
//Step 4. Return the value.
return resultValue;
}//of sequentialListDelete
/**
* Test the delete function.
*/
void sequentialDeleteTest()
{
int tempArray[5]={3,5,2,7,4};
printf("---- sequentialDeleteTest begins.----\r\n");
//Initialize.
SequentialListPtr tempList = sequentialListInit(tempArray,5);
printf("After initialization,the list is : ");
outputList(tempList);
//Delete the first.
printf("Now delete the first,the list is: ");
sequentialListDeleted(tempList,0);
outputList(tempList);
//Delete to the last.
printf("Now delete the last, the lats is: ");
sequentialListDeleted(tempList,3);
outputList(tempList);
//Delete the second.
printf("Now delete the second,the list is: ");
sequentialListDeleted(tempList,1);
outputList(tempList);
//Delete the second.
printf("Now delete the second,the list is: ");
sequentialListDeleted(tempList,5);
outputList(tempList);
//Delete the second.
printf("Now delete the second,the list is: ");
sequentialListDeleted(tempList,-6);
outputList(tempList);
printf("---- sequentialListDeleteTest ends. ----\r\n");
outputMemory(tempList);
}//of sequentialListDeleteTest
/**
* Locate an element in the list.
* @param paraListPtr The pointer to the list.
* @param paraValue the indicated value.
* @return The position of the value,or -1 indicating not exists
*/
int locateElement(SequentialListPtr paraListPtr,int paraValue)
{
for(int i=0;i<paraListPtr->actualLength;i++)
{
if (paraListPtr->data[i]==paraValue)
{
return i;
}//of if
}//of for i
return -1;
}//of locateElement
/**
* Get an element in the list.
* @param paraListPtr The pointer to the list.
* @param paraPosition The given position.
* @return The position of the value,or -1 indicating not exists.
*/
int getElement(SequentialListPtr paraListPtr,int paraPosition){
//Step 1. Position check.
if(paraPosition <0)
{
printf("Invalid position: %d.\r\n",paraPosition);
return -1;
}//of if
if(paraPosition >=paraListPtr->actualLength)
{
printf("Cannot get element: the position %d is beyond the length %d.\r\n",paraPosition,paraListPtr->actualLength);
return -1;
}//of if
return paraListPtr->data[paraPosition];
}//of locateElement
/**
* Clear elements in the list.
* @param paraListPtr The pointer to the list.
* @return The position of the value,or -1 indicating not exists
*/
void clearList(SequentialListPtr paraListPtr)
{
paraListPtr->actualLength = 0;
}//of clearList
/**
the entrance.
*/
viod main()
{
sequentialInsertTest();
sequentialDeleteTest();
}//of main
运行结果:
心得体会:
顺序表是一种线性表的存储结构,元素之间通过连续的内存空间进行存储,可以通过下标进行随机访问。顺序表本质就是一个数组,这个数组长度不规定,它是指其中的数据是以顺序存储的方式进行存储的,不管在物理上还是逻辑上都是连续的,在逻辑上是连续的我们称之为线性表,物理上也是连续存储的我们称之为顺序表,其存储数据必须由数组下标为0开始进行存储,并且需要连续存储。其中用molloc函数为顺序表开辟空间。
顺序表的运算主要包括初始化、插入、删除和查找等操作。
1. 初始化:通俗来讲,就是赋值,给顺序表中所有元素都赋值为0或者空字符;
2. 插入:需要将新元素插到指定位置,同时更新新顺序表的长度,举个例子,就是同学排队,原先总共十个人,有一个人插队,他可以插到第一个,插到中间,或是最后(插最后意味着没有插队,所以影响最小),此时总人数变为11人,总人数加一;
3. 删除:需要将元素从顺序表中移除,并更新顺序表长度,还是拿排队举例,十个人不同意,想将那个插队的人踢出,移除队伍,此时总人数从11变为10;
4. 查找:需要在顺序表中查找一个元素,并返回其位置或未找到的表中。假如不知道插队的人,就需要从队伍第一个人开始查询,一个接一个,直至找到目标,若到最后一人还未查出,则返回队伍。
通过顺序表的四种操作方式可以看出,顺序表存在缺点。
1.每次操作需要从第一个元素开始,这就导致操作复杂度比较高,虽然顺序表提供了快速的随机访问,但在插入和删除操作上可能会遇到性能问题。特别是在数组的中间或开头插入或删除元素时,可能需要移动大量的元素来维护连续性。
2.顺序表需要连续的内存空间来存储元素,这意味着在内存分配上可能会有空间浪费。例如,如果表的大小经常变化,可能会导致过多的空闲空间或需要频繁地进行内存重新分配操作。
图像展示: