数据结构之线性表(c++实现

#include<iostream>
using namespace std;

#define List_MAX_Length 10//宏定义 

typedef struct SequentialList
{
	int data[List_MAX_Length];//存储数据 
	int actualLength;//记录线性表长度 
}*SequentialListPtr;//定义线性表 

void printSequentialList(SequentialListPtr L)
{
	for(int i=0;i<L->actualLength;i++)//下标从零开始 
	{
		printf("%d ",L->data[i]); //打印元素 
	} 
	printf("\n");
}//打印线性表中的元素

void outputMemory(SequentialListPtr L) 
{
    printf("The address of the structure: %ld\r\n", L);
    printf("The address of actualLength: %ld\r\n", &L->actualLength);
    printf("The address of data: %ld\r\n", &L->data);
    printf("The address of actual data: %ld\r\n", &L->data[0]);
    printf("The address of second data: %ld\r\n", &L->data[1]);
}//打印地址

bool checkSequentialList(SequentialListPtr L)//判断线性表是否为空 
{
	return L->actualLength==0;
} 

SequentialListPtr SequentialListInit(int para[],int para_Length)
{
	SequentialListPtr L=(SequentialListPtr)malloc(sizeof(struct SequentialList));
	//分配空间
	 for(int i=0;i<para_Length;i++)
	 {
	 	L->data[i]=para[i];//赋值 
	 } 
	 L->actualLength=para_Length;//记录长度 
	 return L;
} 

void SequentialListInsert(SequentialListPtr L,int pos,int value)
{
	if(L->actualLength>=List_MAX_Length)
	{
		printf("Cannot insert element: list full.\r\n");//判断线性表是否装满 
		return ;
	}
	if(pos<0)
	{
		printf("Cannot insert element: negative position unsupported.\r\n");
		//判断插入位置是否合理 
		return ;
	} 
	if(pos>L->actualLength)
	{
		printf("Cannot insert element: the position %d is bigger than the list length %d.\r\n",pos,L->actualLength);
		//判断是否合理 
		return ;
	}
	for(int i=L->actualLength;i>pos;i--)
	{
		L->data[i]=L->data[i-1];
		//插入时pos之后的都向后移动 
	} 
	L->data[pos]=value;//pos位置重新赋值 
	L->actualLength++;//线性表长度增加 
}

void SequentialInsertTest() {
	int tempArray[5] = {3, 5, 2, 7, 4};//临时数组 
    printf("---- SequentialInsertTest begins. ----\n");
	//初始化 
    SequentialListPtr tempList=SequentialListInit(tempArray, 5);
    printf("After initialization, the list is: ");
	printSequentialList(tempList);
	// 在第一个位置插入新元素 
    printf("Now insert to the first, the list is: ");
	SequentialListInsert(tempList, 0, 8);
	printSequentialList(tempList);
	if(checkSequentialList(tempList))
	{
		printf("the list is  empty.\r\n");
		printf("\n");
	}
	else
	{
		printf("the list is not empty.\r\n");
		printf("\n");
	} 
	//在最后一个位置插入新元素 
    printf("Now insert to the last, the list is: ");
	SequentialListInsert(tempList, 6, 9);
	printSequentialList(tempList);
    if(checkSequentialList(tempList))
	{
		printf("the list is  empty.\r\n");
		printf("\n");
	}
	else
	{
		printf("the list is not empty.\r\n");
		printf("\n");
	} 
	//在超出线性表长度的位置插入新元素 
    printf("Now insert beyond the tail. \r\n");
	SequentialListInsert(tempList, 8, 9);
    printf("The list is:");   
	printSequentialList(tempList);
	if(checkSequentialList(tempList))
	{
		printf("the list is  empty.\r\n");
		printf("\n");
	}
	else
	{
		printf("the list is not empty.\r\n");
		printf("\n");
	} 
	//打印线性表 
	for (int i=0;i<5;i++) 
	{
		printf("Inserting %d.\r\n", (i + 10));
		SequentialListInsert(tempList, 0, (i + 10));
		printSequentialList(tempList);
	}//重复插入和输出 
    printf("---- SequentialInsertTest ends. ----\r\n");
    printf("\n");
}

int SequentialListDelete(SequentialListPtr L,int  pos)
{
	if(pos<0)
	{
		printf("Invalid position: %d.\r\n", pos);
        return -1;
	}
	if(pos>=L->actualLength)
	{
		 printf("Cannot delete element: the position %d is beyond the list length %d.\r\n", pos, L->actualLength);
        return -1;
	}
	int value=L->data[pos];
	for(int i=pos;i<L->actualLength;i++)
	{
		L->data[i]=L->data[i+1];
	}
	L->actualLength--;
	return value;
}

void SequentialDeleteTest() {
	int tempArray[5]={3, 5, 2, 7, 4};
    printf("---- sequentialDeleteTest begins. ----\r\n");
    printf("\n");
    //初始化 
    SequentialListPtr tempList=SequentialListInit(tempArray,5);
    printf("After initialization, the list is: ");
	printSequentialList(tempList);
	if(checkSequentialList(tempList))
	{
		printf("the list is  empty.\r\n");
		printf("\n");
	}
	else
	{
		printf("the list is not empty.\r\n");
		printf("\n");
	} 
	//删除第一个元素 
    printf("Now delete the first, the list is: ");
	SequentialListDelete(tempList, 0);
	printSequentialList(tempList);
	if(checkSequentialList(tempList))
	{
		printf("the list is  empty.\r\n");
		printf("\n");
	}
	else
	{
		printf("the list is not empty.\r\n");
		printf("\n");
	} 
	//删除最后一个元素 
    printf("Now delete the last, the list is: ");
	SequentialListDelete(tempList, 3);
    printSequentialList(tempList);
	//删除第二个 
	if(checkSequentialList(tempList))
	{
		printf("the list is  empty.\r\n");
		printf("\n");
	}
	else
	{
		printf("the list is not empty.\r\n");
		printf("\n");
	} 
	printf("the list is not empty.\r\n");
    printf("Now delete the second, the list is: ");
	SequentialListDelete(tempList, 1);
	printSequentialList(tempList);
	if(checkSequentialList(tempList))
	{
		printf("the list is  empty.\r\n");
		printf("\n");
	}
	else
	{
		printf("the list is not empty.\r\n");
		printf("\n");
	} 
	//删除第五个 
    printf("Now delete the 5th, the list is: ");
	SequentialListDelete(tempList, 5);
	printSequentialList(tempList);
	if(checkSequentialList(tempList))
	{
		printf("the list is  empty.\r\n");
		printf("\n");
	}
	else
	{
		printf("the list is not empty.\r\n");
		printf("\n");
	} 
    printf("Now delete the (-6)th, the list is: ");
	SequentialListDelete(tempList, -6);
	printSequentialList(tempList);
    if(checkSequentialList(tempList))
	{
		printf("the list is  empty.\r\n");
		printf("\n");
	}
	else
	{
		printf("the list is not empty.\r\n");
		printf("\n");
	}  
    printf("---- sequentialDeleteTest ends. ----\r\n");
    printf("\n");
	outputMemory(tempList);
}

int locatedElement(int pos,SequentialListPtr L)//查找线性表中pos元素的下标 
{
	for(int i=0;i<L->actualLength;i++)
	{
		if(L->data[i]==pos)//找到该元素直接返回其下标 
		{
			return i;
		}
	}
	return -1;//找不到返回-1 
} 

int  getElement(int pos,SequentialListPtr L)
{
	//先判断所取的位置是否合理 
	if(pos<0)
	{
		printf("Invalid position: %d.\r\n", pos);
        return -1;
	}
	if(pos>=L->actualLength)
	{
		printf("Cannot get element: the position %d is beyond the list length %d.\r\n", pos, L->actualLength);
		return -1;
	}
	//直接返回元素 
	return L->data[pos];
}

void clearSequentialList(SequentialListPtr L)//清空线性表 
{
	L->actualLength=0;
	//长度为零即为清除 
} 
int main()
{
	SequentialInsertTest();
	SequentialDeleteTest();
	return 0;
} 

运行结果如下图所示:

重要算法:

1.线性表的插入

2.线性表的删除

学习心得:

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值