C语言:链栈的基本操作(代码如下:帮我看看错哪了)

Python015

C语言:链栈的基本操作(代码如下:帮我看看错哪了),第1张

典型的指针形参问题。

源代码见网页端

#include <stdio.h>

#include <stdlib.h>

typedef struct StackNode

{

int data

struct StackNode *next

}SNode

//只进栈一个元素

void Push(SNode *s, int e)

{

SNode *p

p = (SNode*)malloc(sizeof(SNode))

s->data = e

p->next = s

s = p

//return s

}

//连续进栈n个元素

void Push_Series(SNode *s)

{

printf("请输入进栈的元素数:")

int n, i, e

scanf("%d", &n)

for (i = 0 i<n i++)

{

printf("请输入第%2.d个进栈元素:", i + 1)

scanf("%d", &e)

Push(s, e)

//printf("此阶段的元素为:%2.d\n",s->next->data)

}

}

void Pop(SNode *s)

{

if (s == NULL)

{

printf("栈空了!!!")

exit(0)

}

SNode *q

int e

e = s->data

printf("*****************\n")

printf("栈顶元素为:%d ", e)

printf("*****************\n")

q = s

s = s->next

free(q)

}

//★:s是形参,你在里面申请的内存空间地址是无法赋值给调用此函数的s的!

void InirStack(SNode **s) //★:∴应用指针的指针!(或者C++里用引用)

{

*s = (SNode*)malloc(sizeof(SNode))

(*s)->next = NULL

}

int main()

{

SNode *s //★你这个s没有申请内存空间!

/*

SNode *s = (SNode *)malloc(sizeof(SNode))

s->next = NULL*/

InirStack(&s) //★此处要把指针的指针代入

Push_Series(s)

Pop(s)

printf("Hello world!\n")

return 0

}

#include <stdio.h>

//#include<stdlib.h>

#include <malloc.h>

typedef int DataType

typedef struct node {

DataType data

struct node *next

}Node,*Stack,*ps

Stack GetEmptyStack() {

Stack s = (ps)malloc(sizeof(Node))

s->next = NULL

return s

}

void InitStack(Stack &s) { //链栈初始化

s =  (ps)malloc(sizeof(Node)) // 配置头结点方便后续操作

s->next = NULL 

}

void DestroyStack(Stack s) {//销毁链栈

ps q,p = s->next

while(p) { //依次释放链栈的每一个结点

q = p

p = p->next

free(q)

//p = s

}

free(s) //最后删除头结点

}

void Push(Stack &s,DataType x) { //入栈操作

ps t = (ps)malloc(sizeof(Node)) 

t->data = x

t->next = s->next

s->next = t

}

int Empty(Stack s) {

return (s->next == NULL)

}

int Pop(Stack s,DataType *ptr) {

ps p

if(Empty(s)) {

printf("下溢错误,删除失败。\n")

ptr = NULL

return 0 // 0返回值时,ptr指向的内容不可用

}

p = s->next

*ptr = p->data // 存取要删除的栈顶元素

s->next = p->next // 头指针指向下一个数据结点

free(p)

return 1

}

int GetTop(Stack s,DataType *ptr) { //取栈顶元素

if(Empty(s)) {

printf("下溢错误。\n")

return 0

}

*ptr = s->next->data

return 1

}

int main() {

DataType x

Stack s = GetEmptyStack() // 定义链栈的栈顶指针并初始化

// InitStack(s) // 指针相当于一个地址&

printf("对15和10进行入栈操作\n")

Push(s,15)

Push(s,10)

if(GetTop(s,&x)) printf("当前栈顶元素为:%d\n",x) // 输出当前栈顶元素10

if(Pop(s,&x)) printf("执行一次出栈操作,等同于删除栈顶元素:%d\n",x) // 输出出栈元素10

if(GetTop(s,&x)) printf("现在的栈顶元素为:%d\n",x) // 输出当前栈顶元素15

printf("请输入待插元素:")

scanf("%d",&x)

Push(s,x)

if(Empty(s)) printf("栈为空。\n")

else printf("栈并不为空。\n")

return 0

}

//顺序栈

#include

#include

#include

#define

STACK_INIT_SIZE

100

#define

STACKINCREMENT

10

typedef

struct

{

int

*base

int

*top

int

stacksize

}SqStack

typedef

int

ElemType

int

InitStack(SqStack

&S)

//为栈S分配存储空间,并置S为空栈

{

int

size

=

STACK_INIT_SIZE

S.base=(int

*)malloc(size*sizeof(ElemType))

if(!S.base)

return

0

S.top=S.base

//置栈S为空栈

S.stacksize=STACK_INIT_SIZE

return

1

}

int

GetTop(SqStack

S,int

&e)

//若栈不空,则用e返回S的栈顶元素

{

if(S.top==S.base)

return

0

e=*(S.top-1)

return

1

}

int

Push(SqStack

&S,

int

e)

/*进栈函数,将e插入栈S中,并使之成为栈顶元素*/

{

if(S.top-S.base>=S.stacksize)

/*栈满,追加存储空间*/

{

int

stackinvrement

=

STACKINCREMENT

S.base=(ElemType

*)

realloc(S.base,(S.stacksize+stackinvrement)*sizeof(ElemType))

if(!S.base)

return

0

/*存储分配失败*/

S.stacksize+=STACKINCREMENT

}

*S.top++=e

return

1

}

int

Pop(SqStack

&S,int

&e)/*出栈函数,若栈S不空,则删除S的栈顶元素,用e返回其值*/

{

if(S.top==S.base)

return

0

e=*--S.top

return

1

}

void

OutputStack(SqStack

&S)

{int

*q

q=S.top-1

for(int

i=0i

#include

typedef

struct

SNode

{

int

data

struct

SNode

*next

}SNode,*LinkStack

LinkStack

top

LinkStack

PushStack(LinkStack

top,int

x)

//入栈

{

LinkStack

s

s=(LinkStack)malloc(sizeof(SNode))

s->data=x

s->next=top

top=s

return

top

}

LinkStack

PopStack(LinkStack

top)

//退栈

{

LinkStack

p

if(top!=NULL)

{

p=top

top=top->next

free(p)

printf("退栈已完成\n")

return

top

}

else

printf("栈是空的,无法退栈!\n")

return

0

}

int

GetStackTop(LinkStack

top)

//取栈顶元素

{

return

top->data

}

bool

IsEmpty()//bool取值false和true,是0和1的区别,bool只有一个字节,BOOL为int型,bool为布尔型

{

return

top==NULL

?

true:false

}

void

Print()

{

SNode

*p

p=top

if(IsEmpty())

{

printf("The

stack

is

empty!\n")

return

}

while(p)

{

printf("%d

",

p->data)

p=p->next

}

printf("\n")

}

void

main()

{

int

x,a,b

char

m

do

{

printf("\n")

printf("###############链栈的基本操作##################\n")

printf("××××××××1.置空栈××××××××××\n")

printf("××××××××2.进栈×××××××××××\n")

printf("××××××××3.退栈×××××××××××\n")

printf("××××××××4.取栈顶元素××××××××\n")

printf("××××××××5.退出程序×××××××××\n")

printf("##############################################\n")

printf("\n请选择一个字符:")

scanf("%c",&m)

switch(m){

case

'1':

top=NULL

printf("\n栈已置空!")

break

case

'2':

printf("\n请输入要进栈的元素个数是:")

scanf("%d",&a)

printf("\n请输入要进栈的%d个元素:",a)

for(b=0b

评论

0

0

加载更多