“c
this is code
”
#include<stdio.h>
#include<malloc.h>
#define LIST_MAX_LENGTH 10
typedef struct sequentiallist
{
int actuallength;
int data[LIST_MAX_LENGTH];
}*sequentiallistptr;
void outputlist(sequentiallistptr paralist)
{
for (int i = 0; i < paralist->actuallength; i++)
{
printf("%d", paralist->data[i]);
}
printf("\r\n");
}
void outputmemory(sequentiallistptr paralist)
{
printf("The address of the structure: %lld\r\n", paralist);
printf("The address of the actuallength: %lld\r\n", ¶list->actuallength);
printf("The address of the data: %lld\r\n", ¶list->data);
printf("The address of the actul data: %lld\r\n", ¶list->data[0]);
printf("The address of the second data: %lld\r\n", ¶list->data[1]);
}
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];
}
resultPtr->actuallength = paralength;
return resultPtr;
}
void sequentiallistinsert(sequentiallistptr paralistptr, int paraPosition, int paraValue)
{
if (paralistptr->actuallength >= LIST_MAX_LENGTH) {
printf("Cannot insert element: list full.\r\n");
return;
}
if (paraPosition < 0) {
printf("Cannot insert element: 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;
}
for (int i = paralistptr->actuallength; i > paraPosition; i--) {
paralistptr->data[i] = paralistptr->data[i - 1];
}
paralistptr->data[paraPosition] = paraValue;
paralistptr->actuallength++;
}
void sequentialInsertTest() {
int i;
int tempArray[5] = { 3, 5, 2, 7, 4 };
printf("---- sequentialInsertTest begins. ----\r\n");
sequentiallistptr tempList = sequentiallistInit(tempArray, 5);
printf("After initialization, the list is: ");
outputlist(tempList);
printf("Now insert to the first, the list is: ");
sequentiallistinsert(tempList, 0, 8);
outputlist(tempList);
printf("Now insert to the last, the list is: ");
sequentiallistinsert(tempList, 6, 9);
outputlist(tempList);
printf("Now insert beyond the tail. \r\n");
sequentiallistinsert(tempList, 8, 9);
printf("The list is:");
outputlist(tempList);
for (i = 0; i < 5; i++) {
printf("Inserting %d.\r\n", (i + 10));
sequentiallistinsert(tempList, 0, (i + 10));
outputlist(tempList);
}
printf("---- sequentialInsertTest ends. ----\r\n");
}
int sequentialListDelete(sequentiallistptr paraListPtr, int paraPosition) {
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;
}
int resultValue = paraListPtr->data[paraPosition];
for (int i = paraPosition; i < paraListPtr->actuallength; i++) {
paraListPtr->data[i] = paraListPtr->data[i + 1];
}
paraListPtr->actuallength--;
return resultValue;
}
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);
}
int locateElement(sequentiallistptr paraListPtr, int paraValue) {
for (int i = 0; i < paraListPtr->actuallength; i++) {
if (paraListPtr->data[i] == paraValue) {
return i;
}
}
return -1;
}
int getElement(sequentiallistptr paraListPtr, int paraPosition) {
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];
}
void clearList(sequentiallistptr paraListPtr) {
paraListPtr->actuallength = 0;
}
void main() {
sequentialInsertTest();
sequentialDeleteTest();
}
1.前面的if都用了return,后就不用else
2.插入时,边界是length,删除时边界是length-1;
3.插入时,从尾部开始向后移一位,直到指定位置;删除时,从指定位置开始,其下一位向前移动。
4.地址用%p来写可能更好一点,VS里%ld报错了。
5。malloc时可能出现申请失败,可以加在malloc下面加
if(reultPtr == NULL)
{
printf("动态内存分配失败\n");
exit(-1);
}
6.插入
7.删除