Data structure review-detailed explanation of sequence table operation and C language implementation


1 Introduction

  The previous article mainly described the concept, characteristics, composition and application scenarios of linear tables. This article describes the insertion, deletion, and search operations of sequence tables and the realization of C language.


2 Sequence table insertion

  The insertion time complexity of the sequence table is O(n). There are three types of sequence table insertion. The different insertion types are mainly due to different elements collection movement operations, that is, different efficiency.

  • Header insertion, move all existing element collections to the right, insertion efficiency is the lowest
  • Insert at the end of the table, move a part of the existing element set to the right, the insertion efficiency is second
  • Insertion in the middle of the table, no need to move the existing element collection, insertion efficiency is the highest

 Sequence table insertion steps:

[1] Traverse to the target location

[2] Move the set element from the target position to the right

[3] Insert the element into the target position


Insert picture description here

Sequence table insert operation


C language implementation:

int insert_sequence_list_node(sequence_list_t *list, list_node_type_t *value, int pos)  
{
    
    
	int i = 0;

	if ((list==NULL) || (value==NULL))
	{
    
    
		return -1;
	}
	if (pos > list->capacity)			/* 插入位置大于表长度 */
	{
    
    
		return -1;
	}
	if (list->occupy == list->capacity)	/* 表已满 */
	{
    
    
		return -1;
	}
	pos = pos > list->occupy ? list->occupy : pos;	/* 插入位置大于当前表占用长度 */
	for (i=list->occupy; i>pos; i--)
	{
    
    
		list->data[i] = list->data[i-1];
	}
	memcpy(&list->data[pos], value, sizeof(list_node_type_t));
	list->occupy++;
    
    return 0;
}

3 Sequence table deletion

  Sequence table deletion and insertion are the opposite process. There are three types of deletion, corresponding to the type of insertion.

  • Delete the header, move all the existing element collections to the left, the deletion efficiency is the lowest
  • Delete the end of the table, move a part of the existing element set to the left, and delete efficiency second
  • Delete in the middle of the table, no need to move the existing element collection, the most efficient deletion

 Sequence table deletion steps:

[1] Traverse to the target location

[2] Delete the target location element

[3] Move the set element from the target position to the left


Insert picture description here

Sequence table insert operation


C language implementation:

int delete_sequence_list_node(sequence_list_t *list, int pos) 
{
    
    
	list_node_type_t *node = NULL;
	int i = 0;
	
	if (list == NULL)
	{
    
    
		return -1;
	}
	if (pos > list->occupy)			
	{
    
    
		return -1;
	}
	
	node = get_sequence_list_node(list, pos);
	if (node != NULL)
	{
    
    
		for(i=pos; i<(list->occupy-1); i++)
        {
    
    
            list->data[i] = list->data[i+1];
        }
        list->occupy--;
		return 0;
	}
	return -1;
}

4 Sequence table lookup

  The search time complexity of the sequence table is O(1), and it only needs to search according to the index (subscript) value, and the search efficiency is extremely high.

Insert picture description here

Sequence table lookup operation

C language implementation:

list_node_type_t *get_sequence_list_node(sequence_list_t *list, int pos)  
{
    
    
	if (list == NULL)
	{
    
    
		return NULL;
	}
	if (0 == list->occupy)
	{
    
    
		return NULL;
	}
	
	if((pos>=0) && (pos<list->occupy))
    {
    
    
        return (list_node_type_t*)&list->data[pos];
    }
}

5 Examples

  • Implement a sequence table
  • Provide sequence table creation, insertion, deletion, search, destruction operation interface

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

typedef int list_node_type_t;

typedef struct _sequence_list
{
    
    
    int capacity;
    int occupy;
    list_node_type_t *data;
}sequence_list_t;

sequence_list_t* create_sequence_list(int capacity, list_node_type_t node)
{
    
    
	sequence_list_t* list = NULL;
    
    if (capacity >= 0)
    {
    
    
        list = (sequence_list_t*)malloc(sizeof(sequence_list_t) + sizeof(list_node_type_t) * capacity);
    }
    
    if (list != NULL)
    {
    
    
        list->capacity = capacity;
        list->occupy = 0;
        list->data= (int*)(list + 1);
    }
    
    return list;
}

int destory_sequence_list(sequence_list_t *list)
{
    
    
	if (list == NULL)
	{
    
    
		return -1;
	}
	free(list);
	list = NULL;

	return 0;
}

int get_sequence_list_capacity(sequence_list_t *list)
{
    
    
	if (list == NULL)
	{
    
    
		return 0;
	}
	return list->capacity;
}

int get_sequence_list_occupy(sequence_list_t *list)
{
    
    
	if (list == NULL)
	{
    
    
		return 0;
	}
	return list->occupy;
}

int clear_sequence_list(sequence_list_t *list)
{
    
    
	if (list == NULL)
	{
    
    
		return -1;
	}
	list->occupy = 0;
	
	return 0;
}

int insert_sequence_list_node(sequence_list_t *list, list_node_type_t *value, int pos)  
{
    
    
	int i = 0;

	if ((list==NULL) || (value==NULL))
	{
    
    
		return -1;
	}
	if (pos > list->capacity)			/* 插入位置大于表长度 */
	{
    
    
		return -1;
	}
	if (list->occupy == list->capacity)	/* 表已满 */
	{
    
    
		return -1;
	}
	pos = pos > list->occupy ? list->occupy : pos;	/* 插入位置大于当前表占用长度 */
	for (i=list->occupy; i>pos; i--)
	{
    
    
		list->data[i] = list->data[i-1];
	}
	memcpy(&list->data[pos], value, sizeof(list_node_type_t));
	list->occupy++;
    
    return 0;
}

list_node_type_t *get_sequence_list_node(sequence_list_t *list, int pos)  
{
    
    
	if (list == NULL)
	{
    
    
		return NULL;
	}
	if (0 == list->occupy)
	{
    
    
		return NULL;
	}
	
	if((pos>=0) && (pos<list->occupy))
    {
    
    
        return (list_node_type_t*)&list->data[pos];
    }
}

int delete_sequence_list_node(sequence_list_t *list, int pos) 
{
    
    
	list_node_type_t *node = NULL;
	int i = 0;
	
	if (list == NULL)
	{
    
    
		return -1;
	}
	if (pos > list->occupy)			
	{
    
    
		return -1;
	}
	
	node = get_sequence_list_node(list, pos);
	if (node != NULL)
	{
    
    
		for(i=pos; i<(list->occupy-1); i++)
        {
    
    
            list->data[i] = list->data[i+1];
        }
        list->occupy--;
		return 0;
	}
	return -1;
}

int main(int argc, char *argv[]) 
{
    
    
	sequence_list_t *seqlist = NULL;
	list_node_type_t node;
	list_node_type_t *ptemp;
	int i;
	
	/* 创建顺序表 */
	seqlist = create_sequence_list(10, node);	

	/* 插入操作 */
	node = 0;
	insert_sequence_list_node(seqlist, &node, 0);
	node = 1;
	insert_sequence_list_node(seqlist, &node, 1);
	node = 3;
	insert_sequence_list_node(seqlist, &node, 2);
	node = 5;
	insert_sequence_list_node(seqlist, &node, 1);

	printf("sequence list capacity:[%d]\n", seqlist->capacity);
	printf("sequence list occupy:[%d]\n", seqlist->occupy);
	for(i=0; i<get_sequence_list_occupy(seqlist); i++)
    {
    
    
        ptemp = get_sequence_list_node(seqlist, i);
        printf("sequence list node[%d]=%d\n", i, *ptemp);
    }
	destory_sequence_list(seqlist);	/* 销毁线性表 */
}

Compile and execute

  • Execution results under Ubuntu16.04
acuity@ubuntu:/mnt/hgfs/LSW/STHB/temp$ gcc list.c -o list
acuity@ubuntu:/mnt/hgfs/LSW/STHB/temp$ ./list
sequence list capacity:[10]
sequence list occupy:[4]
sequence list node[0]=0
sequence list node[1]=5
sequence list node[2]=1
sequence list node[3]=3

Guess you like

Origin blog.csdn.net/qq_20553613/article/details/108025910