Jump to content

C++ создать очередь помогите пожалйста


Recommended Posts

надо создать очередь. Вывести ее на экран. затем удалить с неё все элементы кратные 5 и снова вывести на экран. Я создала очередь но не могу удалить. Надо предусмотреть что числа, кратные 5 могут быть и в серелдине очереди и на конце. Это 2 случая. как это сделать прошу помочь.

#include "stdafx.h"

#include <stdio.h>

#include <conio.h>

struct Stack{

int info;

Stack *ref;

};

void MakeQueue()

{ Stack *beg, *end, *q;

int i = 0;

printf(" Filling queue: \n ");

printf("Enter number: ");

scanf("%d", &i);

q = new Stack;

q->ref = NULL;

q->info = i;

beg = end = q;

while (i >= 0)

{

printf(" Enter number: ");

scanf("%d", &i);

q = new Stack;

q->ref = NULL;

q->info = i;

end->ref = q;

end = q;

}

printf("\n Taking from queue\n");

while (beg != NULL)

{

printf ("Element: %d\n", beg->info);

q = beg->ref;

delete beg;

beg = q;

}

}

int main(int argc, char* argv[])

{

MakeQueue();

return 0;

}

Link to comment
Share on other sites

#include <iostream.h>int main(){ struct Queue {  struct Stack{int info; Stack *ref;}; Stack *beg, *end, *q;   void into(int i) {q=new Stack; q->ref=NULL; q->info=i; end->ref=q; end=q;}  int out(void)  {   if(beg->ref!=NULL)	 {int j=beg->ref->info; q=beg->ref; delete beg; beg=q; return j;}   else return -1; }  Queue(){q=new Stack; q->ref=NULL; q->info=0; beg=end=q;} };Queue q,p; int i;while(i>=0){cout<<" Enter number: "; cin>>i; if(i>=0)q.into(i);}cout<<endl; i=1; while(i>=0){i=q.out(); if(i>=0){cout<<i<<endl; if(i%5)p.into(i);}}cout<<endl; i=1; while(i>=0){i=p.out(); if(i>=0)cout<<i<<endl;}getchar(); getchar(); return 0;}
Edited by Тролль
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...