c语言编写栈程序,C语言 链表实现栈的程序

//stack.h

#ifndef STACK_H_

#define STACK_H_

#endif

#define STACK_TYPE int

typedef struct node{

STACK_TYPE data;

struct node

*next;

}StackNode,*LinkStack;

//stack.c

#include

#include

#include

#include "Stack.h"

void Init_Stack(LinkStack top) {

top->next = NULL;

}

int Empty_Stack(LinkStack top) {

if (top->next == NULL)

return 1;

return 0;

}

int Push_Stack(LinkStack top, int element) {

StackNode *temp;

temp = (StackNode

*)malloc(sizeof(StackNode));

if (temp == NULL)

return 0;

// temp->name =

*trgcode;//4.不能直接等,需要用strcpy或memcpy

temp->data = element;

temp->next =

top->next;

top->next = temp;

return 1;

}

int Pop_Stack(LinkStack top, int *element) {

StackNode *temp;

if (Empty_Stack(top))

return 0;

temp = top->next;

*element = temp->data;

top->next =

temp->next;

free(temp);

return 1;

}

int Peek_Stack(LinkStack top, int *element) {

if (Empty_Stack(top))

return 0;

else

// *trgcode =

top->next->name;//6.不能直接等,需要用strcpy或memcpy

*element =

top->next->data;

return 1;//7.增加一个return语句,避免警告

}

void Destroy_Stack(LinkStack top) {

StackNode *cp, *np;

cp = top->next;

while (cp != NULL) {

np =

cp->next;

free(cp);

cp = np;

}

top->next = NULL;

return;

}

int main() {

LinkStack s;

// double rslt;//8.没有用到的变量

// double i;

int pushe;

// char *ss;

// char *sss;

// char *ssss = (char*)malloc(20);//9.给ssss分配空间,与name字段的最大长度一样

int i = 1, j = 2, k = 3;

s = (LinkStack)malloc(sizeof(StackNode));

Init_Stack(s);

// ss = "abh0000001";

// i = 99993333333333333.24;

Push_Stack(s, i);

Push_Stack(s, j);

Push_Stack(s, k);

printf("you pushed data is:\n");

Pop_Stack(s, &pushe);

printf("%d\n ", pushe);

Peek_Stack(s, &pushe);

printf("%d \n ", pushe);

return 1;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值