本文目录一览:
数据结构实验(用c语言写) 栈的基本操作
//顺序栈
#include
#include
#include
#define
STACK_INIT_SIZE
100;
#define
STACKINCREMENT
10;
typedef
struct
{
int
*base;
int
*top;
int
stacksize;
}SqStack;
typedef
int
ElemType;
int
InitStack(SqStack
S)
//为栈S分配存储空间,并置S为空栈
{
int
size
=
STACK_INIT_SIZE;
S.base=(int
*)malloc(size*sizeof(ElemType));
if(!S.base)
return
0;
S.top=S.base;
//置栈S为空栈
S.stacksize=STACK_INIT_SIZE;
return
1;
}
int
GetTop(SqStack
S,int
e)
//若栈不空,则用e返回S的栈顶元素
{
if(S.top==S.base)
return
0;
e=*(S.top-1);
return
1;
}
int
Push(SqStack
S,
int
e)
/*进栈函数,将e插入栈S中,并使之成为栈顶元素*/
{
if(S.top-S.base=S.stacksize)
/*栈满,追加存储空间*/
{
int
stackinvrement
=
STACKINCREMENT;
S.base=(ElemType
*)
realloc(S.base,(S.stacksize+stackinvrement)*sizeof(ElemType));
if(!S.base)
return
0;
/*存储分配失败*/
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return
1;
}
int
Pop(SqStack
S,int
e)/*出栈函数,若栈S不空,则删除S的栈顶元素,用e返回其值*/
{
if(S.top==S.base)
return
0;
e=*--S.top;
return
1;
}
void
OutputStack(SqStack
S)
{int
*q;
q=S.top-1;
for(int
i=0;i
#include
typedef
struct
SNode
{
int
data;
struct
SNode
*next;
}SNode,*LinkStack;
LinkStack
top;
LinkStack
PushStack(LinkStack
top,int
x)
//入栈
{
LinkStack
s;
s=(LinkStack)malloc(sizeof(SNode));
s-data=x;
s-next=top;
top=s;
return
top;
}
LinkStack
PopStack(LinkStack
top)
//退栈
{
LinkStack
p;
if(top!=NULL)
{
p=top;
top=top-next;
free(p);
printf("退栈已完成\n");
return
top;
}
else
printf("栈是空的,无法退栈!\n");
return
0;
}
int
GetStackTop(LinkStack
top)
//取栈顶元素
{
return
top-data;
}
bool
IsEmpty()//bool取值false和true,是0和1的区别,bool只有一个字节,BOOL为int型,bool为布尔型
{
return
top==NULL
?
true:false;
}
void
Print()
{
SNode
*p;
p=top;
if(IsEmpty())
{
printf("The
stack
is
empty!\n");
return;
}
while(p)
{
printf("%d
",
p-data);
p=p-next;
}
printf("\n");
}
void
main()
{
int
x,a,b;
char
m;
do
{
printf("\n");
printf("###############链栈的基本操作##################\n");
printf("××××××××1.置空栈××××××××××\n");
printf("××××××××2.进栈×××××××××××\n");
printf("××××××××3.退栈×××××××××××\n");
printf("××××××××4.取栈顶元素××××××××\n");
printf("××××××××5.退出程序×××××××××\n");
printf("##############################################\n");
printf("\n请选择一个字符:");
scanf("%c",m);
switch(m){
case
'1':
top=NULL;
printf("\n栈已置空!");
break;
case
'2':
printf("\n请输入要进栈的元素个数是:");
scanf("%d",a);
printf("\n请输入要进栈的%d个元素:",a);
for(b=0;b
评论
加载更多
c语言实现队列和栈的方法
#define OVERFLOW -1
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define N 20
typedef char SElemType;
typedef int Status;typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;#includestdio.h
#includestdlib.hStatus CreatStack(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;
}//CreatStackStatus Push(SqStack S,SElemType e){
//插入e为新的栈顶元素
if(S.top-S.base=S.stacksize){//栈满,追加存储空间
S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base)exit (OVERFLOW);//存储空间分配失败
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}//PushStatus Pop(SqStack S ,SElemType e){
//若栈不空,删除栈顶元素,并用e返回其值
if(S.top==S.base) return ERROR;
e=*--S.top;
return OK;
}//Pop typedef char QElemType;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,*QNodePtr;typedef struct{
QNodePtr front;
QNodePtr rear;
}LinkQueue;Status CreatQueue(LinkQueue Q){
//建立一个空的链式栈
Q.front=Q.rear=(QNodePtr)malloc(sizeof(QNode));
if(!Q.front)exit(OVERFLOW);
Q.front-next=NULL;
return OK;
}Status EnQueue(LinkQueue Q,QElemType e){ QNodePtr p;
p=(QNodePtr)malloc(sizeof(QNode));
if(!p)exit(OVERFLOW);
p-data=e;p-next=NULL;
Q.rear-next=p;
Q.rear=p;
return OK;
}Status DeQueue(LinkQueue Q,QElemType e){QNodePtr p;brif(Q.front==Q.rear) return ERROR;brp=Q.front-next; e=p-data;brQ.front-next=p-next;brif(Q.rear==p) Q.rear=Q.front;brfree(p);brreturn OK;br}int stackPalindrom_Test()//判别输入的字符串是否回文序列,是则返回1,否则返回0
{
printf("栈练习,请输入要判断的字符串以#作为结束符,不要超过二十个字符\n");
SqStack S;
CreatStack(S);
char c;
SElemType a;
SElemType b[N];
int count = 0;
fgets( b, N, stdin );
while((b[count])!='#')
{
Push(S,b[count]); //进栈
count++;
}
int i = 0;
while(i count)
{
Pop(S,a);
if(a!=b[i])
return ERROR;
i++;
}
return OK;}//stackPalindrom_Test int queuePalindrom_Test()//判别输入的字符串是否回文序列,是则返回1,否则返回0
{
printf("队列练习,请输入要判断的字符串以#作为结束符,不要超过二十个字符\n");
LinkQueue Q;
CreatQueue(Q); char c;
SElemType a;
SElemType b[N];
int count = 0;
fgets( b, N, stdin );
while((b[count])!='#')
{
EnQueue(Q,b[count]);; //入列
count++;
} while(count-- 0)
{
DeQueue(Q,a);
if(a!=b[count])
return ERROR;
}
return OK;
}//queuePalindrom_Test Status main(){ if(stackPalindrom_Test()==1)
printf("是回文\n");
else printf("不是回文\n"); if(queuePalindrom_Test()==1)
printf("是回文\n");
else printf("不是回文\n");
return OK;
}
栈的c语言实现基本操作
写了一个链式栈,你看看
# include stdio.h
# include malloc.h
# include stdlib.h
typedef struct Node
{
int data;
struct Node *pNext;
}NODE, *PNODE;
typedef struct Stack
{
PNODE pTop;
PNODE pBottom;//pBottem是指向栈底下一个没有实际意义的元素
}STACK, *PSTACK;
void init( PSTACK );
void push( PSTACK, int );
void traverse( PSTACK );
int pop( PSTACK, int * );
int empty( PSTACK pS );
int main( void )
{
STACK S;//STACK等价于struct Stack
int val;
init( S );//目的是造出一个空栈
push( S, 1 );//压栈
push( S, 2 );
push( S, 3 );
push( S, 4 );
push( S, 5 );
push( S, 6 );
push( S, 7 );
traverse( S );//遍历输出
// clear( S ); //清空数据
// traverse( S );//遍历输出
if( pop( S, val ) )
{
printf( "出栈成功,出栈的元素是%d\n", val );
}
else
{
printf( "出栈失败" );
}
traverse( S );//遍历输出出栈之后的元素
return 0;
}
void init( PSTACK pS )
{
pS-pTop = ( PNODE )malloc( sizeof( NODE ) );
if( NULL == pS-pTop )
{
printf( "动态内存分配失败!\n" );
exit( -1 );
}
else
{
pS-pBottom = pS-pTop;
pS-pTop-pNext = NULL;//或是pS-pBottom = NULL;
}
}
void push( PSTACK pS, int val )
{
PNODE pNew = ( PNODE )malloc( sizeof( NODE ) );
pNew-data = val;
pNew-pNext = pS-pTop;//pS-Top不能改为pS-pBottom
pS-pTop = pNew;
}
void traverse( PSTACK pS )
{
PNODE p = pS-pTop;
while( p != pS-pBottom )
{
printf( "%d ", p-data );
p = p-pNext;
}
printf( "\n" );
}
int empty( PSTACK pS )
{
if( pS-pTop == pS-pBottom )
return 1;
else
return 0;
}
//把pS所指向的栈出栈一次,并把出栈的元素存入pVal形参所指向的变量中,如果出栈失败,则返回false,否则true
int pop( PSTACK pS, int *pVal)
{
if( empty( pS ) )//pS本身存放的就是S的地址
{
return 0;
}
else
{
PNODE r = pS-pTop;
*pVal = r-data;
pS-pTop = r-pNext;
free( r );
r = NULL; //为什么要把r赋给NULL呢??
return 1;
}
}
//clear清空
void clear( PSTACK pS )
{
if( empty( pS ) )
{
return ;
}
else
{
PNODE p = pS-pTop;
PNODE q = p-pNext;
while( p != pS-pBottom )
{
q = p-pNext;
free( p );
p = q;
}
pS-pTop = pS-pBottom;
}
}