栈和队列的操纵

程序代码:

#include<stdio.h>
#include<stdlib.h>

//最小栈(俩种方法)

#define queueMax 100
//#define NodeType int          //符合其他题
#define NodeType char           //符合匹配题
typedef struct queue{
int data[queueMax];
int size;
}queue;

typedef struct Que{
queue* A;
queue* B;
}Que;
//关于结点到结构体
typedef struct Node{
NodeType data;
struct Node* next;
}LinkNode;


typedef struct ueue{
LinkNode* in;
LinkNode* out;
}Queue;


//创建新的结点
LinkNode* CreateNewNode(NodeType value){
LinkNode * new_node = (LinkNode*)malloc(sizeof(LinkNode));
new_node->data = value;
new_node->next = NULL;
}

//删除新的结点
void Destroy(LinkNode* p){
free(p);
}

//栈到初始化
int StackInit(LinkNode** head){
if(head == NULL){
//非法输入
return 0;
}
if((*head)== NULL){
//空栈
return 1;
}
LinkNode* cur = (*head);
while(cur !=NULL){
LinkNode * tmp = cur;
cur = cur->next;
Destroy(tmp);
}
(*head) = cur;
return 1;
}


//取栈顶元素
LinkNode*  GetStackTop(LinkNode* head){
if(head == NULL){
//空栈
return NULL;
}
LinkNode* cur = head;
return cur;
}


//出栈
void StackPop(LinkNode** head){
if(head == NULL){
//非法输入
return;
}
if((*head)==NULL){
//空栈
return;
}
LinkNode* cur = (*head);
LinkNode* tmp = cur;
cur = cur->next;
Destroy(tmp);
LinkNode* tmp1 = cur;
cur = cur->next;
Destroy(tmp1);
(*head) = cur;
return;
}


//入栈
void StackPush(LinkNode** head,NodeType value){
if(head == NULL){
//非法 输入
return;
}
NodeType num;
if((*head) == NULL){
num = value;
}else{
LinkNode* ret = GetStackTop((*head));
num = ret->data;
}
LinkNode* new = CreateNewNode(value);
LinkNode* cur = (*head);
new->next = cur;
cur = new; 
LinkNode* newTop = CreateNewNode(num);
newTop->next = cur;
cur = newTop;
(*head) = cur;
return;
}


//打印栈中元素,便于查看栈到操作结果
void PrintStack(LinkNode* head,char * msg){
printf("\n*********%s**************\n",msg);
LinkNode* cur = head;
while(cur != NULL){
printf("%d ",cur->data);
cur = cur->next;
}
printf("\n");
}


void TextStack(){
LinkNode* head = (LinkNode*)malloc(sizeof(LinkNode));
StackInit(&head);
StackPush(&head,1);
StackPush(&head,2);
StackPush(&head,3);
StackPush(&head,4);
PrintStack(head,"插入四个元素");
StackPop(&head);
PrintStack(head,"出栈一个元素");
LinkNode* ret = GetStackTop(head);
printf("\n*********取栈中最小元素**************\n");
printf("%d \n",ret->data);
}




//俩个栈实现一个队列
//出栈
void StackPop1(LinkNode** head){
if(head == NULL){
//非法输入
return;
}
if((*head)==NULL){
//空栈
return;
}
LinkNode* cur = (*head);
LinkNode* tmp = cur;
cur = cur->next;
Destroy(tmp);
(*head) = cur;
return;
}
//入栈
void StackPush1(LinkNode** head,NodeType value){
if(head == NULL){
//非法 输入
return;
}
LinkNode* new = CreateNewNode(value);
LinkNode* cur = (*head);
new->next = cur;
cur = new; 
(*head) = cur;
return;
}
void QueuePush(Queue* que,NodeType value){
if(que == NULL){
//非法输入
return;
}
if(que->out == NULL){
StackPush1(&que->in,value);
}else{
while(que->out!=NULL){
LinkNode* ret = GetStackTop(que->out);
StackPush1(&que->in,ret->data);
StackPop1(&que->out);
}
StackPush1(&que->in,value);
}
}
void QueuePop(Queue* que){
if(que == NULL){
//非法输入
return;
}
if(que->in==NULL&&que->out==NULL){
//空队列
return;
}else if(que->in==NULL&&que->out!=NULL){
StackPop1(&que->out);
}else if(que->in!=NULL&&que->out == NULL){
while(que->in!=NULL){
LinkNode* ret = GetStackTop(que->in);
StackPush1(&que->out,ret->data);
StackPop1(&que->in);
}
StackPop1(&que->out);
}
}


void QueueInit(Queue* que){
que->in = (LinkNode*)malloc(sizeof(LinkNode));
que->out = (LinkNode*)malloc(sizeof(LinkNode));
StackInit(&(que->in));
StackInit(&(que->out));
//if(que == NULL){
// //非法输入
// return;
//}
//if(que->in==NULL&&que->out==NULL){
// //空队列
// return;
//}else if(que->in!=NULL&&que->out ==NULL){
// while(que->in!=NULL){
// StackPop(&(que->in));
// }
//}else if(que->in==NULL&&que->out!=NULL){
// while(que->out!=NULL){
// StackPop(&(que->out));
// }
//}
}
void textStack_to_Queue(){
Queue que;
QueueInit(&que);
QueuePush(&que,1);
QueuePush(&que,2);
QueuePush(&que,3);
QueuePush(&que,4);
PrintStack(que.in,"入队列4个元素");
QueuePop(&que);
PrintStack(que.out,"出队列1个元素");
QueuePop(&que);
PrintStack(que.out,"出队列2个元素");
QueuePop(&que);
PrintStack(que.out,"出队列3个元素");
QueuePop(&que);
PrintStack(que.out,"出队列4个元素");
QueuePop(&que);
PrintStack(que.out,"出队列5个元素,空");
}


//俩个队列实现一个栈
//初始化
void queueInit(queue** q){
(*q)->size = 0;
}
void StackRInit(Que* q){
q->A = (queue*)malloc(sizeof(queue));
q->B = (queue*)malloc(sizeof(queue));
queueInit(&(q->A));
queueInit(&(q->B));
// q->A->size = 0;
// q->B->size = 0;
// q->A->data[queueMax] = {0};
// q->B->data[queueMax] ={0};
}




//出队
void queuePop(queue**q){
(*q)->size = 0;
}
//入栈
void StackRPush(Que* q,int value){
if(q == NULL){
//非法输入
return;
}
if(q->A->size==0&&q->B->size==0){
q->A->data[q->A->size++] = value;
}else if(q->A->size==0&&q->B->size!=0){
q->B->data[q->B->size++] = value;
}else if(q->A->size!=0&q->B->size==0){
q->A->data[q->A->size++]= value;
}
}
//出栈
void StackRPop(Que* q){
int i=0;
if(q ==NULL){
//非法输入
return;
}
if(q->A->size==0&&q->B->size==0){
//空
return;
}else if(q->A->size!=0&&q->B->size==0){
for(i=0;i<q->A->size-1;i++){
q->B[i] = q->A[i];
}
q->B->size = i;
queuePop(&(q->A));
}else if(q->A->size==0&&q->B->size!=0){
for(i=0;i<q->B->size-1;++i){
q->A[i] = q->B[i];
}
q->A->size = i;
queuePop(&(q->B));
}
}
//输出栈中元素
void Print(Que* q,char* msg){
printf("\n*************%s*************\n",msg);
int i=0;
if(q->A->size==0){
for(;i<q->B->size;++i){
printf("%d ",q->B->data[i]);
}
}else{
for(i=0;i<q->A->size;++i){
printf("%d ",q->A->data[i]);
}
}
printf("\n");
}
void textQueue_to_stack(){
Que q;
StackRInit(&q);
StackRPush(&q,1);
StackRPush(&q,2);
StackRPush(&q,3);
StackRPush(&q,4);
Print(&q,"入栈4个元素");
StackRPop(&q);
Print(&q,"出栈1个元素");
StackRPop(&q);
Print(&q,"出栈2个元素");
StackRPop(&q);
Print(&q,"出栈3个元素");
StackRPop(&q);
Print(&q,"出栈4个元素");
StackRPop(&q);
Print(&q,"出栈5个元素,空");
}


//判断字符串是否按照出栈顺序

int is_mate(char*des,char*src ){
LinkNode* head = (LinkNode*)malloc(sizeof(LinkNode));
StackInit(&head);
LinkNode* ret;
while(*des!='\0'){
ret = GetStackTop(head);
if(ret == NULL){
StackPush1(&head,(*des));
++des;
continue;
}else{
char p = ret->data; 
if(p == *src){
StackPop1(&head);
++src;
continue;
}else{
StackPush1(&head,(*des));
++des;
}
}
}
ret = GetStackTop(head);
while(ret!=NULL&&ret->data==*src){
StackPop1(&head);
ret = GetStackTop(head);
++src;
}
if(ret==NULL&&*src=='\0'){
return 1;
}
return 0;
}


void textis_mate(){
char* des = "de";
char* src = "ed";
printf("des = %s   src = %s\n",des,src);
int ret = is_mate(des,src);
if(ret == 1){//匹配
printf("is mate\n");
}else{//不匹配
printf("is not mate!\n");
}
}
//共享栈(一个数组实现俩个栈)
typedef struct node{
int* cur;
int size;
}Node;
typedef struct head{
Node* head1;
Node* head2;
}Head;
int arr[100]={0};
void headInit(Head*head){
if(head == NULL){
//非法输入
return;
}
head->head1 = (Node*)malloc(sizeof(Node));
head->head2 = (Node*)malloc(sizeof(Node));
head->head1->cur = &arr[0];
head->head1->size = 0;
head->head2->cur = &arr[100];
head->head2->size = 0;
}
//head1入栈
void head1Push(Head* H,int value){
H->head1->cur[H->head1->size++]=value;
}
//head1出栈
void head1Pop(Head* H){
--H->head1->size;
}
//head2入栈
void head2Push(Head* H,int value){
H->head2->cur[100-(++H->head2->size)] = value;
}
//haed2出栈
void head2Pop(Head* H){
--H->head2->size;
}
//将H中的元素进行输出
void printfH(Head* H){
printf("head1:");
int i=0;
for(;i<H->head1->size;++i){
printf("%d ",H->head1->cur[i]);
}
printf("\n");
printf("head2:");
for(i=1;i<=H->head2->size;++i){
printf("%d ",H->head2->cur[100-i]);
}
printf("\n");
}
void textshared(){
Head H;
headInit(&H);
head1Push(&H,1);//对head1进行入栈操作
head1Push(&H,2);//对head1进行入栈操作
head1Push(&H,3);//对head1进行入栈操作
head1Push(&H,4);//对head1进行入栈操作
printfH(&H);
head1Pop(&H);   //对head1进行出栈操作
printfH(&H);
head2Push(&H,1);//对head2进行入栈操作
head2Push(&H,2);//对head2进行入栈操作
head2Push(&H,3);//对head2进行入栈操作
head2Push(&H,4);//对head2进行入栈操作
printfH(&H);
head1Pop(&H);   //对head2进行出栈操作
printfH(&H);
}
int main(){
TextStack();
textStack_to_Queue();
textQueue_to_stack();
textis_mate();
textshared();
}

检测结果:

[chaiyandong@localhost shujujiegou]$ ./stack_exec 


*********插入四个元素**************
1 4 1 3 1 2 1 1 


*********出栈一个元素**************
1 3 1 2 1 1 


*********取栈中最小元素**************



*********入队列4个元素**************
4 3 2 1 


*********出队列1个元素**************
2 3 4 


*********出队列2个元素**************
3 4 


*********出队列3个元素**************



*********出队列4个元素**************




*********出队列5个元素,空**************




*************入栈4个元素*************
1 2 3 4 


*************出栈1个元素*************
1 2 3 


*************出栈2个元素*************
1 2 


*************出栈3个元素*************



*************出栈4个元素*************




*************出栈5个元素,空*************


des = de   src = ed
is mate
head1:1 2 3 4 
head2:
head1:1 2 3 
head2:
head1:1 2 3 
head2:1 2 3 4 
head1:1 2 
head2:1 2 3 4 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值