Review two data structures: linear form

EDITORIAL: we only have two days to review the exam discrete data structures, as well as seven chapters, I may have gone ...... Chong Chong Chong!

What is the linear form

It is defined as: a n (> = 0) identical finite sequence of data elements constituting the characteristics when n = 0 is the empty list

Nonempty linear structure features:

1, "the first" single element

2, the "last" unique element

3, except the first one, each data element of the data structure are only a precursor

4, except the last addition, each data element are only a successor

Order table

Not defined in detail, as the name suggests. Storage structure random access characteristic.

A sequence table with the dynamically allocated one-dimensional array (generally designated), as follows:

typedef struct{
    ElemType *elem;         //存储空间基地址
    int length;             //当前长度
}sqList;

A sequence table with the dynamically allocated one-dimensional array (unary order polynomial storage structure as an example), as follows:

typedef struct{
    float coef;     //系数
    int expn;       //指数
}Polynomial;
typedef struct{
    Polynomial *elem;       //存储空间的基地址
    int length;             //多项式中当前项的个数
}sqList;

Order to achieve the basic operation of the table

1, initialization

Algorithm steps: dynamic allocation list L to a predetermined spatial array-defined size, so that this address space elem point, the current length of the table and set to 0.

Code:

L.elem=new ElemType[maxn];       //为顺序表分配一个大小为maxn的空间
if(!L.elem) exit(OVERFLOW);     //存储分配失败退出

Special attention program fails export!

2, the value

Algorithm steps: determining the value of the specified position number i is reasonable, if a reasonable value of i, then the corresponding data element L.elem [i-1] is assigned to the parameter E, the return value of the i-th data element through E, otherwise ERROR.

Note logic algorithm steps! After the first judgment execution

Code is no longer given.

Obviously time complexity O (1).

3. Find

Algorithm to find the content will be in the future as part of the detailed description specifically, here, only to find the order of traversal, the time complexity is O (n).

4, insert

Algorithm steps:

1, it is determined whether a legitimate position i

2, it is determined whether the sequence table storage legitimate

3, the n th element to the i sequentially moves a rearward position, the i-th position vacated

4, to be inserted into the first new element i e position

5、length++

Also note that logic algorithm!

Code is not given, the following complexity analysis time:

Inserted prior probability of the i th element is p_{i}=\frac{1}{n+1}the mobile number of times n-i+1;

So the number of elements required for a desired value at the time of moving length n linear element is inserted in the table:E=\frac{1}{n+1}\sum_{i=1}^{n+1}(n-i+1)=\frac{n}{2}

This shows that the average time complexity is O (n).

 

5, delete

Algorithm steps:

1, to determine whether the legal position i deleted

2, the element i + 1-th to n-th positions are sequentially moved forward one position

3、length--

Time complexity analysis above:E=\frac{1}{n}\sum_{i=1}^{n}(n-i)=\frac{n-1}{2}

List

I.e. linear form of the chain memory structure is characterized by a set of data element storage unit stores an arbitrary linear form (continuous or discontinuous)

Storing information including itself and direct subsequent storage location, called a data field and a pointer field

The number of nodes contained in the pointer list, and a pointer to a pointer connection, can be divided into single linked list, a circular linked list, a doubly linked list, a binary list, orthogonal list, the adjacent table, the adjacent multi-table and the like.

Define and express a single list

The first node can be added from time to increase, the increasing operation is more convenient.

Single linked list head pointer can be uniquely determined, non-random access storage structure code is as follows:

typedef struct LNode{
    ElemType data;              //结点的数据域
    struct LNode *next;         //结点的指针域
}LNode,*LinkList;               //LinkList为LNode的指针类型,定义时LNode *p与LinkList p完全相同

Achieve the basic operation of a single linked list

1, initialization

L=new LNode;                //生成新结点作为i头节点,用头指针L指向头结点
L->next=NULL;               //头结点指针域置空

2, the value

p = p-> next traversal operation, relatively simple, not described in detail.

Average lengthASL=\frac{1}{n}\sum_{i=1}^{n}(i-1)=\frac{n-1}{2}

The time complexity is O (n).

3. Find

Also traversal, the time complexity Ibid

4, insert

Is relatively simple, mainly to find the location, and create a new node is inserted, the key code:

s->next=p->next;        //两个p->next可分别理解为一个有值的结点
p->next=s;             //一片待赋值的空间

5, delete

The main code:

q=p->next;
p->next=q->next;        //将待删除结点的前驱结点的后继指向待删除结点的后继
delete q;

 

6, create a single list

Here is a small focus

Create a single list forward runs law

Algorithm steps:

1. Create an empty list only a head node

2, in accordance with the number of elements to create the list comprising n, n times the following cycle:

(1) generates a new node * p

(2) an input element assigned to the new node value * p data fields

(3) After the new node inserted into the head node * p

Code:

void CreateList_H(LinkList &L, int n)
{
    LinkList p;
    L = new LNode;
    L->next = NULL;
    for (int i = 0; i < n; i++)
    {
        p = new LNode;
        cin >> p->data;
        p->next = L->next;
        L->next = p;
    }
}

Obviously time complexity is O (n).

Here the insert elements from scratch each time, the resulting list reverse order of the original array, so that the actual code is usually written by interpolation

After you create a single list interpolation

Algorithm steps:

1. Create an empty list only a head node

2, the tail pointer r initialized to point to the head node

3, according to the number of elements including the created list of n, n times the following loop

(1) generates a new node * p

(2) an input element assigned to the new node value * p data fields

(3) After the new node is inserted into the tail * p * r node

(4) the end of the tail pointer to the new node * p

Code:

void CreateList_R(LinkList &L, int n)
{
    LinkList r, p;
    L = new LNode;
    L->next = NULL;
    r = L;              
    for (int i = 0; i < n; i++)
    {
        p = new LNode;
        cin >> p->data;
        p->next = NULL;
        r->next = p;
        r = p;
    }
}

If r here as an external things will be difficult to understand, as the current pointer it directly, such as seen with L r put something at the beginning (in fact, the code is such a performance), back r is each step p.

Time complexity is n.

Circular list

It characterized by a last node table pointer field points to the first node, the entire list to form a ring.

Determining whether the current node pointer to the table by the termination condition p! = NULL or p-> next! = NULL becomes p! = L or p-> next! = L.

Sometimes only the circular list tail pointer set up, this will ease the list merge operation.

Doubly linked list

The node pointer field by two, a direct successor point, another point immediate predecessor

Storage structure:

typedef struct DuLNode{
    ElemType data;
    struct DuLNode *prior;
    struct DuLNode *next;
}DuLNode,*DuLinkList;

Doubly linked list insertion, deletion involving pointers in both directions, with a single list is very different from other operations and a single list is no different

Doubly linked list insertion, the main code:

s->prior=p->prior;
p->prior->next=s;
s->next=p;
p->prior=s;

To delete simpler:

p->prior->next=p->next;
p->next->prior=p->prior;

It is both time complexity of O (n).

Comparison of the sequence table and the list

1, comparing the performance space

Based allocation and the size of memory storage density, when the change in length of the linear form of relatively large, it is difficult to estimate the size of the storage, the list should be used as the storage structure

Conversely, when the change in length of the linear form is not easy when the size determined in advance, in order to save memory space, it should be used as the sequence table storage structure

= Storage density of data storage capacity occupied by the element itself: a / the amount of memory occupied by the node structure

2, comparing performance time

Based efficiency, and the efficiency of insertion and deletion of the access elements, such operation when the value of the main operation and the preceding table element position is closely related, do little insert or delete operation, should be adopted as the sequence table storage structure

Conversely, for the first table frequent insert or delete operation, the list should be used as the storage structure

 

 

 

————————————————————————————————————————————————————

 

Note: This article All content is derived from the "data structure (C language Second Edition)" (Yan Wei Min teacher a)

table of Contents

What is the linear form

Order table

Order to achieve the basic operation of the table

1, initialization

2, the value

3. Find

4, insert

5, delete

List

Define and express a single list

Achieve the basic operation of a single linked list

1, initialization

2, the value

3. Find

4, insert

5, delete

6, create a single list

Circular list

Doubly linked list

Comparison of the sequence table and the list

1, comparing the performance space

2, comparing performance time


 

 

 

 

Released six original articles · won praise 0 · Views 138

Guess you like

Origin blog.csdn.net/Roy_F_M/article/details/103829508