Data structures, algorithms, and linear table summarizes

A data structure

1, the concept of a data structure

  Data structure is the presence of one or more data elements of the particular relationship between each set. By the data elements, data elements of the data items, the data element is a basic unit of data, the data item is the smallest unit of data.

2, the logical structure

  Logical structure data is divided into linear and nonlinear structural configuration, wherein the linear structure includes a linear table, stacks, queues, and the like strings, and the nonlinear structure comprises a tree structure graphic junction structure and the like.

3, storage structure

  Storing configuration data into sequential storage structure and chain storage structure. Sequential storage structure is logically adjacent storage locations in the physical storage unit, such as adjacent array; chain memory structure is not required to be logically connected nodes adjacent physical locations, which can be represented by a logical pointer borrow data between relationship.

4, algorithm

  Five features (1) comprises a finite algorithm, certainty, feasibility, and an output.

  The method described in (2) the algorithm can start from a natural language, and programming languages pseudocode. Where the natural language description strongest, but write it complicated; programming language but easy readability of the worst writing; advantage before pseudo-code combination of the two, making the code writing process more often.

  (3) algorithm to analyze the efficiency of the algorithm from time complexity and space complexity degree: methods to measure the efficiency of the algorithm also include ex post and ex ante analysis statistics estimation method.

    ① time complexity: the number of times the algorithm performs basic operations T (n) is a function of the problem size denoted as n T (n) = O (f (n)). Often appear constant order O (1), the linear order O (n), the square of the order O (n ^ 2), of the order O (log2n); calculating time complexity is obtained only T (n) of the highest order, ignored order and low-order constants, which can be seen as the problem size n is large time efficiency of the algorithm.

    ② space complexity: Measurement algorithms during operation temporary occupation of storage space.

    In general, the calculation is more abundant space, often as a spatial complexity of the algorithm Algorithm measure of the merits.

5, the data type and data structure :

  Abstract data type (ADT) frame:

  {Data Type Name

    Data objects;

    Data relationships;

    Basic operations;

  }

 Second, the linear form

1, sequence table

#define Maxsize 50
typedef int Elemtype;
typedef struct {
    Elemtype data[Maxsize];
    int length;
} SqList;

Features: ① logically adjacent <-> adjacent physical address; ② random access memory (faster access).

Advantages: ① relatively high space utilization with a full (but also do not like the list as a pointer to the allocated memory); ② fast read speed, random excellent in storage.

Disadvantages: ① do not know in advance if you want to allocate memory space will be how much waste a lot of memory space or lack of space resulted in an overflow; ② insert and delete operations is slow.

(1) insert elements

 

bool InsertList(SqList*& L, int i, Elemtype e)
{
    if (i < 1 || L->length + 1)
        return false;
    i--;
    for (int j = L->length; j > i; j--)
        L->data[j] = L->data[j - 1];
    L->data[i] = e;
    L->length++;
    return true;
}

Time complexity: O (L-> length).

(2) remove elements

bool DeleteList(SqList*& L, int i, Elemtype &e)
{
    if (i < 1 || i->length + 1)
        return false;
    i--;
    e = L->data[i];
    for (int j = i; j < L->length-1; j++)
        L->data[j] = L->data[j + 1];
    L->length--;
    return true;
}

Time complexity: O (L-> length).

2, the list

typedef struct LNode {
    Elemtype data;
    struct LNode* next;
}LinkList;

Features: linear table data element stored in a group address of any storage node connection between the next memory.

Advantages: ① dynamic memory allocation; ② delete and insert operation without moving the position of elements in the table.

Disadvantages: ① sequential access storage element structures compared to the relatively slow; ② the need to allocate space for a pointer to the next node.

Node configuration: pointer data elements +; wherein the values ​​of the data elements of the data storage node, the next node pointer storage address of the node.

Data elements (1) Find the location specified in the table

 

bool GetElem(LinkList* L, int i, Elemtype& e)
{
    int j = 0;
    LinkList* p = L;
    while (j < i && p != NULL) {
        j++;
        p = p->next;
    }
    if (p == NULL)
        return false;
    else {
        e = p->data;
        return true;
    }
}

时间复杂度:O(L->length)

(2)插入元素

bool InsertList(LinkList*& L, int i, Elemtype e)
{
    int j = 0;
    LinkList* p = L;
    while (j < i - 1 && p != NULL) {
        j++;
        p = p->next;
    }
    if (p == NULL)
        return false;
    else {
        s = new LNode;
        s->data = e;
        s->next = p->next;
        p->next = s;
    }
}

时间复杂度:O(L->length)。

(3)删除元素

bool DeleteList(LinkList*& L, int i, Elemtype e)
{
    int j = 0;
    LinkList* p = L, * q;
    while (j < i - 1 && p != NULL) {
        j++;
        p = p->next;
    }
    if (p == NULL)
        return false;
    else {
        q = p->next;
        if (q == NULL)
            return false;
        e = q->data;
        p->next = q->next;
        delete p;
        return true;
    }
}

时间复杂度:O(L->length)。

(4)建单链表-头插法

void CreateListF(LinkList*& L, Elemtype a[], int n)
{
    LinkList* s;
    L = new LNode;
    L->next = NULL;
    for (int i = 0; i < n; i++) {
        s = new LNode;
        s->data = a[i];
        s->next = L->next;
        L->next = s;
    }
}

输出的结果与输入相反。

(5)建单链表-尾插法

void CreateListR(LinkList*& L, Elemtype a[], int n)
{
    LinkList* s, * r;
    L = new LNode;
    r = L;
    for (int i = 0; i < n; i++) {
        s = new LNode;
        s->data = a[i];
        r->next = s;
        r = s;
    }
    r->next = NULL;
}

输出的结果与输入相同。

3、双链表

 

typedef struct DNode {
    Elemtype data;
    struct DNode* prior;
    struct DNode* next;
}DLinkList;

 

(1)节点插入

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

(2)节点删除

q = p->next;
p->next = q->next;
q->next->prior = p;
delete q

 4、有序表:其中所有元素以递增或递减的方式有序排列

(1)插入

//顺序表
void ListInsert(SqList*& L, Elemtype e)
{
    int i = 0, j;
    while (i < L->length && L->data[i] < e)
        i++;
    for (j = L->length; j > i; j--)
        L->data[j] = L->data[j - 1];
    L->data[i] = e;
    L->length++;
}

//链式表
void ListInsert(LinkList*& L, Elemtype e)
{
    LinkList* pre = L, * p;
    while (pre->next != NULL && pre->next->data < e)
        pre = pre->next;
    p = new LNode;
    p->data = e;
    p->next = pre->next;
    pre->next = p;
}

三、栈

1、栈

  概念:栈是限制仅在线性表的一端进行插入与删除操作的线性表

  特点:后进先出(LIFO)

2、顺序栈

#define Maxsize 100
typedef struct {
    Elemtype data[Maxsize];
    int top;
}SqStack;

(1)进栈

bool Push(SqStack*& s, ELemtype e)
{
    if (s->top == Maxsize - 1)
        return false;
    s->top++;
    s->data[s->top] = e;
    return true;
}

(2)出栈

bool Pop(SqStack*& s, Elemtype& e)
{
    if (s->top == -1)
        return false;
    e = s->data[s->top];
    s->top--;
    return true;
}

(3)取栈顶元素

bool GetTop(SqStack* s, Elemtype& e)
{
    if (s->top == -1)
        return false;
    e = s->data[s->top];
    return true;
}

3、链式栈

typedef struct linknode {
    Elemtype data;
    struct linknode* next;    
}LiStack;

(1)进栈

void Push(LiStack*& s, Elemtype e)
{
    LiStack* p;
    p = new LiStack;
    p->data = e;
    p->next = s->next;
    s->next = p;
}

(2)出栈

void Pop(LiStack*& s, Elemtype& e)
{
    LiStack* p;
    if (s->next == NULL)
        return false;
    p = s->next;
    e = p->data;
    s->next = p->next;
    delete p;
    return true;
}

(3)取栈顶元素

void GetTop(LiStack* s, Elemtype& e)
{
    if (s->next == NULL)
        return false;
    e = s->next->data;
    return true;
}

4、#include<stack>

stack

stack模板类的定义在<stack>头文件中。

stack模板类需要两个模板参数,一个是元素类型,一个容器类型,但只有元素类型是必要的,在不指定容器类型时,默认的容器类型为deque。

定义stack对象的示例代码如下:

stack<int> s1;
stack<string> s2;

stack的基本操作有:

入栈,如例:s1.push(x);

出栈,如例:s1.pop();注意,出栈操作只是删除栈顶元素,并不返回该元素。

访问栈顶,如例:s1.top()

判断栈空,如例:s1.empty(),当栈空时,返回true。

访问栈中的元素个数,如例:s1.size()

四、队列

1、队列

  概念:只允许在表的一端(队尾)进行插入,而在表的另一端(队头)进行删除。

  特点:先进先出(FIFO)。

2、顺序队列

#define MaxSize 100
typedef struct {
    Elemtype data[Maxsize];
    int front;
    int rear;
}SqQu

(1)入队

bool enQueue(SeCiQueue*& q, Elemtype e)
{
    if ((q->rear+1)%Maxsize==q->front)
        return false;
    q->rear = (q->rear + 1) % Maxsize;
    q->data[q->rear] = e;
    return true;
}

(2)出队

bool deQueue(SeciQueue*& q, Elemtype& e)
{
    if (QueueEmpty(q))
        return false;
    q->front = (q->front + 1) % Maxsize;
    e = q->data[q->front];
    return true;
}

3、链式队伍

typedef struct QNode {
    Elemtype data;
    struct QNode* next;
}QNode;

 

(1)入队

void enQueue(LiQueue*& q, Elemtype e)
{
    QNode* newNode = new QNode;
    newNode->data = e;
    newNode->next = NULL;
    if (QueueEmpty(q)) {
        q->rear = newNode;
        q->front = newNode;
    }
    else {
        q->rear->next = newNode;
        q->rear = newNode;
    }
}

(2)出队

bool deQueue(LiQueue*& q, Elemtype& e)
{
    QNode* del;
    if (QueueEmpty(q))
        return false;
    del = q->front;
    if (q->front = q->rear)
        q = front = q->rear = NULL;
    else
        q->front = q->front->next;
    e = del->data;
    delete del;
    return true;
}

4、#include<queue>

queue

queue模板类的定义在<queue>头文件中。

与stack模板类很相似,queue模板类也需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为deque类型。

定义queue对象的示例代码如下:

queue<int> q1;
queue<double> q2;

queue的基本操作有:

入队,如例:q1.push(x); 将x接到队列的末端。
出队,如例:q1.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
访问队首元素,如例:q1.front(),即最早被压入队列的元素。
访问队尾元素,如例:q1.back(),即最后被压入队列的元素。
判断队列空,如例:q1.empty(),当队列空时,返回true。
访问队列中的元素个数,如例:q1.size()

五、串

1、串

  概念:由零个或多个字符组成的有限序列。

2、#include<string>

  详情访帖:http://blog.sina.com.cn/s/blog_453a02280100r8tv.html

3、BF算法

 

1.在串S和串T中设置比较的起始下标i=0,j=0
2.重复下述操作,直到S或T的所有字符比较完毕
    ①如果S[i]=T[j],则继续比较S和T的下一对字符
    ②否则,回溯下标i=i-j+1,j=0
3.如果T中所有字符比较完毕,则返回匹配的开始位置
    i-t.length(t为T字串);否则返回-1
时间复杂度:O(n*m)

4、KMP算法

1.在串S和串T中,分别设置比较的起始下标,i=0,j=0
2.重复下述操作,直到S或T的所有字符均比较完毕
    ①如果S[i]等于T[j],则继续比较S和T的下一对字符
    ②否则,将下标j回溯到next[j]位置,即j=next[j]
    ③如果j=-1,则将下标i和j分别加1,准备下一趟比较
3.如果T中所有字符均比较完毕,则返回本趟匹配的开始位置,否则返回-1

六、总结

1、在这次总结中,巩固了几种线性表的基本操作,对数据结构中结构部分有了更深的了解。

2、因为总结的时间没有把握好,写的仍不是很具体,有些操作还没有彻底理解。

3、在实际操作中仍不熟练,之后会加深代码的应用,增加熟练度。

 

Guess you like

Origin www.cnblogs.com/Tvoimvyan/p/12586529.html