DS sequence table

Source: Internet
Author: User

DS sequence table

Sequence table definition

The sequential representation of a linear table refers to storing the data elements of a linear table in sequence with a set of sequential storage units. The sequence storage structure or sequence image of a linear table is usually called a sequence table. An ordered table is characterized by an element "physically adjacent" in a computer to represent the logical relationship between data elements in a linear table. The storage position of each data element is different from the starting position of the linear table. A constant proportional to the order of the data element in the linear table. Therefore, as long as the starting position of the linear table is determined, any data element in the linear table can be randomly accessed. Therefore, the sequential storage structure of the linear table is a random access storage structure. The following figure shows the structure.

 

Structure of insertion and deletion of data elements in the sequence table:

 

Code for the storage structure of the sequence table:

<Span style = "font-size: 18px;"> # define LIST_INIT_SIZE 100 # define LISTINCREMENT 10 typedef int ElemType; typedef struct {ElemType * elem; // The base address of the bucket int length; // The current length int listsize; // currently allocated storage capacity} SqList; // defines a struct type and name it </span>

Basic operations on sequence tables

0 preparations before basic operations

<Span style = "font-size: 18px;" >#include <iostream> using namespace std; # include <malloc. h> # include <stdlib. h> # define TRUE 1 # define FALSE 0 # define OK 1 # define ERROR 0 # define OVERFLOW-2 # define LIST_INIT_SIZE 100 # define LISTINCREMENT 10 typedef int ElemType; typedef int Status; typedef struct {ElemType * elem; // The base address of the bucket int length; // The current length int listsize; // currently allocated storage capacity} SqList; // defines a struct type, and name it </span>

1. Construct an empty sequence table

<Span style = "font-size: 18px;"> // 1 initialize the order Table Status InitList (SqList & L) {L. elem = (ElemType *) malloc (LIST_INIT_SIZE * sizeof (ElemType); if (! L. elem) {exit (OVERFLOW);} L. length = 0; L. listsize = LIST_INIT_SIZE; return OK ;}</span>

2. Determine whether the sequence table is empty.

<Span style = "font-size: 18px;"> // 2 checks whether the sequence table is empty. Status ListEmpty (SqList L) {return L. length = 0 ;}</span>

3. Length of the sequence table

<Span style = "font-size: 18px;"> // 3. determine the length of the sequence table. Status ListLength (SqList L) {return L. length ;}</span>

4. Destroy the sequence table

<Span style = "font-size: 18px;"> // 4 destroy the sequence table Status DestroyList (SqList & L) {if (L. elem) {free (L. elem);} L. elem = NULL; return OK ;}</span>

5. Clear the sequence table

<span style="font-size:18px;">Status ClearList(SqList &L){L.length=0;return OK;}</span>

6. insert data elements into the sequence table

<span style="font-size:18px;">Status ListInsert(SqList &L,int i, ElemType e){if(i<1||i>L.length+1){return ERROR;}if (L.length>=L.listsize){ElemType *newbase=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT )*sizeof(ElemType));        if(!newbase){exit(OVERFLOW);}        L.elem=newbase;        L.listsize+=LISTINCREMENT;}    ElemType *q=&(L.elem[i-1]);    ElemType *p;    for(p=&(L.elem[L.length-1]);p>=q;p--)    {      *(p+1)=*p;    }    *q=e;      ++L.length;     return OK;</span><span style="font-size:18px;">}</span>

7. Return the I-th element in the sequence table.

<Span style = "font-size: 18px;"> // 7 return the I-th element of the sequence table Status GetElem (SqList L, int I, ElemType & e) {if (I <1 | I> L. length) {return ERROR;} e = L. elem [I-1]; return OK ;}</span>

8. Delete the I data element of the sequence table.

<Span style = "font-size: 18px;"> // 8. Delete Status ListDelete (SqList & L, int I, ElemType & e), the I-th data element of the sequence table) {if (I <1 | I> L. length) {return ERROR;} e = L. elem [I-1]; for (I = 0; I <L. length; I ++) {L. elem [I-1] = L. elem [I]; -- L. length;} return OK;} </span>

 


 

 




 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.