#include<stdio.h>
#include<malloc.h>
#define List_Max_Length 10
typedef struct SequentialList{
int actualLength;
int data[List_Max_Length];
}*SequentialListPtr;
/***
output
***/
void outputList(SequentialListPtr paraList){
int i;
for(i=0;i<paraList->actualLength;i++){
printf("%d ",paraList->data[i]);
}
printf("\n");
}
/***
output memory
***/
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",*paraListPtr->data);
printf("The address of the actual data:%ld\r\n0",¶ListPtr->data[0]);
printf("The address of the second data:%ld\r\n",¶ListPtr->data[1]);
}
SequentialListPtr SequentialListInit(int paraData[],int paraLength){
SequentialListPtr resultPtr=(SequentialListPtr)malloc(sizeof(struct SequentialList));
int i;
for(i=0;i<paraLength;i++){
resultPtr->data[i]=paraData[i];
}
resultPtr->actualLength=paraLength;
return resultPtr;
}
/***
insert an element
***/
void SequentialListInsert(SequentialListPtr paraListPtr,int paraPosition,int paraValue){
//step 1
if(paraListPtr->actualLength>=List_Max_Length){
printf("Cannot insert element:list full.\r\n");
return;
}
//step 2
if(paraPosition<0){
printf("Cannot insert elemnt:negative position unsupported.");
return;
}
if(paraPosition>paraListPtr->actualLength){
printf("cannot insert element :the position %d is bigger than the list length %d.\r\n",
paraPosition,paraListPtr->actualLength);
return;
}
//step 3
int i=0;
for(i=paraListPtr->actualLength;i>paraPosition;i--){
paraListPtr->data[i]=paraListPtr->data[i-1];
}
//step 4
paraListPtr->data[paraPosition]=paraValue;
//step 5
paraListPtr->actualLength ++;
}
/***
test insert function
***/
void SequentialInsertTest(){
int i;
int tempArray[5]={3,5,2,7,4};
printf("------sequentialInsertTest begins--------\n");
//initialize
SequentialListPtr tempList=SequentialListInit(tempArray,5);
printf("After initialization,the list is: ");
outputList(tempList);
//insert
printf("Now insert to the first,the list is: ");
SequentialListInsert(tempList,0,8);
outputList(tempList);
//insert to the list
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);
}
printf("-------------sequentialInsertTest ends------------");
}
/***
delet an element
***/
int SequentialListDelete(SequentialListPtr paraListPtr,int paraPosition){
//step 1
if(paraPosition<0){
printf("Invalid position:%d\r\n",paraPosition);
return -1;
}
if(paraPosition>=paraListPtr->actualLength){
printf("cannot delete element: the position %d is beyond the list length %d.\r\n", paraPosition, paraListPtr->actualLength);
return -1;
}
//step 2
int resultValue = paraListPtr->data[paraPosition];
int i=0;
for (i = paraPosition; i < paraListPtr->actualLength; i ++) {
paraListPtr->data[i] = paraListPtr->data[i + 1];
}
//step 3
paraListPtr->actualLength --;
//step 4
return resultValue;
}
/***
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 first
printf("Now delete the first, the list is: ");
SequentialListDelete(tempList, 0);
outputList(tempList);
//delete the last
printf("Now delete the last, the list is: ");
SequentialListDelete(tempList, 3);
outputList(tempList);
//delete the second.
printf("Now delete the second, the list is: ");
SequentialListDelete(tempList, 1);
outputList(tempList);
//delete the fifth.
printf("Now delete the 5th, the list is: ");
SequentialListDelete(tempList, 5);
outputList(tempList);
//delete the negative sixth.
printf("Now delete the (-6)th, the list is: ");
SequentialListDelete(tempList, -6);
outputList(tempList);
printf("-------sequentialDeleteTest ends-------\r\n");
outputMemory(tempList);
}
/***
locate :whether the element exist in the list
***/
int locateElement(SequentialListPtr paraListPtr, int paraValue) {
int i=0;
for (i = 0; i < paraListPtr->actualLength; i ++) {
if (paraListPtr->data[i] == paraValue) {
return i;
}
}
return -1;
}
/***
get an element in the list
***/
int getElement(SequentialListPtr paraListPtr,int paraPosition){
//step 1
if(paraPosition<0){
printf("Invalid position:%d\r\n",paraPosition);
return -1;
}
if(paraPosition>=paraListPtr->actualLength){
printf("Cannot get element: the position %d is beyond the list length %d.\r\n", paraPosition, paraListPtr->actualLength);
return -1;
}
return paraListPtr->data[paraPosition];
}
/***
clear the list
***/
void clearList(SequentialListPtr paraListPtr) {
paraListPtr->actualLength = 0;
}
int main() {
SequentialInsertTest();
SequentialDeleteTest();
return 0;
}
结果
心得体会:
大致知道顺序表插入、删除、定位、清理的过程与原理
顺序表中元素与元素之间没有空的位置
实际长度小于等于最大长度。
对函数的理解比之前清晰,体会到函数的好处,改BUG的时候比较方便,主函数十分简洁。
改bug用了好久!!