C语言面向对象——堆栈的链式存储
来源:互联网 发布:简易三维画图软件 编辑:程序博客网 时间:2024/05/19 03:46
使用函数指针,把函数的调用封装为对象方法。
//stack.h
#ifndef _STACK_H_#define _STACK_H_#include <stdio.h>#include <stdlib.h>typedef struct _node Node;typedef struct _stack Stack;void StackInit(Stack*);Node* StackPush(Stack*, int);void StackPop(Stack*);void StackPrint(Stack*);struct _node {int value;Node* next;};struct _stack {Node* Head;Node*(*Push)(Stack*, int);void(*Pop)(Stack*);void(*Print)(Stack*);};#endif
//stack.c
#include "stack.h"void StackInit(Stack* s) //初始化{s->Head = NULL;s->Push = StackPush;s->Pop = StackPop;s->Print = StackPrint;}Node* StackPush(Stack* s, int n) //入栈{printf("Push %d:\n", n);Node* node = (Node*)malloc(sizeof(Node));node->value = n;if (NULL == s->Head) //链表空{node->next = NULL;}else{node->next = s->Head;}s->Head = node;return s->Head;}void StackPop(Stack* s) //出栈{printf("Pop:\n");if (NULL == s->Head){printf("Stack in empty.\n");}else{Node* t = s->Head;s->Head = t->next;free(t);}}void StackPrint(Stack* s) //输出{Node* t;printf("Head ->");for (t = s->Head; t; t = t->next){printf(" %d ->", t->value);}printf(" NULL\n");}
//main.c
#include "stack.h"int main(){Stack stack; //创建一个Stack类的stack对象 StackInit(&stack);//入栈int n;do {printf("\nPush n:(input -1 to stop)\n");scanf_s("%d", &n);if (-1 != n){stack.Push(&stack, n);stack.Print(&stack);}} while (-1 != n);char c;c = getchar(); //读取缓冲区中多余的\n//出栈do {printf("\nPop:(input 1 to stop)\n");scanf_s("%c", &c);if ('1' != c){stack.Pop(&stack);stack.Print(&stack);}} while ('1' != c);c = getchar();return 0;}
0 0
- C语言面向对象——堆栈的链式存储
- c 堆栈的链式存储
- 链式堆栈——C语言实现
- 数据结构C语言实现系列——链式堆栈
- 堆栈的链式存储实现
- C语言栈的链式存储结构
- 栈的链式存储实现c语言
- 队列的链式存储实现c语言
- C 堆栈的链式表示
- 单链表的应用——多项式加法的C语言实现(链式存储结构)
- c语言链式存储实现
- 数据结构之堆栈的链式存储
- 数据结构:堆栈的链式存储实现
- 堆栈的链式存储(链表)
- 堆栈的相关操作_链式存储
- 2.2.3 堆栈的链式存储实现
- 设计模式——C语言的面向对象
- C语言的面向对象
- 在群晖 DS213j下安装ipkg,aria2
- QQ粘虫密码破解器ASP后台稳定收信免杀版
- php从入门到放弃.Day2
- 线程篇(一):Java中的线程(基础)
- 使用maven构建web项目实例
- C语言面向对象——堆栈的链式存储
- js 三大家族(offset/scroll/client)
- unity-单件模式的使用
- android数据库sqlite常用命令
- Android-WebView加载网页
- 友盟集成中“找不到符号”问题的解决
- 公开图标库
- IT--linux-时间-date
- Linux基础知识学习:linux用命令重启