数据结构(3)—— 链表和二叉树

1 链表

对链式存储结构进行系列操作的代码—链表,为了简化链式存储结构的使用而提出和编写的数据结构。
带头结点的单链表是最简单的链表,单链表中每个节点都只有一个指针。

/*链表练习*/
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

typedef struct node{
    int num;
    struct node *p_next;
}node;
node head;//借用头结点是一个很妙的处理

//初始化
void init(){
    head.p_next = NULL;
}

//销毁
void destroy(){
    while (head.p_next){
        node *p_tmp = head.p_next;
        head.p_next = p_tmp->p_next;
        free(p_tmp);
        p_tmp = NULL;
    }
}

//在末尾扩展新节点
void append(int num){
    node *p_tmp = &head;
    node *p_node = (node *)malloc(sizeof(node));
    if (!p_node){
        return;
    }
    p_node->num = num;
    p_node->p_next = NULL;
    while (p_tmp->p_next){
        p_tmp = p_tmp->p_next;
    }
    p_tmp->p_next = p_node;
}

//删除第一个有效节点
void remove_head(){
    node *p_tmp = head.p_next;
    if (p_tmp){
        head.p_next = p_tmp->p_next;
        free(p_tmp);
        p_tmp = NULL;
    }
}

//获得有效节点个数
int get_size(){
    node *p_tmp = head.p_next;
    int size = 0;
    while (p_tmp){
        size++;
        p_tmp = p_tmp->p_next;
    }
    return size;
}

//根据编号获得有效数字
int get_number(int pos){
    node *p_tmp = head.p_next;
    int size = 0;
    while (p_tmp){
        size++;
        if (size == pos){
            return p_tmp->num;
        }
        p_tmp = p_tmp->p_next;
    }
    return -1;
}

//把一个新数字插入在指定的编号位置上
bool insert_at_pos(int pos,int num){
    node *p_tmp = &head;
    int loop = 0;
    for (;loop < pos - 1;loop++){
        p_tmp = p_tmp->p_next;
        if (!p_tmp){
            break;
        }
    }
    if (p_tmp){
        node *p_node = (node *)malloc(sizeof(node));
        p_node->num = num;
        p_node->p_next = p_tmp->p_next;
        p_tmp->p_next = p_node;
        return true;
    }
    else {
        return false;
    }
}

int main(){
    init();
    printf("个数是%d\n",get_size());
    append(1);
    append(2);
    append(3);
    insert_at_pos(3,4);
    printf("编号为2的数字是%d\n",get_number(2));
    printf("有效节点个数是%d\n",get_size());
    remove_head();
    printf("编号为2的数字是%d\n",get_number(2));
    printf("个数是%d\n",get_size());
    destroy();
    return 0;
}

  我有一次去深圳的一家小公司进行面试,然后人家给你提供了一台电脑,纯Linux环境,就一终端,一黑板的屏,要你写一个链表的操作,三个程序,一个头文件,一个实现文件,一个测试文件,然后再写一个Makefile来编译,最后再用gdb来进行调试,感觉还是一道不错的题。


2 二叉树

  树状逻辑结构通常采用链式存储方式实现。树状结构中每个数据使用一个结构体变量表示,两个数据之间的关系使用一个指针变量表示,每个指针变量记录在上一个数据的结构体变量中。

  两个直接相关的节点中,靠上的节点叫父节点,另外一个叫子节点,根节点是树中最靠上的节点。

二叉树中每个节点都最多有两个子节点。
二叉树的数据部分包括结构体声明和根指针声明。

二叉树操作的三种遍历方式:
  1、前序遍历(根节点,左子树,右子树)
  2、中序遍历(左子树,根节点,右子树)
  3、后序遍历(左子树,右子树,根节点)
对二叉树的操作分成三个部分:对根节点本身的操作、对节点左子树的操作、对节点右子树的操作,所以,对树的操作,递归思想很重要。

/*树练习*/
#include<stdio.h>
#include<stdlib.h>

typedef struct node{
    int num;
    struct node *p_left;
    struct node *p_right;
}node;
node *p_root;//根指针

//初始化函数
void init(node **pp_root){
    *pp_root = NULL;
}

//销毁函数
void destroy(node **pp_root){
    /*if (!*pp_root){
        return;
    }
    if ((*pp_root)->p_left){
        destroy(&((*pp_root)->p_left));
    }
    if ((*pp_root)->p_right){
        destroy(&((*pp_root)->p_right));
    }
    free(*pp_root);
    *pp_root = NULL;*/
    if (*pp_root){    //这个代码原理和上面一样,但更简洁
        destroy(&((*pp_root)->p_left));
        destroy(&((*pp_root)->p_right));
        free(*pp_root);
        *pp_root = NULL;
    }
}

//有序显示
void show(node *p_r){
    /*if (!p_r){
        return ;
    }
    if (p_r->p_left){
        show(p_r->p_left);
    }
    printf("%-5d",p_r->num);
    if (p_r->p_right){
        show(p_r->p_right);
    }*/
    if (p_r){  //这也是上面代码的简化
        show(p_r->p_left);
        printf("%-5d ",p_r->num);
        show(p_r->p_right);
    }
}

//查找合适的插入位置
node **find(node **pp_r,int num){
    if (!(*pp_r) || num == (*pp_r)->num){
        return pp_r;
    }
    else {
        if (num > (*pp_r)->num){
            return find(&((*pp_r)->p_right),num);
        }
        else {
            return find(&((*pp_r)->p_left),num);
        }
    }
}

//插入数据
void insert(node **pp_r,int num){
    node **pp_tmp = find(pp_r,num);
    if (!(*pp_tmp)){
        *pp_tmp = (node *)malloc(sizeof(node));
        (*pp_tmp)->num = num;
        (*pp_tmp)->p_left = NULL;
        (*pp_tmp)->p_right = NULL;
    }
}

//删除某个节点
void removenode(node **pp_root,int num){
    node **pp_tmp = find(pp_root,num);
    if (*pp_tmp){
        node *p_tmp = *pp_tmp;
        if (!(p_tmp->p_left)){
            *pp_tmp = p_tmp->p_right;
        }
        else if (!(p_tmp->p_right)){
            *pp_tmp = p_tmp->p_left;
        }
        else {
            node **pp_tmp1 = find(&(p_tmp->p_right),
                    p_tmp->p_left->num);
            *pp_tmp1 = p_tmp->p_left;
            *pp_tmp = p_tmp->p_right;
        }
        free(p_tmp);
        p_tmp = NULL;
    }
}

int main(){
    init(&p_root);
    insert(&p_root,50);
    insert(&p_root,30);
    insert(&p_root,20);
    insert(&p_root,70);
    insert(&p_root,80);
    insert(&p_root,60);
    insert(&p_root,90);
    insert(&p_root,100);
    removenode(&p_root,70);
    show(p_root);
    printf("\n");
    destroy(&p_root);
    return 0;
}
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值