我使用数组在堆栈实现上制作了该程序。它正在无限循环中运行,而无需询问用户输入。

有人可以指出该程序所需的必要更改吗?代码正在运行,没有编译错误,在哪里放置CreateStack()存在逻辑错误。

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

/* Stack has three properties. capacity stands for the maximum number of  elements stack can hold.
    top stands for the current size of the stack and array is the array of elements */

typedef struct Stack
{
    int capacity;
    int top;
    int *array;
}stack;


stack* CreateStack(int);
void StackDestroy(stack*);

int StackIsEmpty(stack*);
int StackIsFull(stack*);

void StackPush(stack*, int);
int StackPop(stack*);

void Display(stack*);
void StackSearch(stack*, int);

int StackTop(stack*);
int StackSize(stack*);


/* CreateStack function takes as argument the maximum number of elements the stack can hold, creates
  a stack according to it and returns a pointer to the stack. */

stack* CreateStack(int maxSize)
{
    /* Create a stack*/
    stack *s;
    s=(stack*)malloc(sizeof(stack));

    /* Initialize it's properties*/
    s->array = (int *)malloc(sizeof(int)*maxSize);

    s->capacity=maxSize;
    s->top=-1;

    return s;
}


/* StackDestroy function will destroy the stack after the function is   called. */

void StackDestroy(stack *s)
{
    free(s->array);

    s->array=NULL;
    s->capacity=0;
    s->top=-1;
}


int StackIsEmpty(stack *s)
{
    return s->top<0;
}


int StackIsFull(stack *s)
{
    return (s->top==s->capacity-1);
}


void StackPush(stack *s,int data)
{
    if(StackIsEmpty(s))
    {
        printf("Stack overflow\n");
        exit(1);
    }

    s->array[++s->top]=data;
}


int StackPop(stack *s)
{
    if(StackIsEmpty(s))
    {
        printf("Stack Underflow\n");
        exit(1);
    }

    return (s->array[s->top--]);
}


void Display(stack *s)
{
    int i;

    if(StackIsEmpty(s))
        printf("Stack Is Empty\n");

    else
        for(i=s->top;i>=0;i--)
            printf("%d\n", s->array[i]);
}


void StackSearch(stack *s, int data)
{
    int i, j, element;
    int counter = 0;
    if(StackIsEmpty(s))
        printf("Stack Is Empty\n");

    else
    {
        for(i=s->top,j=1;i>=0;i--,j++)
        {
            if(data==s->array[i])
            {
                counter++;
                printf("Element %d found at position %d",element, j);
                printf("\n");
            }
        }
        if(counter == 0)
            printf("Element not found in stack\n");
    }
    printf("\n");
}


int StackTop(stack *s)
{
    return s->array[s->top];
}


int StackSize(stack *s)
{
    return s->top;
}

int main() {
    int choice, item, size;

    printf("Enter size of stack:");
    scanf("%d", &size);
    stack *s = CreateStack(size);

    printf("Implementation of Stack\n");
    do
    {
        printf("\nMain Menu");
        printf("\n1.Push \n2.Pop \n3.Search \n4.Display \n5.Stack Top \n6.Stack Size \n7.Destroy Stack \n8.exit");
        printf("\nEnter Your Choice");
        scanf("%d", &choice);

        switch (choice)
        {
            case 1:
                printf("\nEnter The element to be pushed");
                scanf("%d", &item);
                StackPush(s, item);
            break;

            case 2:
                item =StackPop(s);
                printf("\nThe popped element is %d", item);
            break;

            case 3:
                printf("\nEnter element to be searched");
                scanf("%d", &item);
                StackSearch(s, item);
            break;

            case 4:
                Display(s);
            break;

            case 5:
                item=StackTop(s);
                printf("\nThe last inserted element is %d",item);
            break;

            case 6:
                item=StackSize(s);
                printf("\nThe size of stack is %d",item);
            break;

            case 7:
                StackDestroy(s);
            break;

            case 8:
                exit(0);
        }
    } while(1);


    return 0;
}

最佳答案

您可以从CreateStack返回某些内容开始,如预期的那样。

10-08 02:10