栈:
代码 1 : stack
#include <stdio.h>
#include <stdlib.h>
//类型
typedef struct node{
int *data;
int top;
int size;
}stacks_t;
//创建
stacks_t * create_stack(int size)
{
if(size<0||size>1024)
return NULL;
stacks_t* stack=malloc(sizeof(stacks_t));
stack->size=size;
stack->top=-1;
stack->data=malloc(sizeof(int)*size);
return stack;
}
//判空
int isempty(stacks_t* stack)
{
if(stack==NULL)
return 0;
return stack->top==-1;
}
//判满
int isfull(stacks_t* stack)
{
if(stack==NULL)
return 0;
return stack->top==stack->size-1;
}
//入栈 push
int push_stack(stacks_t* stack,int data)
{
if(stack==NULL||isfull(stack))
return -1;
stack->top++;
stack->data[stack->top]=data;
return 0;
}
//出栈
//不使用 返回值的情况下,把栈顶元素拿出来 放到某个变量里面去
//指针
int pop_stack(stacks_t* stack,int *data)// int * 双向传参 可读可写 const 只读
{
if(stack==NULL||isempty(stack)||data==NULL)
return -1;
*data=stack->data[stack->top--];
return 0;
}
//打印 (假打印)
int print_stack_notreal(stacks_t* stack)
{
if(stack==NULL||isempty(stack))
return -1;
int i;
for(i=stack->top;i>=0;i--)
{
printf("%3d ",stack->data[i]);
}
printf("\n");
return 0;
}
//看一眼 查看栈顶元素
int view_stack(stacks_t* stack)
{
if(stack==NULL||isempty(stack))
return -1;
printf("%3d ",stack->data[stack->top]);
return 0;
}
//清空
int clear_stack(stacks_t* stack)
{
if(NULL==stack)
return -1;
stack->top=-1;
return 0;
}
//销毁
int destroy_stack(stacks_t* stack)
{
if(stack==NULL)
return -1;
free(stack->data);
free(stack);
return 0;
}
//长度
int length_stack(stacks_t* stack)
{
if(stack==NULL||isempty(stack))
return 0;
return stack->top+1;
}
//真打印
int print_stack_real(stacks_t* stack)
{
if(stack==NULL||isempty(stack))
return -1;
stacks_t* temp=create_stack(length_stack(stack));
int data;
while(!isempty(stack))
{
view_stack(stack);
pop_stack(stack,&data);
push_stack(temp,data);
}
printf("\n");
while(!isempty(temp))
{
pop_stack(temp,&data);
push_stack(stack,data);
}
destroy_stack(temp);
return 0;
}
//真逆打印
int reprint_stack_real(stacks_t* stack)
{
if(stack==NULL||isempty(stack))
return -1;
stacks_t* temp=create_stack(length_stack(stack));
int data;
while(!isempty(stack))
{
pop_stack(stack,&data);
push_stack(temp,data);
}
while(!isempty(temp))
{
view_stack(temp);
pop_stack(temp,&data);
push_stack(stack,data);
}
printf("\n");
destroy_stack(temp);
return 0;
}
int main(int argc, const char *argv[])
{
stacks_t* stack=create_stack(30);
int i;
for(i=1;i<=15;i++)
{
push_stack(stack,i);
print_stack_notreal(stack);
}
print_stack_real(stack);
reprint_stack_real(stack);
print_stack_notreal(stack);
return 0;
}
代码 2 : 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;
}
代码 3 : 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;
}
网友评论