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

第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;

}

时间: 2024-08-27 09:34:29

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

【C++】--关于指针的学习笔记

1.指针的定义 指针(pointer)是指向另外一种类型的复合类型,与引用类似,指针也实现了对其他对象的间接访问. 与引用不同的几点: 1.指针本身就是一个对象,允许对指针赋值和拷贝,而且在指针的生命周期内它可以先后指向几个不同的对象. 2.指针不一定要在定义时赋初值. note:在块作用域内定义的指针如果没有被初始化,也将拥有一个不确定的值. 3. ...(待之后的学习实践中补充) 2.定义指针类型的方法 定义指针类型的方法是在变量名前面添加[*]星号,即将声明符写成[*d]的形式,d是变量名

《C和指针》学习笔记(1)

最近C语言已经学完,布置的大作业:学生管理系统5个版本也完成了.但是又买了一本<C和指针>,主要是感觉自己的指针还是没有完全熟悉.所以还是要好好研究一下.闲话不多说,直接第一章.一看是快速入门,以为很简单,但那个程序就把我卡了半天才看懂,按照作者说的的确运用了C语言中的大部分技巧. 程序1.1:首先读取一串列标号,这些列标号成对出现,便是输入行的列范围.这串列标号以一个负值结尾,作为结束标志.剩余的输入行被程序读入并打印,然后输入行中被选中范围的字符串被提取出来打印. 书中代码如下: #inc

智能指针_auto_ptr2_学习笔记

//1,release函数只是简单的转移对内存的拥有权,自己变成null,不会delete,一般在将所有权转移给别的智能指针的时候使用.具体可参加源码. 例: #include <memory> #include <iostream> using namespace std; class A { public:  A(int a):m_na(a)  {      cout<<"A cont" <<endl;  }  virtual ~A(

《C和指针》学习笔记(4)

进入<pointer on C>的第二章,不过感觉这一章没讲什么实质性的内容,主要是一些风格.字符以及一些古老的东西(比如三字母词)自己也不是很感兴趣.不过也算是了解一下C的历史了吧.不过问题和程序还是好好看了看. 比如又知道了一些转义字符: \?在书写多个分号的情况下使用,防止被解释为三字母词. \”用于表示一个字符串常量内部的双引号. \’用于表示字符常量. \\用于表示一个\ 写了一个例子: #include <stdio.h> #include <stdlib.h&g

《C和指针》学习笔记(2)

1.1  "hello world"程序不说了,codeblocks直接生成. 1.2   题目:从标准输入读取几行输入.每行输入都要打印到标准输出上,前面要加上行号.让程序能够处理的输入行没有长度限制. /*我的程序*//* int main() { int ch; int line=1; while((ch=getchar())!=EOF) { printf("%d",line); if(ch!='\n') printf("%c",ch);

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

第8章 数组 1.数组与指针 数组名是一个指针常量,也就是数组第1个元素的地址. int  a[10];  int  b[10];  int  *c; (1) c = & a[0]; &a[0]表示一个指向数组第1个元素的指针. (2) c=a; 与 c = & a[0]; 等价 (3) b = a; 非法,不能使用赋值符把一个数组的所有元素复制到另一个数组,必须使用一个循环,每次复制一个元素. (4) a = c; 非法,a是指针常量,而c是指针变量. 2. 下标引用和间接访问完

《深入理解C指针》学习笔记(1)--- 指针之外

C语言从诞生之初就非常善于和硬件打交道,经过这么多年的发展之后,其灵活性和超强的特征是受到几乎所有程序员的肯定.C语言的这种灵活性很大一部分程度来源与C指针,指针为C语言动态操控内存提供了支持,同时也便于访问硬件.由于编程的本质就是操控数据,而数据大多都在内存中,理解C管理内存的工作原理,就显得尤为重要了.知道malloc()函数能够从堆上申请内存,理解内存分配的本质则是另外的事. 请看代码例子: 1 #include <stdio.h> 2 #include <stdlib.h>

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

第9章 字符串 字符串的输入与输出 int  ch;  char strings[80];  FILE *input; (1)scanf(“%c”,&ch);   printf(“%c \n”,ch);    (输入字符串时自动添加’\0’) (2)ch = getchar();   putchar(ch); (3)gets( strings );    puts( strings );      (输入字符串时自动添加’\0’) (4)fgets( strings, 80, stdin);  

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

第10章 使用结构和指针 单链表 typedef struct NODE { struct NODE *link; int value; } Node; 插入到一个有序单链表: #include<stdio.h> #include<stdlib.h> #include "sll_node.h" #define FALSE 0 #define TRUE 1 int sll_insert( Node **linkp, int new_value)  //指针的指针