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;

}

数据结构:

是计算机存储、组织数据的方式62616964757a686964616fe58685e5aeb931333365643662。数据结构是指相互之间存在一种或多种特定关系的数据元素的集合。通常情况下,精心选择的数据结构可以带来更高的运行或者存储效率。数据结构往往同高效的检索算法和索引技术有关。

常用数据结构:

数组 (Array)、栈 (Stack)、队列 (Queue)、链表 (Linked List)、树 (Tree)、图 (Graph)、堆 (Heap)、散列表 (Hash)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值