鏈棧的主要操作

#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;
}



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