作业
栈的进制转换:
代码:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef int DT;
typedef struct link
{
union
{
DT data;
int len;
}txt;
struct link* next;
}LT;
LT* create_linklist()
{
LT* head=(LT*)malloc(sizeof(LT));
if(NULL==head)
{
printf("创建失败,请稍后重试\n");
return NULL;
}
head->txt.len=0;
head->next=NULL;
return head;
}
//入栈
void ruzhan(LT* head,int i)
{
LT* temp =(LT*)malloc(sizeof(LT*));
if(NULL==temp)
{
printf("创建失败\n");
return ;
}
temp->txt.data=i;
temp->next=NULL;
if(NULL==head->next)
{
head->next=temp;
head->txt.len++;
}
else
{
temp->next=head->next;
head->next=temp;
head->txt.len++;
}
return ;
}
//出栈
void chuzhan(LT* head)
{
if(NULL==head->next)
{
printf("无数据\n");
return ;
}
LT* temp =(LT*)malloc(sizeof(LT*));
if(NULL==temp)
{
printf("创建失败\n");
return ;
}
temp->txt.data=0;
temp->next=NULL;
if(NULL==head->next->next)
{
temp=head->next;
printf("%d ",temp->txt.data);
free(temp);
head->next=NULL;
head->txt.len--;
}
else
{
temp=head->next;
printf("%d ",temp->txt.data);
head->next=head->next->next;
free(temp);
head->txt.len--;
}
return ;
}
int main(int argc, const char *argv[])
{
printf("请输入>>>");
int j;
scanf("%d",&j);
LT* head=create_linklist();
int k=0;
while(j>1)
{
int a=j%2;
j=j/2;
ruzhan(head,a);
k++;
}
printf("%d\n",j);
ruzhan(head,j);
for(int n=0;n<=k;n++)
{
chuzhan(head);
}
printf("\n");
return 0;
}
运行结果:
作业2:栈实现判断括号是否匹配
代码:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef char DT;
typedef struct link
{
union
{
DT data;
int len;
}txt;
struct link* next;
}LT;
LT* create_linklist()
{
LT* head=(LT*)malloc(sizeof(LT));
if(NULL==head)
{
printf("创建失败,请稍后重试\n");
return NULL;
}
head->txt.len=0;
head->next=NULL;
return head;
}
//入栈
void ruzhan(LT* head,char i)
{
LT* temp =(LT*)malloc(sizeof(LT*));
if(NULL==temp)
{
printf("创建失败\n");
return ;
}
temp->txt.data=i;
temp->next=NULL;
if(NULL==head->next)
{
head->next=temp;
head->txt.len++;
}
else
{
temp->next=head->next;
head->next=temp;
head->txt.len++;
}
return ;
}
//出栈
void chuzhan(LT* head)
{
if(NULL==head->next)
{
printf("无数据\n");
return ;
}
LT* temp =(LT*)malloc(sizeof(LT*));
if(NULL==temp)
{
printf("创建失败\n");
return ;
}
temp->txt.data=0;
temp->next=NULL;
if(NULL==head->next->next)
{
temp=head->next;
free(temp);
head->next=NULL;
head->txt.len--;
}
else
{
temp=head->next;
head->next=head->next->next;
free(temp);
head->txt.len--;
}
return ;
}
int main(int argc, const char *argv[])
{
LT* head=create_linklist();
char a[100];
char i;
printf("请输入字符串:");
scanf("%s",a);
int len=strlen(a);
for(int i=0;i<len;i++)
{
if(a[i]=='(')
{
ruzhan(head,a[i]);
}
if(a[i]==')')
{
if(NULL==head->next)
{
printf("括号匹配不成功\n");
return 0;
}
chuzhan(head);
}
}
if(NULL==head->next)
{
printf("括号匹配成功\n");
}
else
{
printf("括号匹配不成功\n");
}
return 0;
}
运行结果:
作业3:链式队列
代码:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "./3_func.h"
int main(int argc, const char *argv[])
{
Lkq_pos* pos=create_list();
while(1)
{
printf("1.入队\n");
printf("2.出队\n");
printf("3.遍历\n");
printf("4.退出\n");
printf("选择>>>");
int xz;
scanf("%d",&xz);
switch(xz)
{
case 1:
rudui(pos);
break;
case 2:
chudui(pos);
break;
case 3:
bianli(pos);
break;
case 4:
goto END;
default:
printf("输入错误\n");
}
}
END:
return 0;
}
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "./3_func.h"
Lkq_pos* create_list()
{
Lkqueue* head = (Lkqueue*)malloc(sizeof(Lkqueue));
if(NULL==head)
{
printf("创建链表失败\n");
return NULL;
}
head->txt.len=0;
head->next=NULL;
Lkq_pos* pos = (Lkq_pos*)malloc(sizeof(Lkq_pos));
if(NULL==pos)
{
printf("创建指向失败\n");
return NULL;
}
pos->front=head;
pos->rear=head;
return pos;
}
//入队
void rudui(Lkq_pos* pos)
{
printf("请输入要入队的数据>>>");
int sj;
scanf("%d",&sj);
Lkqueue* temp=(Lkqueue*)malloc(sizeof(Lkqueue));
if(NULL==temp)
{
printf("创建失败\n");
}
temp->txt.data=sj;
temp->next=NULL;
pos->rear->next=temp;
pos->rear=temp;
pos->front->txt.len++;
return ;
}
//遍历
void bianli(Lkq_pos* pos)
{
if(pos->front->next == NULL)
{
printf("队列无数据\n");
return ;
}
Lkqueue* p=pos->front;
while(p->next!=NULL)
{
p=p->next;
printf("%d ",p->txt.data);
}
printf("\n");
return ;
}
//出队
void chudui(Lkq_pos *pos)
{
if(NULL==pos->front->next)
{
printf("无数据可出队\n");
return ;
}
Lkqueue* temp=(Lkqueue*)malloc(sizeof(Lkqueue));
if(NULL==temp)
{
printf("创建失败\n");
}
temp->txt.data=0;
temp->next=NULL;
if(NULL==pos->front->next->next)
{
temp=pos->front->next;
printf("%d\n",temp->txt.data);
pos->front->next=NULL;
free(temp);
pos->front->txt.len++;
}
else
{
temp=pos->front->next;
printf("%d\n",temp->txt.data);
pos->front->next=pos->front->next->next;
free(temp);
pos->front->txt.len++;
}
return ;
}
#ifndef __FUNC_H__
#define __FUNC_H__
typedef int dataType; //链表中每个结点的类型
typedef struct linkqueue
{
union{
int len; //头结点使用
dataType data; //有 效数据结点使用
}txt; //数据域
struct linkqueue* next ; //指针域
}Lkqueue;
typedef struct
{
Lkqueue* front; //指向头结点,存储头结点地址
Lkqueue* rear; //指向尾结点,存储尾结点地址
}Lkq_pos;
Lkq_pos* create_list();
void rudui(Lkq_pos* pos);
void chudui(Lkq_pos *pos);
void bianli(Lkq_pos* pos);
#endif