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;

}

你可能感兴趣的:(c语言编写栈程序)