1、准备知识
1.1 写通用的 链表函数库

typedef int INT32;

软件设计的要求:

1、将软件分为2层:
应用层----main.cpp            ----高层 模块
链表层----list.h  list.cpp    ----底层 模块

2、封装
即:链表模块 通过list.h,对外公开一些数据类型、函数,则调用者(如main.cpp文件)只能调用这些 数据类型和函数;而list.cpp中,还有一些函数可能
是链表模块自己调用的,则不需要公开它们的函数原型,这样main.cpp就无法调用它们了----相当于被隐藏了。

//main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
struct Student
{
    int number;
    char *name;
};

void print_student(void *data)
{
    struct Student *p=(struct Student *)data;
    printf("number is %d\tname is %s\n",p->number,p->name);
    return;
}

int main()
{
    struct Student stu[3]={1001,"zhangsan",1002,"lisi",1003,"wangwu"};
    struct Node *head=link_create();
    link_add(&head,(void *)&stu[0]);
    link_add(&head,(void *)&stu[1]);
    link_add(&head,(void *)&stu[2]);
    link_foreach(head,print_student);

return 0;
}

//list.h
struct Node;

typedef void (*pfun)(void *data);     //将函数原型中的 函数 括起来,再加*-->函数指针变量;前面加typedef则变量-->类型
struct Node * link_create();
void link_add(struct Node **head,void *data);
void link_foreach(struct Node *head, pfun print_student);            //pfun函数指针类型

//list.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "list.h"

struct Node
{
    void *data;
    struct Node *next;
};

struct Node * link_create()
{
    struct Node * head=NULL;

return head;
}

void link_add(struct Node **head,void *data)
{
     struct Node *p=(struct Node *)malloc(sizeof(struct Node));
     struct Node *p2;
     p->data=data;
     p->next=NULL;
     if(*head==NULL)
     {
         *head=p;
     }
     else
     {
        p2=*head;
         while(p2->next!=NULL)
        {
            p2=p2->next;
        }
         p2->next=p;
     }
    return;
}

void link_foreach(struct Node *head, pfun print_student)
{
    struct Node *p=head;
    if(head==NULL)
    {
        return;
    }
    else
    {
        while(p!=NULL)
        {
            //printf("%d  %s\n",p->number,p->name);
            print_student(p->data);
            p=p->next;
        }

}
    return;
}

函数指针:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef int (*pfun)(int a,int b);    //函数原型(pfun就是函数名)------>括起来加*(函数指针 变量)--------->加typedef(函数指针 类型)

int sum_bmd(int a,int b)
{
    return a+b;
}

int main()
{
    int a=10;
    int b=20;

pfun m;
    m=sum_bmd;

int result=m(a,b);
    printf("%d\n",result);

//typedef int INT32;

return 0;
}

函数指针使用原则1:尽量不用;迫不得已才用

队列:
1 概念
队列
公交车站排队(像链表)------------加了护栏--------->队列

链表的操作:建立空链表  追加节点  删除第1个节点   求链表长度    返回头节点的值   销毁链表   前插节点   中间插节点  删除中间节点
队列的操作:建立空队列  进队      出队            求队列长度    返回队首里的值   销毁队列


1 概念
"abcdefgh"-----》反过来输出   ------ 用栈

链表的操作:建立空链表  前插节点  删除第1个节点   求链表长度    返回头节点的值   销毁链表   前插节点   中间插节点  删除中间节点
栈的操作:  建立空栈    进栈      出栈            求栈长度      返回栈顶里的值   销毁栈

队列代码:
//main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "queue.h"
struct Student
{
    int number;
    char *name;
};

int main()
{
    struct Student stu[3]={1001,"zhangsan",1002,"lisi",1003,"wangwu"};
    struct Node*head=queue_create();
    queue_push(&head,(void *)&stu[0]);
    queue_push(&head,(void *)&stu[1]);
    queue_push(&head,(void *)&stu[2]);
    void *data=queue_top(head);
    struct Student *stuu=(struct Student *)data;
    printf("%d   %s\n",stuu->number,stuu->name);

return 0;
}

//queue.h
#include "list.h"
struct Node* queue_create();
void queue_push(struct Node**head,void *data);
void *queue_top(struct Node *head);

//queue.cpp
#include "queue.h"

struct Node* queue_create()
{
    return link_create();
}

void queue_push(struct Node**head,void *data)
{
    link_add(head,data);
    return;
}

void *queue_top(struct Node *head)
{
    return link_top(head);
}

//list.h
struct Node;

typedef void (*pfun)(void *data);     //将函数原型中的 函数 括起来,再加*-->函数指针变量;前面加typedef则变量-->类型

struct Node * link_create();
void link_add(struct Node **head,void *data);
void link_foreach(struct Node *head, pfun print_student);            //pfun函数指针类型

void *link_top(struct Node *head);

//list.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"

struct Node
{
    void *data;
    struct Node *next;
};

struct Node * link_create()
{
    struct Node * head=NULL;

return head;
}

void link_add(struct Node **head,void *data)
{
     struct Node *p=(struct Node *)malloc(sizeof(struct Node));
     struct Node *p2;
     p->data=data;
     p->next=NULL;
     if(*head==NULL)
     {
         *head=p;
     }
     else
     {
        p2=*head;
         while(p2->next!=NULL)
        {
            p2=p2->next;
        }
         p2->next=p;
     }
    return;
}

void link_foreach(struct Node *head, pfun print_student)
{
    struct Node *p=head;
    if(head==NULL)
    {
        return;
    }
    else
    {
        while(p!=NULL)
        {
            //printf("%d  %s\n",p->number,p->name);
            print_student(p->data);
            p=p->next;
        }

}
    return;
}

void *link_top(struct Node *head)
{
    if(head==NULL)
    {
        return NULL;
    }
    else
    {
        return head->data;
    }
}


1 概念
"abcdefgh"-----》反过来输出   ------ 用栈

链表的操作:建立空链表  前插节点  删除第1个节点   求链表长度    返回头节点的值   销毁链表   前插节点   中间插节点  删除中间节点
栈的操作:  建立空栈    进栈      出栈            求栈长度      返回栈顶里的值   销毁栈

栈操作的代码:----不全,只有main函数里的调用。以后,需要增加stack.h和stack.cpp进行完善
//main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stack.h"
struct Student
{
    int number;
    char *name;
};

int main()
{
    struct Student stu[3]={1001,"zhangsan",1002,"lisi",1003,"wangwu"};
    struct Node *head=stack_create();
    stack_push(&head,(void *)&stu[0]);
    stack_push(&head,(void *)&stu[1]);
    stack_push(&head,(void *)&stu[2]);
    void *data=stack_top(head);
    struct Student *stuu=(struct Student *)data;
    printf("%d   %s\n",stuu->number,stuu->name);

return 0;
}

数据结构和算法之基于链表的栈和队列、多文件和模块分层设计、函数指针相关推荐

  1. 学习JavaScript数据结构与算法(一):栈与队列

    本系列的第一篇文章: 学习JavaScript数据结构与算法(一),栈与队列 第二篇文章:学习JavaScript数据结构与算法(二):链表 第三篇文章:学习JavaScript数据结构与算法(三): ...

  2. python中栈的描述是_数据结构与算法:Python语言描述 栈和队列.ppt

    数据结构与算法:Python语言描述 栈和队列 迷宫问题 迷宫问题的特点: 存在一集可能位置,一些位置相互连通,一步可达 一个位置可能连通若干位置,出现向前探查的多种可能(有分支) 目标是找到一条路径 ...

  3. Java数据结构与算法(第四章栈和队列)

    2019独角兽企业重金招聘Python工程师标准>>> 本章涉及的三种数据存储类型:栈.队列和优先级队列. 不同类型的结构 程序员的工具 数组是已经介绍过的数据存储结构,和其他结构( ...

  4. 两顺序栈共享Java_数据结构与算法(三),栈与队列

    上一篇<数据结构与算法(二),线性表>中介绍了数据结构中线性表的两种不同实现--顺序表与链表.这一篇主要介绍线性表中比较特殊的两种数据结构--栈与队列.首先必须明确一点,栈和队列都是线性表 ...

  5. 数据结构与算法(C语言) | 栈和队列——栈(自己做过测试)

    栈是一种重要的线性结构,通常称,栈和队列是限定插入和删除只能在表的"端点"进行的线性表.(后进先出) –栈的元素必须"后进先出". –栈的操作只能在这个线性表的 ...

  6. java数据结构与算法之双链表设计与实现

    转载请注明出处(万分感谢!): http://blog.csdn.net/javazejian/article/details/53047590 出自[zejian的博客] 关联文章: java数据结 ...

  7. 算法章节 数组、链表、栈、队列

    数组 概念与特性 1,数组是线性表,用一组连续的内存空间存储⼀组具有相同类型的数据 2,最大的特性是⽀持按照下标O(1)时间复杂度内快速访问数组元素 3,⼀维数组寻址公式:a[i]_addr = ba ...

  8. DSt:数据结构的最强学习路线之数据结构知识讲解与刷题平台、刷题集合、问题为导向的十大类刷题算法(数组和字符串、栈和队列、二叉树、堆实现、图、哈希表、排序和搜索、动态规划/回溯法/递归/贪心/分治)总

    DSt:数据结构的最强学习路线之数据结构知识讲解与刷题平台.刷题集合.问题为导向的十大类刷题算法(数组和字符串.栈和队列.二叉树.堆实现.图.哈希表.排序和搜索.动态规划/回溯法/递归/贪心/分治)总 ...

  9. 数据结构与算法:用链表实现无序列表。

    目录 1.无序列表的介绍 2.开始构建无序列表 2.1Node类(节点) 2.2UnorderList类(无序列表) 2.2.1isEmpty()方法 2.2.2add()方法 2.2.3length ...

最新文章

  1. Lync Server外部访问系列PART3:准备反向代理
  2. 计算机网络管理云红艳电子版,计算机网络管理pdf
  3. python【力扣LeetCode算法题库】13- 罗马数字转整数
  4. python小游戏源码-python 像素小鸟小游戏源码(flappybird)
  5. php获取curl头_php中CURL请求头和响应头获取方法
  6. 学习编程的基础四大件
  7. Qt undefined reference to,Error 255,找不到库函数的解决办法
  8. 记录一次SQL优化,增加索引,随便写的当笔记了
  9. 网页设计 html鼠标悬停,利用css3实现的简单的鼠标悬停按钮
  10. C++:类占用的字节内存
  11. Docker学习文档之一 安装软件-Windows环境
  12. RemapKey等:小巧实用的键盘映射工具
  13. 2020年度美国最受市场欢迎的十大编程语言
  14. Jetson tx2记录422测试笔记和wifi信号测试笔记
  15. 一个人内心强大的4个迹象
  16. Ubuntu连接WIFI并开启热点
  17. 电商APP的流量及用户运营分析
  18. 长龙航空软件测试招聘,长龙航空空乘面试经验
  19. ebc是什么意思_ebc是什么意思?金蝶软件可靠吗?
  20. 算法——暴力之美(volence‘s beautify of algorithm)

热门文章

  1. 数据结构与算法--字符串匹配算法
  2. 最好的爱情,是彼此觉得高攀了对方
  3. 3分钟搞定微信小程序类美团用户商家距离计算
  4. 有没有完全自主的国产化数据库技术?
  5. Docker + Nginx 部署Vue项目
  6. Kindeditor(版本号4.0.5)编辑器添加上传flv视频功能
  7. 【eNSP 华为模拟器】常用的使用技巧与配置命令,提高你的配置速度
  8. 华为的狼性——8年女硕离职实录:我为什么离开?
  9. canvas笔记-设置底纹(createPattern相关)
  10. SQL Server 2016 用户登录错误18456的三种解决方法