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;
}