第17章 经典数据结构类型

  1. 堆栈

堆栈接口提供三种基本的操作:push、pop 和 top。

Push:把一个新值压入到堆栈的顶部。

Pop: 只把顶部元素从堆栈中移除,它并不返回这个值。

Top: 返回顶部元素的值,但它并把顶部元素从堆栈中移除

 

(1)堆栈接口

#ifndef STACK_H

#define STACK_H

#include<stdlib.h>

#define STACK_TYPE int

//push 把一个新值压入到堆栈中,它的参数是需要被压入的值

void push( STACK_TYPE value);

//pop 从堆栈中弹出一个值,并将其丢弃

void pop();

//top 返回堆栈顶元素的值,但不对堆栈进行修改

STACK_TYPE top();

//is_empty 如果堆栈为空,返回TRUE,否则返回FALSE

int is_empty();

//is_full 如果堆栈已满,返回TRUE,否则返回FALSE

int is_full();

//create_stack 创建堆栈,参数指定堆栈可以保存多少个元素(动态才有)

void create_stack(size_t size);

//destroy_stack 销毁堆栈,它释放堆栈所使用的内存(动态才有)

void destroy_stack();

#endif

(2)数组堆栈(静态数组存储)

#include "stack.h"

#include <assert.h>

#define STACK_SIZE 100

//存储堆栈中值的数组和一个指向堆栈顶部元素的指针

static  STACK_TYPE  stack[STACK_SIZE];

static  int         top_element=-1;

void push( STACK_TYPE value )

{

assert ( ! is_full() ); //和if用法差不多,如assert参数为0,则终止

top_element += 1;

stack [top_element] = value;

}

void pop ()

{

assert ( !is_empty() );

top_elememt -= 1;

}

STACK_TYPE top ()

{

assert ( !is_empty() );

return stack[top_element];

}

int is_empty ()

{

return top_element == -1;

}

int is_full ()

{

return top_element == STACK_SIZE - 1;

}

(3)动态数组堆栈(动态数组存储)

#include "stack.h"

#include <stdio.h>

#include <stdlib.h>

#include <malloc.h>

#include <assert.h>

static  STACK_TYPE  *stack;

static  size_t      stack_size;

static  int         top_element = -1;

void create_stack ( size_t size )

{

assert ( stack_size == 0 );   //还没有分配内存空间

stack_size = size;

  stack = malloc ( stack_size * sizeof( STACK_TYPE ));  //分配内存空间

assert ( stack != NULL );

}

void destroy_stack ()

{

assert ( stack_size > 0 );

stack_size = 0;

free ( stack );

stack = NULL;

}

void push ( STACK_SIZE value )   //进栈

{

assert ( !is_full() );

top_element += 1;

stack [top_element] = value;

}

void pop ()    //移动顶端指针

{

assert ( !is_empty() );

top_element -= 1;

}

STACK_TYPE top ()     //出栈

{

assert ( !is_empty() );

return stack[top_element];

}

int is_empty ()

{

assert ( stack_size > 0 );     //有内存空间只是没有值

return top_element == -1;

}

int is_full ()

{

assert ( stack_size > 0 );

return top_element == stack_size -1;

}

(4)链式堆栈(链表存储)

#include "stack.h"

#include <stdio.h>

#include <stdlib.h>

#include <malloc.h>

#include <assert.h>

#define FALSE 0

typedef  struct  STACK_NODE {

STACK_TYPE  value;

struct  STACK_NODE  *next;

}StackNode;

static  StackNode  *stack;

void  create_stack ( size_t size )

{

}

void destroy_stack ()

{

while  ( !is_empty() );

pop ();

}

void push ( STACK_TYPE value)

{

StackNode  *new_node;

new_node = malloc (sizeof( StackNode ))

assert ( new_node ! = NULL );

new_node -> value = value;

new_node -> next = stack;

stack = new_node;

}

void pop ()    //顶部元素移除

{

StackNode  *first_node;

assert ( !is_empty() );

first_node = stack;

stack = first_node -> next;

free ( first_node );

}

STACK_TYPE top ()   //返回顶部元素值

{

assert ( !is_empty() );

return stack -> value;

}

int is_empty ()

{

return stack == NULL;

}

int is_full ()   //链式堆栈不会填满,所以is_full始终返回false

{

return FALSE;

}

2.队列

  (1)队列接口

#ifndef QUEUE_H

#define QUEUE_H

#include <stdlib.h>

#define QUEUE_TYPE

//create_queue 创建一个队列,参数指定队列可以存储的元素的最大数量(动态)

void create_queue ( size_t size );

//destroy_queue 销毁一个队列。

void destroy_queue ();

//insert 向队列添加一个新元素,参数就是要添加的元素

void insert ( QUEUE_TYPE value );

//delete 从队列中移除一个元素并将其丢弃

void delete ();

//first 返回队列中第一个元素的值,但不修改队列本身

QUEUE_TYPE first ();

//is_empty 如果队列为空,返回true,否则返回false

int is_empty ();

//is_full 如果队列已满,返回true,否则返回false

int is_full ();

#endif

(2)数组队列

#include "queue.h"

#include <stdio.h>

#include <assert.h>

#define  QUEUE_SIZE  100    //队列中元素最大数量

#define  ARRAY_SIZE  ( QUEUE_SIZE + 1) //数组长度

//用于存储队列元素的数组和指向队列头和尾的指针

static  QUEUE_TYPE  queue[ QUEUE_SIZE ];

static  size_t      front = 1;

static  size_t      rear = 0;

void  insert ( QUEUE_TYPE value )

{

assert ( !is_full() );

rear = ( rear + 1 ) % ARRAY_SIZE;

queue[rear] = value;

}

void  delete ()

{

assert ( !is_empty() );

front = ( front + 1 ) % ARRAY_SIZE;

}

QUEUE_TYPE  first ()

{

assert ( !is_empty() );

return queue[front];

}

int  is_empty ()

{

return  ( rear + 1 ) % ARRAY_SIZE == front;

}

int  is_full ()

{

return  ( rear + 2 ) % ARRAY_SIZE == front;

}

(3)动态数组队列

#include "queue.h"

#include <stdio.h>

#include <stdlib.h>

#include <malloc.h>

#include <assert.h>

static  QUEUE_TYPE  *queue;

static  size_t      queue_size;

static  size_t      array_size = queue_size + 1;

static  size_t      front = 1;

static  size_t      rear = 0;

void  create_queue ( size_t size )

{

assert ( queue_size == 0 );

queue_size = size;

queue = malloc ( queue_size * sizeof( QUEUE_TYPE ) );

assert ( queue != NULL );

}

void  destory_queue ()

{

assert ( queue_size > 0 );

queue_size == 0;

free ( queue );

queue == NULL;

}

void  insert ( QUEUE_TYPE value )

{

assert ( !is_full() );

rear = ( rear + 1 ) % array_size;

queue[rear] = value;

}

void  delete ()

{

assert ( !is_empty() );

front = ( front + 1 ) % array_size;

}

QUEUE_TYPE  first ()

{

assert ( !is_empty() );

return queue[front];

}

int  is_empty ()

{

assert ( queue_size > 0 );    //有内存没有存值

return  ( rear + 1 ) % ARRAY_SIZE == front;

}

int  is_full ()

{

assert ( queue_size > 0 );

return  ( rear + 2 ) % ARRAY_SIZE == front;

}

(4)链式队列

#include "queue.h"

#include <stdio.h>

#include <stdlib.h>

#include <malloc.h>

#include <assert.h>

#define  FALSE  0

typedef  struct  QUEUE_NODE {

QUEUE_TYPE  value;

struct  QUEUE_NODE  *next;

}QueueNode;

static  QueueNode  *front;

static  QueueNode  *rear;

void  destroy_queue ()

{

while ( !is_empty() );

delete ();

}

void  insert ( QUEUE_TYPE value )

{

QueueNode  *new_node;

new_node = ( QueueNode *) malloc ( sizeof( QueueNode ) );

assert ( new_node != NULL );

new_node -> value = value;

new_node -> next = NULL;

//把节点插到队列尾部

if ( rear == NULL )

front = new_node;

else

rear -> next = new_node;

rear = new_node;

}

void  delete ()

{

QueueNode  *first_node;

assert ( !is_empty() );

first_node = front -> next;

free ( front );

front = first_node;

if ( front == NULL )

rear = NULL;

}

QUEUE_TYPE  first ()

{

assert ( !is_empty() );

return  front -> value;

}

int  is_empty ()

{

return  front == NULL;

}

int  is_full ()

{

return  FALSE;

}

C和指针之学习笔记(6)的更多相关文章

  1. 02.C语言关于指针的学习笔记

    指针是一个特殊的变量,它里面存储的数值被解释成为内存里的一个地址. 要搞清一个指针需要搞清指针的四方面的内容: 指针的类型,指针所指向的 类型,指针的值或者叫指针所指向的内存区,还有指针本身所占据的内 ...

  2. 《征服c指针》学习笔记-----统计文本单词数目的程序word_count

    1.程序的要求:对用户指定的英文文本文件(包括标准输入),将英文单词按照字母顺序输出到用户指定的文本文件中(包括标准输出),并且在各单词后面显示单词的出现次数. 2.模块设计: 主要分为:1.从输入流 ...

  3. &lt;深入理解C指针&gt;学习笔记和总结 第四章 指针和数组

    数组是一个什么玩意: 数组和指针我的理解,有同样之处也有不同之处.因有同样之处,因此一些资料上说,数组和指针本质是同样的.因有不同之处,因此也有一些资料上说,数组和指针是不一样的. 同样之处: 数组名 ...

  4. C和指针之学习笔记(5)

    第10章 使用结构和指针 单链表 typedef struct NODE { struct NODE *link; int value; } Node; 插入到一个有序单链表: #include< ...

  5. C和指针之学习笔记(4)

    第9章 字符串 字符串的输入与输出 int  ch;  char strings[80];  FILE *input; (1)scanf(“%c”,&ch);   printf(“%c \n” ...

  6. C和指针之学习笔记(3)

    第8章 数组 1.数组与指针 数组名是一个个元素的地址. int  a[10];  int  b[10];  int  *c; (1) c = & a[0]; &a[0]表示一个指向数 ...

  7. C和指针之学习笔记(2)

    第6章 指针 1.在一组字符串中查找字符: #include<stdio.h> #include<assert.h> #include<stdlib.h> #def ...

  8. C和指针之学习笔记(1)

    第1章 1.输入字符串 while((ch=getchar())!=EOF  &&  ch!=’\n’) ; ch=getchar() while(ch!=EOF  && ...

  9. C语言学习笔记之成员数组和指针

    成员数组和指针是我们c语言中一个非常重要的知识点,记得以前在大学时老师一直要我们做这类的练习了,但是最的还是忘记了,今天来恶补一下.     单看这文章的标题,你可能会觉得好像没什么意思.你先别下这个 ...

随机推荐

  1. [转]C++中cin、cin.get()、cin.getline()、getline()函数的简单总结

    参考原文:http://www.cnblogs.com/flatfoosie/archive/2010/12/22/1914055.html,另外做了一些修改~ 1.cin 2.cin.get() 3 ...

  2. 【BZOJ】4316: 小C的独立集 静态仙人掌

    [题意]给定仙人掌图,求最大独立集(选择最大的点集使得点间无连边).n<=50000,m<=60000. [算法]DFS处理仙人掌图 [题解]参考:[BZOJ]1023: [SHOI200 ...

  3. VC调用易语言DLL

    易语言方面: .版本 .子程序 show, , 公开 ' 本名称子程序用作测试程序用,仅在开发及调试环境中有效,编译发布程序前将被系统自动清空,请将所有用作测试的临时代码放在本子程序中. ***注意不 ...

  4. 线程句柄和线程ID的区别

    ●CreateThread() API 用于创建线程. API 返回同时线程句柄,并通过参数得到线程标识符 (ID). 线程句柄有完全访问权创建线程对象. 运行线程时线程 ID 唯一标识线程在系统级别 ...

  5. 对RSA的认识

     #没有经过专业老师的指导,所以您在阅读本文时建议参考即可. 学习视屏:https://www.youtube.com/watch?v=TqX0AHHwRYQ   https://www.youtub ...

  6. wifi钓鱼 强势拿你的wifi密码

    钓鱼wifi 首先设一个场景!!! 如何得到一个免费的wifi  有人可能做过抓包跑包的方法或者跑pin码的方法然而这些方法可能会耗去你大量的时间(我曾经跑包花了一天的时间  跑pin码花了一晚上)感 ...

  7. scandir函数的研究【笔记】

    以下是本人的学习笔记,代码并非原创,均摘自官方源码,贴出来仅供学习记录用 scandir 的使用要注意内存泄漏的问题 scandir函数实现: vi ./uClibc-0.9.33.2/libc/mi ...

  8. Java线上应用故障排查之一:高CPU占用【转】

    近期java应用,CPU使用率一直很高,经常达到100%,通过以下步骤完美解决,分享一下. 方法一: 转载:http://www.linuxhot.com/java-cpu-used-high.htm ...

  9. 94.Binary Tree Inorder Traversal---二叉树中序非递归遍历

    题目链接 题目大意:中序遍历二叉树.先序见144,后序见145. 法一:DFS,没啥说的,就是模板DFS.代码如下(耗时1ms): public List<Integer> inorder ...

  10. [会装]Hive安装(基于mysql数据库)

    环境信息:Mac 安装步骤: 1. 下载hive组件(我选择的是社区的2.0.1版本) http://apache.mirror.globo.tech/hive/hive-2.0.1/ 2. 下载my ...