码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构--栈的实现

时间:2016-01-26 20:23:23      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

数据结构中的节点结构体
struct inv
{
    struct inv*prev;
    int i;
    struct inv*next;
};

 

 

用于管理栈的数据结构
struct stackmanage
{
    int count;
    struct inv*head;
    struct inv*topp;
    struct inv*bottomp;
};

 

 

创建管理数组结构的结构体和初始化成员变量

struct stackmanage* createstack()
{
    struct stackmanage* stackmanagevalue=(struct stackmanage*)malloc(sizeof(struct stackmanage));
    stackmanagevalue->count=0;
    stackmanagevalue->head=(struct inv*)malloc(sizeof(struct inv));
    stackmanagevalue->head->i=0;
    stackmanagevalue->head->next=NULL;
    stackmanagevalue->head->prev=NULL;
    stackmanagevalue->bottomp=NULL;
    stackmanagevalue->topp=NULL;
    return stackmanagevalue;
}

 

 

 

压栈操作
void push(int i,struct stackmanage** stackmanagevalue)
{
    if(!stack_is_valid(*stackmanagevalue))return;
    struct inv*newp=(struct inv*)malloc(sizeof(struct inv));
    newp->i=i;
    newp->next=NULL;
    newp->prev=(*stackmanagevalue)->topp;
    if(is_empty(*stackmanagevalue))
    {
        (*stackmanagevalue)->head->next=newp;
        (*stackmanagevalue)->bottomp=newp;
    }
    else
    {
        (*stackmanagevalue)->topp->next=newp;
    }

    (*stackmanagevalue)->topp=newp;
    (*stackmanagevalue)->count++;
}

 

 

弹栈操作,栈为空时打印error 并返回0
int pop(struct stackmanage** stackmanagevalue)
{
    int i;
    if(!stack_is_valid(*stackmanagevalue))
    {
    }

    if(is_empty(*stackmanagevalue))
    {
        printf("error:the stack is empty>>>>>\n");
        return 0;
    }
    
    struct inv*topelement=(*stackmanagevalue)->topp;
    i=(*stackmanagevalue)->topp->i;
    if((*stackmanagevalue)->count==1)
    {
        free(topelement);
        (*stackmanagevalue)->head->next=NULL;
        (*stackmanagevalue)->bottomp=NULL;
        (*stackmanagevalue)->topp=NULL;
    }
    else
    {
        (*stackmanagevalue)->topp=topelement->prev;
        topelement=topelement->prev;
        topelement->next=NULL;
        free(topelement->next);
    }
    (*stackmanagevalue)->count--;
    return i;
}

 

 

只出栈,不溢出栈顶元素,栈为空时也是打印并返回0
int peek(struct stackmanage** stackmanagevalue)
{
    if(!stack_is_valid(*stackmanagevalue))
    {
        printf("the stack is not create>>>>>\n");
        return 0;
    }
    if(is_empty(*stackmanagevalue))
    {
        printf("the stack is empty>>>>>\n");
        return 0;
    }
    return (*stackmanagevalue)->topp->i;
}

 

查询栈中是否包含元素i

int contain(int i,struct stackmanage* stackmanagevalue)
{
    if(!stack_is_valid(stackmanagevalue))return 0;
    struct inv*headp=stackmanagevalue->bottomp;
    while(headp!=NULL)
    {
        if(headp->i==i)
        {
            return 1;
        }
        headp=headp->next;
    }
    return 0;
}

 

 

清空栈,同时把管理栈的结构体也释放掉
void clear(struct stackmanage** stackmanagevalue)
{
    if(!stack_is_valid(*stackmanagevalue))return;
    struct inv*head=(*stackmanagevalue)->head;
    struct inv*temp=NULL;
    while(head!=NULL)
    {
        temp=head;
        head=head->next;
        free(temp);
    }
    free((*stackmanagevalue));
    *stackmanagevalue=NULL;
}
扫描整个栈并打印
void print(struct stackmanage*stackmanagevalue)
{
    if(!stack_is_valid(stackmanagevalue))
        return;
    printf("**print start from the bottom**\n");
    struct inv* head=stackmanagevalue->bottomp;
    while(head!=NULL)
    {
        printf("%d\n",head->i);
        head=head->next;
    }
    printf("*************end***************\n\n");
}

 

 

检查栈是否为空
int is_empty(struct stackmanage* stackmanagevalue)
{
    if(!stack_is_valid(stackmanagevalue))    
    {
        printf("error:the stack is not create>>>>>\n");
        return 1;
    }
    return !(stackmanagevalue->count);//when is zero return true
}



检查栈管理结构体的指针是否有效
int stack_is_valid(struct stackmanage* stackmanagevalue)
{
    return stackmanagevalue!=NULL;
}

 

 

 cpp源码文件:   http://pan.baidu.com/s/1bodkQRX   

stack.cpp文件就是了

 

数据结构--栈的实现

标签:

原文地址:http://www.cnblogs.com/magicianlyx/p/5161298.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!