1.链式队列
1)头文件
#ifndef __LD_FUNC_H__
#define __LD_FUNC_H__
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef int dataType;
typedef struct Linkqueue
{
union
{
dataType data;
int len;
} txt;
struct Linkqueue* next;
}Linkqueue;
struct pos
{
Linkqueue* front;
Linkqueue* real;
};
struct pos* create_lk();
//尾插
void insert_head(struct pos* pos);
//头删
void delate_tail(struct pos*pos);
//遍历
void show(struct pos* pos);
#endif
2)功能函数
#include"ld_func.h"
struct pos* create_lk()
{
Linkqueue* head =(Linkqueue*)malloc(sizeof(Linkqueue));
if(head == NULL)
{
printf("头结点创建失败!!\n");
}
memset(head,0,sizeof(Linkqueue));
struct pos* pos = (struct pos*)malloc(sizeof(struct pos));
if(pos == NULL)
{
printf("位置指针创建失败!!\n");
free(head);
}
pos->front = head;
pos->real = head;
return pos;
}
//尾插
void insert_head(struct pos* pos)
{
printf("请输入要插入的数据:\n");
dataType data;
scanf("%d",&data);
Linkqueue *temp = (Linkqueue*)malloc(sizeof(Linkqueue));
temp->txt.data = data;
temp->next = NULL;
pos->real->next = temp;
pos->real = pos->real->next;
pos->front->txt.len++;
return;
}
//头删
void delate_tail(struct pos*pos)
{
dataType data = pos->front->next->txt.data;
Linkqueue* temp = pos->front->next;
pos->front->next = pos->front->next->next;
free(temp);
pos->front->txt.len--;
printf("数据%d出队成功!\n",data);
return;
}
//遍历
void show(struct pos* pos)
{
Linkqueue* p = pos->front;
while(p->next!= NULL)
{
p=p->next;
printf("%d ",p->txt.data);
}
printf("\n");
}
3)主函数
#include"ld_func.h"
int main(int argc, const char *argv[])
{
struct pos* pos = create_lk();
insert_head(pos);
insert_head(pos);
insert_head(pos);
insert_head(pos);
insert_head(pos);
show(pos);
delate_tail(pos);
delate_tail(pos);
show(pos);
return 0;
}
2.栈的进制转换,将十进制换成二进制【链栈实现】
//进制转换
void turn(struct Lz* head)
{
printf("请输入需要转换的数字:\n");
int data;
scanf("%d",&data);
int len=0;
while(data/2 != 0)
{
push(head,data%2);
data = data/2;
len++;
}
push(head,data%2);
len++;
for(int i =0;i<len;i++)
{
pop(head);
}
return;
}
3.判断括号是否匹配