代码 1 : tree
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node * lchild;
struct node * rchild;
}tree_t;
//创建
tree_t* create_tree(int data,int max)
{
if(data>max)
return NULL;
tree_t* tree=malloc(sizeof(tree_t));
tree->data=data;
tree->lchild=create_tree(2*data,max);
tree->rchild=create_tree(2*data+1,max);
return tree;
}
//先序 根-》左-》右 pre
void print_pre(tree_t* tree)
{
if(tree==NULL)
return ;
printf("%3d ",tree->data);
print_pre(tree->lchild);
print_pre(tree->rchild);
return ;
}
//中序 左-》根-》右 mid
void print_mid(tree_t* tree)
{
if(tree==NULL)
return ;
print_mid(tree->lchild);
printf("%3d ",tree->data);
print_mid(tree->rchild);
return ;
}
//后序 左-》右-》根 post
void print_post(tree_t* tree)
{
if(tree==NULL)
return ;
print_post(tree->lchild);
print_post(tree->rchild);
printf("%3d ",tree->data);
return ;
}
//层次 从上往下,从左到右 1,2,3,4,5,6,7,
int print_tree_level(tree_t* tree)
{
if(tree==NULL)
return -1;
tree_t* queue[30];// tree_t **queue=malloc(sizeof(tree_t *)*size);
int head=0;
int tail=0;
queue[tail++]=tree
while(head!=tail)
{
if(queue[head]->lchild!=NULL)
queue[tail++]=queue[head]->lchild;
if(queue[head]->rchild!=NULL)
queue[tail++]=queue[head]->rchild;
printf("%3d ",queue[head++]->data);
}
printf("\n");
}
int main(int argc, const char *argv[])
{
tree_t* tree=create_tree(1,7);
print_pre(tree);
printf("\n");
print_mid(tree);
printf("\n");
print_post(tree);
printf("\n");
print_tree_level(tree);
return 0;
}
代码 2 : queue
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int head;
int tail;
int size;
int * data;
}queue_t;
//创建
queue_t* create_queue(int size)
{
if(size<=0||size>1024)
return NULL;
queue_t* queue=malloc(sizeof(queue_t));
queue->head=0;
queue->tail=0;
queue->size=size;
queue->data=malloc(sizeof(int)*size);
return queue;
}
//判空
int isempty(queue_t* queue)
{
if(queue==NULL)
return 0;
return queue->head==queue->tail;
}
//判满
int isfull(queue_t* queue)
{
if(queue==NULL)
return 0;
return (queue->tail+1)%queue->size==queue->head;
//判满公式1
//基于位置 t +1 空间上 追上 h
//return (queue->tail-queue->head+queue->size)%queue->size==queue->size-1;
//判满公式2
//基于长度 满的时候 == size -1
}
//入队
int push_queue(queue_t* queue,int data)
{
if(queue==NULL||isfull(queue))
return -1;
queue->data[queue->tail]=data;
queue->tail=(queue->tail+1)%queue->size;
return 0;
}
//出队
int pop_queue(queue_t* queue,int *data)
{
if(queue==NULL||isempty(queue))
return -1;
*data=queue->data[queue->head];
// queue->data[queue->head]=0;
queue->head=(queue->head+1)%queue->size;
return 0;
}
//测试打印1 从0元素 打印到 size-1
int print_queue_test(queue_t* queue)
{
if(queue==NULL||isempty(queue))
return -1;
int i;
for(i=0;i<=queue->size-1;i++)
{
printf("%3d ",queue->data[i]);
}
printf("\n");
return 0;
}
//测试打印2 从h 打印到 t
int print_queue_htot(queue_t* queue)
{
if(queue==NULL||isempty(queue))
return -1;
int i;
for(i=queue->head;i!=queue->tail;i=(i+1)%queue->size)
{
printf("%3d ",queue->data[i]);
}
printf("\n");
return 0;
}
//长度
int length_queue(queue_t* queue)
{
if(queue==NULL||isempty(queue))
return 0;
return (queue->tail-queue->head+queue->size)%queue->size;
}
//清空
int clear_queue(queue_t* queue)
{
if(queue==NULL)
return -1;
queue->head=queue->tail=0;
return 0;
}
//销毁
int destroy_queue(queue_t* queue)
{
if(queue==NULL)
return -1;
free(queue->data);
free(queue);
return 0;
}
//真正打印
int print_queue_real(queue_t* queue)
{
if(queue==NULL||isempty(queue))
return -1;
queue_t* temp=create_queue(length_queue(queue)+1);
int data;
while(!isempty(queue))
{
pop_queue(queue,&data);
printf("%3d ",data);
push_queue(temp,data);
}
printf("\n");
while(!isempty(temp))
{
pop_queue(temp,&data);
push_queue(queue,data);
}
destroy_queue(temp);
return 0;
}
//真逆打印
int reprint_queue(queue_t* queue)
{
if(queue==NULL||isempty(queue))
return -1;
int *stack1=malloc(sizeof(int)*length_queue(queue));
int top1=-1;
int *stack2=malloc(sizeof(int)*length_queue(queue));
int top2=-1;
int data;
while(!isempty(queue))
{
pop_queue(queue,&data);
stack1[++top1]=data;
}
while(top1!=-1)
{
data=stack1[top1--];
printf("%3d ",data);
stack2[++top2]=data;
}
printf("\n");
while(top2!=-1)
{
data=stack2[top2--];
push_queue(queue,data);
}
free(stack1);
free(stack2);
return 0;
}
int main(int argc, const char *argv[])
{
queue_t* queue=create_queue(15);
int i;
for(i=1;i<=15;i++)
{
if(push_queue(queue,i)==0)
print_queue_real(queue);
}
int data;
for(i=1;i<=15;i++)
{
if(pop_queue(queue,&data)==0)
{
reprint_queue(queue);
}
}
return 0;
}
代码 3 : linkstack
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node * next;
}linkstack_t;
//创建
linkstack_t* create_stack()
{
linkstack_t* stack=malloc(sizeof(linkstack_t));
stack->next=NULL;
return stack;
}
//判空
int isempty_stack(linkstack_t* stack)
{
if(stack==NULL)
return 0;
return stack->next==NULL;
}
//入栈
int push_stack(linkstack_t* stack,int data)
{
if(stack==NULL)
return -1;
linkstack_t* newnode=create_stack();
newnode->data=data;
newnode->next=stack->next;
stack->next=newnode;
return 0;
}
//出栈
int pop_stack(linkstack_t* stack,int *data)
{
if(stack==NULL||isempty_stack(stack))
return -1;
linkstack_t* temp=stack->next;
*data=temp->data;
stack->next=temp->next;
free(temp);
return 0;
}
//假打印
int print_stack_notreal(linkstack_t* stack)
{
if(stack==NULL||isempty_stack(stack))
return -1;
while(stack->next!=NULL)
{
printf("%3d ",stack->next->data);
stack=stack->next;
}
printf("\n");
return 0;
}
//清空
int clear_stack(linkstack_t* stack)
{
if(stack==NULL)
return -1;
int data;
while(!isempty_stack(stack))
{
pop_stack(stack,&data);
}
return 0;
}
//销毁
int destroy_stack(linkstack_t* stack)
{
if(NULL==stack)
return -1;
if(!isempty_stack(stack))
clear_stack(stack);
free(stack);
return 0;
}
//真打印
int print_stack_real(linkstack_t* stack)
{
if(stack==NULL||isempty_stack(stack))
return -1;
int data;
linkstack_t* temp=create_stack();
while(!isempty_stack(stack))
{
pop_stack(stack,&data);
printf("%3d ",data);
push_stack(temp,data);
}
printf("\n");
while(!isempty_stack(temp))
{
pop_stack(temp,&data);
push_stack(stack,data);
}
destroy_stack(temp);
return 0;
}
//真逆打印
int reprint_stack_real(linkstack_t* stack)
{
if(stack==NULL||isempty_stack(stack))
return -1;
int data;
linkstack_t* temp=create_stack();
while(!isempty_stack(stack))
{
pop_stack(stack,&data);
push_stack(temp,data);
}
while(!isempty_stack(temp))
{
pop_stack(temp,&data);
printf("%3d ",data);
push_stack(stack,data);
}
printf("\n");
destroy_stack(temp);
return 0;
}
int main(int argc, const char *argv[])
{
linkstack_t* stack=create_stack();
int i;
for(i=1;i<=20;i++)
{
push_stack(stack,i);
print_stack_real(stack);
}
int data;
for(i=1;i<=20;i++)
{
pop_stack(stack,&data);
reprint_stack_real(stack);
}
return 0;
}
代码 4 : linkqueue
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node* next;
}list_t;
typedef struct queuenode{
list_t* head;
list_t* tail;
}queue_t;
//创建
queue_t* create_queue()
{
queue_t* queue=malloc(sizeof(queue_t));
queue->head=malloc(sizeof(list_t));
queue->head->next=NULL;
queue->tail=queue->head;
return queue;
}
//判空
int isempty(queue_t* queue)
{
if(queue==NULL)
return 0;
return queue->head->next==NULL;
}
//入队
int push_queue(queue_t* queue,int data)
{
if(queue==NULL)
return -1;
list_t* newnode=malloc(sizeof(list_t));
newnode->next=NULL;
newnode->data=data;
queue->tail->next=newnode;
queue->tail=newnode;
return 0;
}
//出队
int pop_queue(queue_t* queue,int *data)
{
//尝试 全部取出数据,再放入几个数据,再打印看一下,删除真的没问题么????
if(queue==NULL||isempty(queue))
return -1;
list_t* temp=queue->head->next;
if(temp==queue->tail)
queue->tail=queue->head;
*data=temp->data;
queue->head->next=temp->next;
free(temp);
return 0;
}
//长度
int length_queue(queue_t* queue)
{
if(queue==NULL||isempty(queue))
return 0;
list_t* temp=queue->head;
int sum=0;
while(temp->next!=NULL)
{
sum++;
temp=temp->next;
}
return sum;
}
//假打印
int print_queue_notreal(queue_t* queue)
{
if(queue==NULL||isempty(queue))
return -1;
list_t* temp=queue->head;
while(temp->next!=NULL)
{
printf("%3d ",temp->next->data);
temp=temp->next;
}
printf("\n");
return 0;
}
//清空
int clear_queue(queue_t* queue)
{
if(queue==NULL||isempty(queue))
return -1;
int data;
while(!isempty(queue))
{
pop_queue(queue,&data);
}
return 0;
}
//销毁
int destroy_queue(queue_t* queue)
{
if(queue==NULL)
return -1;
if(!isempty(queue))
clear_queue(queue);
free(queue->head);
free(queue);
return 0;
}
//真正打印1
int print_queue_real(queue_t* queue)
{
if(queue==NULL||isempty(queue))
return -1;
queue_t* temp=create_queue();
int data;
while(!isempty(queue))
{
pop_queue(queue,&data);
printf("%3d ",data);
push_queue(temp,data);
}
printf("\n");
while(!isempty(temp))
{
pop_queue(temp,&data);
push_queue(queue,data);
}
destroy_queue(temp) ;
return 0;
}
//真正打印2
int print_queue_real2(queue_t* queue)
{
if(queue==NULL||isempty(queue))
return -1;
int *stack1=malloc(sizeof(int)*length_queue(queue));
int top1=-1;
int *stack2=malloc(sizeof(int)*length_queue(queue));
int top2=-1;
int data;
while(!isempty(queue))
{
pop_queue(queue,&data);
printf("%3d ",data);
stack1[++top1]=data;
}
while(top1!=-1)
{
data=stack1[top1--];
stack2[++top2]=data;
}
while(top2!=-1)
{
data=stack2[top2--];
push_queue(queue,data);
}
printf("\n");
free(stack1);
free(stack2);
return 0;
}
//真逆打印
int reprint_queue_real(queue_t* queue)//经典面试题:两个栈实现一个队列功能
{
if(queue==NULL||isempty(queue))
return -1;
int *stack1=malloc(sizeof(int)*length_queue(queue));
int top1=-1;
int *stack2=malloc(sizeof(int)*length_queue(queue));
int top2=-1;
int data;
while(!isempty(queue))
{
pop_queue(queue,&data);
stack1[++top1]=data;
}
while(top1!=-1)
{
data=stack1[top1--];
printf("%3d ",data);
stack2[++top2]=data;
}
while(top2!=-1)
{
data=stack2[top2--];
push_queue(queue,data);
}
printf("\n");
free(stack1);
free(stack2);
return 0;
}
int main(int argc, const char *argv[])
{
queue_t* queue=create_queue();
int i;
for(i=1;i<=15;i++)
{
push_queue(queue,i*2);
print_queue_real(queue);
}
reprint_queue_real(queue);
print_queue_real2(queue);
print_queue_real(queue);
print_queue_notreal(queue);
return 0;
}
网友评论