• 数据结构--链栈操作


    描述

     

    创建一个链栈,能够完成栈的初始化、入栈、出栈、获取栈顶元素、销毁栈等操作。

    部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码。

    void Destroy(LinkStack *top)
    {
    	LinkStack *p=top;
    	while(p)
    	{
    		top=p->next;
    		free(p);
    		p=top;
    	}
    	
    }
    
    int main()
    {
    	LinkStack *s;
    	s=InitStack();
    	char cmd[10];
    	int x, res;
    	while(scanf("%s", cmd)!=EOF)
    	{
    		if(strcmp(cmd, "push")==0)
    		{
    			scanf("%d", &x);
    			s=Push(s, x);
    		}
    		else if(strcmp(cmd, "top")==0)
    		{
    			res = GetTop(s, &x);
    			if(res==0)
    				printf("EMPTY
    ");
    			else
    				printf("%d
    ", x);
    		}
    		else
    			s = Pop(s);
    		
    	}
    	Destroy(s);
    	return 0;
    }

    输入

     

    输入数据由以下几种命令组成:

    (1)push x:将x压入栈

    (2)pop:出栈

    (3)top:获取栈顶元素

    每个命令占一行,以EOF结束。

     

    输出

     

    当执行pop时输出出栈的元素,当执行top时输出栈顶元素。


    当栈为空时,需要输出EMPTY。

     

    样例输入

     

    push 7
    push 3
    top
    pop
    pop
    pop

     

    样例输出

    3
    3
    7
    EMPTY

    代码测试:

    #include<stdio.h>
    #include<string.h>
    #include<malloc.h>
    typedef struct LinkStack{
        int data;
        struct LinkStack *next; 
    }LinkStack;
    
    LinkStack* InitStack(){  //初始化栈 
        LinkStack *top;
        top=(LinkStack*)malloc(sizeof(LinkStack));
        top->next=NULL;
        return top;
    }
    
    LinkStack* Push(LinkStack *top,int x){  //压栈 
        LinkStack *p;
        p=(LinkStack*)malloc(sizeof(LinkStack));
        p->data=x;
        p->next=top;
        return p;
    }
    
    int GetTop(LinkStack *top,int *x){  //获取栈顶元素 
        if(top->next==NULL) return 0;
        //*x=top->data;
        else return 1; 
    }
    
    LinkStack* Pop(LinkStack *top){  //出栈 
        if(top->next==NULL)
            printf("EMPTY
    ");
        else{
            printf("%d
    ",top->data);
            top=top->next;
        }
        return top;
    }
    
     void Destroy(LinkStack *top) //销毁栈 
    {
        LinkStack *p=top;
        while(p)
        {
            top=p->next;
            free(p);
            p=top;
        }
        
    }
    
    int main()
    {
        LinkStack *s;
        s=InitStack();
        char cmd[10];
        int x, res;
        while(scanf("%s", cmd)!=EOF)
        {
            if(strcmp(cmd, "push")==0)
            {
                scanf("%d", &x);
                s=Push(s, x);
            }
            else if(strcmp(cmd, "top")==0)
            {
                res = GetTop(s, &x);
                if(res==0)
                    printf("EMPTY
    ");
                else
                    printf("%d
    ", x);
            }
            else
                s = Pop(s);
            
        }
        Destroy(s);
        return 0;
    } 
    View Code
  • 相关阅读:
    MFC绘图机制(二)-双缓存
    C89:论数组/指针/引用
    C89:论预处理命令
    图像优化大坑
    MFC 对话框和属性表
    jQuery-plugin-pagePiling
    jquery-ui-chosen
    JavaScript DOM编程艺术小笔记
    微信公众号素材
    iOS沙箱传值
  • 原文地址:https://www.cnblogs.com/momo-88/p/8940968.html
Copyright © 2020-2023  润新知