簡單寫了一下順序棧的實現,不知道功能是否齊全,只是自己覺得應該有這些功能。
頭文件:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define STACK_CURRENT_SIZE 100
#define STACK_INCREMENT_SIZE 50
typedef int elemType;
typedef struct Stack
{
elemType *top; //棧頂指針
elemType *base; //棧底指針
int stack_size; //棧的容量
}Stack,*p_Stack;
p_Stack Init_Stack(void)
{//初始化一個棧
p_Stack stack;
stack=new Stack;
stack->base=(elemType *)malloc(STACK_CURRENT_SIZE *sizeof(Stack));
if(!stack->base)
{
printf("allocation is failed.\n");
exit(0);
}
stack->top=stack->base;
stack->stack_size=STACK_CURRENT_SIZE;
return stack;
}
bool IsStackEmpty(p_Stack stack)
{//判斷棧是否爲空
if(stack->base==stack->top)
{
return true;
}
return false;
}
bool IsStackFull(p_Stack stack)
{//判斷棧滿否
if(stack->top-stack->base>=stack->stack_size)
{
return true;
}
return false;
}
int StackLength(p_Stack stack)
{//獲取棧的長度
return stack->top-stack->base;
}
void Push(p_Stack stack,elemType elem)
{//入棧
if(IsStackFull(stack))
{//棧滿
stack->base=(elemType *)realloc(stack->base,stack->stack_size+STACK_INCREMENT_SIZE);
if(!stack->base)
{
printf("reallocation is failed.\n");
}
else
{
stack->top=stack->base+stack->stack_size;
stack->stack_size+=STACK_INCREMENT_SIZE;
}
}
else
{
*stack->top=elem;
++stack->top;
}
}
void Pop(p_Stack stack,elemType *elem)
{//出棧
if(IsStackEmpty(stack))
{
printf("The stack is empty.\n");
}
else
{
--stack->top;
*elem=*stack->top;
}
}
bool GetTop(p_Stack stack,elemType *elem)
{//得到棧頂元素
elemType *p;
if(IsStackEmpty(stack))
{
printf("You can't get the top_elem,the stack is empty.\n");
return false;
}
else
{
p=stack->base;
while(p!=stack->top-1)
{
p++;
}
*elem=*p;
return true;
}
}
bool Position_Elem(p_Stack stack,int pos,elemType *elem)
{//獲取pos位置處的元素
elemType *p=stack->base;
int i=0;
if(IsStackEmpty(stack))
{
printf("The stack is empty.\n");
return false;
}
else
{
if(pos<1 || pos>StackLength(stack))
{
printf("The position is invalidate.\n");
return false;
}
else
{
while(i<pos-1)
{
i++;
p++;
}
*elem=*p;
return true;
}
}
}
void ClearStack(p_Stack stack)
{//清空棧
elemType *p=stack->top;
elemType elem;
if(!IsStackEmpty(stack))
{
while(p!=stack->base)
{
Pop(stack,&elem);
--p;
}
}
}
void VisitStack(p_Stack stack)
{
elemType *p=stack->base;
if(!IsStackEmpty(stack))
{
while(p!=stack->top)
{
printf("%4d",*p);
p++;
}
}
printf("\n");
}
#include <stdlib.h>
#include <malloc.h>
#define STACK_CURRENT_SIZE 100
#define STACK_INCREMENT_SIZE 50
typedef int elemType;
typedef struct Stack
{
elemType *top; //棧頂指針
elemType *base; //棧底指針
int stack_size; //棧的容量
}Stack,*p_Stack;
p_Stack Init_Stack(void)
{//初始化一個棧
p_Stack stack;
stack=new Stack;
stack->base=(elemType *)malloc(STACK_CURRENT_SIZE *sizeof(Stack));
if(!stack->base)
{
printf("allocation is failed.\n");
exit(0);
}
stack->top=stack->base;
stack->stack_size=STACK_CURRENT_SIZE;
return stack;
}
bool IsStackEmpty(p_Stack stack)
{//判斷棧是否爲空
if(stack->base==stack->top)
{
return true;
}
return false;
}
bool IsStackFull(p_Stack stack)
{//判斷棧滿否
if(stack->top-stack->base>=stack->stack_size)
{
return true;
}
return false;
}
int StackLength(p_Stack stack)
{//獲取棧的長度
return stack->top-stack->base;
}
void Push(p_Stack stack,elemType elem)
{//入棧
if(IsStackFull(stack))
{//棧滿
stack->base=(elemType *)realloc(stack->base,stack->stack_size+STACK_INCREMENT_SIZE);
if(!stack->base)
{
printf("reallocation is failed.\n");
}
else
{
stack->top=stack->base+stack->stack_size;
stack->stack_size+=STACK_INCREMENT_SIZE;
}
}
else
{
*stack->top=elem;
++stack->top;
}
}
void Pop(p_Stack stack,elemType *elem)
{//出棧
if(IsStackEmpty(stack))
{
printf("The stack is empty.\n");
}
else
{
--stack->top;
*elem=*stack->top;
}
}
bool GetTop(p_Stack stack,elemType *elem)
{//得到棧頂元素
elemType *p;
if(IsStackEmpty(stack))
{
printf("You can't get the top_elem,the stack is empty.\n");
return false;
}
else
{
p=stack->base;
while(p!=stack->top-1)
{
p++;
}
*elem=*p;
return true;
}
}
bool Position_Elem(p_Stack stack,int pos,elemType *elem)
{//獲取pos位置處的元素
elemType *p=stack->base;
int i=0;
if(IsStackEmpty(stack))
{
printf("The stack is empty.\n");
return false;
}
else
{
if(pos<1 || pos>StackLength(stack))
{
printf("The position is invalidate.\n");
return false;
}
else
{
while(i<pos-1)
{
i++;
p++;
}
*elem=*p;
return true;
}
}
}
void ClearStack(p_Stack stack)
{//清空棧
elemType *p=stack->top;
elemType elem;
if(!IsStackEmpty(stack))
{
while(p!=stack->base)
{
Pop(stack,&elem);
--p;
}
}
}
void VisitStack(p_Stack stack)
{
elemType *p=stack->base;
if(!IsStackEmpty(stack))
{
while(p!=stack->top)
{
printf("%4d",*p);
p++;
}
}
printf("\n");
}
測試代碼:
#include "stdafx.h"
#include "stack.h"
#include <conio.h>
int _tmain(int argc, _TCHAR* argv[])
{
p_Stack stack;
elemType elem;
int pos;
int i;
stack=Init_Stack();
printf("The length of the stack is: %d\n",StackLength(stack));
Push(stack,3);
Push(stack,5);
Push(stack,8);
Push(stack,10);
VisitStack(stack);
printf("The length of the stack is: %d\n",StackLength(stack));
if(GetTop(stack,&elem))
{
printf("The top_elem is:%d\n",elem);
}
Pop(stack,&elem);
VisitStack(stack);
if(GetTop(stack,&elem))
{
printf("The top_elem is:%d\n",elem);
}
Pop(stack,&elem);
VisitStack(stack);
printf("The length of the stack is: %d\n",StackLength(stack));
printf("Please input the position:");
scanf("%d",&pos);
if(Position_Elem(stack,pos,&elem))
{
printf("The elem at the position %d is:%d\n",pos,elem);
}
printf("Please input the position:");
scanf("%d",&pos);
if(Position_Elem(stack,pos,&elem))
{
printf("The elem at the position %d is:%d\n",pos,elem);
}
ClearStack(stack);
if(GetTop(stack,&elem))
{
printf("The top_elem is:%d\n",elem);
}
for(i=0;i<5;++i)
{
Push(stack,2*i+1);
}
VisitStack(stack);
printf("The length of the stack is: %d\n",StackLength(stack));
getch();
return 0;
}
#include "stack.h"
#include <conio.h>
int _tmain(int argc, _TCHAR* argv[])
{
p_Stack stack;
elemType elem;
int pos;
int i;
stack=Init_Stack();
printf("The length of the stack is: %d\n",StackLength(stack));
Push(stack,3);
Push(stack,5);
Push(stack,8);
Push(stack,10);
VisitStack(stack);
printf("The length of the stack is: %d\n",StackLength(stack));
if(GetTop(stack,&elem))
{
printf("The top_elem is:%d\n",elem);
}
Pop(stack,&elem);
VisitStack(stack);
if(GetTop(stack,&elem))
{
printf("The top_elem is:%d\n",elem);
}
Pop(stack,&elem);
VisitStack(stack);
printf("The length of the stack is: %d\n",StackLength(stack));
printf("Please input the position:");
scanf("%d",&pos);
if(Position_Elem(stack,pos,&elem))
{
printf("The elem at the position %d is:%d\n",pos,elem);
}
printf("Please input the position:");
scanf("%d",&pos);
if(Position_Elem(stack,pos,&elem))
{
printf("The elem at the position %d is:%d\n",pos,elem);
}
ClearStack(stack);
if(GetTop(stack,&elem))
{
printf("The top_elem is:%d\n",elem);
}
for(i=0;i<5;++i)
{
Push(stack,2*i+1);
}
VisitStack(stack);
printf("The length of the stack is: %d\n",StackLength(stack));
getch();
return 0;
}