数据结构复习总结
2016-04-17 11:53
274 查看
数据结构复习总结
一、线性表
A.顺序表
数据结构设计
1.插入元素(seq)
2.按位置插入(seq,post,data)
3.删除一个指定的元素(seq,key)
B.链表
数据结构的设计
1.创建
2.头插法
3.尾插法
思想:遍历到链表的尾部
//1 4 6 7
4.按序插入
//1 2 4 8 9
5.删除一个指定的元素
6.逆置一个链表
7.链表的遍历
C.顺序栈
数据结构设计
1.
3.
数据结构设计
1.
2.
判空
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
;
int matrix
;
}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);
}
}
}
一、线性表
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
;
int matrix
;
}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);
}
}
}
相关文章推荐
- 20145205 java语言实现数据结构实验一
- redis之数据结构探究
- 树状数组求逆序数
- MySQL索引背后的数据结构及算法原理
- 重学数据结构系列之——图论算法之Dijkstra算法
- 重学数据结构系列之——图论算法之Prim算法
- 数据结构_对称矩阵
- 数据结构之并查集
- 数据结构和算法13 之快速排序
- 数据结构笔记之线性表
- 【数据结构】广义表的默认成员函数、深度、大小、打印
- 栈操作,构造,插入,删除,转换
- 数组与链表的异同(数据结构)
- 数据结构之链表
- Linux内核数据结构 kfifo
- 数据结构和算法12 之希尔排序
- 堆栈操作合法性
- [算法导论笔记]建立二叉堆
- 数据结构(c)——线性表:顺序表和链式表
- 数据结构学习笔记(八)--最短路径