0
点赞
收藏
分享

微信扫一扫

2021-4-9 【堆栈】【】

mm_tang 2022-08-13 阅读 24

#include<stdlib.h>

#include<stdio.h>

typedef int ElemType;

typedef struct Stack *link;

typedef struct Stack Snode;

struct Stack

{

ElemType data;

struct Stack *next;

};

建立主函数

为了简便起见,下面只给出主函数的基本框架,具体功能的实现请参见后面的完整代码。

int main(){

int i,x;

link head1;

head1=init();

....

}

初始化栈

link init()

{

link p;

p=NULL;

return p;

}

入栈

link push(link Head,ElemType x)

{

link p;

p=(link)malloc(sizeof(Snode));

if(p==NULL)

{

printf("\nMemory Error\n");

return Head;

}

p->data=x;

p->next=Head;

return p;

}

出栈

li

2021-4-9 【堆栈】【】

nk pop(link Head)

{

link p;

p=Head;

if(p==NULL)

printf("\nStack is Empty!\n");

else

{

p=p->next;

free(Head);

}

return p;

}

取栈顶元素

ElemType gettop(link Head)

{

if(Head==NULL)

{

printf("\nStack is Empty\n");

return -1;

}

else

return Head->data;

}

判断栈是否为空

int empty(link Head)

{

if(Head==NULL)

return 1;

else

return 0;

}

显示栈元素

void display(link Head)

{

link p;

p=Head;

if(p==NULL)

printf("\nStack is empty\n");

else

do

{

printf("%d",p->data);

p=p->next;

}while(p!=NULL);

}

释放栈

link setnull(link Head)

{

link p;

p=Head;

while(p!=NULL)

{

p=p->next;

free(Head);

Head=p;

}

return Head;

}

指针仿真堆栈

/ 堆栈演示!/

#include<iostream>

using namespace std;

typedef struct Stack *link;

typedef struct Stack Snode;

struct Stack

{

int data;

struct Stack *next;

};

link init()//初始化栈

{

link p;

p=NULL;

return p;

}

link push(link Head,ElemType x)//入栈

{

link p;

p=(link)malloc(sizeof(Snode));

if(p==NULL)

{

printf("\nMemory Error\n");

return Head;

}

p->data=x;

p->next=Head;

return p;

}

link pop(link Head)//出栈

{

link p;

p=Head;

if(p==NULL)

printf("\nStack is Empty!\n");

else

{

p=p->next;

free(Head);

}

return p;

}

link setnull(link Head)//释放栈

{

link p;

p=Head;

while(p!=NULL)

{

p=p->next;

free(Head);

Head=p;

}

return Head;

}

int lenth(link Head)//获得栈内元素个数

{

int len=0;

link p;

p=Head;

while(p!=NULL)

{

len++;

p=p->next;

}

return len;

}

int gettop(link Head)//获取栈顶元素

{

if(Head==NULL)

{

printf("\nStack is Empty\n");

return -1;

}

else

return Head->data;

}

int empty(link Head)//判断栈是否为空

{

if(Head==NULL)

return 1;

else

return 0;

}

void display(link Head)//显示栈内元素

{

link p;

p=Head;

if(p==NULL)

printf("\nStack is empty\n");

else

do

{

printf("%d",p->data);

p=p->next;

}while(p!=NULL);

}

int main()

{

int i,x;

link head1;

head1=init();

while(i!=6)

{

system("cls");

cout<<"\n 1.Input a stack data";

cout<<"\n 2.Output a stack data";

cout<<"\n 3.Empty or Not";

cout<<"\n 4.Display a top of stack";

cout<<"\n 5.Display the lenth of stack";

cout<<"\n 6.Exit and Free Stack\n";

cout"\n Stack is: ";

display(head1);

cout<<"\n";

cin>>i;

switch(i)

{

case 1:while(1)

{

system("cls");

cout<<"\n -.Input a stack data";

cout<<"\n -.Output a stack data";

cout<<"\n -.Empty or Not";

cout<<"\n -.Display a top of stack";

cout<<"\n -.Display the lenth of stack";

cout<<"\n -.Exit and Free Stack\n";

cout"\n Stack is: ";

display(head1);

cout<<"\n";

cout<<"\ninput a number: until enter -1:\n";

cin>>x;

if(x==-1)

break;

head1=push(head1,x);

}

break;

case 2:head1=pop(head1);

break;

case 3:if(empty(head1))

cout<<"\nStack is empty\n";

else

cout<<"\nStack is not empty\n";

break;

case 4:cout<<"\n The top is "<<gettop(head1)<<endl;

break;

case 5:cout<<"\n The length of stack is "<<length(head1)<<endl;

break;

}

}

system("cls");

head1=setnull(head1);

display(head1);

getchar();

return 0;

}

数组仿真堆栈

不仅用链表可以仿真堆栈,还可以用数组仿真堆栈。堆栈数组声明如下:

int stack[MaxSize];

int top=-1;

举报

相关推荐

0 条评论