栈的基本操作和应用

本文探讨了栈这种数据结构的基本操作,包括入栈、出栈、查看栈顶元素等,并详细阐述了栈在实际问题中的应用,如括号匹配、深度优先搜索等场景。

栈的基本操作和应用

栈的顺序存储

实现代码:
#include <iostream>    
using namespace std;    
#define MAX 120//栈最大容量   
class stack       
{       
private:       
    int arr[MAX];     
    int top;     
public:    
    void inItStack()     
    {     
        top=-1;     
    }       
    stack()     
    {       
        inItStack();     
    }      
    bool isEmpty()    
    {    
        if(top == -1) return true;    
        else return false;    
    }      
    void push(int a)     
    {      
        top++;    
        if(top < MAX)  {       
            arr[top]=a;     
        }else{       
            cout<<"栈已满,无法存入"<<a<<endl;       
        }       
    }                                                                        
    int pop()    
    {        
        if(isEmpty())   {       
            cout<<"STACK IS EMPTY ";    
            return NULL;       
        } else {       
            int data=arr[top];     
            arr[top]=NULL;     
            top--;    
            return data;     
        }       
    }       
};       
int main()       
{       
    int i;  
    stack a;   
    for(i=0;i<120;i++){  
        a.push(i);      
    }     
    cout<<"Pop:"<<a.pop()<<endl;    
    a.push(120);//由于前面一行,出栈了一个数,所以还可以入栈一个 
	a.push(121);//继续入栈则会报错  
    cout<<"Pop:"<<a.pop()<<endl;          
    return 0;       
}


输出结果:
Pop:119
栈已满,无法存入121
Pop:120
请按任意键继续. . .

二、栈的链式存储

实现代码:
#include<iostream>
#include<malloc.h>
#define maxsize 100
using namespace std;
typedef struct node
{
	int data;
	struct node *next;
}lnode ,*linkstack;
//初始化栈
void init(linkstack *top)
{
	if( ( (*top)=(linkstack)malloc(sizeof(lnode)) )==NULL )//(给*top分配一个存储空间让top指向这个空间) 
	exit(-1);
	(*top)->next=NULL;
} 
//判断是否为空
int empty(linkstack top)
{
	if(top->next==NULL)
		return 1;
	else 
		return 0;
}
//取出栈顶元素 
int get(linkstack top,int *e)
{
	lnode *p;
	p=top->next; 
	if(!p)
	{
	cout<<"栈已空!";
	return 0;
	}
	else
	{
		*e=p->data;
		cout<<*e<<endl;
		return *e;
	}
}
//入栈操作 
int push(linkstack top,int e)
{
  	lnode *p;
	if( (p=(linkstack)malloc(sizeof(lnode)))==NULL )//(给*top分配一个存储空间让top指向这个空间) 
	{
		printf("分配内存失败");
		exit(-1);
		return 0;
	}

	p->data=e;
	p->next=top->next;
	top->next=p;
	return 1;
}
int pop1(linkstack top,int *e)
{
	linkstack p=top->next;
	if(p==NULL)
	{
		cout<<"栈已空!";
		return 0; 
	}
	top->next=p->next;
	*e=p->data;
	free(p);	
	return 1;
}
int length(linkstack top)
{
	int i=0;
	lnode *p=top;
	while(p->next!=NULL)
	{
	p=p->next;
	i++;
	}
	return i;
}
void clear(linkstack top)
{
	lnode *p,*q;
	p=top;
	while(!p)
	{
		q=p;
		p=p->next;
		free(q);
	}
}
int main()
{
	linkstack s;
	lnode *p;
	int i;
	int a[]={1,2,3};
	int e;
	init(&s);
	cout<<"将1 2 3依次入栈!"<<endl; 
	for(i=0;i<sizeof(a)/sizeof(a[0]);i++)
	{
		push(s,a[i]);
	}

	cout<<"栈顶元素为:";
	if(get(s,&e)==0)
	{
		cout<<"栈为空";
		return 0; 
	}
	cout<<"将4入栈"<<endl; 
	push(s,4);
	cout<<"将5入栈"<<endl; 
	push(s,5);
	cout<<"栈中元素个数为:"<<length(s)<<endl;
	cout<<"将5出栈"<<endl;
 	pop1(s,&e); 
	cout<<"将4出栈"<<endl;
	pop1(s,&e);
	cout<<"将3出栈"<<endl;
	pop1(s,&e); 
	cout<<"栈中元素个数为:"<<length(s);
	cout<<endl;
	cout<<"出栈元素的序列:";
	while(!empty(s))
	{
		pop1(s,&e);
		cout<<e<<"  ";
	}	
	cout<<endl;
}


输出结果:
将1 2 3依次入栈!
栈顶元素为:3
将4入栈
将5入栈
栈中元素个数为:5
将5出栈
将4出栈
将3出栈
栈中元素个数为:2
出栈元素的序列:2  1
请按任意键继续. . .
三、栈的项目实践:
#include <stdio.h>  
#include <stdlib.h>  
#include <conio.h>  
#define N 10  
typedef struct   
{  
    int data[N];//栈的元素  
    int top; //数组的下标,表示栈顶下标  
}srack;  
//初始化栈  
void initsrack(srack *s)  
{  
    s->top=-1;  
}  
//判断栈是否为空  
void srackempty(srack *s)  
{  
    if (s->top==-1)  
        printf("栈为空\n");  
    else  
        printf("栈不为空\n");  
}  
//删除栈顶元素  
void pop(srack *s)  
{  
    int e;  
    if (s->top==-1)  
        printf("栈为空");  
    else  
    {  
        e=s->data[s->top];  
        s->top--;  
        printf("删除元素%d后还剩%d个元素\n",e,s->top+1);  
    }  
}  
//入栈  
void push(srack *s)  
{  
    if (N-s->top==1)//栈满  
    {  
        printf("栈已满\n");  
    }  
    else  
    {  
        printf("输入进栈的值:");  
        scanf("%d",&s->data[++s->top]);  
        printf("入栈成功\n");  
    }  
}  
void print()  
{  
    printf("输入口令:\n");  
    printf("1.判断栈是否为空\n");  
    printf("2.入栈\n");  
    printf("3.显示栈元素个数以及栈顶元素\n");  
    printf("4.删除栈顶元素\n");  
    printf("5.退出\n\n\n");  
}  
void display(srack *s)  
{  
    if (s->top==-1)  
        printf("栈为空");  
    else  
    printf("此时栈有%d个元素,栈顶元素为%d\n",s->top+1,s->data[s->top]);  
}  
int main()  
{  
    print();//显示菜单  
    int n;//口令变量  
    int c=1;  
    srack *s;  
    s=(srack*)malloc(sizeof(srack));  
    initsrack(s);//初始化栈  
  
    while (c)  
    {  
    printf("\n输入操作口令:\n");  
    scanf("%d",&n);   
    system("cls");//清屏    
    print();  
  
    switch(n)  
    {  
    case 1:srackempty(s);break;//判断栈是否为空  
    case 2:push(s);break;//入栈  
    case 3:display(s);break;//显示  
    case 4:pop(s);break;//出栈  
    case 5:c=0;  
    }  
    }  
}  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值