Advertisement
S_h_u_v_r_o

stackwithlinkedlist

Mar 23rd, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct x
  5. {
  6. char b[50];
  7. struct x *ptr;
  8. } x;
  9. x *a,*head=NULL,*temp,*temp1;
  10.  
  11. void print()
  12. {
  13. temp=head;
  14. while(temp!=NULL)
  15. {
  16. printf("%s ",temp->b);
  17. temp=temp->ptr;
  18. }
  19. printf("\n");
  20. }
  21.  
  22. void push(char c[50])
  23. {
  24. a=(x*)malloc(sizeof(x));
  25. strcpy(a->b, c);
  26. a->ptr=NULL;
  27.  
  28. if(head==NULL)
  29. {
  30. head = a;
  31. }
  32. else
  33. {
  34. a->ptr=head;
  35. head=a;
  36. }
  37. }
  38.  
  39. void pop()
  40. {
  41. temp=head;
  42. head=head->ptr;
  43. free(temp);
  44. }
  45.  
  46. int main()
  47. {
  48. printf("Input The Size Of The Stack ");
  49. int n,b,d,e=0;
  50. char c[50];
  51. scanf("%d",&n);
  52. printf("Input Name\n");
  53. for(b=0; b<n; b++)
  54. {
  55. scanf("%s",c);
  56. push(c);
  57. }
  58. print();
  59.  
  60. scanf("%d",&n);
  61. for(b=0; b<n; b++)
  62. {
  63. pop();
  64. }
  65. print();
  66.  
  67. return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement