gpt4 book ai didi

c - reverseList 函数不在队列中工作

转载 作者:太空宇宙 更新时间:2023-11-04 03:23:44 25 4
gpt4 key购买 nike

这是使用两个栈的队列的主程序

#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 中它只插入零。

最佳答案

你的反向堆栈逻辑是错误的。您正在推送到 head2。如果 temp->nextNULL,而 temphead1

如果我是正确的,head1 指向堆栈中的最顶层数据。

逻辑上不应该是:

while (temp) {
push(head2,temp->ch);
temp = temp->next;
}

所以 head2 指向接收到的第一个数据并充当队列。

关于c - reverseList 函数不在队列中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43062343/

25 4 0
文章推荐: python - PEP 8 和延迟导入
文章推荐: css - 为什么 会包含
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com