开发者

C stack array problem

My function code for peek is not working? why is that? can anyone help me with my peek function?

#include<stdio.h>
#include<stdlib.h>
#define maxsize 10

int stack[maxsize];
int stacktop=0;

void instructions();
int process();
int push(int value);
int pop();
void display();
void peek();

int main()
{
    process();
    getch();
}

int process()
{
    int val;
    int choice;
    do
    {
        instructions();

        printf("Enter Your Choice: ");
        scanf("%d",&choice);

        switch( choice )
        {
        case 1:
            printf("\nElement to be Pushed : ");
            scanf("%d",&val);
            push(val);
            break;

        case 2:
            val=pop();
            if(val!=-1)
            {
                printf("Popped Element : %d\n",val);
            }
            break;

        case 3:
            peek();
            break;

开发者_如何学运维        case 4:
            display();
            break;

        case 5:
            break;       
        }

    }while(choice !=5);
}

void instructions()
{
    printf("Enter Your choice for the following process\n");
    printf("\n[1]Push a Node on top of the list");
    printf("\n[2]Pop a node off the list");
    printf("\n[3]Peek The Top Node");
    printf("\n[4]Display The Whole list");
    printf("\n[5]Exit The Program\n");
}

int push(int val)
{
    if(stacktop<maxsize)
    {
        stack[stacktop++]=val;
    }
    else
    {
        printf("Stack is full");
    }
}

int pop()
{
    int a;
    if(stacktop>0)
    {
        a=stack[--stacktop];
        return a;
    }
}

void display()
{
    int i;
    i = 0;
    if(stacktop>0)
    {
        printf("Elements are:");
        while(i<stacktop)
        {
            printf("\n%d--\n",stack[i++]);
        }

    }
}

void peek()
{
    printf("%d",stacktop);  
}


Is it supposed to be:

printf("%d\n", stack[stacktop - 1]);

Print the contents, rather than the size of the stack?

Obviously you'd also need to bounds check to make sure you're not printing outside of the range of your stack (when it's empty)


I know this isn't Code Review, but I thought I would give you a few bits of advice.

  1. When you call scanf, always check the result. For example, if the user enters something other than a decimal number, your code will end up putting an indeterminate value into the choice or val variables. The scanf function returns the number of items that were successfully read. If you asked for one item, and scanf returns 1, then you can rely on the value of that object:

    int choice;
    if (scanf("%d", &choice) != 1)
        // handle error, can't rely on value of "choice"
    else
        // continue onwards, can rely on value of "choice"
    
  2. Usually, the \n escapes go at the end of the string literal, not at the beginning. It is more common to do it this way, but it doesn't mean it should always go at the end.

    printf("Enter Your choice for the following process\n\n");
    printf("[1]Push a Node on top of the list\n");
    printf("[2]Pop a node off the list\n");
    printf("[3]Peek The Top Node\n");
    
  3. For outputting simple strings, consider just using the puts function, which automatically appends the new-line character for you:

    puts("Enter Your choice for the following process");
    puts("");
    puts("[1]Push a Node on top of the list");
    puts("[2]Pop a node off the list");
    puts("[3]Peek The Top Node");
    
  4. Your display method is a perfect example of when to use a for loop instead of a while loop. Generally speaking, use a for loop when you know exactly how many items you have and you want to iterate over each of them:

    void display()
    {
        int i;
    
        puts("Elements are:");
    
        for (i = 0; i < stacktop; i++)
            printf("\n%d--\n", stack[i]);
    }
    
  5. To reverse the order of the stack, simply start at the top and go backwards:

    void display()
    {
        int i;
    
        puts("Elements are:");
    
        for (i = stacktop - 1; i >= 0; i--)
            printf("\n%d--\n", stack[i]);
    }
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜