代碼 1 : tree
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node * lchild;
struct node * rchild;
}tree_t;
//創(chuàng)建
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;
//創(chuàng)建
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
//基于長(zhǎng)度 滿的時(shí)候 == size -1
}
//入隊(duì)
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;
}
//出隊(duì)
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;
}
//測(cè)試打印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;
}
//測(cè)試打印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;
}
//長(zhǎng)度
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;
//創(chuàng)建
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;
//創(chuàng)建
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;
}
//入隊(duì)
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;
}
//出隊(duì)
int pop_queue(queue_t* queue,int *data)
{
//嘗試 全部取出數(shù)據(jù),再放入幾個(gè)數(shù)據(jù)逼友,再打印看一下呀闻,刪除真的沒(méi)問(wèn)題么化借??捡多?蓖康?
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;
}
//長(zhǎng)度
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)//經(jīng)典面試題:兩個(gè)棧實(shí)現(xiàn)一個(gè)隊(duì)列功能
{
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;
}