数据结构第四天作业

1.1.栈的进制转换,将十进制转换成二进制。[链栈实现]思路:循环把余数入栈

#include <stdio.h>                          
#include <stdlib.h>                         
#include "./01_head.h"                      
#include <string.h>                         
//创建链式栈                                
Seq*create_link_stack()                     
{                                           
    Seq*head=(Seq*)malloc(sizeof(Seq));     
    memset(head,0,sizeof(Seq));             
    if(head==NULL)                          
    {                                       
        printf("创建失败\n");               
        return NULL;                        
    }                                       
                                            
    return head;                            
}                                           
//压栈                                      
void push(Seq*head,int data)                
{                                           
    Seq*temp=(Seq*)malloc(sizeof(Seq));     
    //初始化                                
    memset(temp,0,sizeof(Seq));             
                                            

    if(head->next==NULL)                    
    {                                       
        temp->txt.data=data;
        temp->next=NULL;                    
        head->next=temp;                    
    }                                       
    else                                    
    {                                       
        temp->next=head->next;
        head->next=temp;
        temp->txt.data=data;                
    }                                       
    head->txt.len++;                        
}                                            
//弹栈                                       
void pop_stack(Seq*head)
{                                            
    if(head->next==NULL)                     
    {                                        
        printf("栈为空\n");                  
        return ;                             
    }                                        
    Seq*temp=head->next;                     
    while(head->next!=NULL)                  
    {                                        
        printf("%2d",head->next->txt.data);  
        head->next=head->next->next;         
    }                                        
    free(temp);                              
                                             
}                                            

 

2.(O(O(O))(O))0)())判断括号是否匹配

#include <stdio.h>                        
#include <stdlib.h>                       
#include "./01_head.h"                    
#include <string.h>                       
//创建链式栈                              
Seq*create_link_stack()                   
{                                         
    Seq*head=(Seq*)malloc(sizeof(Seq));   
    memset(head,0,sizeof(Seq));           
    if(head==NULL)                        
    {                                     
        printf("创建失败\n");             
        return NULL;                      
    }                                     
                                          
    return head;                          
}                                         
//压栈                                    
void push(Seq*head,int data)              
{                                         
    Seq*temp=(Seq*)malloc(sizeof(Seq));   
    //初始化                              
    memset(temp,0,sizeof(Seq));           
                                          
                                          
    if(head->next==NULL)                  
    {                                     
        temp->txt.data=data;              
        temp->next=NULL;                  
        head->next=temp;                  
    }                                     
    else                                  
    {                                     
        temp->next=head->next;            
        head->next=temp;                  
        temp->txt.data=data;              
    }                                     
    head->txt.len++;                      
                                          
}                                         
//弹栈                                    
void pop_stack(Seq*head)                      
{                                             
    if(head->next==NULL)                      
    {                                         
        printf("栈为空\n");                   
        return ;                              
    }                                         
    Seq*temp=head->next;                      
    while(head->next!=NULL)                   
    {                                         
        printf("%2d",head->next->txt.data);   
        head->next=head->next->next;          
    }                                         
    free(temp);                               
                                              
}                                             
主函数
#include <stdio.h>                       
#include "./01_head.h"                   
int main(int argc, const char *argv[])   
{                                        
    //创建链式栈                         
    Seq*head=create_link_stack();        
                                         
    char a[]="(()())()(())";             
    char *p;                             
    p=a;                                 
    while(*p!='\0')                      
    {                                    
        if(strcmp(*p,")")==0)            
        {                                
        printf("不匹配\n");              
        break;                           
        }                                
        else                             
        {                                
            if(strcmp(*p,"(")==0)        
            {                            
                push(head,*p);           
                continue;                
            }                            
        }                                
        p++;                             
    }                                    
    show(head);                          
                                         
    return 0;                            
}                                        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值