1.------------------------用數組實現棧
#include <stdio.h>
char stack[512];
int top=0;
void push(char c)
{
stack[top]=c;
top++;
}
int pop()
{
top--;
return stack[top];
}
int is_empty(void)
{
return top==0;
}
void oper(int n)
{
do{
push(n%10+48);
}while(n=n/10);
}
int main(void)
{
oper(12345);
while(!is_empty())
putchar(pop());
putchar('\n');
return 0;
}
2.------------------------用數組實現隊列
#include <stdio.h>
#include <stdlib.h>
struct node{
char data;
int flag;
};
int head=0,tail=0;
struct node queue[512];
void enqueue(char c)
{
queue[tail].data=c;
queue[tail].flag=1;
tail=(tail+1)%512;
}
char dequeue()
{
char c=queue[head].data;
queue[head].flag=0;
head=(head+1)%512;
return c;
}
int is_empty(void)
{
return (head==tail)&&(queue[head].flag==0);
}
void oper(int n)
{
do{
enqueue(n%10+'0');
}while(n=n/10);
}
int main(void)
{
oper(12345);
while(!is_empty())
putchar(dequeue());
putchar('\n');
return 0;
}
3.------------------------用鏈錶實現棧(頭插法)
#include <stdio.h>
#include <stdlib.h>
typedef struct node *link;
struct node{
int item;
link next;
};
link head=NULL;
link make_node(int item)
{
link p=malloc(sizeof *p);
p->item=item;
p->next=NULL;
return p;
}
void insert_head(link r)
{
r->next=head;
head=r;
}
void push(int c)
{
link p;
p=make_node(c);
insert_head(p);
}
int pop()
{
int item=head->item;
head=head->next;
return item;
}
int is_empty(void)
{
if(head==NULL)
return 0;
}
void oper(int n)
{
do{
push(n%10+'0');
}while(n=n/10);
}
int main(void)
{
oper(12345);
while(is_empty())
putchar(pop());
putchar('\n');
return 0;
}
4.------------------------用鏈錶實現棧(尾插法)
#include <stdio.h>
#include <stdlib.h>
typedef struct node *link;
struct node{
int item;
link next;
};
link head=NULL;
link make_node(int item)
{
link p=malloc(sizeof *p);
p->item=item;
p->next=NULL;
return p;
}
void insert_end(link r)
{
if(head==NULL)
{
r->next=head;
head=r;
return;
}
link p,q;
for(q=p=head;p;p=p->next) q=p;
q->next=r;
}
void push(int c)
{
link p;
p=make_node(c);
insert_end(p);
}
void free_node(link p)
{
free(p);
p=NULL;
}
int pop()
{
int item;
link p,q;
if(head->next==NULL)
{
item=head->item;
head=head->next;
return item;
}
for(q=head,p=head;p->next;p=p->next) q=p;
item=p->item;
q->next=p->next;
free_node(p);
return item;
}
int is_empty(void)
{
if(head==NULL)
return 0;
else
return 1;
}
void oper(int n)
{
do{
push(n%10+'0');
}while(n=n/10);
}
int main(void)
{
oper(12345);
while(is_empty())
putchar(pop());
putchar('\n');
5.------------------------用鏈錶實現隊列
#include <stdio.h>
#include <stdlib.h>
typedef struct node *link;
struct node{
char item;
int flag;
link next;
};
link head=NULL;
link tail=NULL;
void free_node(link p)
{
free(p);
p=NULL;
}
link make_node(char item)
{
link p=malloc(sizeof *p);
p->item=item;
p->flag=0;
p->next=NULL;
return p;
}
void insert_end(link r)
{
r->flag=1;
if(head==NULL)
{
r->next=head;
head=r;
tail=r;
return;
}
tail->next=r;
tail=r;
}
void enqueue(char c)
{
link p;
p=make_node(c);
insert_end(p);
}
char dequeue()
{
char item=head->item;
head->flag=0;
head=head->next;
return item;
}
int is_empty(void)
{
return head==tail;
}
void oper(int n)
{
do{
enqueue(n%10+'0');
}while(n=n/10);
}
int main(void)
{
oper(12345);
link p,q;
tail->next=head;
while(!is_empty())
putchar(dequeue());
putchar(dequeue());
putchar('\n');
return 0;
}
6.------------------------用隊列實現棧
#include <stdio.h>
#include <stdlib.h>
typedef struct node *link;
struct node{
char item;
link next;
};
link head_A=NULL;
link tail_A=NULL;
link head_B=NULL;
link tail_B=NULL;
void free_node(link p)
{
free(p);
p=NULL;
}
link make_node(char item)
{
link p=malloc(sizeof *p);
p->item=item;
p->next=NULL;
return p;
}
void insert_A(link r)
{
if(head_A==NULL)
{
r->next=head_A;
head_A=r;
tail_A=r;
return;
}
tail_A->next=r;
tail_A=r;
}
void insert_B(link r)
{
if(head_B==NULL)
{
r->next=head_B;
head_B=r;
tail_B=r;
return;
}
tail_B->next=r;
tail_B=r;
}
void enqueue_A(char c)
{
link p;
p=make_node(c);
insert_A(p);
}
void enqueue_B(char c)
{
link p;
p=make_node(c);
insert_B(p);
}
char dequeue_A()
{
if(head_A==tail_A)
tail_A=NULL;
char item=head_A->item;
head_A=head_A->next;
return item;
}
char dequeue_B()
{
if(head_B==tail_B)
tail_B=NULL;
char item=head_B->item;
head_B=head_B->next;
return item;
}
int is_empty_A(void)
{
return (head_A==NULL);
}
int is_empty_B(void)
{
return (head_B==NULL);
}
int is_empty(void)
{
return is_empty_A()&&is_empty_B();
}
void push(char c)
{
enqueue_A(c);
}
char pop()
{
if(head_A!=NULL)
{
while(head_A!=tail_A)
enqueue_B(dequeue_A());
return dequeue_A();
}
else
{
while(head_B!=tail_B)
enqueue_A(dequeue_B());
return dequeue_B();
}
}
void oper(int n)
{
do{
push(n%10+'0');
}while(n=n/10);
}
int main(void)
{
oper(12345);
while(!is_empty())
putchar(pop());
putchar('\n');
return 0;
}
7.------------------------用棧實現隊列
#include <stdio.h>
#include <stdlib.h>
typedef struct node *link;
struct node{
int item;
link next;
};
link head_A=NULL;
link head_B=NULL;
link make_node(int item)
{
link p=malloc(sizeof *p);
p->item=item;
p->next=NULL;
return p;
}
void insert_A(link r)
{
r->next=head_A;
head_A=r;
}
void insert_B(link r)
{
r->next=head_B;
head_B=r;
}
void push_A(int c)
{
link p;
p=make_node(c);
insert_A(p);
}
void push_B(int c)
{
link p;
p=make_node(c);
insert_B(p);
}
int pop_A()
{
int item=head_A->item;
head_A=head_A->next;
return item;
}
int pop_B()
{
int item=head_B->item;
head_B=head_B->next;
return item;
}
int is_empty(void)
{
if(!is_empty_A()&&!is_empty_B())
return 0;
}
int is_empty_A(void)
{
if(head_A==NULL)
return 0;
}
int is_empty_B(void)
{
if(head_B==NULL)
return 0;
}
void enqueue(char c)
{
if(is_empty_B())
{
while(is_empty_B())
push_A(pop_B());
}
push_A(c);
}
char dequeue()
{
if(is_empty_A())
{
while(is_empty_A())
push_B(pop_A());
}
return pop_B();
}
void oper(int n)
{
do{
enqueue(n%10+'0');
}while(n=n/10);
}
int main(void)
{
oper(12345);
while(is_empty())
putchar(dequeue());
putchar('\n');
return 0;
}