Featured post

-Write C program to find even and odd number using macro

-Write C program to find even and odd number using macro #include<stdio.h> #define number(n)((n%2==0)?1:0) int main() { int num; printf("enter any number"); scanf("%d",&num); if(number(num)) printf("number is even"); else printf("number is odd"); return 0; } ***************************Output*********************** amr@amrj-virtual-machine:~$ gccevenodd.c amr@samr-virtual-machine:~$ ./a.out enter any number 2 number is evensmj@amr-virtual-machine:~$

Stack in Data structure

      Stack Definition:          
Amreshtechknowledge.blogspot.com

Stack is a order collection of homogeneous data elements where the insertion and deletion operation take place only one end that is called Stack.

  • It is a LIFO (Last in First out) Technique. 
  • It is a Abstract Data type.
  • The position of stack where the operation is perform is called TOS(Top of Stack).                     
                              Stack is Basically there are two operation.

  1. Push:-This is a insert or adding element in a stack i.e is called Push.
  1. Pop:-This is a delete or remove the element of stack is called POP.
     

Insert and delete  a specific number in stack by using push() and pop()


#include<stdio.h>
struct Node
{
   int data;
   struct Node *next;
}
*top = NULL;
void main()
{
   int ch, value;
      printf("\n:: Stack using Linked List ::\n");
   do{
      printf("\n****** MENU ******\n");
      printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
      printf("Enter your choice: ");
      scanf("%d",&ch);
      switch(ch)
{
      case 1: printf("Enter the value to be insert: ");
            scanf("%d", &value);
            push(value);
            break;
      case 2: pop(); break;
      case 3: display(); break;
      case 4: exit(0);
      default: printf("\nWrong selection!!! Please try again!!!\n");
      }
     } while(ch!=4);
     return 0;
}
void push(int value)
{
   struct Node *newNode;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = value;
   if(top == NULL)
      newNode->next = NULL;
   else
      newNode->next = top;
   top = newNode;
   printf("\nInsertion is Success!!\n");
}
void pop()
{
   if(top == NULL)
      printf("\nStack is Empty!!!\n");
   else{
      struct Node *temp = top;
      printf("\nDeleted element: %d", temp->data);
      top = temp->next;
      free(temp);
   }
}
void display()
{
   if(top == NULL)
      printf("\nStack is Empty!!!\n");
   else{
      struct Node *temp = top;
      while(temp->next != NULL){
      printf("%d\t",temp->data);
      temp = temp -> next;
      }
      printf("%d--->NULL",temp->data);
   }

Comments

Post a Comment

Welcome to FlutterForge please write your Query

Popular posts from this blog

-Write C program to find even and odd number using macro

BCA (SCIENCE) JAVA PRACTICAL SLIPS PAGES.(JAVA) UNIVERSITY OF PUNE

How to OCR Text Extraction in Flutter Using Google ML Kit