c c++ 栈

本文通过两种方式实现栈的数据结构,一种是使用数组实现静态分配的顺序栈,另一种是使用链表实现动态分配的链栈。文章提供了完整的代码示例,并详细解释了每种实现方式的特点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
#define STACK_INIT_SIZE 100;
#define STACKINCREMENT 10;
#define OK 1


typedef struct Stack{

int *base;
int *top;
int stacksize;
//int data[stacksize];
}SqStack;


int GreatStack(struct Stack *S)

{
//S.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
//if(!S.base) exit(OVERFLOW);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return OK;
}


int Push(struct Stack *S,int 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;printf("e=%d",*S->top);//这的错误
S->top++;//printf("e=%d",S->top);
return OK;
}


void main()

{
int n,e;
struct Stack S;
GreatStack(&S);
scanf("%d",&n);
while(n)
{ e=n%2;//printf("e=%d",e);
Push(&S,e);
printf("e=%d",e);
n=n/2;printf("n=%d",n);
}
}
这是我在51cto论坛看到的一篇关于堆栈的帖子。上面这段代码有一个问题:
它建立了一个用于存储栈的头尾指针大小等数据的结构体,但是指针没有初始化。往栈存数据的时候,栈在哪里呢,数据都扔到了内存的哪一部分呢?这就是问题所在。下面是一个论坛大大的代码,敬上:
用链表表示:
// * ======================================== */
// *    程式实例: 5_2b.c                     */
// *    使用链结串列来构建两个栈            */
// * ======================================== */
#include <stdlib.h>

struct stack_node                 // * 栈的结构宣告     */
{
   int data;                      // * 栈资料           */
   struct stack_node *next;       // * 指向下一节点       */
};
typedef struct stack_node stack_list; // * 串列新型态     */
typedef stack_list *link;         // * 串列指标新型态     */

link stack1 = NULL;               // * 栈指标1          */
link stack2 = NULL;               // * 栈指标2          */

// * ---------------------------------------- */
// *  栈资料的存入                          */
// * ---------------------------------------- */
link push(link stack,int value)
{
   link new_node;                 // * 新节黠指标         */

   // * 配置节点记忆体 */
   new_node = ( link ) malloc(sizeof(stack_list));
   if ( !new_node )
   {
      printf("记忆体配置失败! \n");
      return NULL;                // * 存入失败           */
   }
   new_node->data = value;        // * 建立节点内容       */
   new_node->next = stack;        // * 新节点指向原开始   */
   stack = new_node;              // * 新节点成为栈开始 */
   return stack;
}

// * ---------------------------------------- */
// *  栈资料的取出                          */
/  * ---------------------------------------- */
link pop(link stack,int *value)
{
   link top;                      // * 指向栈顶端       */

   if ( stack != NULL )
   {
      top = stack;                // * 指向栈顶端       */
      stack = stack->next;        // * 栈指标指向下节点 */
      *value = top->data;         // * 取出资料           */
      free(top);                  // * 释回节点记忆体     */
      return stack;               // * 传回栈指标       */
   }
   else
      *value = -1;
}

// * ---------------------------------------- */
// *  检查栈是否是空的                      */
// * ---------------------------------------- */
int empty(link stack)
{
   if ( stack == NULL )           // * 是否是空           */
      return 1;                   // * 空的               */
   else
      return 0;                   // * 不是空的           */
}

// * ---------------------------------------- */
// *  主程式: 用数组建立两个栈.             */
// * ---------------------------------------- */
void main()
{
   int list1[6] = { 1, 2, 3, 4, 5, 6 };  // * 数组1内容   */
   int list2[6] = { 7, 6, 9, 4, 3, 0 };  // * 数组2内容   */
   int i,temp;

   for ( i = 0; i < 6; i++ )      // * 推入数组内容       */
   {
      stack1 = push(stack1,list1[i]);    // * 存入至栈1 */
      stack2 = push(stack2,list2[i]);    // * 存入至栈2 */
   }
   printf("原来的数组顺序(1): ");
   for ( i = 0; i < 6; i++ )
      printf("[%d]",list1[i]);
   printf("\n");                  // * 换行               */
   printf("栈取出的顺序(1): ");
   while ( !empty(stack1) )       // * 取出全部栈内容   */
   {
      stack1 = pop(stack1,&temp); // * 取出栈1资料      */
      printf("[%d]",temp);
   }
   printf("\n");                  // * 换行               */
   printf("原来的数组顺序(2): ");
   for ( i = 0; i < 6; i++ )
      printf("[%d]",list2[i]);
   printf("\n");                  // * 换行               */
   printf("栈取出的顺序(2): ");
   while ( !empty(stack2) )       // * 取出全部栈内容   */
   {
      stack2 = pop(stack2,&temp); // * 取出栈1资料      */
      printf("[%d]",temp);
   }
   printf("\n");                  // * 换行               */
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值