Rohit4Pal

Untitled

Jun 27th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. //
  2. // Created by Rohit on 6/27/2020.
  3. //
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6.  
  7. typedef struct node {
  8. int val;
  9. struct node *next = NULL;
  10. } node;
  11. void myEnque(node *last,int val);
  12. int myDeque(node *head);
  13. int pop(int stack[],int *top);
  14. void push(int stack[],int val,int *top);
  15.  
  16. int main() {
  17.  
  18. int n,k;
  19. cin >> n>>k;
  20.  
  21. node *head = NULL, *add = NULL, *last = NULL;
  22. int stack[n],top=-1;
  23.  
  24. //input and creating the queue
  25. for (int i = 0; i < n; i++) {
  26.  
  27. add = (node *) malloc(sizeof(node));
  28. cin >> add->val;
  29.  
  30. if (head == NULL) {
  31. head = add;
  32. last = add;
  33. } else {
  34. last->next=add;
  35. last=add;
  36. }
  37. }
  38.  
  39. //print
  40. for(node *p=head;p!=NULL;p=p->next){
  41. cout<<p->val<<" ";
  42. }
  43.  
  44. //1. loop to 0 to k-1
  45. for(int i=0;i<k;i++){
  46. push(stack,myDeque(head),&top);
  47. }
  48.  
  49. //2.until s is not empty
  50. while(top!=-1){
  51. myEnque(last,pop(stack,&top));
  52. }
  53.  
  54. //3. n-k times
  55. for(int i=0;i<n-k;i++)
  56. myEnque(last,myDeque(head));
  57.  
  58. //print
  59. for(node *p=head;p!=NULL;p=p->next){
  60. cout<<p->val<<" ";
  61. }
  62.  
  63. }
  64.  
  65. void myEnque(node *last,int val){
  66.  
  67. node *add=(node *) malloc(sizeof(node));
  68. add->val=val;
  69. last->next=add;
  70. last=add;
  71. }
  72. int myDeque(node *head){
  73. int x=head->val;
  74. head=head->next;
  75. return x;
  76. }
  77. void push(int stack[],int val,int *top){
  78. stack[++(*top)]=val;
  79. }
  80. int pop(int stack[],int *top){
  81. return stack[--(*top)];
  82. }
Add Comment
Please, Sign In to add comment