链栈的主要操作

#ifndef _ERROR_H    //error.h 头文件
#define _ERROR_H

#include <stdio.h>

#define ERROR -1
#define FULL_ERROR -2
#define EMPTY_ERROR -3
#define MALLOC_ERROR -4

int errno;   //错误号
void myerror(char *str);

char *mystrerror(int num);


#endif


//error.c文件
#include "error.h"

void myerror(char * str)
{
	switch(errno)
	{
		case -1:
			printf("%s:输入的参数错误\n",str);
			break;
			
		case FULL_ERROR:
			printf("%s:满栈状态\n",str);
			break;
		case EMPTY_ERROR:
		
			printf("%s:空栈状态\n",str);
			break;
		case MALLOC_ERROR:
			printf("%s: 创建失败\n",str);
			break;
	}
	
}


char *mystrerror(int num)
{
	switch (errno)
	{
		case ERROR:
			return "输入参数错误";
		case FULL_ERROR:
			return "满栈状态";
		case EMPTY_ERROR:
			return "空栈状态";
		case MALLOC_ERROR:
			printf("s: 创建失败");
			break;
	}
}

//linkstack.h链栈头文件

#ifndef _linkstack_h_
#define _linkstack_h_

#include <stdio.h>
#include "error.h"

#define true 1
#define false 0

typedef int stackdata;
typedef struct _node
{
	stackdata data;
	struct _node *next;
}Node;


typedef struct _linkstack
{
	Node *top;
}linkstack;

linkstack * create_stack();  // 创建栈

int stackempty(linkstack *s);  //是否空栈

int push(linkstack *s,stackdata x);//进栈

int pop(linkstack *s,stackdata *x);//出栈

int get_top(linkstack *s,stackdata *x);//获取栈顶元素

int destroy(linkstack *s);//销毁链栈





#endif


//linkstack.c源代码

#include <stdlib.h>
#include "linkstack.h"

linkstack * create_stack() //创建栈
{
	linkstack *s = (linkstack *)malloc(sizeof(linkstack) / sizeof(char));
	if(s == NULL)
	{
		errno = MALLOC_ERROR;
		return NULL;
	}
	
	s->top = NULL;
	
	return s;
}

int stackempty (linkstack *s)  //是否空栈
{
	if(s == NULL)
	{
		errno = ERROR;
		return false;
	}
	return s->top == NULL;
}

int push(linkstack *s,stackdata x)//进栈
{
	if(s == NULL)
	{
		errno = ERROR;
		return false;
	}
	
	Node *node = (Node *)malloc(sizeof(Node) / sizeof(char));
	
	if(node == NULL)
	{
		errno = MALLOC_ERROR;
		return false;
	}
	
	node->data = x;
	node->next = s->top;
	s->top = node;
	
	return true;
}


int pop(linkstack *s,stackdata *x)//出栈
{
	if(s == NULL)
	{
		errno = ERROR;
		return false;
	}
	if(stackempty(s))
	{
		errno = EMPTY_ERROR;
		return false;
	}
	
	Node *p = s->top;
	*x = p->data;
	
	s->top = p->next;
	free(p);
	
	return true;	
}

int get_top(linkstack *s,stackdata *x)//获取栈顶元素
{
	if (s == NULL)
	{
		errno = ERROR;
		return false;
	}
	
	if (stackempty(s))
	{
		errno = EMPTY_ERROR;
		return false;
	}
	
	*x = s->top->data;
	
	return true;
}

int destroy(linkstack *s)//销毁链栈
{
	
	if (s == NULL)
	{
		errno = ERROR;
		return false;
	}
	int x;
	while(stackempty(s) != true) //不是空栈继续出栈
	{
		pop(s,&x);
	}
	free(s);
	
	return true;
}

//主函数main.c

#include <stdio.h>
#include "linkstack.h"

int main()
{
	linkstack *s = create_stack();
	if(s == NULL)
	{
		return -1;
	}
	
	int i;
	int x;
	
	for(i = 0;i < 10;i++)
	{
		push(s,i);
	}

	char str[100];
	for(i = 1;i < 15;i++)
	{
		if(pop (s,&x) != true)
		{	
			sprintf(str,"pop第%d个元素",i);
			myerror(str);
		}
		printf("x: %d\n",x);
	}
	
	if(stackempty(s))
	{
		printf("空栈\n");
	}
	
	
	if(pop(s,&x) != true)
	{
		myerror("pop错误");
	}

	if(destroy(s) != true)
	{
		myerror("destroy");
	}
	
	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章