数据结构复习总结

数据结构复习总结

一、线性表

A.顺序表

数据结构设计
typedef struct
{
    DATATYPE buf[MAX];
    int n;
}SEQLIST;

1.插入元素(seq)

seq->buf[seq->n] = data;

2.按位置插入(seq,post,data)

mov_n = seq->n + 1 - post + 1

for(i = seq->n; mov_n > 0; i --,mov_n --)
{
    seq->buf[i+1] = seq->buf[i]
}

seq->buf[post-1] = data;

3.删除一个指定的元素(seq,key)

for(i = 0,j = 0;i <= seq->n ;i ++)
{
    if(seq->buf[i]  != key)
        seq->buf[j++] = seq->buf[i];
}

seq->n = j - 1;

B.链表

数据结构的设计

typedef struct node
{
    DATATYPE data;
    struct node *next;
}LINKLIST;

1.创建

//a.分配头结点(head)
//b.head->next = NULL;
//c.return head;

2.头插法

struct node *temp;

temp = (struct node *)malloc(sizeof(struct node));
temp->data = data;

temp->next = head->next;
head->next = temp;

3.尾插法

思想:遍历到链表的尾部
struct node *p = head;

while(p->next != NULL) p = p->next;
p->next = temp;
temp->next = NULL;

//1 4 6 7

4.按序插入
struct node *p = head;

while(p->next != NULL && p->next->data < data) p = p->next;
temp->next = p->next;
p->next = temp;

//1 2 4 8 9

5.删除一个指定的元素
struct node *p = head;

while(p->next != NULL && p->next->data != data) p = p->next;

temp = p->next;
p->next = temp->next;
free(temp); 


6.逆置一个链表

struct node *p = head;
struct node *q = p ->next;
struct node *current;

current = q->next;
q->next = NULL;

while(current)
{
    p = current->next;
    current->next = head->next;
    head->next = current;
    current = p;
} 

7.链表的遍历

struct node * p = head->next;

while(p)
{
    printf("%d ",p->data);
    p = p->next;
}

C.顺序栈

数据结构设计

typedef struct
{
    DATATYPE buf[MAX];
    int top;//记录栈顶元素的位置
}STACK; 

1.

create_empty_stack()
{
    STACK *s;

    s = malloc();
    s->top = -1;
} 
2.
push_stack(STACK *s,DATATYPE data)
{
   // a.判满
    //b.s->top ++;
    //s->buf[s->top] = data;
} 

3.

DATATYPE pop_stack(STACK *)
{
    //a.判空  
    data = s->buf[s->top];
    s->top --;

   // b.return data;
} 
D.链式栈

数据结构设计

struct node
{
    DATATYPE data;
    struct node *next; };

typedef struct
{
    struct node *top;
    int n;
}STACK; 

1.

create_empty_stack()
STACK *s = (STACK *)malloc(sizeof(STACK));
s->top = NULL;
s->n = 0; 



2.
push_stack(STACK *s,DATATYPE data)
{
    struct node *temp;

    temp = malloc();
    temp->data = data;

    temp->next = s->top;
    s->top = temp;
    s->n ++;

    return 0;
} 



判空
is_empty_stack
{
    //return s->top == NULL ? 1 : 0;
    return s->n == 0 ? 1 : 0;
    
} 




3.DATAYPE pop_stack(STACK *s)
{
    struct node *temp;
    DATATYPE data;

    temp = s->top;
    data = temp->data;

    s->top = temp->next;
    free(temp);

    s->n --;

    return data;
}

E.队列

循环队列数据结构设计

typedef struct
{
    DATATYPE buf[MAX];
    int front;
    int rear;
}QUEUE;

规定
队空:q->front == q->rear;

队满:(q->rear + 1) % MAX == q->front;

1.create_queue
q = malloc();
q->front = q->rear = 0;

2.入队
a.判满
q->buf[q->rear] = data;
b.
q->rear = (q->rear + 1) % MAX;

3.出队
a.判空

b.
data = q->buf[q->front];
q->front = (q->front + 1) % MAX;
return data;

链式队列

数据结构设计

struct node
{
    DATATYPE data;
    struct node *next;
}

typedef struct
{
    struct node *front;
    struct node *rear;
}QUEUE;

1.create_empty_queue()
a.分配链表的头
head = (struct node *)malloc(sizeof(struct node));
head->next = NULL;

b.分配队列的头
q = (QUEUE *)malloc(sizeof(QUEUE));
q->front = q->rear = head;

return q;

2.EnterQueue()

temp = (struct node *)malloc(sizeof(struct node));
temp->data = data;

q->rear->next = temp;
q->rear = temp;
temp->next = NULL;

3.DeleteQueue()
a.判空

b.struct node *temp;

  temp = q->front;
  q->front = temp->next;
  free(temp);

  return q->front->data;


二、非线性表

A.树(二叉树)

数据结构设计

typedef struct TreeNode
{
  DATATYPE data;
  struct TreeNode *lchild;
  struct TreeNode *rchild;
}BITREE;

1.创建完全二叉树

BITREE *create_binary_tree(int n)
{
    BITREE *root;

    root = space_node(1);
    push_stack(s,root);

    while(!is_empty_stack(s))
    {
        tree = pop_stack(s);
        if(2 * tree->data <= n)
        {
            tree->lchild = space_node(2 * tree->data);
            push_stack(tree->lchild);
        }

        if(2 * tree->data + 1 <= n)
        {
            tree->rchild = space_node(2 * tree->data + 1);
            push_stack(tree->rchild);
        }
    }

    return root;
}

//n = 1
BITREE *_create_binary_tree(int n)
{
    BITREE *root;

    root = space_node(n);

    if(2 * root->data <= N)
        root->lchid = _create_bianry_tree(2 * root->data);

    if(2 * root->data + 1<= N)
        root->rchid = _create_bianry_tree(2 * root->data + 1);

    return root;
}

2.遍历二叉树

a.前序遍历

PreOrder(BITREE *tree)
{
    if(tree == NULL)
        return;
    printf("%d ",tree->data);
    PreOrder(tree->lchild);
    PreOrder(tree->rchild);
}

b.层次遍历

NoOder(BITREE *tree)
{
    q = create_empty_queue();

    EnterQueue(q,tree);

    while(!is_empty_queue(q))
    {
        tree = DeleteQueue(q);
        printf("%d",tree->data);

        if(tree->lchild != NULL)
        {
            EnterQueue(q,tree->lchild);
        }

        if(tree->rchild != NULL)
        {
            EnterQueue(q,tree->rchild);
        }
    }
}

B.图

1.存储

a.邻接矩阵
typedef struct
{
    vtype v[N];
    int matrix[N][N];
}Graph

b.链接表
typedef struct node
{
    vtype v;
    struct node *next;
}LINKGRAPH;

2.遍历

a.深度遍历

DFS(Graph * G,int v)
{
    visited[v] = 1;
    printf("V%d",v);

    u = FirstAdj(G,v);

    while(u >= 0)
    {
        if(!visited[u])
            DFS(G,u);

        u = NextAdj(G,u,v);
    }
}

b.广度遍历
BFS(Graph *G,int v)
{
    visited[v] = 1;
    EnterQueue(G,v);

    while(!is_empty_queue(q))
    {
        v = DelelteQueue(q);
        printf("V%d ",v);

        u = FirstAdj(G,v);

        while(u >= 0)
        {
            if(!visited[u])
            {
                visited[u] = 1;
                EnterQueue(q,u);
            }

            u = NextAdj(G,u,v);
        }
    }
}

你可能感兴趣的:(数据结构复习总结)