使用数组

时间:2015-06-03 15:12:42

标签: c arrays stack

我使用数组在堆栈实现上创建了这个程序。它在无限循环中运行而不询问用户输入。

有人可以指出此计划所需的必要更改。代码正在运行且没有编译错误,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;
}

2 个答案:

答案 0 :(得分:1)

你可以从CreateStack返回一些东西开始,正如预期的那样。

答案 1 :(得分:0)

首先,我尝试了ideone.com,而不是要求用户输入。因此,我建议您在自己的计算机上安装编译器并自行编译。

1。 yourPictureBox.Invalidate()方法中的if语句错误,应该是StackPush(stack, int),而不是StackIsFull(stack)

StackIsEmpty(stack)

2。您未在void StackPush(stack *s, int data) { if(StackIsFull(s)) { printf("Stack overflow\n"); exit(1); } s->array[++s->top] = data; } 方法中使用变量element。所以我用变量StackSearch(stack, int)

替换它
data

此外,对于记录,此方法根据顶部而非底部打印索引。

这是我所做的唯一重要改变。但是我修改了代码格式和输出格式,你可以在下面找到完整的代码:

void StackSearch(stack *s, int data)
{
    int i, j;
    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\n", data, j);
            }
        }

        if(counter == 0)
            printf("Element not found in stack\n");
    }
    printf("\n");
}