顺序栈的基本操作中Push压入后的- S.top = S.base + S.stacksize; 作用

#include <stdio.h>
#include <malloc.h>
#define TRUE 1
#define OK 1
#define ERROR 0
#define FALSE 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

typedef int Status;
typedef int SElemType;

struct SqStack
{
    SElemType *base;
    SElemType *top;
    int stacksize;
};

Status InitStack(SqStack &S)
{
    S.base = (SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
    if(!S.base) return ERROR;
    S.top = S.base;
    S.stacksize = STACK_INIT_SIZE;
    return OK;
}
Status Push(SqStack &S,SElemType e)
{
    if(S.top-S.base>=S.stacksize)
    {
        S.base = (SElemType*)realloc(S.base,((S.stacksize + STACKINCREMENT)*sizeof(SElemType)));
        if(!S.base) return ERROR;
        S.top = S.base + S.stacksize; 
    /*
        这一个问题的关键在于 realloc 是怎么实现的,有两种情况:
      1. 如果有足够空间用于扩大mem_address指向的内存块,则分配额外内存,并返回mem_address。这里说的是“扩大”,我们知道,realloc是从堆上分配内存的,当扩大一块内存空间时, realloc()试图直接从堆上现存的数据后面的那些字节中获得附加的字节,如果能够满足,自然天下太平。也就是说,如果原先的内存大小后面还有足够的空闲空间用来分配,加上原来的空间大小= newsize。那么就ok。得到的是一块连续的内存。  

      2. 如果原先的内存大小后面没有足够的空闲空间用来分配,那么从堆中另外找一块newsize大小的内存。并把原来大小内存空间中的内容复制到newsize中。返回新的mem_address指针。(数据被移动了)。老块被放回堆上。

             如果是第二种情况的话,s->top 就不是原来的 top 了。。

             所以结论就是,很有必要。

      */


        S.stacksize += STACKINCREMENT;
    }
    *S.top++ = e;
    return OK;
}

Status Pop(SqStack &S,SElemType &e)
{
    if(S.top == S.base) return ERROR;
    e = *--S.top;
    return OK;
}
Status GetTop(SqStack &S,SElemType &e)
{
    if(S.top == S.base) return ERROR;
    e = *(S.top - 1);
    return OK;
}

int StackLength(SqStack S)
{
    int count = 0;
    int i;
    while(S.top != S.base)
    {
        count++;
        S.top--;
    }
    return count;
// 返回栈S的元素个数
// 请补全代码

}

Status StackTraverse(SqStack S)
{
// 从栈顶到栈底依次输出栈中的每个元素
    SElemType *p = (SElemType *)malloc(sizeof(SElemType));
    p = S.top;       //请填空
    if(S.top == S.base)printf("The Stack is Empty!"); //请填空
    else
    {
        printf("The Stack is: ");
        p--;
        while(p >= S.base)        //请填空
        {
            printf("%d ", *p);
            p--;               //请填空
        }
    }
    printf("\n");
    return OK;
}

int main()
{
     int a;
     SqStack S;
     SElemType x, e;
     if(InitStack(S))    // 判断顺序表是否创建成功,请填空
    {
        printf("A Stack Has Created.\n");
    }
    while(1)
    {
        printf("1:Push \n2:Pop \n3:Get the Top \n4:Return the Length of the Stack\n5:Load the Stack\n0:Exit\nPlease choose:\n");
        scanf("%d",&a);
        switch(a)
        {
            case 1: scanf("%d", &x);
              if(!Push(S,x)) printf("Push Error!\n"); // 判断Push是否合法,请填空
              else printf("The Element %d is Successfully Pushed!\n", x);
              break;
        case 2: if(!Pop(S,e)) printf("Pop Error!\n"); // 判断Pop是否合法,请填空
              else printf("The Element %d is Successfully Poped!\n", e);
                break;
        case 3: if(!GetTop(S,e))printf("Get Top Error!\n"); // 判断Get Top是否合法,请填空
              else printf("The Top Element is %d!\n", e);
                 break;
            case 4: printf("The Length of the Stack is %d!\n",StackLength(S)); //请填空
                  break;
            case 5: StackTraverse(S);  //请填空
                  break;
            case 0: return 1;
        }
    }
}

 

转载于:https://www.cnblogs.com/lianghaijie-ctw/p/5584810.html

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一个连通图采用邻接表作为存储结构。设计一个算法,实现从顶点v出发的深度优先遍历的非递归过程。#include<iostream> #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define MAXSIZE 100 using namespace std; typedef struct ArcNode {//边结点 int data; struct ArcNode *nextarc; //链域:指向下一条边的指针 }ArcNode; typedef struct VNode {//顶点信息 int data; ArcNode *firstarc; //链域:指向第一条依附该顶点的边的指针 }VNode,AdjList[MAXSIZE]; //AdjList表示邻接表类型 typedef struct {//邻接表 AdjList vertices; int vexnum,arcnum; //图的当前顶点数和边数 }ALGraph; typedef struct {//顺序栈 int *base; //栈底指针 int *top; //栈顶指针 int stacksize; //栈可用的最大容量 }SqStack; void InitStack(SqStack &S) {//顺序栈的初始化 S.base=new int[MAXSIZE]; //动态分配一个最大容量MAXSIZE的数组空间 S.top=S.base; //top初始为base,空栈 S.stacksize=MAXSIZE; } void Push(SqStack &S,int e) {//入栈操作 if(S.top-S.base==S.stacksize) //栈满 return; *S.top=e; //元素e压入栈顶 S.top++; //栈顶指针加1 } void Pop(SqStack &S,int &e) {//出栈操作 if(S.base==S.top) //栈空 return; S.top--; //栈顶指针减1 e=*S.top; //将栈顶元素赋给e } bool StackEmpty(SqStack S) {//判空操作 if(S.base==S.top) //栈空返回true return true; return false; } bool visited[MAXSIZE]; //访问标志数组,初始为false int CreateUDG(ALGraph &G,int vexnum,int arcnum) {//采用邻接表表示法,创建无向图G G.vexnum=vexnum; //输入总顶点数 G.arcnum=arcnum; //输入总边数 if(G.vexnum>MAXSIZE) return ERROR; //超出最大顶点数则结束函数 int i,h,k; for(i=1;i<=G.vexnum;i++) //构造表头结点表 { G.vertices[i].data=i; visited[i]=false; G.vertices[i].firstarc=NULL; } ArcNode *p1,*p2; for(i=0;i<G.arcnum;i++) //输入各边,头插法构造邻接表 { cin>>h>>k; p1=new ArcNode; p1->data=k; p1->nextarc=G.vertices[h].firstarc; G.vertices[h].firstarc=p1; p2=new ArcNode; p2->data=h; p2->nextarc=G.vertices[k].firstarc; G.vertices[k].firstarc=p2; } return OK; } void DFS(ALGraph G,int v,SqStack S) {//从第v个顶点出发非递归实现深度优先遍历图G /**begin/ /**end/ } int main() { int n,m; while(cin>>n>>m) { if(n==0&&m==0) break; ALGraph G; SqStack S; CreateUDG(G,n,m); //创建无向图G int d; //从d开始遍历 cin>>d; DFS(G,d,S); //基于邻接表的深度优先遍历 } return 0; }
最新发布
06-12

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值