Sunday 21 February 2021

C Program to search for a data in Stack

     The Linked List data structure is the best way to implement Stack operations. This program has a search function to which the data to be searched is passed as an argument. All the nodes are traversed to check whether the given data is present in any node.


Program

//Stack using Linked list

#include<stdio.h>

#include<conio.h>

struct node

{

 struct node *next;

 int data;

}*head,*temp;


void push()

{

 struct node *newnode;

 newnode=(struct node *)malloc(sizeof(struct node));

 printf("Enter the data\t");

 scanf("%d",&newnode->data);

 if(head==NULL)

 {

  head=newnode;

  head->next=NULL;

 }

 else

 {

  newnode->next=head;

  head=newnode;

  }

}

void pop()

{

 if(head==NULL)

 {

  printf("Empty stack\n");

 }

 else

 {

  printf("The popped element is ");

  temp=head;

  head=head->next;

  printf("%d",temp->data);

  free(temp);

 }

}

void display()

{

 if(head==NULL)

 {

  printf("Empty stack\n");

 }

 else

 {

  temp=head;

  while(temp!=NULL)

  {

   printf("%d->",temp->data);

   temp=temp->next;

  }

 }

}

int search(int k)

{

 if(head==NULL)

 {

  printf("Empty stack\n");

 }

 else

 {

  temp=head;

  while(temp!=NULL)

  {

   if (temp->data==k)

     return 1;

   temp=temp->next;

  }

  }

  return 0;

}


main()

{

     int ch,k;

     void push();

     void pop();

     void display();

    int search(int);

     clrscr();

      do

      {

           printf("\n1.Push\n");

          printf("2.Pop\n");

          printf("3.Display\n");

          printf("4. Search\n");

          printf("Enter the choice\t");

          scanf("%d",&ch);

          switch(ch)

          {

               case 1:push();

                            break;

               case 2:pop();

                            break;

               case 3:display();

                            break;

               case 4: printf("Enter the data to be searched ");

                   scanf("%d",&k);

                   if (search(k))

                        printf("The searched data is present in the stack");

                   else

                        printf("The searched data not present in the stack");

                   break;

               default:exit(0);

          }

     }while(1);

}

OUTPUT

1.Push                                                                       

2.Pop                                                                           

3.Display                                                                       

4. Search                                                                       

Enter the choice        3                                                       

3->2->1->                                                                       

1.Push                                                                          

2.Pop                                                                           

3.Display                                                                       

4. Search                                                                       

Enter the choice        4                                                       

Enter the data to be searched 2                                                 

The searched data is present in the stack                                       

1.Push                                                                          

2.Pop                                                                           

3.Display                                                                       

4. Search                                                                       

Enter the choice        4                                                       

Enter the data to be searched 7                                                 

The searched data not present in the stack                                      

1.Push                                                                          

2.Pop                                                                           

3.Display                                                                       

4. Search                                                                       

Enter the choice        5