c语言栈的实现以及操作(c语言栈的基本运算)
本篇文章给大家谈谈c语言栈的实现以及操作,以及c语言栈的基本运算对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
数据结构定义一个栈并实现入栈和出栈操作的程序c语言完整版
如下:
#include "stdio.h"
struct stackNode{
int data;
struct stackNode *nextPtr;
};
typedef struct stackNode LISTSTACK;
typedef LISTSTACK *STACKNODEPTR;
void push(STACKNODEPTR *,int);
int pop(STACKNODEPTR *);
int isEmpty(STACKNODEPTR);
void printStack(STACKNODEPTR);
void instruct();
int main()
{
int item;
int choice;
STACKNODEPTR sPtr=NULL;
instruct();
printf("choose your choice\n");
scanf("%d",choice);
while(choice!=3)
{
switch(choice)
{
case 1:
printf("please input an integer!\n");
scanf("%d",item);
春键 //printf("%d\n",item);
push(sPtr,item);
printStack(sPtr);
break;
case 2:
if(!isEmpty(sPtr))
{
printf("deleting element of top stack\n");
pop(sPtr);
printStack(sPtr);
}
else{
printf("no element in the stack\n");
}
break;
default:
printf("invalid input,check your input!\n");
break;
}
printf("pleace choose your choice ");
instruct();
scanf("%d",choice);
}
}
void instruct()
{
printf("Following the instruction below:\n"
扒知巧 "1:insert new elment into the stack\n"
"2:delete the top element of the stack\n"
"3:to end of run\n");
}
int isEmpty(STACKNODEPTR sPtr)
{
return sPtr==NULL;
}
void printStack(STACKNODEPTR sPtr)
{
if(sPtr==NULL)
{
printf("The stack is empty!\n");
}
else{
printf("The elements of the stack:\n");
while(sPtr!=NULL)
{
printf("%d--",sPtr-data);
sPtr=sPtr-nextPtr;
}
printf("NULL\n\n");
}
}
void push(STACKNODEPTR *topPtr,int value)
{
STACKNODEPTR newPtr;
newPtr=malloc(sizeof(STACKNODEPTR));
if(newPtr!=NULL)
{
newPtr-data=value;
newPtr-nextPtr=*topPtr;
*topPtr=newPtr;
}
else
{
printf("%d is not inserted into stack.No memory is availiable\n");
}
}
int pop(STACKNODEPTR *topPtr)
{
STACKNODEPTR newPtr;
int topValue;
newPtr=*topPtr;
*topPtr=(*topPtr)-nextPtr;
free(newPtr);
topValue=(*topPtr)-data;
printf("deleting--- %d\n",topValue);
return topValue;
}
数据结构:
是计算机存储、组织数据的方式。数据结构是指相互之间存在一种或多种特定关系的数据元素的集合。通常情况下,精心选择的数据结构可以带来更高的猛春运行或者存储效率。数据结构往往同高效的检索算法和索引技术有关。
常用数据结构:
数组 (Array)、栈 (Stack)、队列 (Queue)、链表 (Linked List)、树 (Tree)、图 (Graph)、堆 (Heap)、散列表 (Hash)
数据结构实验(用c语言写) 栈的基本操作
//顺序栈
#includestdio.h
#includestdlib.h
#includemalloc.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;iS.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;ba;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');
}
//链栈
#includestdio.h
#includestdlib.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;ba;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语言 栈的操作
#include
#include
#define Max 100
typedef char T;
typedef struct MyStack
{
T aa[Max];
unsigned int p;
} stack;
//创建空栈
stack* createEmptyStack()
{
stack* st = (stack *)malloc(sizeof(stack));
int i=0;
for(i=0;iMax;i++)
st-aa[i]=0;
st-p=0;
return st;
};
//栈判空
int isEmpty(const stack* st)
{
if(st-p==0) return 1;
else return 0;
};
//求栈的大小
unsigned int size(const stack* st)
{
return st-p;
};
//push操作
void push(stack* st,const T a)
{
st-p=st-配圆桥p+1;
if(st-p==Max)
{
printf("栈培猛满\n");
st-p--;
return;
}
st-aa[st-p]=a;
};
//pop操腔碰作
T pop(stack* st)
{
if(isEmpty(st))
{
printf("栈空");
return NULL;
}
char t=st-aa[st-p];
st-p=st-p-1;
printf("%c ",t);
return t;
};
//栈销毁
void destroy(stack* st)
{
free(st);
};
int main()
{
stack* st = createEmptyStack();
if(isEmpty(st)) printf("MyStack is empty\n");
else printf("MyStack is not empty\n");
push(st,'a');
push(st,'b');
push(st,'c');
push(st,'d');
push(st,'e');
printf("%d\n",size(st));
while(!isEmpty(st)) pop(st);
destroy(st);
system("pause");
return 0;
}
用C语言实现栈的基本操作(数制的转换)
/型猛蔽腊/顺序栈以及基本操作卜并桥如下:
#includeiostream.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(iS.top-S.base)
{
coutS.base[i++]endl;
}
}
C语言,栈的实现~
#includestdio.h
#includemalloc.h
//弯碰并enum bool {false,true};
typedef struct Node{
int a;
int Number; //在栈中的序号,栈底为0
struct Node *next;
}Node,*LpNode;
typedef struct SqStack{
Node *top;
Node *prev;
Node *base;
int length;
}*LpSqStack;
//将e的能容吵槐复制到S中并将e摧毁
bool Node_evaluation(LpNode S,LpNode e,bool M)
{
//赋值操作
//S-Number = e-Number;
if(M == true) free(e);
return true;
}
bool InitStack(LpSqStack S)
{
S-length = 0;
S-base = (LpNode)malloc(sizeof(Node));
if(!S-埋迹base) return false;
S-top = S-base;
S-prev = S-base;
S-base-Number = 0;
return true;
}
bool StackEmpty(LpSqStack S)
{
if(S-top != S-base) return false;
return true;
}
bool GetTop(LpSqStack S,LpNode e)
{
if(S-top == S-base) return false;
e = S-top;
return true;
}
bool Push(LpSqStack S,LpNode e)
{
if(!Node_evaluation(S-top,e,true)) return false;
S-top-Number = S-prev-Number + 1;
S-prev = S-top;
S-top = (LpNode)malloc(sizeof(Node));
S-prev-next = S-top;
S-top-next = NULL;
return true;
}
bool Pop(LpSqStack S,LpNode e)
{
if(S-top == S-base) return false;
if(!Node_evaluation(e,S-top,true)) return false;
S-top = S-prev;
S-top-next = NULL;
return true;
}
bool Vistit(LpSqStack S,LpNode e,int i)
{
LpNode p;
p = S-base;
for(int j = 0; j = i; j++)
{
if(p-next == NULL) return false;
p = p-next;
}
if(!Node_evaluation(p,e,false)) return false;
return true;
}
int main()
{
SqStack a;
InitStack(a);
LpNode b=new Node;
LpNode c=new Node;
LpNode d=new Node;
//free(b);这free了你下面又赋值。。。
b-a=1;
Push(a,c);
GetTop(a,c);
printf("%d",c-a);
return 0;
}
栈里的内存是不能free的,你要free你就自己在堆里分配。
[img]关于c语言栈的实现以及操作和c语言栈的基本运算的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。