作业:01数据结构顺序表

提示:大佬请绕路,该博客仅为完成作业而写,而且顺序表说简单点就是数组,所以也没啥难度,
就这么写写,就当复习c语言了。


一、c语言一些语法的回顾

提示:以下仅为我个人的一些关于c语言的回顾,毕竟一年没碰C语言了,所以大佬请绕路。

1、开始编写程序的准备

首先,在编写和数据结构相关的程序之前,一定要先写这两行代码。
#include <stdio.h>
#include <malloc.h>
其次,每写完一行代码一定要加分号,最近写python写多了,再让我写其他编程语言就真的很容易
忘加分号.

2、关于指针

创建指针:【数据类型】【*】【变量名】
在变量声明的时候,如果没有确切的地址可以赋值,
为指针变量赋一个 NULL 值是一个良好的编程习惯,
赋为 NULL 值的指针被称为空指针,
NULL 指针是一个定义在标准库中的值为零的常量。
type *var_name = NULL;
使用指针:
符号 & 可以将对应变量的地址取出复制给指针;
符号 * 出了在创建指针使用外,还可以将指针存储地址的值取出;
int  var = 20;   /* 实际变量的声明 */
int  *ip;        /* 指针变量的声明 */
ip = &var;  /* 在指针变量中存储 var 的地址 */
printf("ip指针储存的地址: %p\n", ip  );
printf("ip指针存储地址所对应的值: %d\n", *ip  );

3、关于结构体

定义结构体:
name和aliasl-ist是定义结构体的名称,在声明结构体的时候,二者必须出现其一;
member为结构体的成员;
使用typedef后在创建结构体时可以少写“struct ”;
alia之前若是有*号,则使用alia声明变量时就直接是指针类型
切记,定义玩结构体之后一定要写分号;
typedef struct name{ 
    member
    member
    member  
    ...
} alias-list;
获取结构体的成员变量:
如果是正常声明结构体,使用【结构体变量】.【成员】
如果是指针指向结构体,则使用【指针】->【成员变量】或者【(*指针)】.【成员】
(*pointer).memberName
pointer->memberName

二、代码运行结果和分析

1、老师初始代码运行结果展示

在这里插入图片描述

2、我的顺序表的操作分析

提醒:函数没有顺序表本身的参数传递,是因为我将顺序表定义为了全局变量

1、添加操作:
由于无循环操作,所以时间复杂度T(n) = O(1)
//往顺序表中添加数据 
//这里添加操作默认往表尾添加数据
//后续有时间也许会慢慢增加一些骚操作 
void add(int value)
{
//	先判断顺序表是否已满
	if(sequentialList->actualLength==LIST_MAX_LENGTH)
	{
		printf("表已满,无法继续添加数据!\n");
		return; 
	}
//	指定表尾索引 
	int index = sequentialList->actualLength;
	sequentialList->data[index] = value; 
	sequentialList->actualLength++;
	printf("已成功将%d添加到顺序表的尾部\n",value);
	
	printf("现在顺序表的信息为:\n");
	traversal_sequentialList();
}

结果展示:
在这里插入图片描述


2、删除操作:
有循环,且时间由顺序表长度和指定索引共同决定,所以时间复杂度为T(n) = O(n)
//这里根据索引删除相应的值
//后续也会慢慢加些骚操作 
void delete_by_index(int index)
{
//	先判断顺序表是否为空,为空则无法删除 
	if(sequentialList->actualLength==0)
	{
		printf("表已空,无法继续删除!\n");
		return; 
	}
//	在判断索引是否有效 
	if(index<0||index>=sequentialList->actualLength) 
	{
		printf("索引越界!\n");
		return; 
	}
//	将索引之后数据往前覆盖及完成删除操作
	for(int i = index-1;i < sequentialList->actualLength-1; i++) 
	{
		sequentialList->data[i] = sequentialList->data[i+1];
	}
	sequentialList->actualLength -- ;
	printf("已将索引为%d的数据删除!\n",index);
	
	printf("现在顺序表的信息为:\n");
	traversal_sequentialList();
	 
}

结果展示:
在这里插入图片描述


3、修改操作:
由于无循环操作,所以时间复杂度T(n) = O(1)
//将指定索引的值修改 
void modify_by_index(int index,int value)
{
	//	先判断顺序表是否为空,为空则无法修改 
	if(sequentialList->actualLength==0)
	{
		printf("表已空,无法继续修改!\n");
		return; 
	}
//	在判断索引是否有效 
	if(index<0||index>=sequentialList->actualLength) 
	{
		printf("索引越界!\n");
		return; 
	}
	sequentialList->data[index-1] = value;
	printf("修改完成!\n");
	
	printf("现在顺序表的信息为:\n");
	traversal_sequentialList();
}

结果展示:
在这里插入图片描述


4、遍历操作:
有循环,且时间由顺序表长度决定,所以时间复杂度为T(n) = O(n)

void traversal_sequentialList()
{
//	打印之前先判断是否为空 
	if(sequentialList->actualLength==0)
	{
		printf("顺序表已经为空!\n");
		return;
	}
//	开始打印顺序表 
	for(int i = 0;i < sequentialList->actualLength; i++)
	{
		printf("顺序表的第%d个元素为%d。\n",(i+1),sequentialList->data[i]); 
	}
	printf("打印完毕!\n"); 
}

在这里插入图片描述


5、清空操作:
由于无循环操作,所以时间复杂度T(n) = O(1)
//清空顺序表所有数据
//不过就是让表长度变为0而已
//顺序表里的值仍然保存的有 
void clear_all()
{
	sequentialList->actualLength = 0;
	printf("顺序表清空完毕!\n");	
} 

结果展示:
在这里插入图片描述

三、代码展示

1、我的代码

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

//指定顺序表的最大长度为10 
#define LIST_MAX_LENGTH 10
//定义顺序表数据结构
typedef struct SequentialList
{
//	存储顺序表真实长度 
	int actualLength;
//	直接定义顺序表的大小 
	int data[LIST_MAX_LENGTH];
//	*加别名,用别名申明变量时,该变量就是结构体指针 
}*SequentialListPtr; 

//初始化顺序表 
SequentialListPtr sequentialListInit()
{
//	为顺序表分配内存空间 
	SequentialListPtr resultPtr = (SequentialList *)malloc(sizeof(SequentialList));
//	创建完之后默认为顺序插入三个数,1,2,3 
	for(int i = 0;i<=3;i++)
	{
		resultPtr->data[i] = (i+1);
	} 
//	顺序表实际长度指定为3 
	resultPtr->actualLength = 3;
//	返回顺序表的地址 
	return resultPtr;
} 

//直接把顺序表定义为全局变量并初始化,
//虽然不是很符合规范,但是考试的时候谁用谁知道
SequentialListPtr  sequentialList = sequentialListInit(); 

void traversal_sequentialList()
{
//	打印之前先判断是否为空 
	if(sequentialList->actualLength==0)
	{
		printf("顺序表已经为空!\n");
		return;
	}
//	开始打印顺序表 
	for(int i = 0;i < sequentialList->actualLength; i++)
	{
		printf("顺序表的第%d个元素为%d。\n",(i+1),sequentialList->data[i]); 
	}
	printf("打印完毕!\n"); 
}
//往顺序表中添加数据 
//这里添加操作默认往表尾添加数据
//后续有时间也许会慢慢增加一些骚操作 
void add(int value)
{
//	先判断顺序表是否已满
	if(sequentialList->actualLength==LIST_MAX_LENGTH)
	{
		printf("表已满,无法继续添加数据!\n");
		return; 
	}
//	指定表尾索引 
	int index = sequentialList->actualLength;
	sequentialList->data[index] = value; 
	sequentialList->actualLength++;
	printf("已成功将%d添加到顺序表的尾部\n",value);
	
	printf("现在顺序表的信息为:\n");
	traversal_sequentialList();
}
//这里根据索引删除相应的值
//后续也会慢慢加些骚操作 
void delete_by_index(int index)
{
//	先判断顺序表是否为空,为空则无法删除 
	if(sequentialList->actualLength==0)
	{
		printf("表已空,无法继续删除!\n");
		return; 
	}
//	在判断索引是否有效 
	if(index<0||index>=sequentialList->actualLength) 
	{
		printf("索引越界!\n");
		return; 
	}
//	将索引之后数据往前覆盖及完成删除操作
	for(int i = index-1;i < sequentialList->actualLength-1; i++) 
	{
		sequentialList->data[i] = sequentialList->data[i+1];
	}
	sequentialList->actualLength -- ;
	printf("已将索引为%d的数据删除!\n",index);
	
	printf("现在顺序表的信息为:\n");
	traversal_sequentialList();
	 
}
//将指定索引的值修改 
void modify_by_index(int index,int value)
{
	//	先判断顺序表是否为空,为空则无法修改 
	if(sequentialList->actualLength==0)
	{
		printf("表已空,无法继续修改!\n");
		return; 
	}
//	在判断索引是否有效 
	if(index<0||index>=sequentialList->actualLength) 
	{
		printf("索引越界!\n");
		return; 
	}
	sequentialList->data[index-1] = value;
	printf("修改完成!\n");
	
	printf("现在顺序表的信息为:\n");
	traversal_sequentialList();
}
//清空顺序表所有数据
//不过就是让表长度变为0而已
//顺序表里的值仍然保存的有 
void clear_all()
{
	sequentialList->actualLength = 0;
	printf("顺序表清空完毕!\n");	
} 

int main(void)
{
//	就简单实现一下后端的基础操作:增删改查
//	开始慢慢找回当年写c语言的感觉 
	printf("开始tanshaoqi的顺序表测试\n");
	int select = 0;
	while(1)
	{
		printf("1:添加数据。\n"); 
		printf("2:删除数据。\n");
		printf("3:修改数据。\n");
		printf("4:查看数据。\n");
		printf("5:清空数据。\n");
		printf("0:退出程序。\n");
		printf("请输入选项号来开始你的操作:\n");
		scanf("%d",&select);
		switch(select)
		{
			case 1:
				{
					int t = 0;
					printf("请输入你要插入的值:\n");
					scanf("%d",&t);
					add(t);	
					break;
				}
				
			case 2:
				{
					int index = 0;
					printf("请输入你要删除的下标:\n");
					scanf("%d",&index);
					delete_by_index(index);	
					break;
				}
				
			case 3:
				{
					int index = 0;
					int value = 0; 
					printf("请输入你要修改的下标:\n");
					scanf("%d",&index);
					printf("请输入你要改变后的值:\n");
					scanf("%d",&value);
					modify_by_index(index,value);	
					break;
				}
				
			case 4:
				{
					printf("现在顺序表的信息为:\n");
					traversal_sequentialList();
					break;
				}
			case 5:
				 clear_all();
				 break;
			case 0:
				exit(1); 
				break; 
		} 
	}
} 

3、老师的代码

#include <stdio.h>
#include <malloc.h>
//定义列表的最大长度为10 
#define LIST_MAX_LENGTH 10

/**
 * Linear list of integers. The key is data.
 */
 
// 定义了顺序列表的结构体 
typedef struct SequentialList {
    int actualLength;

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

/**
 * 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 memeory 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", &paraListPtr->actualLength);
    printf("The address of data: %ld\r\n", &paraListPtr->data);
    printf("The address of actual data: %ld\r\n", &paraListPtr->data[0]);
    printf("The address of second data: %ld\r\n", &paraListPtr->data[1]);
}// Of outputMemory

/**
 * Initialize a sequential list. No error checking for this function.
 * @param paraListPtr The pointer to the list. It must be a pointer to change the list.
 * @param paraValues An int array storing all elements.
 */
 
// 创建一个顺序表结构体 
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 pointer to the list. It must be a pointer to change the list.
 * @param paraPosition The position, e.g., 0 stands for inserting at 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 > paraListPtr->actualLength) {
        printf("Cannot insert element: the position %d is bigger than the list length %d.\r\n", 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;

    // Update the length.
//    将顺序表的长度加1 
    paraListPtr->actualLength ++;
}// Of sequentialListInsert

/**
 * Test the insert function.
 */
 
void sequentialInsertTest() {
	int i;
	int tempArray[5] = {3, 5, 2, 7, 4};

    printf("---- sequentialInsertTest 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 position 3.
   // 依次往顺序表表头插入5个数据
   
	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 sequentialInsertTest

/**
 * 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 position, e.g., 0 stands for inserting at the first position.
 * @return The deleted value.
 */
int sequentialListDelete(SequentialListPtr paraListPtr, int paraPosition) {
    // Step 1. Position check.
   //  检查要删除的索引是否有效
   // 检查索引是否小于0
    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

    // 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

/**
 * 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 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. ----\r\n");

	outputMemory(tempList);
}// Of sequentialDeleteTest

/**
 * 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 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

/**
 * Get an element in the list.
 * @param paraListPtr The pointer to the list.
 * @param paraPosition 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
 */
// 清空索引
int clearList(SequentialListPtr paraListPtr) {
	paraListPtr->actualLength = 0;
}// Of clearList

/**
 The entrance.
 */




// 我自己完成localElement函数
int myLocateElement(SequentialList paraListPtr,int paraValue)
{
    for (int i = 0; i < paraListPtr->actualLength; i++)
    {
        /* code */
        if (paraListPtr->data[i]==paraValue)
        {
            /* code */
            return i;
        }
    }
    return -1  
}
int main(int argc, char const *argv[])
{
    /* code */
    sequentialInsertTest();
	sequentialDeleteTest();
    return 0;
}

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值