1.定义顺序表
typedef struct SequentialList{
int actualLength; //(实际长度)
int data[LIST_MAX_LENGTH];//The maxinum length is fixed.(最大值的长度是固定的).
} *SequentialListPtr;//(顺序列表Ptr)
2.依次输出顺序表
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
3.初始化顺序表
SequentialListPtr sequentialListInit(int paraData[],int paraLength){
SequentialListPtr resultPtr = (SequentialList*)malloc(sizeof(SequentialList));
for (int i = 0; i < paraLength; i++){
resultPtr->data[i] = paraData[i];
}//of for i
resultPtr->actualLength = paraLength;
return resultPtr;
}//of sequentialLIstInit
4.插入元素
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 biggger than the length %d.\r\n",paraPosition,paraListPtr -> actualLength);
return;
}//of it
//step 3. Move the remainig part.移动剩下的元素;
for (int i = paraListPtr -> actualLength;i > paraPosition; i--){
paraListPtr -> data[i] = paraListPtr -> data[i-1];
}//of it i
//Step 4. Insert.插入;
paraListPtr -> data[paraPosition] = paraValue;
//Updata the length.更新以前的数据;
paraListPtr -> actualLength ++;
}//of sequentialListInsert
5.测试插入功能
void sequentialInsertTest(){
int i;
int tempArray[] = {3,5,2,7,4};
printf("--- sequentialIsertTest 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 position3.插入到第三个位置;
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 suquentialInsertTest
6.测试删除功能
void sequentialDeleteTest(){
int tempArray[5] = {3,5,2,7,4};
printf("--- sequentialDeleteTest begins. ---\r\n");
//初始化;
SequentialListPtr tempList = sequentialListInit(tempArray,5);
printf("After initialization,the list is : ");
outputList(tempList);
//删除顺序表的第一位置;
printf("Now delete the first,the list is : ");
sequentialListDelete(tempList,0);
outputList(tempList);
//删除顺序表的最后位置;
printf("Now delete the last, the list is : ");
sequentialListDelete(tempList,3);
outputList(tempList);
//删除顺序表的第二个置:
printf("Now delete the second, the list is : ");
sequentialListDelete(tempList,1);
outputList(tempList);
//删除顺序表的第六个置:
printf("Now delete the 5th, the list is : ");
sequentialListDelete(tempList,5);
outputList(tempList);
//.删除顺序表的最后位置后;
printf("Now delete the (-6)th, the list is : ");
sequentialListDelete(tempList,-6);
outputList(tempList);
printf("--- sequentialDeleteTest ends. ---\r\n");
outputMemory(tempList);
}
7.顺序表的清除
void clearList(SequentialListPtr paraListPtr){
paraListPtr -> actualLength = 0;
} //of clearList
8.主函数
int main(){
sequentialInsertTest();
sequentialDeleteTest();
}
9.总代码
#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 maxinum length is fixed.(最大值的长度是固定的).
} *SequentialListPtr;//(顺序列表Ptr)
/**
*Output 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 memeroy for the list.打印地址;
**/ //输出存储器
void outputMemory(SequentialListPtr paraListPtr){
printf("The address of the structure : %ld\r\n",paraListPtr);
printf("The address of actualLength : %ld\r\n",¶ListPtr->actualLength);
printf("The address of data : %ld\r\n",¶ListPtr->data);
printf("The address of actual data : %ld\r\n",¶ListPtr->data[0]);
printf("The address of second data : %ld\r\n",¶ListPtr->data[1]);
}//of outputMemory
/**
*Intialize a sequential list.No error cheking for this function.
*@param paraListPtr The to the list. It must be a pointer to change the list.
*@param paraValues An int array storing alll elments.
**/
//初始化;
SequentialListPtr sequentialListInit(int paraData[],int paraLength){
SequentialListPtr resultPtr = (SequentialList*)malloc(sizeof(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 poniter to the list. It muxt be a pointer to change the list.
*@param paraPosition The position,e.g.,0 stands for inserting the first 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 biggger than the length %d.\r\n",paraPosition,paraListPtr -> actualLength);
return;
}//of it
//step 3. Move the remainig part.移动剩下的元素;
for (int i = paraListPtr -> actualLength;i > paraPosition; i--){
paraListPtr -> data[i] = paraListPtr -> data[i-1];
}//of it i
//Step 4. Insert.插入;
paraListPtr -> data[paraPosition] = paraValue;
//Updata the length.更新以前的数据;
paraListPtr -> actualLength ++;
}//of sequentialListInsert
/**
*Test the insert function.测试插入功能;
*/
void sequentialInsertTest(){
int i;
int tempArray[] = {3,5,2,7,4};
printf("--- sequentialIsertTest 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 position3.插入到第三个位置;
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 suquentialInsertTest
/**
*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 positian, e.g.,0 stands for inserting at the first position.
*@return The deleted value.
**/ //将数据插入到顺序表中;
int sequentialListDelete(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("Cannnot 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 fot 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");
//初始化;
SequentialListPtr tempList = sequentialListInit(tempArray,5);
printf("After initialization,the list is : ");
outputList(tempList);
//删除顺序表的第一位置;
printf("Now delete the first,the list is : ");
sequentialListDelete(tempList,0);
outputList(tempList);
//删除顺序表的最后位置;
printf("Now delete the last, the list is : ");
sequentialListDelete(tempList,3);
outputList(tempList);
//删除顺序表的第二个置:
printf("Now delete the second, the list is : ");
sequentialListDelete(tempList,1);
outputList(tempList);
//删除顺序表的第六个置:
printf("Now delete the 5th, the list is : ");
sequentialListDelete(tempList,5);
outputList(tempList);
//.删除顺序表的最后位置后;
printf("Now delete the (-6)th, the list is : ");
sequentialListDelete(tempList,-6);
outputList(tempList);
printf("--- sequentialDeleteTest ends. ---\r\n");
outputMemory(tempList);
} //测试删除功能是否正确;
/**
*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 locateElmenet(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 paraPoisition 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 delete element : the position %d is beyond the list 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.
**/ //程序主函数
int main(){
sequentialInsertTest();
sequentialDeleteTest();
}//of main
10.运行结果: