【问题标题】:reverseList function not working in queuereverseList 函数在队列中不起作用
【发布时间】:2017-08-21 02:25:57
【问题描述】:

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

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

【问题讨论】:

标签: c linked-list queue


【解决方案1】:

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

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

逻辑不应该是:

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

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

【讨论】:

  • 即使使用您的代码也不能解决问题。 for stack1=[8,4,0] 给出 stack2=[0,4,8,0,4,0]
  • 我上面提到的逻辑是反转一次,通过查看 o/p 你在每次推送时都在做反转堆栈。我认为如果你稍微改变一下逻辑会更好。由于只有在弹出时才需要反向逻辑,所以在弹出时需要反向堆栈,删除元素并重新填充堆栈。
  • 好像你在每次推送时都试图以相反的方式维护堆栈,它只不过是一个队列。所以,在某种程度上它违反了问题陈述。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-26
  • 1970-01-01
  • 1970-01-01
  • 2017-01-08
  • 2015-08-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多