| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 509 人关注过本帖
标题:栈和队列程序
只看楼主 加入收藏
诶诶
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2014-3-27
结帖率:0
收藏
 问题点数:0 回复次数:7 
栈和队列程序
#include<stdio.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define STAACKINCREMENT
#define OK 1
#define OVERFLOW -1
#define ERROR 0
typedef struct
{
    int *base;
    int *top
    int stacksize;
}sqstack;
int Initstack(sqstack &S)
{
    S.base=(SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
    if(!S.base)exit(OVERFLOW);
    S.top=S.base;
    S.stacksize=STACK_INIT_SIZE;
    return OK;
}
int GetTop(sqStack S,SElemType &e)
{
    if(S.top==S.base) return ERROR;
        e=*(S.top-1);
        return OK;
}
int Push(SqStack &S,SElemtype e)
{
    if(S.top-S.base>=S.stacksize)
    {
        S.base=(SEletype *)realloc(S.base,(S.stacksize+STACKINCREMENT));
        if(!S.base)exit(OVERFLOW);
        S.top=S.base+S.stacksize;
        S.stacksize+=STACKINCREMENT;
    }
    *S.top++=e;
    return OK;
}
int Pop(SqStack &S,SElemType &e)
{
    if(S.top==S.base)return ERROR;
    e=*--S.top;
    return OK;
}
main()




帮忙补全程序实现初始化栈 取栈顶元素 插入元素 删除元素   以前C语言没好好学  现在学数据结构不会写主程序= =
搜索更多相关主题的帖子: include return 
2014-03-27 20:09
蚕头燕尾
Rank: 10Rank: 10Rank: 10
来 自:Gryffindo
等 级:贵宾
威 望:12
帖 子:734
专家分:1546
注 册:2013-3-24
收藏
得分:0 
鉴于没有人回复。。。挽尊。。。

我分享一份我之前写的代码给楼主吧,是栈操作的练手代码,很久之前写的了,如果有错误多多指教~

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXLEN 50
#define SIZE 50

typedef struct
{
    char name[10];
    int age;
}DATA;
typedef struct stack
{
    DATA data[SIZE+1];
    int top;   
}StackType;
////////////////////////////////////////////////////////
/*
                                     _______________________________
                                          初始化栈
                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
StackType *STInit()
{
    StackType *p;
    if(p=(StackType *)malloc(sizeof(StackType)))
    {
        p->top=0;
        return p;
    }
    return NULL;
}
/*
                                                             _____________________________
                                                                  判断栈是否为空
                                                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
int STIsEmpty(StackType *s)
{
    int t;
    t=(s->top==0);
    return t;
}
/*
                                                _____________________________
                                                       判断栈是否为满
                                                       ~~~~~~~~~~~~~~~~~~~~~~~~~
*/
int STIsFull(StackType *s)
{
    int t;
    t=(s->top==MAXLEN);
    return t;
}
/*
                                              ________________________________
                                                        清空栈
                                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
void STClear(StackType *s)
{
    s->top=0;
}
/*
                                             _____________________________________
                                                        释放栈所占用空间
                                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
void STFree(StackType *s)
{
    if(s)
    {
        free(s);
    }

}
/*
                                                      ____________________________________
                                                               入栈操作
                                                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
int PushST(StackType *s,DATA data)
{
    if((s->top+1)>MAXLEN)
    {
        printf("栈溢出!\n");
        return 0;
    }
    s->data[++s->top]=data;
    return 1;
}
/*
                                                     _______________________________
                                                            出栈操作
                                                            ~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
DATA PopST(StackType *s)
{
    if(s->top==0)
    {
        printf("栈为空!\n");
        exit(0);
    }
    return (s->data[s->top--]);
}
/*
                                                ___________________________________
                                                          读栈顶数据
                                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
DATA PeekST(StackType *s)
{
    if(s->top==0)
    {
        printf("栈为空!\n");
        exit(0);
    }
    return (s->data[s->top]);
}
/*
                                    ______________________________
                                              主函数
                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/


void main()
{
    StackType *stack;
    DATA data,data1;

    stack=STInit();
    printf("入栈操作:\n");
    printf("输入姓名 年龄进行入栈操作:\n");
    do
    {
        scanf("%s%d",data.name,&data.age);
        if(strcmp(data.name,"0")==0)
        {
            break;
        }
        else
        {
            PushST(stack,data);
        }

    }while(1);
             //看好这里的while(1);

    do
    {
        printf("\n出栈操作:按回车键进行出栈操作:");
        getchar();
        data1=PopST(stack);
        printf("出栈的数据是(%s,%d)\n",data1.name,data1.age);
    }while(1);
            //这里也是while(1);

    STFree(stack);

}

学习编程,为的是表达自己的思想,而不是被别人的思想所禁锢。要先明白自己想干嘛,而不要先问别人让你干嘛。               

                                                                                                                    Black Cat      Hello Tomorrow~
2014-03-27 21:06
蚕头燕尾
Rank: 10Rank: 10Rank: 10
来 自:Gryffindo
等 级:贵宾
威 望:12
帖 子:734
专家分:1546
注 册:2013-3-24
收藏
得分:0 
队列操作的练手程序如下【额,说明一点,这是练手时候写的,很可能会有错误,而且鲁棒性可能也不咋地……】

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define QUEUELEN 15

//SQType *SQTypeInit(void);      
//                    //生成队列。
//int SQTypeIsEmpty(SQType *q);   
//                   //判断空队列。
//int SQTypeIsFull(SQType *q);   
//                   //判断满队列。
//void SQTypeClear(SQType *q);   
//                  //清空队列。
//void SQTypeFree(SQType *q);  
//                  //释放队列。
//int InSQType(SQType *q,DATA data);
//                  //入队列。
//DATA *OutSQType(SQType *q);
//                  //出队列。
//DATA *PeekSQType(SQType *q);
//                 //读结点数据。
//int SQTypeLen(SQType *q);
                 //计算队列长度。
//////////////////////////////////////////////////主函数///////////////////////////////


////////////////////////////////定义队列结构//////////////////////
typedef struct
{
    char name[10];
    int age;
}DATA;
typedef struct
{
    DATA data[QUEUELEN];
    int head;
    int tail;
}SQType;
///////////////////////////////定义函数/////////////////////
/*
                                        __________________________________
                                              生成队列结构
                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
SQType *SQTypeInit(void)
{
    SQType *q;
    if(q=(SQType *)malloc(sizeof(SQType)))
    {
        q->head=0;
        q->tail=0;
        return q;
    }
    else
    {
        return NULL;
    }

}
/*
                                             ___________________________________
                                                       判断队列是否为空
                                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
int SQTypeIsEmpty(SQType *q)
{
    int temp;
    temp=q->head==q->tail;
    return temp;
}
/*
                                                 ________________________________
                                                       判断队列是否为满队列
                                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
int SQTypeIsFull(SQType *q)
{
    int temp;
    temp=q->tail==QUEUELEN;
    return temp;
}
/*
                                          ______________________________________
                                                 清空队列
                                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
void SQTypeClear(SQType *q)
{
    q->head=0;
    q->tail=0;
}
/*
                                           __________________________________
                                                  释放队列空间
                                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
void SQTypeFree(SQType *q)
{
    if(q!=NULL)
    {
        free(q);
    }

}
/*
                                          ______________________________
                                                入队列
                                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
int InSQType(SQType *q,DATA data)
{
    if(q->tail==QUEUELEN)
    {
        printf("队列已满!操作失败!\n");
        return 0;
    }
    else
    {
        q->data[q->tail++]=data;


        /////////////////////////
        return 1;
    }

}
/*
                                               _________________________________
                                                     出队列
                                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
DATA *OutSQType(SQType *q)
{
    if(q->head==q->tail)
    {
        printf("队列已空!操作失败!\n");
        exit (0);
    }
    else
    {
        return &(q->data[q->head++]);
    }

}
/*
                                                ________________________________
                                                      读结点数据
                                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
DATA *PeekSQType(SQType *q)
{
    if(SQTypeIsEmpty(q))
    {
        printf("\n空队列!\n");
        return NULL;
    }
    else
    {
        return &(q->data[q->head]);
    }

}
/*
                                     ______________________________
                                          计算队列长度
                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
int SQTypeLen(SQType *q)
{
    int temp;
    temp=q->head-q->tail;
    return temp;
}
void main()
{
    SQType *stack;
    DATA data;
    DATA *data1;

    stack=SQTypeInit();
    printf("输入姓名 年龄进行入队列操作:\n");
    do
    {
        scanf("%s%d",data.name,&data.age);
        if(strcmp(data.name,"0")==0)
        {
            break;
        }
        else
        {
            InSQType(stack,data);
        }

    }while (1);

    do
    {
        printf("出队列操作:按任意键进行出队列操作:\n");
        getchar();
        data1=OutSQType(stack);
        printf("出队列的数据是(%s,%d)\n\n",data.name,&data.age);
    }while(1);
    SQTypeFree(stack);
}

学习编程,为的是表达自己的思想,而不是被别人的思想所禁锢。要先明白自己想干嘛,而不要先问别人让你干嘛。               

                                                                                                                    Black Cat      Hello Tomorrow~
2014-03-27 21:08
蚕头燕尾
Rank: 10Rank: 10Rank: 10
来 自:Gryffindo
等 级:贵宾
威 望:12
帖 子:734
专家分:1546
注 册:2013-3-24
收藏
得分:0 

学习编程,为的是表达自己的思想,而不是被别人的思想所禁锢。要先明白自己想干嘛,而不要先问别人让你干嘛。               

                                                                                                                    Black Cat      Hello Tomorrow~
2014-03-27 21:09
诶诶
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2014-3-27
收藏
得分:0 
回复 3楼 蚕头燕尾
大神啊 不过连着我上面写得函数怎么运行哇= =   貌似我们学的是C++环境下的。。。
2014-03-27 21:43
诶诶
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2014-3-27
收藏
得分:0 
回复 楼主 诶诶
大神怎么能连着我上面写得函数运行啊
2014-03-27 21:45
蚕头燕尾
Rank: 10Rank: 10Rank: 10
来 自:Gryffindo
等 级:贵宾
威 望:12
帖 子:734
专家分:1546
注 册:2013-3-24
收藏
得分:0 
“我们学的是C++环境下的”是什么意思?

我的代码都给你了,你自己先改改自己的呗。不能啥事儿都让别人干了是不是的呢~


学习编程,为的是表达自己的思想,而不是被别人的思想所禁锢。要先明白自己想干嘛,而不要先问别人让你干嘛。               

                                                                                                                    Black Cat      Hello Tomorrow~
2014-03-27 21:58
诶诶
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2014-3-27
收藏
得分:0 
回复 7楼 蚕头燕尾
好吧  我参悟一下   三Q哈
2014-03-27 22:05
快速回复:栈和队列程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020799 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved