栈的操作(顺序栈,链栈)
//顺序栈
typedef struct
{
int data[100];
int top;
}sqstack;
void jianzhan(sqstack*s)
{
s=(sqstack*)malloc(sizeof(sqstack));
s->top=-1;
}
int pan(sqstack*s)
{
if(s->top==-1)
{
return 1;
}
else return 0;
}
void push(sqstack*s,int key)
{
s->data[s->top++]=key;
}
void pop(sqstack*s)
{
int e;
if(!pan(s))
{
e=s->data[s->top];
s->top–;
}
return e;
}
void gettop(sqstack*s)
{
int e;
if(!pan(s))
{
e=s->data[s->top];//取栈顶的元素不是删除栈顶的元素,不用top–;
}
return e;
}
//课本上的比较详细的
include
include
define sqstackchu 100
define sqstackjia 10
typedef struct
{
int *base;
int *top;
int sqstacksize;
}sqstack;
int initstack(sqstack &s)
{
s.base=(int *)malloc(sqstackchu*sizeof(int));
if(!s.base)exit(-1);
s.top=s.base;
s.sqstacksize=sqstackchu;
return 1;
}
int gettop(sqstack &s,int e)
{
if(s.top==s.base)return 0;
e=*(s.top-1);
return e;
}
int push(sqstack&s,int e)
{
if(s.top-s.base>=s.sqstacksize)
{
s.base=(int *)malloc(s.base,s.sqstacksize+sqstackjia)*sizeof(int);
if(!s.base) exit(-1);
s.top=s.base+s.sqstacksize;
s.sqstacksize+=sqstackjia;
}
*s.top++=e;
return 1;
}
void pop(sqstack &s)
{
if(s.top==s.base)return ;
s.top–;
}
int empty(sqstack &s)
{
if(s.top==s.base)return 1;
else return 0;
}
typedef struct node
{
int data;
struct node*next;
}lnode;
typedef struct
{
lnode *front,*rear;
}lqueue;
void intitqueue(lqueue &q)
{
q.front=q.rear=(lnode*)malloc(sizeof(lnode));
if(!q.front)exit(0);
q.front->next=NULL;
}
void enqueue(lqueue&q,int n)
{
lnode *p;
p=(lnode*)malloc(sizeof(lnode));
if(!q.front)exit(0);
p->data=n;
p->next=NULL;
q.rear->next=p;
q.rear=p;
}
include
include
define sqstackchu 100
define sqstackjia 10
typedef struct
{
int *base;
int *top;
int sqstacksize;
}sqstack;
int initstack(sqstack &s)
{
s.base=(int *)malloc(sqstackchu*sizeof(int));
if(!s.base)exit(-1);
s.top=s.base;
s.sqstacksize=sqstackchu;
return 1;
}
int gettop(sqstack &s,int e)
{
if(s.top==s.base)return 0;
e=*(s.top-1);
return e;
}
int push(sqstack&s,int e)
{
if(s.top-s.base>=s.sqstacksize)
{
s.base=(int *)malloc(s.base,s.sqstacksize+sqstackjia)*sizeof(int);
if(!s.base) exit(-1);
s.top=s.base+s.sqstacksize;
s.sqstacksize+=sqstackjia;
}
*s.top++=e;
return 1;
}
void pop(sqstack &s)
{
if(s.top==s.base)return ;
s.top–;
}
int empty(sqstack &s)
{
if(s.top==s.base)return 1;
else return 0;
}
typedef struct node
{
int data;
struct node*next;
}lnode;
typedef struct
{
lnode *front,*rear;
}lqueue;
void intitqueue(lqueue &q)
{
q.front=q.rear=(lnode*)malloc(sizeof(lnode));
if(!q.front)exit(0);
q.front->next=NULL;
}
void enqueue(lqueue&q,int n)
{
lnode *p;
p=(lnode*)malloc(sizeof(lnode));
if(!q.front)exit(0);
p->data=n;
p->next=NULL;
q.rear->next=p;
q.rear=p;
}
“`
上一篇: 汇编语言检测点10.3
下一篇: 内部类方式为HashMap赋初值