Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by Rohit on 6/27/2020.
- //
- #include<bits/stdc++.h>
- using namespace std;
- typedef struct node {
- int val;
- struct node *next = NULL;
- } node;
- void myEnque(node *last,int val);
- int myDeque(node *head);
- int pop(int stack[],int *top);
- void push(int stack[],int val,int *top);
- int main() {
- int n,k;
- cin >> n>>k;
- node *head = NULL, *add = NULL, *last = NULL;
- int stack[n],top=-1;
- //input and creating the queue
- for (int i = 0; i < n; i++) {
- add = (node *) malloc(sizeof(node));
- cin >> add->val;
- if (head == NULL) {
- head = add;
- last = add;
- } else {
- last->next=add;
- last=add;
- }
- }
- //print
- for(node *p=head;p!=NULL;p=p->next){
- cout<<p->val<<" ";
- }
- //1. loop to 0 to k-1
- for(int i=0;i<k;i++){
- push(stack,myDeque(head),&top);
- }
- //2.until s is not empty
- while(top!=-1){
- myEnque(last,pop(stack,&top));
- }
- //3. n-k times
- for(int i=0;i<n-k;i++)
- myEnque(last,myDeque(head));
- //print
- for(node *p=head;p!=NULL;p=p->next){
- cout<<p->val<<" ";
- }
- }
- void myEnque(node *last,int val){
- node *add=(node *) malloc(sizeof(node));
- add->val=val;
- last->next=add;
- last=add;
- }
- int myDeque(node *head){
- int x=head->val;
- head=head->next;
- return x;
- }
- void push(int stack[],int val,int *top){
- stack[++(*top)]=val;
- }
- int pop(int stack[],int *top){
- return stack[--(*top)];
- }
Add Comment
Please, Sign In to add comment