?? 棧.cpp
字號:
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct{
int *base;
int *top;
int stacksize;
}Sqstack;
//創建一個空棧
void Inistack(Sqstack &s)
{
s.base=(int*)malloc(STACK_INIT_SIZE * sizeof(int) );
if(!s.base)
{
printf("創建空棧失敗!\n");
exit(0);
}
s.top=s.base;
s.stacksize=STACK_INIT_SIZE;
printf("成功創建一個空棧。\n");
}
//毀掉一個棧
void Destroystack(Sqstack s)
{
s.base=NULL;
printf("棧已經被銷毀!\n");
}
//清空一個棧
void Clearstack(Sqstack &s)
{
s.base=s.top;
printf("棧已經被清空!\n");
}
//創建一個棧
void Createstack(Sqstack &s)
{
int a,n=0;
s.base=(int*)malloc(STACK_INIT_SIZE * sizeof(int) );
s.top=s.base;
if(!s.base)
{
printf("申請空間失敗!\n");
exit(0);
}
printf("input a number (if(0)stop)\n");
scanf("%d",&a);
while(a!=0)
{
*s.top++=a;
printf("input a number (if(0)stop)\n");
scanf("%d",&a);
}
}
//將棧里的元素輸出
void printstack(Sqstack s)
{
int d;
while(s.top!=s.base)
{
d=*s.base++;
printf("%d\t",d);
}
}
//輸出棧頂元素
void Gettop(Sqstack s, int &e)
{
if(s.top==s.base)
printf("棧是空的!\n");
e=*(s.top-1);
}
//插入新的元素作為棧頂元素
void Push(Sqstack &s,int e)
{
if(s.top-s.base>=STACK_INIT_SIZE)
{
s.base=(int*)malloc((STACK_INIT_SIZE+STACKINCREMENT)*sizeof(int));
if(!s.base)
exit(0);
s.top=s.base +s.stacksize;
s.stacksize +=STACKINCREMENT;
}
*s.top++=e;
}
//求棧中元素的個數
int StackLength(Sqstack s)
{
return s.top-s.base;
}
//刪除棧頂元素
void pop(Sqstack &s, int &e)
{
if(s.top==s.base)
e=0;
else
e=*--s.top;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -