Data Structure beginner 1 (linear table)

Source: Internet
Author: User

To sum up the knowledge of linear tables, I learned only the linear tables of sequential structures tonight, so I didn't even mention the linear tables of storage structures!

Theoretical knowledge module

I. Linear table definition

1. A linear table (List) is a set of zero or multiple data elements.
2. Data Elements in a linear table are ordered.
3. The number of data elements in a linear table is limited.

4. The data element types in a linear table must be the same

Ii. Concepts of linear tables

1. A linear table is a finite sequence of n (≥0) data elements of the same type.

2. A linear table is an ordered and finite set of data elements.
3. Data Elements in a linear table must be of the same type.
4. Linear tables can be used to describe the relationship between "queue types"

Iii. Common Operations on Linear tables
1. Create a linear table
2. Destroy a linear table
3. Clear the linear table
4. Insert elements into a linear table.
5. delete an element from a linear table.
6. Obtain the element at a certain position in a linear table.
7. Obtain the length of a linear table

4. A linear table is a special data type in a program. operations on a linear table are represented as a group of functions in the program.

ListList* List_Create();void List_Destroy(List* list);void List_Clear(List* list);int List_Insert(List* list, ListNode* node, int pos);ListNode* List_Delete(List* list, int pos);ListNode* List_Get(List* list, int pos);int List_Length(List* list);

V. Sequential Storage Structure
1. The sequential storage structure of a linear table refers to storing data elements of a linear table in sequence with sequential storage units of a specific link.

2. in C language, one-dimensional arrays can be used for sequential storage structure.
Starting position of the bucket: array n od e
Maximum capacity of a linear table: ma xsi ze, array Length
Current length of a linear table: length

Example:

Practice Module

The next step is to verify the theory through some programs.

Enrich the previous sequence table operation functions one by one

1. Obtain element Functions

SeqListNode* SeqList_Get(SeqList* list, int pos) // O(1) {    TSeqList* sList = (TSeqList*)list;    SeqListNode* ret = NULL;        if( (sList != NULL) && (0 <= pos) && (pos <= sList->length) )    {        ret = (SeqListNode*)(sList->node[pos]);    }        return ret;}

Ii. Insert Element Algorithm

int SeqList_Insert(SeqList* list, SeqListNode* node, int pos) // O(n) {    TSeqList* sList = (TSeqList*)list;    int ret = (sList != NULL);    int i = 0;        ret = ret && (sList->length + 1 <= sList->capacity);    ret = ret && (0 <= pos);        if( ret )    {        if( pos >= sList->length )        {            pos = sList->length;        }                for(i=sList->length; i>pos; i--)        {            sList->node[i] = sList->node[i-1];        }                sList->node[i] = (TSeqListNode)node;                sList->length++;    }        return ret;}

Iii. Delete element Algorithms

SeqListNode* SeqList_Delete(SeqList* list, int pos) // O(n){    TSeqList* sList = (TSeqList*)list;    SeqListNode* ret = SeqList_Get(list, pos);    int i = 0;        if( ret != NULL )    {        for(i=pos+1; i<sList->length; i++)        {            sList->node[i-1] = sList->node[i];        }                sList->length--;    }        return ret;}

4. Obtain the linear table size

int SeqList_Capacity(SeqList* list) // O(1){    TSeqList* sList = (TSeqList*)list;    int ret = -1;        if( sList != NULL )    {        ret = sList->capacity;    }        return ret;}

5. Linear table length

int SeqList_Length(SeqList* list) // O(1){    TSeqList* sList = (TSeqList*)list;    int ret = -1;        if( sList != NULL )    {        ret = sList->length;    }        return ret;}

6. Clear a linear table

void SeqList_Clear(SeqList* list) // O(1){    TSeqList* sList = (TSeqList*)list;        if( sList != NULL )    {        sList->length = 0;    }}

7. Create a linear table

SeqList* SeqList_Create(int capacity) // O(1){    TSeqList* ret = NULL;        if( capacity >= 0 )    {        ret = (TSeqList*)malloc( sizeof(TSeqListNode) * capacity);    }        if( ret != NULL )    {        ret->capacity = capacity;        ret->length = 0;        ret->node = (TSeqListNode*)(ret + 1);    }        return ret;}

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.