用数组 和 链表 构建栈

栈,用链表构建时,需建立两个基本的struct:一个是Stack struct 包括 栈顶节点 和 栈大小;一个是Stack node 包括 数据和下一节点指针。

栈,用数组构建时,只需一个struct:包括一个数组 和 栈大小;

链表源码如下:

#if 0
#include "stdlib.h" 
#include "stdio.h"
typedef int item;
typedef struct node * pnode;
typedef struct node
{
	item  data;
	pnode dowm;

}Node;

typedef struct stack
{
	pnode top;
	int size;
}Stack;
Stack * initStack();                // 初始化栈,栈大小0,栈顶NULL
void DestoryStack(Stack **ps);      //销毁栈
void CleraStack(Stack * ps);		//清除栈元素
int IsEmpty(Stack * ps);			//判断栈是否为空,是返回1,否返回0
int GetStackSize(Stack * ps);		// 返回栈大小
pnode GetStackTop(Stack * ps,item * pitem);// 返回栈顶节点,如果pitem不为null,*pitem保存栈顶的data
pnode Push(Stack * ps,item tem);     //将 tem 压入栈中,返回新建的节点
pnode Pop(Stack * ps,int * d);       //返回弹出后的栈顶节点,如果d 不为null ,*d保存弹出的data
void Traversal(Stack *ps , void (*visit)());
void print( int a);
Stack * initStack()
{
	Stack * ps = (Stack *)malloc(sizeof(Stack));

	if ( ps !=NULL)
	{
		ps->top = NULL;
		ps->size = 0;

	}
	else
		printf("error");

	return ps;
}
void DestoryStack(Stack **ps)
{	
	if (IsEmpty(*ps) != 1)
	{
		CleraStack(*ps);
	}
	else
	{	
		*ps = NULL;
 		free(*ps);
	
	}

}
void CleraStack(Stack * ps)
{
	while( IsEmpty(ps) != 1)
	{
		Pop(ps,NULL);
	}
}
int IsEmpty(Stack * ps)
{
	if ( ps ->size == 0 && ps ->top == NULL)
	{
		return 1;
	}
	else
		return 0;
}
int GetStackSize(Stack * ps)
{
	return ps ->size;
}
pnode GetStackTop(Stack * ps,item * pitem)
{
	if ( IsEmpty(ps) != 1 && pitem != NULL)
	{
		*pitem = ps ->top->data;
	}
	return ps->top;

}
pnode Push(Stack * ps,item tem)
{
	pnode  pp = (pnode)malloc(sizeof(Node));
	if ( pp != NULL)
	{
		pp ->data = tem;
		pp ->dowm = GetStackTop(ps,NULL);
		(ps->size)++;
		ps->top = pp;
	}
	return pp;
}
pnode Pop(Stack * ps,int * d)
{
	pnode p = ps ->top;

	if ( IsEmpty(ps) != 1 && p != NULL)
	{
		if ( d != NULL)
		{
			*d = p->data;
		}
		ps ->size--;
		ps ->top = p->dowm;
		free(p);
	}
	return ps ->top;
}
void Traversal(Stack *ps , void (*visit)())
{
	
	pnode p;
	int sum ;
	if (!ps)
	{
		printf("destory");
		return;
	}
	if (IsEmpty(ps) )
	{
		printf("stack is  null ");
		return;
	}
	else
	{
		p= ps ->top;
		sum = ps ->size;
		while(sum--)
		{
			visit(p->data);
			p = p->dowm;
		}
	}

	return;
}
void print( int a)
{
	printf(" %d ",a);
}
main() 
{ 
    int i,tmp,size;
	Stack * s;
	s = initStack(s);

	for( i = 0; i<10; i++)
	{
		Push(s,i);
		GetStackTop(s,&tmp);
		printf(" top = %d ",tmp);
		size = GetStackSize(s);
		printf(" size = %d \n",size);
	}
	printf("\n");
	for( i = 0; i<10; i++)
	{
		Pop(s,&tmp);
		printf(" %d ",tmp);
	}
	printf("\n");
	size = GetStackSize(s);
	printf(" size = %d \n",size);
	for( i = 0; i<10; i++)
	{
		Push(s,i);
		GetStackTop(s,&tmp);
		printf(" top = %d ",tmp);
		size = GetStackSize(s);
		printf(" size = %d \n",size);
	}
	printf("\n");
	Traversal(s , print);
	printf("\n");

	CleraStack(s);
	Traversal(s , print);
	printf("\n");

	DestoryStack(&s);
	Traversal(s , print);


	while(1);
} 
#endif
数组源码如下:

#if 1
#include "stdlib.h" 
#include "stdio.h"

#define Stack_Size 100

typedef struct stack
{
	unsigned int top;
	int array[Stack_Size];
} Stack,*pStack;

int Init_Stack(pStack s)
{
	s -> top = 0;
	return 1;
}
int IsEmpty(pStack s)
{
	if ( s->top == 0)
	{
		return 1;
	}
	else
		return 0;
}
int Push(pStack s,int a)
{
	if (s ->top > Stack_Size)
	{
		printf("Stack is full");
		return;
	}
	else
	{	
		s ->top ++;
		s->array[s->top] = a;
	}
	return s ->top;
}
void Pop(pStack s,int *tem)
{
	if (IsEmpty(s) != 1 && s != NULL)
	{
		if (tem != NULL)
		{	
			*tem = s ->array[s->top];
		}
		s ->top--;
	}
	else
	{
		printf(" \nStack is empty or not exist \n");
	}
	return;
}	

void CleanStack(pStack s)
{
	while( s->top !=0)
	{
		Pop(s,NULL);
	}
	return ;
}
void DestoryStack( pStack *s)
{
	if (IsEmpty(*s) != 1)
	{
		CleanStack(*s);
	}
	free(*s);
	*s = NULL;
	return;
}
void GetStackTop(pStack s,int * tem)
{
	if ( s->top != 0 && tem != NULL)
	{
		*tem = s->array[s->top];	
	}
	return;
}
void Traversal(pStack s,void(*visit)())
{
	int i =0 ;
	if ( s == NULL)
	{
		printf("\n Stack is destory! \n");
		return;
	}
	if (IsEmpty(s) != 1)
	{
		for ( i = 1; i < s->top+1; i++)
		{
			visit(s->array[i]);
		}
	}
	else
	{
		printf("Stack is empty!");
	}

	return;
}
void print(int data)
{
	printf(" %d ",data); 
}
int main()
{
	int i,tem;
	pStack s = (pStack)malloc(sizeof(Stack));
	Init_Stack(s);
	for( i = 0; i <10; i++)
	{
		Push(s,i);
		GetStackTop( s,&tem);
		printf(" % d",tem);
	}
	printf("\n");
	Traversal(s,print);
	printf("\n");
	for( i = 0; i <12; i++)
	{
		Pop(s,&tem);
		printf(" % d",tem);
	}
	printf("\n");
	CleanStack(s);
	printf("\n");
	Traversal(s,print);
	DestoryStack(&s);
	Traversal(s,print);

}
#endif

两种方法都需注意 内存的分配和释放!

你可能感兴趣的:(数据结构)