这是使用两个堆栈排队的主程序
#include<stdio.h>
#include<stdlib.h>
#include"link.h"
int main(){
int input,element,popped;
int size,emptiness;
printf("queue created\n");
stack demo;
stack *s = malloc(sizeof(demo));
stack *p = malloc(sizeof(demo));
create(&s);
create(&p);
printList(&s);
printList(&p);
while(1){
printf("Choose option: Enqueue, Dequeue, Head-tail, iseMpty, getSize, eXit: ");
scanf("%d",&input);
switch(input){
case 1:
printf("Enter element to be enqueued: ");
scanf("%d",&element);
push(&s,element);
reverseStack(&s,&p);
printf("\nstack1: ");
printList(&s);
printf("\tstack2: ");
printList(&p);
printf("\n");
break;
case 2:
pop(&s);
popped = deleteFirst(&p);
printf("popped: %d\n",popped);
printf("\nstack1: ");
printList(&s);
printf("\tstack2: ");
printList(&p);
printf("\n");
break;
case 5:
size = getSize(&p);
printf("size of queue: %d\n",size);
break;
case 6:
return 0;
case 4:
emptiness = isEmpty(&p);
if(emptiness==0)
printf("yes the queue is empty\n");
else
printf("no, the queue is not empty\n");
break;
}
}
return 0;
}
这是ADT文件
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"link.h"
stack demo;
void create(stack **l){
*l==NULL;
}
void reverseStack(stack **head,stack **head2){
printf("entered here\n");
stack *temp = malloc(sizeof(demo));
int size = getSize(head);
temp=*head;
if(temp==NULL){
printf("entered in if statement\n");
return 0;
}
while(1){
if(temp->next==NULL){
// printf("temp next is null\n");
push(head2,temp->ch);
break;
}
else{
temp=temp->next;
printf("temp->next is done");
}
}
}
void printList(stack **head){
stack *printvar = malloc(sizeof(demo));
printvar = *head;
printf("[");
while(1){
if(printvar==NULL){
break;
}
printf("%d",printvar->ch);
printvar = printvar->next;
}
printf("] ");
}
void push(stack **head,int data){
stack *temp = malloc(sizeof(demo));
temp->ch = data;
if(*head==NULL){
temp->next=NULL;
}
else
temp->next=*head;
*head = temp;
}
int deleteFirst(stack **head){
int flag=1;
stack *first =malloc(sizeof(demo));
stack *second =malloc(sizeof(demo));
first=*head;
second=*head;
if(first==NULL){
return -1;
}
else{
while(flag){
if(first->next==NULL){
second->next=NULL;
flag=0;
}
second=first;
first=first->next;
}
}
return(second->ch);
}
int pop(stack **head){
stack *temp = malloc(sizeof(demo));
temp = *head;
if(temp==NULL){
return -1;
}
else{
*head = (*head)->next;
return(temp->ch);
}
}
int peek(stack **head){
stack *temp = malloc(sizeof(demo));
temp = *head;
if(temp==NULL){
return -1;
}
else{
return(temp->ch);
}
}
int isEmpty(stack **head){
if(*head==NULL)
return 0;
else
return 1;
}
int getSize(stack **head){
int len=0;
stack *nxt_element = malloc(sizeof(demo));
nxt_element = *head;
while(nxt_element!=NULL ){
len++;
nxt_element = nxt_element->next;
}
return len-1;
}
这是头文件-
#ifndef _STACK_H
#define _STACK_H
typedef struct stack stack;
struct stack{
int ch;
stack *next;
};
void create(stack **);
void push(stack **, int );
int pop(stack **);
int peek(stack **);
int isEmpty(stack **);
int getSize(stack **);
void reverseStack(stack** , stack **);
void printList(stack **);
int deleteFirst(stack **);
#endif
每当我在默认情况下运行这个程序时,默认情况下0作为堆栈的第一个元素出现,reverseStack函数也不能正常工作,在stack2中它只插入0。
最佳答案
你的反向堆栈逻辑是错误的。您正在推至head2
。如果temp->next
是NULL
,而temp
是head1
。
如果我是正确的,则head1
指向堆栈中最上面的数据。
逻辑不应该是:
while (temp) {
push(head2,temp->ch);
temp = temp->next;
}
因此
head2
指向接收到的第一个数据并充当队列。