Data structures midterm review

Algorithm overview

Feasibility, finiteness, certainty (input, output)

Design requirements:

Correctness, readability, robustness, high efficiency and low storage

Frequency: How many times the code is executed repeatedly

Generally, the worst time complexity is calculated, and the average number of times can also be calculated.

linear table

tyoedef struct
{
    elemtype *elem;
    int len;
    int listsize;
}sxlist;

int initlist(sxlist &l)
{
    l.elem = new int;
    if(!l.elem)
    exet(overflow);
    l.lenth = 0;
    l.listsize = sizeof(int);
}

Watch out for misjudgments

For a sequence list of length n,

Inserting data at the i-th position requires moving n-i+1 elements. The actual major spend is on moving elements.

If the insertions at any position are equally possible, p = 1/n+1

Expectation: E = n/2,O(n)

When deleting, deleting the element at the i-th position requires moving ni elements.

E = (n-1)/2,O(n)

Linear (single) linked list

linklist deletitem(linklist &L,int i,Elemtype &e)
{
    p = L;int j = 0;
    while(!(p->next==null)&&j<i-1)
        {p = p->next;j++;};
    linlist *t = p->next;
    e = t->data;
    p->next =p->next->next;
    free(p);
    return ok;
}

Head insertion method to create linked list

Create linked list using tail insertion method

Single circular linked list

Set the head pointer or tail pointer, the tail pointer rear will be better

The connection only needs to discard the following head node

 

Sign of the end of traversal: p == L

 Double linked list

 

stack

sequence stack

#define  SElemtype int
#define MAXSIZE 100
typedef struct
{
    SElemtype *base;
    SElemtype *top;
    int stacksize;
}myStack;
typedef struct 
{
    SElemtype data[MAXSIZE];
    int top;
};

 Dynamic allocation:

Stack empty: S.base == top;

Stack does not exist: S.base == Null;

栈满:S.top - S.base>=S.stacksize;

#top points to the top element on the top of the stack#

 Static allocation:

Stack empty: top = 0;

Stack does not exist: data = Null;

The stack is full: top == MAXSIZE;

chain stack

Only initialize a pointer to the top node of the stack

#define  SElemtype int
#define MAXSIZE 100
typedef struct STNode
{
    SElemtype data;
    struct STnode *next;
    
}STNode, *linkStack;

It is defined exactly the same way as a singly linked list, the difference lies in the operation. Moreover, the linked list has a head node with empty data, but the stack does not. Some only have a pointer to the top node of the stack and store data.

push to stack

typedef struct STNode
{
    SElemtype data;
    struct STnode *next;
    
}STNode, *linkStack;

pop

void Pop(linkStack &S,SElemtype &e)
{
    linkStack P = S;
    e = P->data;
    S = P->next;
    delete P;
}

application

 

queue

 chain queue

A head pointer, a tail pointer, a bunch of storage units exist in the form of a linked list

#include <iostream>
typedef int QElemtype;
//定义
typedef struct QNode
{
    QElemtype data;
    struct QNode *next;
}QNode,*QueuePrt;
typedef struct
{
    QueuePrt front;
    QueuePrt rear;
}LinkQueue;
//初始化队列
void initQueue(LinkQueue &Q)
{
    Q.front = Q.rear = new QNode;
    if(!Q.front)exit(0);
    Q.front->next = NULL;
};
//进队列
void InQueue(LinkQueue &Q,QElemtype e)
{
    QueuePrt p = new QNode;
    p->data = e;
    p->next = NULL;
    Q.rear->next = p;
    Q.rear = p;
};
//出队列
void OutQueue(LinkQueue &Q,QElemtype &e)
{
    if(Q.front==Q.rear)std::cout<<"队已空"<<std::endl;return;
    QueuePrt p = Q.front->next;
    e = p->data;
    Q.front->next = p->next;
    if(Q.rear==p)Q.rear = Q.front;
    delete p;
    return ;
}

Team empty sign

Headed node: Q.front = Q.rear;

Headless node: Q.front = NULL

circular queue

typedef struct 
{
    QElemtype *base;
    int front;
    int rear;
}SqQueue;

Empty queue: Q.front = Q.rear;

Queue: Q.rear++;

Dequeue: Q.front++;

In a non-empty queue, the front pointer always points to the head element of the queue.

                The rear pointer always points to the next position of the last element of the queue.

Insert element: rear = (rear+1)%maxsize

Delete element: front = (front+1)%maxsize 

However, it is impossible to determine whether the queue is empty or full, so a special method is used.

Initialization: Q.front = Q.rear = 0;

Team empty: front == rear;

Team is full: Q.front == (Q.rear+1)%maxsize

Queue length: L = (maxsize+rear-front)%maxsize

Insert element: base[rear]=x;

                  rear = (rear+1)%maxsize;

Delete elements: front = (front+1)%maxsize;

string

Heap: the address space dynamically applied for by user programs

Stack: A memory area that stores function parameters and local traversal within a block

string:

The following table uses 0 as the array component to store the actual length of the string (a[0] = maxsize), so maxsize+1 is required when defining the storage array.

At the same time, an end mark character is added to the end of the string regardless of the length of the string.

Storage density = storage bits occupied by string value/actually allocated storage bits

array

Each element in the one-dimensional array occupies L storage units. The storage address of any data element a can be calculated by the following formula:

place(ai) = place(a0)+i*L;

Two-dimensional array

Both main sequence modes:

OK:

 List:

For row major order:

Two-dimensional array: A(mxn)

The storage address of aij is: loc(aij) = loc(a00)+(n*i+j)*L

n-dimensional array: subscript: (j1,j2,j3,...,jn)

loc(j1,j2,...,jn) = loc(0,0,...,0)+(bn*...*b2*j1+bn*...b3*j2+...+bn*jn-1 +jn)*L

For column major order:

Two-dimensional array: A(mxn)

The storage address of aij is: loc(aij) = loc(a00)+(j*m+i)*

Compressed storage of matrices

Symmetric matrix: only stores the elements of the upper triangle or lower triangle, compresses n^2 elements into n*(n-1)/2 spaces, and uses a one-dimensional array as the storage structure

Calculation of the storage subscript of element aij in the one-dimensional array:

#When calculating (i-1), add the elements in the triangle, plus the j elements in the i-th row, and subtract all subscripts from 0 by 1.

i>=j (lower triangle):k = i(i-1)/2+j-1

i<=j(上三角):k = j(j-1)/2+i-1

In fact, the problem is that aij is the k-th element of the triangle.

Diagonal matrix: All non-zero elements are concentrated in the strip area where the focus is the center. Only the elements on the three diagonals of high diagonal, diagonal and low diagonal are non-zero. Also use a one-dimensional array as the storage structure

There are 3n-2 elements in total;

Low diagonal elements:

k = 3(i-1)-1,i =j+1;

Elements on the diagonal:

k = 3(i-1),i=j;

High diagonal elements:

k = 3(i-1)+1,i=j-1

Sparse matrix: When the number of non-zero elements t/(m*n)<=0.05, it is called a sparse matrix

Use triple sequence table (i,j,aij)

Sequence table of row logical links

cross linked list

((1,2,19),(1,3,,9),(3,1,-3),(6,4,7))

generalized table

It is a generalization of linear tables and is a finite sequence composed of 0 or more single elements or sublists.

 

The length of the generalized list: the number of elements in brackets;

Depth of generalized table: After all sub-tables are expanded, the number of ( is the depth # Note that parentheses at the same level are counted as one level

C = (a,C) has length 2 and infinite depth

Head

When the generalized list is not empty, the first element a1 is the head of the generalized list (Head)

Tail

The list (a2, a3,...,an) composed of the remaining elements is called the tail of the list

The head of the table may be an atom or a generalized table, but the tail of the table must be a generalized table.

What the tail takes out must be a watch! ! ! If there is no (), it must be added. If there is, add one layer. For a generalized list with only one element, the one taken out is ().

Guess you like

Origin blog.csdn.net/Gelercat/article/details/127348723