0

Beginner here!

I want to add one more function to my program, the peek operation in the stack, but I don't know how to do it. When the user chooses the third option which is the peek operation. I want the program to print the top id number, name, and course.

The output I want,

The topmost ID number is: The topmost Name is: The topmost Course is:

Here is my code for other functions.

#include<stdio.h>
#include<malloc.h>

typedef struct info
{
    char name[20],courses[50];
    int idnum;
    struct info *next;
};

struct info *push(struct info *);
struct info *pop(struct info *);
struct info *peek(struct info *);
void display(struct info *);
void printline();

int main()
{
    struct info *top=NULL;
    int ch;
    printline();
    printf("\t STUDENT MANGAMENT SYSTEM\n");
    printline();
    printf(" [1] - PUSH");
    printf("\n [2] - POP");
    printf("\n [3] - PEEK");
    printf("\n [4] - DISPLAY");
    printf("\n [0] - EXIT\n");
    printline();
    do
    {
         printf("Enter your choice: ");
         scanf("%d",&ch);
         printline();
         switch(ch)
         {
             case 1:
             top=push(top);
             break;
             case 2:
             top=pop(top);
             break;
             case 3:

                 break;
             case 4:
             display(top);
             case 0:
                 printline();
                 printf("Thank you \nHave a Nice Day!\n");
                break;
        }
     }while(ch!=0);
}

struct info *push(struct info *top)
{
    struct info *p;
    p=(struct info *)malloc(sizeof(struct info));
    if(p==NULL)
    {
         p -> next = NULL;
        top = p;
    }
    else
    {
        printf("Enter the Student name: ");
        scanf("%s",&p->name);
        printf("Enter Student course: ");
        scanf("%s",&p->courses);
        printf("Enter the ID number of Student: ");
        scanf("%d",&p->idnum);
        p->next=top;
        top=p;
    }
    return(top);
}

struct info *pop(struct info *top)
{

   struct info *p;
   if(top==NULL)
   {
       printf("nothing to pop");
   }
   else
   {
       printf("\nThe Student name is: %s",top->name);
       printf("\nThe Student course is: %s",top->courses);
       printf("\nThe ID Number of the student is: %d",top->idnum);
       top=top->next;
   }
   return(top);
}
struct info *peek(struct info *top){

}

void display(struct info *top)
{
    if(top==NULL)
    {
        printf("NOTHING TO DISPLAY\n");
        printline();
    }
    else
    {
        while(top!=NULL)
        {
             printline();
             printf("\t STUDENT MANGAMENT SYSTEM\n");
             printline();
             printf("\nThe student name is: %s",top->name);
             printf("\nThe student address is: %s",top->courses);
             printf("\nThe marks of the student is: %d \n",top->idnum);
             printline();
             top=top->next;
         }
     }
}
void printline(){
    int i;
    for(i=0; i<50; i++)
        printf("-");
        printf("\n");
}
anna
  • 41
  • 5
  • What's the difference between `pop` and `peek` in your words? – Gerhardh Nov 27 '22 at 07:08
  • `typedef struct info` You forgot to add the type that you want to define. – Gerhardh Nov 27 '22 at 07:08
  • In your case `peek` means get the `top` without removing it, isn't it? So is what your are doing in option `[4] -DISPLAY`, you don't need any function. – David Ranieri Nov 27 '22 at 07:14
  • @Gerhardh Pop, delete the top most element, while peek is to see or view the topmost element. – anna Nov 27 '22 at 07:14
  • 1
    Then which line of `pop` is responsible for removing top element? What about just not doing this in `peek`? – Gerhardh Nov 27 '22 at 07:15
  • 1
    @DavidRanieri I also fell for that. `display` iterates over the whole stack. – Gerhardh Nov 27 '22 at 07:18
  • Ah, ok, `display` shows all the list, then you want to copy the `pop` function but removing the `top=top->next;` line. – David Ranieri Nov 27 '22 at 07:21
  • Does this answer your question? [How to do peek operation in queue in C?](https://stackoverflow.com/questions/74665740/how-to-do-peek-operation-in-queue-in-c) – Allan Wind Dec 07 '22 at 03:43

1 Answers1

0

Copy the code in pop function but remove the top=top->next;

anna
  • 41
  • 5
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 30 '22 at 09:35