【问题标题】:Static stack operations in CC中的静态堆栈操作
【发布时间】:2013-01-23 07:16:25
【问题描述】:

我正在尝试使用 C 中的数组在堆栈上执行所有操作(推送、弹出、窥视、更新、显示)。当我在调用我的所有函数后最后调用 show() 时,它工作正常需要。但是每当我在任何操作之前调用show() 时,它并没有给我适当的结果。 我正在使用以下代码:

int main()
{
    push(1);
    push(2);
    push(3);
    push(4);
    push(6);
    pop();
    push(5);
    show();//line 8
    //push(7);//line 9
    //pop();
    //peep();
    //update();
    //show();//line 13
    return;
}

void push(int num){//insert an item

    if(top==MAXSIZE-1)
    {
        printf("Overflow condition");
        return;
    }
    top++;
    stack[top]=num;
    //return;
}

void pop()//delete a item from top
{

    int num;
    if(top==-1)
    {
        printf("Underflow condition");
        return;
    }
    num=stack[top];
    top--;
    //return;
}

void show()//display elements
{

    if(top==-1){
        printf("Underflow");
        return;
    }
    while(top!=-1){
        printf("%d\n",stack[top--]);
    }
    //return;
}

void peep()//extract information
{

    int loc,num;
    printf("enter location:\n");
    scanf("%d",&loc);
    if(top-loc+1 < 0)
    {
        printf("No item at the given location\n");
        return;
    }
    else{
        num=stack[top-loc+1];
        printf("\nItem at location %d is %d",loc,num);
    }
}

void update(){//update information

    int loc,item;
    printf("enter new item:");
    scanf("%d",&item);
    printf("enter location:");
    scanf("%d",&loc);
    if(top-loc+1 < 0)
    {
        printf("No item at the given location\n");
        return;
    }
    else{
        stack[top-loc+1]=item;
        printf("\nItem inserted");
    }
}

这里调用show()后,第8行top会指向-1(empty),所以之后会有以下后果:

  • push() 将插入位置 1 而不是顶部。
  • pop() 将显示下溢情况。
  • peep() 和更新将进入如果条件。

那么在调用show() 之后,如何将顶部设置为堆栈中的顶部元素? 谢谢。

【问题讨论】:

    标签: c stack


    【解决方案1】:

    您的show() 方法修改了top 指针,这是错误的:

    void show()//display elements
    {
    
        if(top==-1){
            printf("Underflow");
            return;
        }
        while(top!=-1){
            printf("%d\n",stack[top--]); // <--- here 'top--' will modify the top pointer
        }
        //return;
    }
    

    您可以像这样更改show() 方法:

    void show()//display elements
    {
    
        if(top==-1){
            printf("Underflow");
            return;
        }
        int i = top; // introducing a new variable to iterate through the stack
        while(i!=-1){
            printf("%d\n",stack[i--]); // now 'i' is modified
        }
        //return;
    }
    

    【讨论】:

      【解决方案2】:

      您的 show 函数的一个问题是,它还试图弹出所有数据。您不应该在 show 函数中使用 top--

      【讨论】:

        【解决方案3】:

        //WAP 使用 C 执行堆栈操作-- 1. push, 2.pop, 3.peep, 4.change, 5.top-of-stack, 6.is-empty, 7.is-full, 8.display

        #include<stdlib.h>
        
        #include<stdio.h>
        
        #define max_size 5
        
        int stack[max_size],top=-1,i,x;
            /*------ Function Prototype------------*/
        void push();
        void pop();
        void peep();
        void display();
        void top_stack();
        void change();
        void is_empty();
           /*-------------------------------------*/
        
          int main()
          {
            int choice;
        
            do{
        
                printf("\n\n--------STACK OPERATIONS-----------\n");
                printf("1.Push\n");
                printf("2.Pop\n");
                printf("3.Peep\n");
                printf("4.Display\n");
                printf("5.Change\n");
                printf("6.TOP\n");
                printf("7.exit\n");
                printf("-----------------------");
                printf("\nEnter your choice:\t");
                scanf("%d",&choice);
        
                switch(choice)
               {
                case 1:    push();
                           break;
                case 2:    pop();
                           break;
                case 3:    peep();
                           break;
                case 4:    display();
                           break;
                case 5:    change();
                           break;
                case 6:    top_stack();
                           break;
                case 7:    exit(0);
                           break;
                default:    printf("\nInvalid choice:\n");
                           break;
                }
        
             }while(choice!=7);
              return 0;
            }
        
        
            void push() //Inserting element in to the stack
            {
              int item;
               if(top==(max_size-1))
                {
                    printf("\nStack Overflow:");
                }
               else
                {
                  printf("Enter the element to be inserted:\t");
                  scanf("%d",&item);
                  top=top+1;
                  stack[top]=item;
                 }
             }
        
             void pop()      //deleting an element from the stack
            {
              int item;
               if(top==-1)
               {
                 printf("Stack Underflow:");
               }
               else
               {
                 item=stack[top];
                 top=top-1;
                 printf("\nThe poped element: %d\t",item);
               }
            }
        
            void peep()
            {            
               printf("enter the i th element");
               scanf("%d",&i);
               if(top-i+1<0)
               {
                 printf("\nStack is empty:");
               }
               else
               {
                 printf("The topmost element of the stack is %d",stack[top-i+1]);
               }
            }
        
            void display()
             {
              int i;
                if(top==-1)
                {
                 printf("\nStack is Empty:");
                }
                else
               {
                printf("\nThe stack elements are:\n" );
                 for(i=top;i>=0;i--)
                 {
                  printf("%d\n",stack[i]);
                  }
                }
             }
        
        
            void change()
            {
              printf("enter the i th element");
              scanf("%d",&i);
                if(top-i+1<0)
                {
                 printf("\nStack is empty:");
                }
                else
                {
                 printf("enter the element to be changed\n");
                 scanf("%d",&x);
        
                 stack[top-i+1]=x ;
                 printf("The topmost element of the stack is %d",stack[top-i+1]);
                }
        
            }
            void top_stack()
            {
            int t;
            t=stack[top];
            printf("The Topmost element of stack is =%d",t);
        
            }
        

        【讨论】:

          猜你喜欢
          • 2012-09-06
          • 2010-10-12
          • 2021-03-08
          • 1970-01-01
          • 2018-08-01
          • 1970-01-01
          • 2023-01-11
          • 2011-09-16
          • 1970-01-01
          相关资源
          最近更新 更多