栈c语言代码
1. c语言关于栈操作
scanf("%s %d",p,&age);
这一句有问题,变量p作为存储地址的指针,不可以作为左值。除非是在用变量的地址初始化的时候。下面那句p="da"; 也一样。
栈的接口函数应该加上栈为空和为满的情况。
你应该吧age去掉,加上指向当前元素的栈指针。也就是栈顶指针。
你的代码的意思是把name数组,age和链接指针作为整体来入栈和出栈,就是说一次进出都是整个的数组,数组传递是通过strcpy实现的。
2. 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 ;
};
3. 数据结构定义一个栈并实现入栈和出栈操作的程序c语言完整版
如下:
#include"stdio.h"
structstackNode{
intdata;
structstackNode*nextPtr;
};
;
typedefLISTSTACK*STACKNODEPTR;
voidpush(STACKNODEPTR*,int);
intpop(STACKNODEPTR*);
intisEmpty(STACKNODEPTR);
voidprintStack(STACKNODEPTR);
voidinstruct();
intmain()
{
intitem;
intchoice;
STACKNODEPTRsPtr=NULL;
instruct();
printf("chooseyourchoice ");
scanf("%d",&choice);
while(choice!=3)
{
switch(choice)
{
case1:
printf("pleaseinputaninteger! ");
scanf("%d",&item);
//printf("%d ",item);
push(&sPtr,item);
printStack(sPtr);
break;
case2:
if(!isEmpty(sPtr))
{
printf("deletingelementoftopstack ");
pop(&sPtr);
printStack(sPtr);
}
else{
printf("noelementinthestack ");
}
break;
default:
printf("invalidinput,checkyourinput! ");
break;
}
printf("pleacechooseyourchoice");
instruct();
scanf("%d",&choice);
}
}
voidinstruct()
{
printf("Followingtheinstructionbelow: "
"1:insertnewelmentintothestack "
"2:deletethetopelementofthestack "
"3:toendofrun ");
}
intisEmpty(STACKNODEPTRsPtr)
{
returnsPtr==NULL;
}
voidprintStack(STACKNODEPTRsPtr)
{
if(sPtr==NULL)
{
printf("Thestackisempty! ");
}
else{
printf("Theelementsofthestack: ");
while(sPtr!=NULL)
{
printf("%d-->",sPtr->data);
sPtr=sPtr->nextPtr;
}
printf("NULL ");
}
}
voidpush(STACKNODEPTR*topPtr,intvalue)
{
STACKNODEPTRnewPtr;
newPtr=malloc(sizeof(STACKNODEPTR));
if(newPtr!=NULL)
{
newPtr->data=value;
newPtr->nextPtr=*topPtr;
*topPtr=newPtr;
}
else
{
printf("%disnotinsertedintostack.Nomemoryisavailiable ");
}
}
intpop(STACKNODEPTR*topPtr)
{
STACKNODEPTRnewPtr;
inttopValue;
newPtr=*topPtr;
*topPtr=(*topPtr)->nextPtr;
free(newPtr);
topValue=(*topPtr)->data;
printf("deleting---%d ",topValue);
returntopValue;
}
4. 数据结构C语言 栈
肯定是你指针指向不对
太长不太想看,只给你指出一处错误
scanf("%d,&c1");//通过键盘输入为变量c1赋值
这一句,看到啥错误了吧?还有你声明了char干嘛又用%d呢,如果用%c的时候注意这样写
scanf(" %c",&c1);
5. 栈的基本操作的C语言程序
#include <stdio.h>
#include <stdlib.h>
#define MAX 1024 ///栈使用数组模拟,MAX是最大元素个数
typedef int DataType; ///数据域使用整形
typedef struct _stack
{
DataType data[MAX]; ///存放数据
int top; ///栈顶指针
}stack;
///初始化
int initStack(stack (*s))
{
return emptyStack(s);
}
///数据压栈,成功返回1,失败返回0
int push(stack (*s), DataType d)
{
if ((*s).top >= MAX - 1) //栈已满
{
printf("栈已满,不能压栈\n");
return 0;
}
//数据压栈
(*s).top++;
(*s).data[(*s).top] = d;
return 1;
}
///数据出栈,成功返回1,d指向的区域存储弹出的数据,失败返回0
int pop(stack (*s), DataType *d)
{
if ((*s).top <= -1)
{
printf("栈为空,不能出栈\n");
return 0;
}
//出栈
*d = (*s).data[(*s).top];
(*s).top--;
return 1;
}
///清空栈
int emptyStack(stack (*s))
{
(*s).top = -1;
return 1;
}
int main(int argc, char** argv)
{
stack s;
int i, d;
initStack(&s);
//压栈
for (i = 0; i < 1025; i++)
{
push(&s, i);
}
//清空
emptyStack(&s);
for (i = 0; i < 10; i++)
{
push(&s, i);
}
//出栈
for (i = 0; i < 11; i++)
{
pop(&s, &d);
printf("%d\n", d);
}
return 0;
}
6. 用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;
}
}
7. 栈的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;
}
8. 栈的操作,用c语言急!
#include<stdio.h>
#include<malloc.h>
#define DataType int
#define MAXSIZE 1024
typedef struct
{
DataType data[MAXSIZE];
int top;
}SeqStack;
SeqStack *Init_SeqStack()//栈初始化
{
SeqStack *s;
s=(SeqStack *)malloc(sizeof(SeqStack));
if(!s)
{
printf("空间不足\n");
return NULL;
}
else
{
s->top=-1;
return s;
}
}
int Empty_SeqStack(SeqStack *s)//判栈空
{
if(s->top==-1)
return 1;
else
return 0;
}
int Push_SeqStack(SeqStack *s,DataType x)//入栈
{
if(s->top==MAXSIZE-1)
return 0;//栈满不能入栈
else
{
s->top++;
s->data[s->top]=x;
return 1;
}
}
int Pop_SeqStack(SeqStack *s,DataType *x)//出栈
{
if(Empty_SeqStack(s))
return 0;//栈空不能出栈
else
{
*x=s->data[s->top];
s->top--;
return 1;
}//栈顶元素存入*x,返回
}
DataType Top_SeqStack(SeqStack *s)//取栈顶元素
{
if(Empty_SeqStack(s))
return 0;//栈空
else
return s->data[s->top];
}
int Print_SeqStack(SeqStack *s)
{
int i;
printf("当前栈中的元素:\n");
for(i=s->top;i>=0;i--)
printf("%3d",s->data[i]);
printf("\n");
return 0;
}
int main()
{
SeqStack *L;
int n,num,m;
int i;
L=Init_SeqStack();
printf("初始化完成\n");
printf("栈空:%d\n",Empty_SeqStack(L));
printf("请输入入栈元素个数:\n");
scanf("%d",&n);
printf("请输入要入栈的%d个元素:\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&num);
Push_SeqStack(L,num);
}
Print_SeqStack(L);
printf("栈顶元素:%d\n",Top_SeqStack(L));
printf("请输入要出栈的元素个数(不能超过%d个):\n",n);
scanf("%d",&n);
printf("依次出栈的%d个元素:\n",n);
for(i=0;i<n;i++)
{
Pop_SeqStack(L,&m);
printf("%3d",m);
}
printf("\n");
Print_SeqStack(L);
printf("栈顶元素:%d\n",Top_SeqStack(L));
return 0;
}
9. 在C语言中该怎样建立栈具体代码是什么
1.栈空间(stack段)用来存放函数中的局部变量和函数调用时的上下文。
2.
全局变量和静态变量存放于进程的数据段。
3.
windows下进程的栈空间会自动增长,一般不会出现空间不足的问题;
4。如果变量实在太大,甚至大于栈可增长的范围,如数百兆,则会编译出错。
