您的位置:首页 > 其它

链表栈-》与数组栈同接口-》更确切地说是单项链表的接口封装

2014-04-10 00:00 225 查看
摘要: 单项链表的接口封装,单项链表程序就不copy上来了

/*************************************************************************
> File Name: stack.h
> Author: Palmer XU
> Mail:
> Created Time: Wed 09 Apr 2014 09:39:55 PM EDT
************************************************************************/

#ifndef __STACK_H__
#define __STACK_H__

#include"single.h"

typedef struct stack
{
pnode_t head;
}stack_t,*pstack_t;

int creat_stack(pstack_t*);

int stack_not_empty(pstack_t);

int push(pstack_t,int);

int pop(pstack_t,int*);

int top(pstack_t,int*);

int clear_stack(pstack_t);

int count_size(pstack_t);

int destory_stack(pstack_t*);

#endif
/*************************************************************************
> File Name: stack.c
> Author: Palmer XU
> Mail: Palmer.Xu.One@Gmail.com
> Created Time: Wed 09 Apr 2014 10:55:25 PM EDT
************************************************************************/
#include<stdio.h>
#include"stack.h"
int main(void)
{
pstack_t p ;
creat_stack(&p);
printf("%d\n",stack_not_empty(p));
int i = 1;
for(;i <= 3;i++)
push(p,i);
pop(p,&i);
printf("%d\n",i);
top(p,&i);
printf("%d\n",i);
printf("%d\n",stack_not_empty(p));
printf("%d\n",count_size(p));
clear_stack(p);
printf("%d\n",stack_not_empty(p));
printf("%d\n",count_size(p));
pop(p,&i);
i = destory_stack(&p);
printf("%d\n",i);
printf("%d\n",count_size(p));

return 0;
}

/*************************************************************************
> File Name: stack_fun.c
> Author: Palmer XU
> Mail: Palmer.Xu.One@Gmail.com
> Created Time: Wed 09 Apr 2014 10:06:42 PM EDT
************************************************************************/
#include"stack.h"
#include<stdlib.h>
int creat_stack(pstack_t *p_temp)
{
*
7fe0
p_temp = (pstack_t)malloc(sizeof(stack_t));
pstack_t p = *p_temp;
if(NULL != p && NULL != (p->head = creat_list()))
{
return 0;
}
printf("error- -creat_stack fail\n");
free(p);
*p_temp = NULL;
return -1;

}
int stack_not_empty(pstack_t p)
{
if(NULL == p)
{
printf("error\n");
return -1;
}
if(NULL != p->head->next)
return 0;
else
return -1;
}
int push(pstack_t p,int data)
{
if(NULL == p)
{
printf("error\n");
return 0;
}
return insert_after(p->head,data);
}
int pop(pstack_t p,int *data)
{
if(NULL == p)
{
printf("error\n");
return -1;
}
if(NULL != p->head->next)
*data = p->head->next->data;
return delete_after(p->head);
}
int top(pstack_t p,int* data)
{
if(NULL == p || NULL == p->head->next)
{
printf("error\n");
return -1;
}
*data = p->head->next->data;
return 0;
}
int clear_stack(pstack_t p)
{
if(NULL == p)
{
printf("error\n");
return -1;
}
return destory_after(p->head);
}
int count_size(pstack_t p)
{
if(NULL == p)
{
printf("error\n");
return -1;
}
return count_size_list(p->head);
}
int destory_stack(pstack_t *p_flag)
{
pstack_t p = *p_flag;
if(NULL == p)
{
printf("error\n");
return -1;
}
p->head = destory_all(p->head);
free(p);
*p_flag = NULL;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  链表