数据结构栈和队列的数组实现和链表实现的4个头文件(完全个人思路)
来源:互联网 发布:linux mysql 启动失败 编辑:程序博客网 时间:2024/05/26 02:55
最近写了4个头文件,并且在ubuntu10.10下用测试函数测试成功,记录如下
//数组栈头文件a_stack.h
#define STACK_SIZE 100
#define STACK_TYPE int
typedef struct{
STACK_TYPE data[STACK_SIZE];
int top_element;
}*stack,st;
stack s;
void initstack( stack s );
int push( stack s, STACK_TYPE value );
int pop( stack s );
STACK_TYPE top( stack s );
int is_empty( stack s );
int is_full( stack s );
void initstack( stack s ){
s = malloc(sizeof(stack));
s->top_element = -1;
}
int push( stack s, STACK_TYPE value ){
if(!is_full(s)){
s->top_element++;
s->data[s->top_element] = value;
}else
return 0;
return 1;
}
int pop( stack s ){
if(!is_empty(s)){
s->top_element --;
}else{
return 0;
}
return 1;
}
STACK_TYPE top( stack s ){
return s->data[s->top_element];
}
int is_empty( stack s ){
return s->top_element == -1;
}
int is_full( stack s ){
return s->top_element == STACK_SIZE - 1;
}
//链表栈l_stack.h
#define STACK_TYPE int
typedef struct node{
STACK_TYPE element;
struct node *next;
struct node *pior;
}*node;
typedef struct ls{
node stack;
node top;
}*stack;
stack s;
void initstack( stack s );
int push( stack s, STACK_TYPE value );
int pop( stack s );
STACK_TYPE top( stack s );
void initstack( stack s ){
s = malloc(sizeof(struct ls));
s->top = NULL;
}
int push( stack s, STACK_TYPE value ){
node n = malloc(sizeof(struct ls));
n->element = value;
if(s->top == NULL)
s->top = n;
else{
s->top->next = n;
n->pior = s->top;
s->top = s->top->next;
}
return 1;
}
int pop( stack s ){
node t = s->top;
s->top = s->top->pior;
free(t);
return 1;
}
STACK_TYPE top( stack s ){
return s->top->element;
}
//数组队列a_queue.h
#define QUEUE_TYPE int
#define QUEUE_SIZE 100
typedef struct{
QUEUE_TYPE queue[QUEUE_SIZE];
int rear;
int front;
}*queue, qu;
queue q;
void initqueue( queue q );
int enqueue( queue q, QUEUE_TYPE value );
int outqueue( queue q );
int is_emptyq( queue q );
int is_fullq( queue q );
void initqueue( queue q ){
q = malloc(sizeof(queue));
q->rear = q->front = -1;
}
int enqueue( queue q, QUEUE_TYPE value ){
if(!is_fullq(q)){
q->rear = (q->rear + 1)%QUEUE_SIZE;
q->queue[q->rear] = value;
}else{
return 0;
}
return 1;
}
int outqueue( queue q ){
if(!is_emptyq(q)){
q->front = (q->front + 1)%QUEUE_SIZE;
}else{
return 0;
}
return 1;
}
int is_fullq( queue q ){
return (q->rear - q->front + QUEUE_SIZE)%QUEUE_SIZE == QUEUE_SIZE;
}
int is_emptyq( queue q ){
return q->rear == q->front;
}
//链表队列 l_queue.h
#define STACK_TYPE int
typedef struct node{
STACK_TYPE element;
struct node *next;
struct node *pior;
}*node;
typedef struct ls{
node stack;
node top;
}*stack;
stack s;
void initstack( stack s );
int push( stack s, STACK_TYPE value );
int pop( stack s );
STACK_TYPE top( stack s );
void initstack( stack s ){
s = malloc(sizeof(struct ls));
s->top = NULL;
}
int push( stack s, STACK_TYPE value ){
node n = malloc(sizeof(struct ls));
n->element = value;
if(s->top == NULL)
s->top = n;
else{
s->top->next = n;
n->pior = s->top;
s->top = s->top->next;
}
return 1;
}
int pop( stack s ){
node t = s->top;
s->top = s->top->pior;
free(t);
return 1;
}
STACK_TYPE top( stack s ){
return s->top->element;
}
#define STACK_TYPE int
typedef struct node{
STACK_TYPE element;
struct node *next;
struct node *pior;
}*node;
typedef struct ls{
node stack;
node top;
}*stack;
stack s;
void initstack( stack s );
int push( stack s, STACK_TYPE value );
int pop( stack s );
STACK_TYPE top( stack s );
void initstack( stack s ){
s = malloc(sizeof(struct ls));
s->top = NULL;
}
int push( stack s, STACK_TYPE value ){
node n = malloc(sizeof(struct ls));
n->element = value;
if(s->top == NULL)
s->top = n;
else{
s->top->next = n;
n->pior = s->top;
s->top = s->top->next;
}
return 1;
}
int pop( stack s ){
node t = s->top;
s->top = s->top->pior;
free(t);
return 1;
}
STACK_TYPE top( stack s ){
return s->top->element;
}
在这过程中出现一个问题描述如下:
下面是一个编译没有问题的C程序,但是执行的时候显示段错误:
#include<malloc.h>
#include<stdio.h>
typedef struct{
int a[10];
}snode;
snode *s;
void init(snode *s){
s = malloc(sizeof(snode));
}
void push(snode *s, int a){
s->a[0] = a;
}
int main(){
init(s);
push(s,20);
printf("OK/n");
return 0;
}
但是只要作一个小小的修改,问题立即就得到了解决:
将main()中的"init(s)”用“s = malloc(sizeof(snode))”代替:
#include<malloc.h>
#include<stdio.h>
typedef struct{
int a[10];
}snode;
snode *s;
void init(snode *s){
s = malloc(sizeof(snode));
}
void push(snode *s, int a){
s->a[0] = a;
}
int main(){
//init(s);
s = malloc(sizeof(snode));
push(s,20);
printf("OK/n");
return 0;
}
说明:这里的4个头文件,只是初稿,还有很多需要简化的地方,并且可能有BUG存在,以后将会进一步修改。
- 数据结构栈和队列的数组实现和链表实现的4个头文件(完全个人思路)
- 数据结构---队列的实现和个人分析
- 数据结构和算法--队列的数组实现
- 栈和队列的基本实现(数组实现和链表实现)
- 学会一种数据结构二:队列的数组实现和链表实现
- 队列的数组和链表实现
- 数据结构----栈的实现和个人分析
- (第4讲)栈和队列的数组实现
- 栈和队列的底层数组实现
- 【数据结构】队列-数组的实现
- 数据结构和算法--栈的数组实现
- 队列的链式表示和实现(数据结构)
- 循环队列的表示和实现(数据结构)
- 数据结构:队列的概念和实现
- 数据结构和算法--队列的链式实现
- 基于数组和链表的队列实现
- FIFO 队列的链表和数组实现
- 队列的两种实现:数组和链表
- switch语句的应用
- for循环中的if嵌套
- Java或web中解决所有路径问题
- yield(),sleep()以及wait()的区别
- [ZT]Linux大骂Google安卓系统欺骗软件开发者欺骗开源界
- 数据结构栈和队列的数组实现和链表实现的4个头文件(完全个人思路)
- vb.net 视频前了解
- 严重: Exception starting filter struts2 Unable to load configuration. - [unknown location] at org.apache.struts2.dispatche
- vs2005下ActiveX控件学习笔记(一)
- usbhid中所需要的函数
- Red Hat Enterprise Linux 5.4关于中文乱码的解决方法
- 成员方法的反射
- 指针比较相关问题.
- JS 规范问题(值得注意)