Miti059

stack singly

Oct 20th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.99 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct node
  4. {
  5.     int data;
  6.     struct node* next;
  7. }node;
  8.  node* top = NULL;
  9.  
  10.  
  11.  
  12.  
  13. void push(int x)
  14. {
  15.     node* Newnode;
  16.      Newnode=(node*)malloc(sizeof(node));
  17.     Newnode->data=x;
  18.     Newnode->next=top;
  19.     top=Newnode;
  20. }
  21. void pop()
  22. {
  23.     node* temp;
  24.    
  25.         temp=top;
  26.         top=top->next;
  27.         temp->next=NULL;
  28.         free(temp);
  29.    
  30. }
  31. void print()
  32. {
  33.     node* temp;
  34.     if(top==NULL)
  35.     {
  36.         printf("stack is empty:");
  37.     }
  38.     else
  39.     {
  40.         temp=top;
  41.         while(temp!=NULL)
  42.         {
  43.             printf("data %d\n",temp->data);
  44.             temp=temp->next;
  45.         }
  46.     }
  47. }
  48.  
  49.  
  50.  
  51.  
  52. int main(){
  53.  
  54. int x,n,i,m;
  55. printf("Enter input number: ");
  56.      scanf("%d",&n);
  57.      for(i=0;i<n;i++){
  58. printf("Enter stack data: ");
  59.     scanf("%d",&x);
  60.     push(x);}
  61.     print();
  62.     printf("Enter the number to delete");
  63.     scanf("%d",&m);
  64.     pop();
  65.     print();
  66.  
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment