c语言创建一个循环栈,c语言创建栈,压栈,出栈,遍历栈,清空.doc

//创建堆栈,压栈,出栈,遍历栈,清空栈 2012.7.20

#include

#include

#include

void init_stack(struct stack *ps);//初始化

void push(struct stack *ps); //压栈

void pop(struct stack *ps); //出栈

void clear_stack(struct stack *ps);//清空栈

void output_stack(struct stack *ps);// 显示栈的内容

int is_empty(struct stack *ps); //判断是否为空栈

//结构体

struct node

{

char no[20];

char name[20];

struct node * pnext;

};

//结构体

struct stack

{

struct node * top;

struct node * bottom;

};

//主函数

int main(void)

{

int i;

struct stack s;

printf("开始初始化..........\n");

init_stack(&s);

printf("初始化成功!\n");

printf("开始压栈!\n");

for(i=0;i<2;i++)

{

push(&s);

}

printf("压栈完成!\n");

printf("显示栈!\n");

output_stack(&s);

printf("开始出栈!\n");

pop(&s);

printf("显示栈!\n");

output_stack(&s);

printf("清空栈!\n");

clear_stack(&s);

printf("显示栈!\n");

output_stack(&s);

return 0;

}

//初始化,建立一个空的栈

void init_stack(struct stack *ps)

{

ps->top = (struct node *)malloc(sizeof(struct node));

if(ps->top == NULL)

{

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

exit(-1);

}

else

{ ps->bottom = ps->top;

(ps->top)->pnext = NULL;

}

return ;

}

//压栈,链表原理

void push(struct stack *ps)

{

struct node *p;

p = (struct node *)malloc(sizeof(struct node));

if(p == NULL)

{

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

exit(-1);

}

else

{printf("请压入学号:");

scanf("%s",p->no);

printf("请压入姓名:");

scanf("%s",p->name);

p->pnext = ps->top;

ps->top = p;

printf(" 压入成功!\n");

}

return;

}

//出栈 链表原理

void pop(struct stack *ps)

{

struct node * p;

if(is_empty(ps))

{ printf("栈是空的,没数据可出!\n");

return;

}

else

{

p = ps->top;

ps->top = p->pnext;

free(p);

}

return;

}

//清空栈里的内容

void clear_stack(struct stack *ps)

{

struct node * p,*q;

if(is_empty(ps))

{printf("栈是空的,不能再清除!\n");

return ;

}

else

{ p = ps->top; // 定义两个指针变量,同时指向栈顶

q = p

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个基本的顺序的实现,包含入(push)和出栈(pop)操作。 ``` #include <stdio.h> #include <stdlib.h> #define MAXSIZE 100 // 定义的最大容量 typedef struct { int data[MAXSIZE]; // 存放中元素 int top; // 顶指针 } SqStack; // 初始化 void InitStack(SqStack *S) { S->top = -1; // 顶指针初始化为-1 } // 判断是否为空 int StackEmpty(SqStack S) { if (S.top == -1) { // 顶指针为-1,为空 return 1; } return 0; } // 判断是否已满 int StackFull(SqStack S) { if (S.top == MAXSIZE - 1) { // 顶指针等于最大容量减一,已满 return 1; } return 0; } // 入操作 int Push(SqStack *S, int x) { if (StackFull(*S)) { // 已满,无法入 return 0; } S->top++; // 顶指针加一 S->data[S->top] = x; // 将元素x入 return 1; } // 出栈操作 int Pop(SqStack *S, int *x) { if (StackEmpty(*S)) { // 为空,无法出栈 return 0; } *x = S->data[S->top]; // 将顶元素出栈 S->top--; // 顶指针减一 return 1; } int main() { SqStack S; int x; InitStack(&S); // 初始化 Push(&S, 1); // 入 Push(&S, 2); Push(&S, 3); Pop(&S, &x); // 出栈 printf("%d\n", x); // 输出出栈元素 Pop(&S, &x); printf("%d\n", x); Pop(&S, &x); printf("%d\n", x); Pop(&S, &x); // 尝试出栈 return 0; } ``` 需要注意的是,顶指针的初始值应该为-1,而不是0。在入操作时,先将顶指针加一,再将元素入;在出栈操作时,先将顶元素出栈,再将顶指针减一。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值