当前位置:首页 » 编程语言 » 栈的实现c语言

栈的实现c语言

发布时间: 2022-06-11 22:01:08

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语言),高手速来!!

/*程序错误太多*/ #include"stdio.h"
#include"stdlib.h"
#include"time.h"
#include"malloc.h"
#define
STACK_INIT_SIZE
10
//栈容量 typedef
struct
SqStack
{

int
top;
//栈顶当前指针

int
*base;
//栈空间数组
}SqStack; void
InitStack(SqStack
&S);
//构造空栈S
int
Push(SqStack
&S,int
e);
//入栈(栈地址,入栈数据)
返回0对,-1错
int
Pop(SqStack
&S);
//出栈
(栈地址)返回栈顶数据
int
StackLength(SqStack
S);
//返回站的元素个数,即求栈长
void
Print_S(SqStack
S);
//显示栈内数据 int
main()
{

SqStack
S;

int
i=0;

int
a,e;

InitStack(S);

srand((unsigned)time(NULL));
//srand((unsigned)time(NULL))以time函数值(当前时间)作为种子
printf("随机填充5个元素为:
");

while(
i<5)

{

a
=
rand()%100;

printf("%d
",
a);

Push(S,a);

i++;

}
Print_S(S);

printf("请输入要插入栈顶的元素:");

scanf("%d",&e);

Push(S,e);

Print_S(S);

printf("再弹出的栈顶元素为:%d
\n",Pop(S));

printf("栈的长度为:%d
\n",StackLength(S));

Print_S(S);

return
0;
} void
InitStack(SqStack
&S)
//构造空栈S
{

S.base
=
(int
*)malloc(STACK_INIT_SIZE
*
sizeof(int));
//分配组数空间,长度STACK_INIT_SIZE

if
(S.base==NULL)

{

printf("内存分配失败!\n");

return;

}

S.top=-1;
} int
Push(SqStack
&S,int
e)
{

if(S.top>=STACK_INIT_SIZE)

{

printf("栈空间已满,入栈失败!\n");

return
-1;

}

else

{

S.base[++S.top]=e;

return
0;

}
} int
Pop(SqStack
&S)
//返回栈顶数据
{

if
(S.top>=0)
//栈内有数据

{

return
S.base[S.top--];

}

else

{

printf("空栈,无数据弹出!\n");

return
-1;

}
} int
StackLength(SqStack
S)
{

return
S.top+1;
} void
Print_S(SqStack
S)
{

printf("\n出栈显示:");

if(S.top
==
-1)
printf("栈内无数据!\n");

else

{

while(S.top>=0
)
printf("%d
",Pop(S));

putchar('\n');

}
}

⑶ C语言,栈的实现~

你写的太复杂,这个拿去用吧
// Stack node
struct Node
{
int data ;
Node* next ;
Node(int d, Node* p):data(d), next(p){}
};

class Stack
{
public:
Stack():top(NULL){}

void Push(int d)
{
top = new Node(d, top) ;
}

int Pop()
{
Node* temp = top ;
top = top->next ;
return temp->data ;
}

bool Empty()
{
return top == NULL ;
}

private:
Node* top ;
};

⑷ 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;#include<stdio.h>
#include<stdlib.h>Status 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;<br>if(Q.front==Q.rear) return ERROR;<br>p=Q.front->next; e=p->data;<br>Q.front->next=p->next;<br>if(Q.rear==p) Q.rear=Q.front;<br>free(p);<br>return 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语言中该怎样建立栈具体代码是什么

1.栈空间(stack段)用来存放函数中的局部变量和函数调用时的上下文。
2.
全局变量和静态变量存放于进程的数据段。
3.
windows下进程的栈空间会自动增长,一般不会出现空间不足的问题;
4。如果变量实在太大,甚至大于栈可增长的范围,如数百兆,则会编译出错。

⑹ 栈的操作算法c语言实现

#include<iostream>
using namespace std;
#define STACK_INIT_SIZE 100
#define STACKINCREASE 1ypedef struct{
int *base;
int *top;
int stacksize;
}SqStack;
int InitStack (SqStack &S)//构造空栈
{
S.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S.base)exit(OVERFLOW);//存储分配失败
S.top=S.base;
return 0;
}
int Push(SqStack &S,int e)//插入新的元素为新的栈顶元素
{
if(S.top-S.base>=S.stacksize)//z栈满,追加存储空间
{
S.base=(int *)realloc(S.base,(S.stacksize+STACKINCREASE)*sizeof(int));
if(!S.base)exit(OVERFLOW);//存储失败
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREASE;
}
*S.top++ =e;//认为是先赋值再加加?
return 0;
}
void Insert(SqStack &S,int e)
{
int *p;
int temp;
p=S.base;
if(S.top-S.base>=S.stacksize)//z栈满,追加存储空间
{
S.base=(int *)realloc(S.base,(S.stacksize+STACKINCREASE)*sizeof(int));
if(!S.base)exit(OVERFLOW);//存储失败
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREASE;
}
while(*p<=e)
p++;
while(p<S.top)
{
temp=*p;
*p=e;
e=temp;
p++;
}
}0
void Pop(SqStack S)
{
if(S.base==S.top)
cout<<"该栈是空栈"<<endl;
while(S.base<S.top)
{
cout<<*++S.base<<" ";
}
}
void main()
{
SqStack S;
int j,i;
InitStack(S);
for(i=1;i<=5;i++)
{
cout<<"请输入"<<i<<"个栈内元素"<<endl;
cin>>j;
Push(S,j);
}
Pop(S);
cout<<"请输入你要插入的元素"<<endl;
cin>>j;
Insert(S,j);
cout<<"输出栈内元素"<<endl;
Pop(S);

}

⑺ 栈的c语言实现基本操作

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#definestack_init_size20
#defineincreasesize10;
typedefintElemType;
typedefstructnode{
ElemType*base;
ElemType*top;
intsize;
}stack;

voidcreat(stack*s)
{
s->base=(ElemType*)malloc(sizeof(ElemType));
if(!s->base)
{
exit(0);
}
s->base=s->top;
s->size=stack_init_size;
}

voidpush(stack*s,ElemTypee)
{
if(s->top-s->base>=s->size)
{
s->base=(ElemType*)realloc(s->base,(s->size+increasesize)*sizeof(ElemType));
if(!s->base)
{
exit(0);
}
s->top=s->base+s->size;
s->size+=increasesize;
}
*(s->top)=e;
s->top++;
}

voidpop(stack*s,ElemType*e)
{
if(s->top==s->base)
{
return;
}
*e=*--(s->top);
}
intstacklen(stacks)
{
return(s.top-s.base);
}
intmain()
{
stacks;
intn;
ElemTypee1,e2,d;
inti=1,j=1;
push(&s,i);
push(&s,j);
scanf("%d",&n);
while(n--)
{
pop(&s,&e1);
pop(&s,&e2);
d=e1+e2;
push(&s,e2);
push(&s,d);
}
pop(&s,&d);
printf("%d",d);
return0;
}

⑻ c语言中怎样用这两个个结构体实现栈的功能

typedef
struct
node
{
int
data;//每个节点中存放的数据
PNode
next;//下一节点
}Node;
/*定义栈*/
typedef
struct
stack
{
PNode
top;//栈顶指针
int
Size;//栈的大小
}Stack;
#include"Stack.h"
#include<malloc.h>
#include<stdlib.h>
/*构造一个空栈*/
Stack
*InitStack()
{
Stack
*ps
=
(Stack
*)malloc(sizeof(Stack));
if(ps!=NULL)
{
ps->top
=
NULL;
ps->size
=
0;
}
return
ps;
}
/*判定是否为空栈*/
int
IsEmpty(Stack
*ps)
{
if(ps->top
==
NULL
&&
ps->size
==
0)
return
1;
else
return
0;
}
/*返回栈大小*/
int
GetSize(Stack
*ps)
{
return
ps->size;
}
/*元素入栈*/
PNode
Push(Stack
*ps,int
data)
{
PNode
pnode
=
(PNode)malloc(sizeof(Node));
if(pnode
!=
NULL)
{
pnode->data
=
data;
pnode->next
=
GetTop(ps,NULL);
ps->size++;
ps->top
=
pnode;
}
return
pnode;
}
/*返回栈顶元素*/
PNode
GetTop(Stack
*ps,int
*data)
{
if(IsEmpty(ps)!=1&&data!=NULL)
{
*data=
ps->top->data;
}
return
ps->top;
}
/*元素出栈*/
PNode
Pop(Stack
*ps,int
*data)
{
PNode
p
=
ps->top;
if(IsEmpty(ps)!=1&&p!=NULL)
{
if(int!=NULL)
*int
=
p->data;
ps->size--;
ps->top
=
ps->top->next;
free(p);
}
return
ps->top;
}
/*销毁一个栈*/
void
DestroyStack(Stack
*ps)
{
if(IsEmpty(ps)!=1)
ClearStack(ps);
free(ps);
}
/*把栈置空*/
void
ClearStack(Stack
*ps)
{
while(IsEmpty(ps)!=1)
{
Pop(ps,NULL);
}
}
/*遍历栈并访问visit函数
*/
void
StackTraverse(Stack
*ps,void
(*visit)())
{
PNode
p
=
ps->top;
int
i
=
ps->size;
while(i--)
{
visit(p->data);
p
=
p->next;
}
}

⑼ 用C语言实现栈的基本操作(数制的转换)

//顺序栈以及基本操作如下:

#include<iostream.h>
enum
{
MAX_SIZE=20
};

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

void InitStack(SqStack& S)
{
S.base=new int[MAX_SIZE];
S.top=S.base;
S.stacksize=MAX_SIZE;
}

bool Push(SqStack& S,int e)
{
if(S.top-S.base>=S.stacksize)
return false;
*S.top=e;
S.top++;
return true;
}

bool Pop(SqStack& S,int& e)
{
if(S.top==S.base)
return false;
S.top--;
e=*S.top;
return true;
}

void DestroyStack(SqStack& S)
{
if(S.base)
delete S.base;
S.top=S.base=NULL;
S.stacksize=0;
}

bool StackEmpty(SqStack S)
{
return (S.top==S.base);
}

void Print(SqStack S)
{
int i=0;

while(i<S.top-S.base)
{

cout<<S.base[i++]<<endl;
}
}

热点内容
bin存储 发布:2025-02-07 20:00:50 浏览:202
android加载界面 发布:2025-02-07 19:55:28 浏览:870
好矿云服务器 发布:2025-02-07 19:54:31 浏览:948
java电话簿 发布:2025-02-07 19:49:26 浏览:796
超级脚本制作 发布:2025-02-07 19:31:30 浏览:486
怎么查看支付宝的账号密码 发布:2025-02-07 19:26:48 浏览:16
惠普服务器查看ip指令 发布:2025-02-07 19:26:47 浏览:434
算法设计模式 发布:2025-02-07 19:15:52 浏览:746
服务器1u能连接几台电脑 发布:2025-02-07 18:50:02 浏览:154
立人编译 发布:2025-02-07 18:48:32 浏览:766