c语言数据结构栈
『壹』 c语言数据结构 栈的初始化
不知道你写的到底是什么,看不懂!
#define MAXSIZE 60
main()
{
char zhan[MAXSIZE],ch,i;
int top=0;
scanf("%c",&ch);
while(ch!='#')
{zhan[top++]=ch; //入栈
scanf("%c",&ch);}
top--;
while(top>=0)
printf("%c",zhan[top--]); //出栈
getch();
}
以上为顺序表存储版!
链式存储
定义 结构体栈元权素
struct vertype DNode
{ char data;
DNode *next;}Node;}
自己多想想就知道了,和上面的大同小异。
『贰』 数据结构实验(用c语言写) 栈的基本操作
//顺序栈
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#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<S.top-S.base;i++)
{
printf("%3d ",*q);q--;}
}
void main()
{
int a,b,c ;
char m;
SqStack s;
InitStack(s);
printf("请输入要进栈的元素个数是:");
scanf("%d",&a);
printf("\n请输入要进栈的%d个元素:",a);
for(b=0;b<a;b++) {
scanf("%d",&c);
Push(s,c); }
do { printf("\n");
printf("*********** 1.输出栈的元素**********\n");
printf("*********** 2.取栈顶元素************\n");
printf("*********** 3.删除栈顶元素**********\n");
printf("*********** 4.退出程序**********\n");
printf("\n请选择一个字符:");
getchar();
scanf("%c",&m);
switch(m) {
case '1': printf("\n输出的栈为:");
OutputStack(s);
break;
case '2': GetTop(s,c);
printf("\n栈顶元素为:%d",c);
printf("\n输出的栈为:");
OutputStack(s);
break;
case '3': Pop(s,c);
printf("\n删除的栈顶元素:%d",c);
printf("\n输出的栈为:");
OutputStack(s);
printf("\n");
break;
case '4':break;
default: printf("输入的数字有错,请重新选择!\n"); break;
}
}while(m!='4');
}
//链栈
#include<stdio.h>
#include<stdlib.h>
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<a;b++) {
scanf("%d",&x);
top=PushStack(top,x); }
printf("进栈已完成!\n");
printf("\n输出栈为:");
Print();
break;
case '3':
printf("\n操作之前的输出栈为:");
Print();
top=PopStack(top);
printf("\n操作过后的输出栈为:");
Print();
break;
case '4':
printf("\n输出栈为:");
Print();
if(top!=NULL)
printf("\n栈顶元素是:%d\n",GetStackTop(top));
else
printf("\n栈是空的,没有元素!");
break;
case '5':break;
default:
printf("\n输入的字符不对,请重新输入!");
break;
}
getchar();
}while(m!='5');
}
『叁』 C语言数据结构实现链栈的入栈、出栈、删除与插入
1、栈(stack)又名堆栈,它是一种运算受限的线性表。
其限制是仅允许在表的一端进行插入和删除运算。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。
2、例程:
#include<stdio.h>
#include<stdlib.h>
#defineMax100
typedefcharT;
typedefstructMyStack
{
Taa[Max];
unsignedintp;
}stack;
//创建空栈
stack*createEmptyStack()
{
stack*st=(stack*)malloc(sizeof(stack));
inti=0;
for(i=0;i<Max;i++)
st->aa[i]=0;
st->p=0;
returnst;
};
//栈判空
intisEmpty(conststack*st)
{
if(st->p==0)return1;
elsereturn0;
};
//求栈的大小
unsignedintsize(conststack*st)
{
returnst->p;
};
//push操作
voidpush(stack*st,constTa)
{
st->p=st->p+1;
if(st->p==Max)
{
printf("栈满 ");
st->p--;
return;
}
st->aa[st->p]=a;
};
//pop操作
Tpop(stack*st)
{
if(isEmpty(st))
{
printf("栈空");
returnNULL;
}
chart=st->aa[st->p];
st->p=st->p-1;
printf("%c",t);
returnt;
};
//栈销毁
voiddestroy(stack*st)
{
free(st);
};
intmain()
{
stack*st=createEmptyStack();
if(isEmpty(st))printf("MyStackisempty ");
elseprintf("MyStackisnotempty ");
push(st,'a');
push(st,'b');
push(st,'c');
push(st,'d');
push(st,'e');
printf("%d ",size(st));
while(!isEmpty(st))pop(st);
destroy(st);
system("pause");
return0;
}
『肆』 C语言版的数据结构中,栈存储结构是什么
栈(stack)在计算机科学中是限定仅在表尾进行插入或删除操作的线性表。
栈是一种回数据结构 ,是只能在答某一端插入和删除的特殊线性表 。它按照后进先出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据(最后一个数据被第一个读出来)。
『伍』 c语言版数据结构 空栈的构造
栈的本意是一个数组,里面存取数据的方式是先进后出。因此,你需要一个cusor来指定当前的栈顶(可能你使用top实现的),你可能还需要当前存放了多少数据进栈了,栈是否空、满,因此你还需要一个int变量计算栈元素个数。没push+1,没pop -1。你完全不需要成员stacksize,还有你需要一个栈元素个数的计数器。另外你不需要将形参由引用该为指针,反而降低效率!
『陆』 数据结构C语言 栈
#include<malloc.h> /* malloc()等 */
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<stdlib.h> /* atoi() */
#include<process.h> /* exit() */
struct StackRecord;
typedef struct StackRecord *Stack;
typedef char ElementType;
#define EmptyTOS -1
#define MinStackSize 5
struct StackRecord
{
int Capacity;
int TopOfStack;
ElementType *Array;
};
void MakeEmpty(Stack S)
{
S->TopOfStack=EmptyTOS;
}
int IsFull(Stack S)
{
return S->TopOfStack== S->Capacity;
}
int IsEmpty(Stack S)
{
return S->TopOfStack==EmptyTOS;
}
Stack CreateStack(int MaxElements )
{
Stack S;
/* 1 */ if(MaxElements<MinStackSize)
/* 2 */ {
printf("Stack size is too small");
exit(0);
}
/* 3 */ S=(Stack)malloc(sizeof(struct StackRecord ) );
/* 4 */ if(S==NULL)
/* 5 */ {
printf("Out of space!!l");
exit(0);
}
/* 6 */ S->Array=(ElementType *)malloc(sizeof(ElementType)*MaxElements);
/* 7 */ if(S->Array==NULL)
/* 8 */ {
printf("Out of space!!l");
exit(0);
}
/* 9 */ S->Capacity=MaxElements;
/*10*/ MakeEmpty(S);
/*11*/ return S;
}
void DisposeStack(Stack S)
{
if(S!=NULL)
{
free(S->Array);
free(S);
}
}
void Push(ElementType X, Stack S)
{
if( IsFull(S) )
{
printf("Full stack");
exit(0);
}
else
S->Array[S->TopOfStack++]=X;//将元素X放入栈中
}
ElementType Top(Stack S)
{
if(!IsEmpty(S) )
return 0;//返回栈顶元素
/* Return value used to avoid warning */
}
void Pop(Stack S)
{
if(IsEmpty(S) )
{
printf("Empty stack");
exit(0);
}
else
S->TopOfStack--;
}
ElementType TopAndPop(Stack S)
{
if(!IsEmpty(S) )
return S->Array[S->TopOfStack--];
}
void main(void)
{
char c1,c2;
Stack S;
int i,MAX;
scanf("%d",&MAX);
fflush(stdin);
S = CreateStack(MAX);//这个函数都没有调用过!
for(i=0;i<MAX;i++)
{
scanf("%c",&c1);//通过键盘输入为变量赋值
Push(c1,S);//将c1压入栈中
}
printf("S->TopOfStack: "); //输出栈顶元素
Pop(S); //栈顶元素出栈
for(i=0;i<MAX;i++)
printf("%c",TopAndPop(S));// 输出栈序列
}
这个栈写的不规范,pop那个函数都没有元素返回只是做了简单的把指针减一
改的这个可以运行了,但是运行结果就有点问题!
不太习惯你这个设计栈的方法,你有了个pop为什么还弄个TopAndPop(Stack S)
『柒』 数据结构 用C语言编程实现进栈出栈
希望如下对你有用:
/*栈的基本操作*/
#
define
stacksize
100
/*定义栈的最大存储空间*/
#
define
LEN
sizeof(struct
stack)
static
size=0;
struct
stack
{
int
data;
int
*top[stacksize];
};
struct
stack
*sqstack;
struct
stack
*s;
static
e;
int
push()
/*将元素压入栈*/
{
if
(size<=stacksize)
*
sqstack->top[size++]=e;
else
printf
("
the
stack
is
full:");
}
int
pop(struct
stack
*sqstack,int
location)
/*元素出栈*/
{
e=*(sqstack->top[location]);
return
(e);
}
main()
{
int
n,i,t,x=0;
int
element;
printf
(
"\n
create
the
stack
first
:");
scanf
("%d
",&n);
for
(i=1;i<=n;i++)
{
scanf
("%d",&e);
push
(e);
}
s=sqstack;
t=size;
printf
("\n
after
pushed
,
the
sqstack
is
:");
while
(t>=0)
{
*s->top[t]=*sqstack->top[t];
t--;
}
t=size;
while
(t!=0)
{
t--;
e=pop(s,t);
printf
("
%d->",e);
}
printf
("\n
which
element
you
want
to
pop
:");
scanf
("%d",&element);
while
(size!=0)
{
e=pop(sqstack,size--);
if
(element==e)
{
printf
("\n
%d
is
poped",element);
x=1;
}
}
if(x==0)
printf
("\n
%d
is
not
found
in
the
sqstack.\n",element);
}
『捌』 数据结构 C语言版,《栈的实现》
#include"head.h"
#defineSTACK_INIT_SIZE100
#defineSTACKINCREMENT10
typedefintSElemType;
typedefstruct
{
SElemType*base;
SElemType*top;
intstacksize;
}SqStack;
StatusInitStack(SqStack&S)
{
S.base=newSElemType[STACK_INIT_SIZE];
if(!S.base)returnOVERFLOW;
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
returnOK;
}
StatusDestroyStack(SqStack&S)
{
S.top=NULL;
S.base=NULL;
delete[]S.base;
S.stacksize=0;
returnOK;
}
intStackEmpty(SqStacks)
{
if(s.top==s.base)return1;
elsereturn0;
}
StatusGetTop(SqStack&s,SElemType&e)
{
if(s.top==s.base)returnERROR;
e=*(s.top-1);
returnOK;
}
intStackLength(SqStacks)
{
returns.top-s.base;
}
StatusClearStack(SqStack&S)
{
S.top=S.base;
returnOK;
}
StatusPush(SqStack&s,SElemTypee)
{
if(s.top-s.base>=s.stacksize)
returnOVERFLOW;
*s.top++=e;
returnOK;
}
StatusPop(SqStack&s,SElemType&e)
{
if(s.top==s.base)returnERROR;
e=*--s.top;
returnOK;
}
StatusStackTraverse(SqStacks,Status(*visit)(SElemTypec))//这个函数最好不要用,因为它已经破坏了栈的特性
{
while(s.top>s.base)
visit(*s.base++);
cout<<endl;
returnOK;
}
Statusvisit(SElemTypec)
{
cout<<c<<"";
returnOK;
}
voidmain()//为进制转换的函数
{
SqStackS;
SElemTypeN;
SElemTypee;
InitStack(S);
cout<<"inputthenumber:";
cin>>N;
while(N)
{
Push(S,N%8);
N=N/8;转换为八进制的数
}
while(!StackEmpty(S))
{
Pop(S,e);
cout<<e;
}
cout<<endl;
『玖』 表达式求值(C语言 数据结构栈)急求。。。。。
其实不用这样子的,要将表达式的值赋值就行了。
『拾』 C语言数据结构关于栈的题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int DataType;
typedef struct node{
DataType data;
struct node * next;
}Stack;
Stack* CreateStack(); //创建栈
void StackEmpty(Stack* ); //清空栈
void DestoryStack(Stack*); //撤销(删除)栈
int IsEmpty(Stack*); //判空
int PushStack(Stack*, DataType); //入栈
int PopStack(Stack*); //出栈
DataType GetTopElement(Stack*); //取栈顶元素
Stack* CreateStack()
{
Stack *stack = (Stack*)malloc(sizeof(Stack));
if(NULL != stack)
{
stack->next = NULL;
return stack;
}
printf("out of place.\n");
return NULL;
}
//清空栈
void StackEmpty(Stack* stack)
{
while(!IsEmpty(stack))
{
PopStack(stack);
}
printf("now stack is empty. \n");
}
//撤销栈
void DestoryStack(Stack* stack)
{
free(stack);
exit(0);
}
int IsEmpty(Stack* stack)
{
return (stack->next == 0);
}
//入栈,成功返回1,失败返回0, 把元素 data 存入 栈 stack 中
int PushStack(Stack* stack, DataType data)
{
Stack* newst = (Stack*)malloc(sizeof(Stack));
if(NULL != newst)
{
newst->data = data;
newst->next = stack->next; //s->next = NULL;
stack->next = newst;
return 1;
}
printf("out of place PushStack.\n");
return 0;
}
/*
出栈,成功返回1,失败返回0,出栈不取出元素值,只是删除栈顶元素。
如出栈要实现,取出元素值,并释放空间,可结合取栈顶元素函数做修改,这里不再给出。
*/
int PopStack(Stack* stack)
{
Stack* tmpst;
if(!IsEmpty(stack))
{
tmpst = stack->next;
stack->next = tmpst->next;
free(tmpst);
return 1;
}
return 0;
}
//取栈顶元素,仅取出栈顶元素的值,取出之后,该元素,任然存在栈中。成功返回元素值,失败输出提示信息,并返回 -1
DataType GetTopElement(Stack* stack)
{
if(!IsEmpty(stack))
{
return stack->next->data;
}
printf("stack is empty GetTopElement.\n");
return -1;
}
int main()
{
Stack* stack = CreateStack();
char str[200];
printf("请输入字符串:");
scanf("%s", str);
int num = 0;
for (int i = 0; i < strlen(str); i++) {
if (!IsEmpty(stack) && GetTopElement(stack) == '(' && str[i] == ')') {
PopStack(stack);
num++;
} else {
PushStack(stack, str[i]);
}
}
if (!IsEmpty(stack)) {
num = -1;
}
printf("匹配结果:%d", num);
DestoryStack(stack);
return 1;
}