Data structures: linear form - sequence

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/qq_38446366/article/details/89396844

The basic concepts of linear # table:

Table ## defines a linear: linear table is composed of a finite sequence of n identical data element characteristics,

## linear operating table: create, destroy, empty, insert elements, delete elements, the elements get a location, the length.

 Except the first element a1, and each element has only a direct precursor element except the last element of an, and each element has only one immediate successor element. Relationships between data elements is one to one relationship.

#ifndef _MY_LIST_H
#define _MY_LIST_H

typedef void List;
typedef void ListNode;

//create list
List* list_Create();

//
void List_Destroy(List *list);

//
void List_Clear(List* list);

//
int List_length(List* list);

//
int List_Insert(List* list, ListNode* node, int pos);

//
ListNode* List_Get(List* list, int pos);

//
ListNode* List_Delete(List* list, int pos);

#endif

# Linear form of sequential storage structure:

##basic concept

Concept: linear sequentially stored tables a group of data elements with consecutive addresses of the storage unit, the linear form of this memory structure is referred to as sequence table.

Features: logically adjacent data elements, are adjacent physical order.

## Design and Implementation

create:

typedef void SeqList;
typedef void SeqListNode;

//结构体中套指针,数组的大小有用户确定
typedef struct _tag_SeqList
{
   int length;
   int capacity;
   unsigned int *node;
}TSeqList;

SeqList* SeqList_Create(int capacity)
{
   TSeqList *tmp = NULL:
   tmp = (TSeqList *)malloc(sizeof(TSeqList));
   if(tmp == NULL)
   {
      return NULL;
   }
   memset(tmp, 0, sizeof(TSeqList));

   tmp->node = (unsigned int *)malloc((sizeof(unsigned int *))*capacity);
   if(tmp->node = NULL)
   {
    return NULL;
   }
   tmp->capacity = capacity;
   tmp->length = 0;
   return tmp;     
}

destroy:

void SeqList_Destroy(SeqList *list)
{
  TSeqList *tList = NULL:
  if(list == NULL)
    return ;
  tList = (TSeqList *)list;
  if(tList->node != NULL)
  {
     free(tList->node);
  }
  free(tList);
}

Empty:

void SeqList_Clear(SeqList* list);
{
   TSeqList *tList = NULL:
   if(list == NULL)
      return;
   tList = (TSeqList *)list;
   tList->length = 0;    
}

insert:

//插入:元素移动, 在位置上插入。
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)
{
   TSeqList *tList = NULL:
   if(list == NULL || node == NULL || pos < 0)
   {
      return -1;
   }
   tList = (TSeqList *)list;

   // 判断是否存满了
   if(tList->length >= tList->capacity)
      return -2;

   // 容错修正, 链表容量是20, 长度为6, 用户想在10的位置插入
   if(pos >= tList->length)
       pos = tList->length;

   for(int i = list->length ; i > pos ; i--)
   {
       tList->node[i] = tList->node[i-1];
    }
    tList->node[pos] = (unsigned int)node;
    tList->length++;
    return 0;
}

Delete the specified location:


SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
   TSeqList *tList = NULL:
   SeqListNode *ret = NULL;
   if(list == NULL || pos < 0)
   {
      return NULL;
   }
   tList = (TSeqList *)list;
   
   ret = (SeqListNode *)tList->node[pos];

   for(int i=pos+1; i<tList->length; i++)
   {
      tList->node[i-1] = tList->node[i];
   }
   tList->length--;

 return ret;
}

length:

//
int SeqList_length(SeqList* list)
{
   TSeqList *tList = NULL:
   if(list == NULL)
      return -1;
   tList = (TSeqList *)list;
   return tList->capacity;   
}

Gets the specified location

//
SeqListNode* SeqList_Get(SeqList* list, int pos)
{
   TSeqList *tList = NULL:
   SeqListNode *ret = NULL;
   if(list == NULL || pos < 0)
   {
      return NULL;
   }
   tList = (TSeqList *)list;
   
   ret = (SeqListNode *)tList->node[pos];
 
   return ret;
}

## advantages and disadvantages

No need to add extra space for linear logic table, the table can quickly obtain legal position of an element

Insertion and deletion operations need to move a large number of elements, the length of the table when the linear variation is large, it is difficult to determine the capacity of the storage space.

 

Guess you like

Origin blog.csdn.net/qq_38446366/article/details/89396844