线性表的基本操作步骤及其实现

以下是一个对线性表的初始化、取值、查找、插入和删除的实现过程,接下来我们主要讨论这5个主要操作的实现。现在我们来单独看一下每一个操作是怎么实现的。

(1)初始化

顺序表的初始化操作就是构造出一个空的顺序表,首先我们对顺序表进行初始化,代码如下:

typedef struct SequentialList
 {
 	int actualLength;//定义最初始的实际长度 
 	int data[LIST_MAX_LENGTH];//最大长度 
 }*SequentialListPtr;

(2)取值

接下来就是取值,我们需要输出顺序表的顺序,可以通过设计一个函数来进行判断,是否可以装下顺序表的实际长度。代码:

void outputList(SequentialListPtr paraList)//设计一个函数,输出所有包含的顺序表,并进行判断是否可以装下整个线性表的实际长度 
 {
 	for(int i=0;i<paraList->actualLength;i++)
 	{
 		printf("%d",paraList->data[i]);
	 }//of for i
	 printf("\r\n");
  }

此外,我们还需要把每一块的地址给打印出来:

void outputmemory(SequentialListPtr paraListPtr)
  
  {
  	printf("the address of the structure: %ld\r\n",paraListPtr);
  	printf("the address of the actualLength: %ld\r\n",&paraListPtr->actualLength);
  	printf("the address of the data: %ld\r\n",&paraListPtr->data);
  	printf("the address of the actual data: %ld\r\n",&paraListPtr->data[0]);
  	printf("the address of the second data: %d\r\n",&paraListPtr->data[1]);
   }//of outputmemory

我们将地址存放到data数组中,以此查找:

	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

(3)插入

我们接下来就要设计插入的函数了,将原先已经存放好的顺序表进行插入:

	void sequentialListInsert(SequentialListPtr paraListPtr,int paraPosition,int paraValue)//对顺序表的长度进行判断,如果不能则无法插入 
	{
		//step 1.对存储空间进行检查 
		if(paraListPtr->actualLength>=LIST_MAX_LENGTH)
		{
			printf("Cannot insert element: list full.\r\n");
			return;
		}//of if
		
		//step 2.对存储位置进行检查 
		if(paraPosition<0)
		{
			printf("cannot insert element: nagative position unsupported.");
		}
		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.移动剩余的部分 
		for(int i=paraListPtr->actualLength;i>paraPosition;i--)
		{
			paraListPtr->data[i] = paraListPtr->data[i-1];
		}//of for i
		
		//step 4.开始插入 
		paraListPtr->data[paraPosition]= paraValue;
		
		//更新顺序表的长度 
		paraListPtr->actualLength ++;
		
		
		 }//of sequentialListInsert

插入之后我们还要设计一个测试插入的函数来验证:

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);
		  	
		  	//插入位置0-4,程序位置10-14
		  	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	
			   

(4)删除

我们需要设计一个删除测试的函数以及测试删除的函数:

int sequentialListDelete(SequentialListPtr paraListPtr,int paraPosition)//设计一个删除测试的函数
                {
                    //step 1.存储位置的检查.
                    if(paraPosition<0)//如果位置无效则需说明无效位置
                    {
                        printf("Invalid position: %d.\r\n",paraPosition);
                        return -1;
                        
                    }//of if
                    
                    //step 2.移动剩余的部分.
                    int resultValue = paraListPtr->data[paraPosition];
                    for(int i = paraPosition; i<paraListPtr->actualLength;i++);
                    {
                            int i=5;
                        paraListPtr->data[i] = paraListPtr->data[i+1];
                    }//of for i
                
                    //step 3.更新顺序表的长度.
                    paraListPtr->actualLength --;
                    
                    //step 4.返回结果.
                    return resultValue;
                     }//of sequatialListDelete
                    
                    
                     /**
                      * 测试删除功能.
                      */
                      void sequentialDeleteTest()
                      {
                          int tempArray[5] = {3,5,2,7,4};
                          printf("After initialization, the list is:");
                          
                          //初始化.
                          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);
                           }//of sequentialDeleteTest

最后我们将函数放到主函数里面便可以正常的实现各个功能了。

int main()//最后放到主函数里面编译并运行 
	                    {
	                    	sequentialInsertTest();
	                    	sequentialDeleteTest();
						}//of main

对于GetElement(L,i)函数,即返回线性表L中下标为i的元素:

int getElement(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;
    }

	return paraListPtr->data[paraPosition];
}

同理,LocateElement(L,e),在线性表L里查找指定元素e,若查找成功,返回e在L中的坐标,若查找不成功,则返回-1

int locateElement(SequentialListPtr paraListPtr, int paraValue) {
	for (int i = 0; i < paraListPtr->actualLength; i ++) {
		if (paraListPtr->data[i] == paraValue) {
			return i;
		}
	}

清零函数:

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

以下是总代码:

#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]);
	 }//of for i
	 printf("\r\n");
  }
  
 /**
  *为输出的线性表请求出一块地址 
  */
  void outputmemory(SequentialListPtr paraListPtr)
  
  {
  	printf("the address of the structure: %ld\r\n",paraListPtr);
  	printf("the address of the actualLength: %ld\r\n",&paraListPtr->actualLength);
  	printf("the address of the data: %ld\r\n",&paraListPtr->data);
  	printf("the address of the actual data: %ld\r\n",&paraListPtr->data[0]);
  	printf("the address of the second data: %d\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 para Values the value to be inserted.
		 */
	void sequentialListInsert(SequentialListPtr paraListPtr,int paraPosition,int paraValue)//对顺序表的长度进行判断,如果不能则无法插入 
	{
		//step 1.对存储空间进行检查 
		if(paraListPtr->actualLength>=LIST_MAX_LENGTH)
		{
			printf("Cannot insert element: list full.\r\n");
			return;
		}//of if
		
		//step 2.对存储位置进行检查 
		if(paraPosition<0)
		{
			printf("cannot insert element: nagative position unsupported.");
		}
		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.移动剩余的部分 
		for(int i=paraListPtr->actualLength;i>paraPosition;i--)
		{
			paraListPtr->data[i] = paraListPtr->data[i-1];
		}//of for i
		
		//step 4.开始插入 
		paraListPtr->data[paraPosition]= paraValue;
		
		//更新顺序表的长度 
		paraListPtr->actualLength ++;
		
		
		 }//of sequentialListInsert
		 
		 /**
		  * 设计一个测试插入的函数 
		  */
		  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);
		  	
		  	//插入位置3.
		  	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 delete value.
				*/
				int sequentialListDelete(SequentialListPtr paraListPtr,int paraPosition)//设计一个删除测试的函数 
				{
					//step 1.存储位置的检查.
					if(paraPosition<0)//如果位置无效则需说明无效位置 
					{
						printf("Invalid position: %d.\r\n",paraPosition);
						return -1;
						
					}//of if
					
					//step 2.移动剩余的部分.
					int resultValue = paraListPtr->data[paraPosition];
					for(int i = paraPosition; i<paraListPtr->actualLength;i++);
					{
							int i=5;
						paraListPtr->data[i] = paraListPtr->data[i+1];
					}//of for i
				
					//step 3.更新顺序表的长度.
					paraListPtr->actualLength --;
					
					//step 4.返回结果.
					return resultValue;
					 }//of sequatialListDelete
					 
					 
					 /**
					  * 测试删除功能.
					  */
					  void sequentialDeleteTest()
					  {
					  	int tempArray[5] = {3,5,2,7,4};
					  	printf("After initialization, the list is:");
					  	
					  	//初始化.
					  	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);
						   }//of sequentialDeleteTest	 
	                    int main()//最后放到主函数里面编译并运行 
	                    {
	                    	sequentialInsertTest();
	                    	sequentialDeleteTest();
						}//of main

运行结果如下:


版权声明:本文为Nactua原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。