數據結構之棧(順序棧和鏈式棧)

棧是隻允許在一端插入和刪除的線性表,只允許插入和刪除的一端稱爲棧頂,另一端稱爲棧底。棧遵循先進後出的規則。實現棧可以使用順序表,也可以使用鏈表。接下來是關於這兩種實現方式的一些簡單操作的函數。

順序棧:

頭文件:

SqStack.h

#ifndef __SQSTACK_H__
#define __SQSTACK_H__

#define TRUE 1
#define FALSE 0

#define SIZE 10
typedef int StackData;

typedef struct _stack
{
	StackData data[SIZE];
	int top;
}Stack;

//置空棧
int InitStack (Stack *s);

//判斷爲空
int Stack_Empty (Stack *s);

//判斷棧滿
int Stack_Full (Stack *s);

//入棧
int Push (Stack *s,StackData x);

//出棧
int Pop (Stack *s,StackData *x);

//取棧頂
int GetTop (Stack *s,StackData *x);

#endif  //__SQSTACK_H__

源文件:

SqStack.c

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

//置空棧
int InitStack (Stack *s)
{
	if (s == NULL)
	{
		errno = ERROR;
		return FALSE;
	}
	s -> top = -1;
	return TRUE;
}

//判斷爲空
int Stack_Empty (Stack *s)
{
	if (s == NULL)
	{
		errno = ERROR;
		return FALSE;
	}
	return s -> top == -1;
}

//判斷棧滿
int Stack_Full (Stack *s)
{
	if (s == NULL)
	{
		errno = ERROR;
		return FALSE;
	}
	return s -> top == (SIZE - 1);
}

//入棧
int Push (Stack *s,StackData x)
{
	if (s == NULL)
	{
		errno = ERROR;
		return FALSE;
	}
	
	if (Stack_Full (s))
	{
		errno = FULL_STACK;
		return FALSE;
	}
	s -> data[s -> top + 1] = x;
	s -> top++;
	//s -> data[++s -> top] = x;
	return TRUE;
}

//出棧
int Pop (Stack *s,StackData *x)
{
	if (s == NULL)
	{
		errno = ERROR;
		return FALSE;
	}
	
	if (Stack_Empty (s))
	{
		errno = EMPTY_STACK;
		return FALSE;
	}
	*x = s -> data[s -> top];
	s -> top--;
	//*x = s -> data[s -> top--];
	return TRUE;
}

//取棧頂
int GetTop (Stack *s,StackData *x)
{
	if (s == NULL)
	{
		errno = ERROR;
		return FALSE;
	}
	
	if (Stack_Empty (s))
	{
		errno = EMPTY_STACK;
		return FALSE;
	}
	*x = s -> data[s -> top];
	return TRUE;
}

鏈式棧:

頭文件:

LinkStack.h

#ifndef __LINKSTACK_H__
#define __LINKSTACK_H__

#define TRUE 1
#define FALSE 0

#define SIZE 10
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 GetTop (LinkStack *s,StackData *x);

//銷燬棧
int Destory(LinkStack *s);

#endif  //__LINKSTACK_H__

源文件:

LinkStack.c

#include <stdio.h>
#include <stdlib.h>
#include "LinkStack.h"
#include "error.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_STACK;
		return FALSE;
	}
	Node *p = s->top;
	*x = p->data;
	s->top = p->next;
	free(p);
	return TRUE;
}

//獲取棧頂元素
int GetTop (LinkStack *s,StackData *x)
{
	if (s == NULL)
	{
		errno = ERROR;
		return FALSE;
	}
	if (StackEmpty (s))
	{
		errno = EMPTY_STACK;
		return FALSE;
	}
	*x = s->top->data;
	return TRUE;
}

//銷燬棧
int Destory(LinkStack *s)
{
	if (s == NULL)
	{
		errno = ERROR;
		return FALSE;
	}
	int x;
	while (StackEmpty (s) != TRUE)
	{
		Pop (s,&x);
	}
	free(s);
	return TRUE;
}

對於其中的一些錯誤信息,我返回特定的錯誤碼,在錯誤處理文件中處理,這樣可以使程序更具可讀性,也容易維護。

關於棧的更多的功能,可以大家一起去實現。

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