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

顺序表(c语言)

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

顺序表(c语言)

老师的代码如下:

#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->actualLength; 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] = paraData[i];
	}// Of for i
	resultPtr->actualLength = paraLength;

	return resultPtr;
}//Of sequentialListInit


void sequentialListInsert(SequentialListPtr 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; 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


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

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

	// 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. rn");
	sequentialListInsert(tempList, 8, 9);
    printf("The list is:");
	outputList(tempList);

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

    printf("---- sequentialInsertTest 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 delete 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 List {
    int Length;
    int data[LIST_MAX_LENGTH];  
} *ListPtr;

//输出顺序表数据
void OutputList(ListPtr L) {
	int i=0;
    for(i = 0 ; i < L->Length; i ++) 
	{
        printf("%d ", L->data[i]);
    }
    if(i==0)
    {
    	printf("the list is empty!");
	}
    printf("n");
}

//打印地址 
void OutputMemory(ListPtr L) {
    printf("The address of the structure: %ldrn", L);
    printf("The address of actualLength: %ldrn", &L->Length);
    printf("The address of data: %ldrn", &L->data);
    printf("The address of actual data: %ldrn", &L->data[0]);
    printf("The address of second data: %ldrn", &L->data[1]);
}

//初始化
ListPtr ListInit(int paraData[], int paraLength) {        //将数组赋值给线性表
	ListPtr resultPtr = (ListPtr)malloc(sizeof(struct List));
	int i;
	for (i = 0; i < paraLength; i ++) {
		resultPtr->data[i] = paraData[i];
	}
	resultPtr->Length = paraLength;
	printf("n");

	return resultPtr;
}

//在顺序表中插入元素
void ListInsert(ListPtr L, int paraPosition, int paraValue) {
    //判断空间是否已满 
    if (L->Length >= LIST_MAX_LENGTH) 
	{
        printf("Cannot insert element: list is fulln");
        return;
    }
    
    // 判断插入位置是否合法 
    if (paraPosition < 0||paraPosition > L->Length) 
	{
        printf("Cannot insert element: the position %d is bigger than the list length %d.n", paraPosition, L->Length);
        return;
    }
    
    //移动数据 
    int i;
    for (i = L->Length; i > paraPosition; i --) 
	{
        L->data[i] = L->data[i - 1];
    }
    
    //插入 
    L->data[paraPosition] = paraValue;
    
    //更新长度 
   L->Length ++;
}

//测试插入功能
void InsertTest() {
	int i;
	int tempArray[5] = {5,0,3,7,6};

    printf("---- sequentialInsertTest begins. ----n");

	//创建线性表 
    ListPtr tempList = ListInit(tempArray, 5);
    printf("After initialization, the list is: ");
	OutputList(tempList);
	
	//在首位插入一个元素
    printf("Now insert to the first, the list is: ");
	ListInsert(tempList, 0, 9);
	OutputList(tempList);

	// 在末尾插入一个元素 
    printf("Now insert to the last, the list is: ");
	ListInsert(tempList, 6, 4);
	OutputList(tempList);

	//在超越尾部插入一个元素 
    printf("Now insert beyond the tail. rn");
	ListInsert(tempList, 9,8 );
    printf("The list is:");
	OutputList(tempList);

	// 连续插入元素 
	for (i = 0; i < 5; i ++) {
		printf("Inserting %dn", (i + 10));
		ListInsert(tempList, 0, (i + 10));
		OutputList(tempList);
	}

    printf("---- sequentialInsertTest ends. ----n");
    printf("n");
}

//从顺序表表中删除元素 
int ListDelete(ListPtr L, int paraPosition) {
    //判断位置是否合法 
    if (paraPosition < 0||paraPosition >= L->Length)
	{
        printf("Cannot delete element: the position %d is beyond the list length %d.rn", paraPosition, L->Length);
        return -1;
    }

    //移动数据
	int resultValue = L->data[paraPosition];
	int i;
    for (i = paraPosition; i < L->Length; i ++) 
	{
        L->data[i] = L->data[i + 1];
    }

    //更新长度 
    L->Length --;

	//返回的值 
	return resultValue;
}

//测试删除功能 
void DeleteTest() {
	int tempArray[5] = {5,0,3,7,6};

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

    ListPtr tempList = ListInit(tempArray, 5);
    printf("After initialization, the list is: ");
	OutputList(tempList);

	//删除第一个元素 
    printf("Now delete the first, the list is: ");
	ListDelete(tempList, 0);
	OutputList(tempList);

	//删除最后一个元素 
    printf("Now delete the last, the list is: ");
	ListDelete(tempList, 3);
	OutputList(tempList);

	//删除第四个元素 
    printf("Now delete the second, the list is: ");
	ListDelete(tempList, 3);
	OutputList(tempList);

	//删负 
    printf("Now delete the (-6)th, the list is: ");
	ListDelete(tempList, -6);
	OutputList(tempList);

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

	OutputMemory(tempList);
}

//在顺序表里找一个元素 
int locateElement(ListPtr L, int paraValue) {
	int i;
	for(i = 0; i < L->Length; i ++) 
	{
		if (L->data[i] == paraValue) 
			return i;
	}
	return -1;
}

//在顺序表中获取一个元素 
int getElement(ListPtr L, int paraPosition) {
    //判断位置是否合法 
    if (paraPosition < 0||paraPosition >= L->Length) 
	{
        printf("Cannot delete element: the position  is beyond the list.rn");
        return -1;
    }
    //返回值 
	return L->data[paraPosition];
}

//清除列表中的元素 
void clearList(ListPtr L) {
	L->Length = 0;
}
 
int main() {
	InsertTest();
	DeleteTest();
}
运行结果如下:
---- sequentialInsertTest begins. ----

After initialization, the list is: 5 0 3 7 6
Now insert to the first, the list is: 9 5 0 3 7 6
Now insert to the last, the list is: 9 5 0 3 7 6 4
Now insert beyond the tail.
Cannot insert element: the position 9 is bigger than the list length 7.
The list is:9 5 0 3 7 6 4
Inserting 10
10 9 5 0 3 7 6 4
Inserting 11
11 10 9 5 0 3 7 6 4
Inserting 12
12 11 10 9 5 0 3 7 6 4
Inserting 13
Cannot insert element: list is full
12 11 10 9 5 0 3 7 6 4
Inserting 14
Cannot insert element: list is full
12 11 10 9 5 0 3 7 6 4
---- sequentialInsertTest ends. ----

---- sequentialDeleteTest begins. ----

After initialization, the list is: 5 0 3 7 6
Now delete the first, the list is: 0 3 7 6
Now delete the last, the list is: 0 3 7
Now delete the second, the list is: Cannot delete element: the position 3 is beyond the list length 3.
0 3 7
Now delete the (-6)th, the list is: Cannot delete element: the position -6 is beyond the list length 3.
0 3 7
---- sequentialDeleteTest ends. ----
The address of the structure: 1643536
The address of actualLength: 1643536
The address of data: 1643540
The address of actual data: 1643540
The address of second data: 1643544

--------------------------------
Process exited after 0.04188 seconds with return value 37
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/836344.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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