栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【无标题】数据结构第一次作业-----线性表

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【无标题】数据结构第一次作业-----线性表

 这是老师的代码:

#include 
#include 


#define LIST_MAX_LENGTH 10


typedef struct SequentialList {
   int actualLength;

 
   int data[LIST_MAX_LENGTH];//The maximum length is fixed.
}*SequentialListptr;


void outputList(SequentialListPtr paraList) {
    for(int i = 0;i < paraList->actualLenfth; i ++){
        printf("%d",paraList->data[i]);
    }//of for i
    printf("rn");
}//of outputList


void outputMemory(SequentialListPtr paraListPtr) {
    printf("The address of the structure: %ldrn", paraListPtr);
    printf("The address of actualLength: %ldrn", ¶ListPtr->actualLength);
    printf(“The address of data: %ldrn", ¶ListPtr->data);
    printf("The address of actual data: %ldrn", ¶ListPtr->data[0]);
    printf("The address of second data: %ldrn", ¶ListPtr->data[1]);
}// of outputMemory


SequentialListPtr sequentialListInit(int paraData[], int paraLength) {
    SequentialListPtr resultPtr = (SequentialListPtr)malloc(sizeof(struct SequentialList));
    for (int i = 0; i < paraLength; i++){
    resultPtr-> data[i] = paraLength;
}// of for i 
    resultPtr->actualLength = paraLength;

    return resultPtr;
}// of sequentialListInit



void sequentialListIntsert(SequentailListPtr paraListPtr,int paraPosition,int paraValue){
    //step 1.space check.
    if {paraListPtr->actualLength >= LIST_MAX_LENGTH) {
        printf("Cannot insert element:  list full.rn);
        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.rn", paraPosition, paraListPtr->actualLength);
        return ;
    }//of if 

    //Step 3.Move the remaining part.
    for (int i = paraListPtr->actualLength->data[i - 1];
}//of for i

    //Step 4.Insert.
    paraListPtr->data[paraPosition] = paraValue;

    //Step 5. Updata the length.
    paraListPtr->actualLength ++;
}// of seuentialListInsert


void sequentialInserTest(){
    int i;
    int tempArray[5] = {3,5,2,7,4};

printf ("---- sequentailInsertTest begins.------rn");

    // Initialze.
    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:");
    sequentailListInsert(tempList, 0,8);
    outputList(tempList);

    //Insert to the last.
    printf("Now insert to the last,the list is:");
    sequentialListInser(tempList,6,9);
    outputList(tempList);

    //Insert beyond the tail.
    printf("Now insert beyond the tail. rn");
    sequentilListInser(tempList,8,9);
    printf("The list is:");
    outputList(tempList);

    //Insert to position 3.
    for (i=0;i<5;i++)
    {   printf("Inseerting %d.rn",(i + 10));
        sequrentialListInsert(tempList,0(i + 10));
        outputList(tempList);
    }//of for i

    printf("----sequrentailInsertTest ends.----rn");
}//of sequentialInsertTest


int sequentialListDelete(SequentialListPtr paraListPtr, int paraPosition) {
    // Step 1. Position check.
    if (paraPosition < 0) {
        printf("Invalid position: %d.rn", paraPosition);
        return -1;
    }//Of if

    if (paraPosition >= paraListPtr->actualLength) {
        printf("Cannot delete element: the position %d is beyond the list length %d.rn", 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


void sequentialDeleteTest() {
	int tempArray[5] = {3, 5, 2, 7, 4};

    printf("---- sequentialDeleteTest begins. ----rn");

	// 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: ");
	sequentialListDelete(tempList, 0);
	outputList(tempList);

	// Delete to 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 second.
    printf("Now delete the 5th, the list is: ");
	sequentialListDelete(tempList, 5);
	outputList(tempList);

	// Delete the second.
    printf("Now delete the (-6)th, the list is: ");
	sequentialListDelete(tempList, -6);
	outputList(tempList);

    printf("---- sequentialDeleteTest ends. ----rn");

	outputMemory(tempList);
}// Of sequentialDeleteTest


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


int getElement(SequentialListPtr paraListPtr, int paraPosition) {
    // Step 1. Position check.
    if (paraPosition < 0) {
        printf("Invalid position: %d.rn", paraPosition);
        return -1;
    }//Of if

    if (paraPosition >= paraListPtr->actualLength) {
        printf("Cannot get element: the position %d is beyond the list length %d.rn", paraPosition, paraListPtr->actualLength);
        return -1;
    }//Of if

	return paraListPtr->data[paraPosition];
}// Of locateElement


void clearList(SequentialListPtr paraListPtr) {
	paraListPtr->actualLength = 0;
}// Of clearList


void main() {
	sequentialInsertTest();
	sequentialDeleteTest();
}// Of main
























 我写的代码(临摹老师的)

#include 
#include 


#define LIST_MAX_LENGTH 10


typedef struct SequentialList {
   int actualLength;

 
   int data[LIST_MAX_LENGTH];//The maximum length is fixed.
}*SequentialListptr;


void outputList(SequentialListPtr paraList) {
    for(int i = 0;i < paraList->actualLenfth; i ++){
        printf("%d",paraList->data[i]);
    }//of for i
    printf("rn");
}//of outputList


void outputMemory(SequentialListPtr paraListPtr) {
    printf("The address of the structure: %ldrn", paraListPtr);
    printf("The address of actualLength: %ldrn", ¶ListPtr->actualLength);
    printf(“The address of data: %ldrn", ¶ListPtr->data);
    printf("The address of actual data: %ldrn", ¶ListPtr->data[0]);
    printf("The address of second data: %ldrn", ¶ListPtr->data[1]);
}// of outputMemory


SequentialListPtr sequentialListInit(int paraData[], int paraLength) {
    SequentialListPtr resultPtr = (SequentialListPtr)malloc(sizeof(struct SequentialList));
    for (int i = 0; i < paraLength; i++){
    resultPtr-> data[i] = paraLength;
}// of for i 
    resultPtr->actualLength = paraLength;

    return resultPtr;
}// of sequentialListInit



void sequentialListIntsert(SequentailListPtr paraListPtr,int paraPosition,int paraValue){
    //step 1.space check.
    if {paraListPtr->actualLength >= LIST_MAX_LENGTH) {
        printf("Cannot insert element:  list full.rn);
        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.rn", paraPosition, paraListPtr->actualLength);
        return ;
    }//of if 

    //Step 3.Move the remaining part.
    for (int i = paraListPtr->actualLength->data[i - 1];
}//of for i

    //Step 4.Insert.
    paraListPtr->data[paraPosition] = paraValue;

    //Step 5. Updata the length.
    paraListPtr->actualLength ++;
}// of seuentialListInsert


void sequentialInserTest(){
    int i;
    int tempArray[5] = {3,5,2,7,4};

printf ("---- sequentailInsertTest begins.------rn");

    // Initialze.
    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:");
    sequentailListInsert(tempList, 0,8);
    outputList(tempList);

    //Insert to the last.
    printf("Now insert to the last,the list is:");
    sequentialListInser(tempList,6,9);
    outputList(tempList);

    //Insert beyond the tail.
    printf("Now insert beyond the tail. rn");
    sequentilListInser(tempList,8,9);
    printf("The list is:");
    outputList(tempList);

    //Insert to position 3.
    for (i=0;i<5;i++)
    {   printf("Inseerting %d.rn",(i + 10));
        sequrentialListInsert(tempList,0(i + 10));
        outputList(tempList);
    }//of for i

    printf("----sequrentailInsertTest ends.----rn");
}//of sequentialInsertTest


int sequentialListDelete(SequentialListPtr paraListPtr, int paraPosition) {
    // Step 1. Position check.
    if (paraPosition < 0) {
        printf("Invalid position: %d.rn", paraPosition);
        return -1;
    }//Of if

    if (paraPosition >= paraListPtr->actualLength) {
        printf("Cannot delete element: the position %d is beyond the list length %d.rn", 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


void sequentialDeleteTest() {
	int tempArray[5] = {3, 5, 2, 7, 4};

    printf("---- sequentialDeleteTest begins. ----rn");

	// 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: ");
	sequentialListDelete(tempList, 0);
	outputList(tempList);

	// Delete to 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 second.
    printf("Now delete the 5th, the list is: ");
	sequentialListDelete(tempList, 5);
	outputList(tempList);

	// Delete the second.
    printf("Now delete the (-6)th, the list is: ");
	sequentialListDelete(tempList, -6);
	outputList(tempList);

    printf("---- sequentialDeleteTest ends. ----rn");

	outputMemory(tempList);
}// Of sequentialDeleteTest


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


int getElement(SequentialListPtr paraListPtr, int paraPosition) {
    // Step 1. Position check.
    if (paraPosition < 0) {
        printf("Invalid position: %d.rn", paraPosition);
        return -1;
    }//Of if

    if (paraPosition >= paraListPtr->actualLength) {
        printf("Cannot get element: the position %d is beyond the list length %d.rn", paraPosition, paraListPtr->actualLength);
        return -1;
    }//Of if

	return paraListPtr->data[paraPosition];
}// Of locateElement


void clearList(SequentialListPtr paraListPtr) {
	paraListPtr->actualLength = 0;
}// Of clearList


void main() {
	sequentialInsertTest();
	sequentialDeleteTest();
}// Of main

 因为太菜了只能用老师的代码思路,希望以后能自己独立思考出来整个代码。加油

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/847834.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号