queue

Hi I have an assignment to creat a queue and remove the N element from the end.So far I have only manage to write a code that inputs the numbers and I am looking for some advices to fishish the program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  #include<iostream>
using namespace std;
int br(0),br1(0);
struct elem {
    int key;
    elem *next;
}*first,*last;

void push(int n)
{elem *p=last;
last= new elem;
last->key=n;
last->next=NULL;
if(p!=NULL)
p->next=last;
if(first==NULL)
first=last;
}

int pop(int n)
{if(first)
   {n=first->key;
   elem *p=first;
   first=first->next;
   delete p;
   return 1;
   }else return 0;
}

int main()
{int n;
cout<<"How many number do you want to input";
cin>>br;
for(int i=0;i<br;i++)
{  cout<<"Input number";
   cin>>n;
   push(n);
}

for(int i=0;i<br;i++)
{pop(n);}
}
Hi I have an assignment to creat a queue and remove the N element from the end.This is the code I manage to write but Im not sure if it is correct

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<iostream>
using namespace std;
int br(0),br1(0);
struct elem {
    int key;
    elem *next;
}*first,*last;

void push(int n)
{elem *p=last;
last= new elem;
last->key=n;
last->next=NULL;
if(p!=NULL)
p->next=last;
if(first==NULL)
first=last;
}

int pop(int n)
{if(first)
   {n=first->key;
   elem *p=first;
   first=first->next;
   delete p;
   return 1;
   }else return 0;
}

void push_d(int n)
{elem *p=last;
last= new elem;
last->key=n;
last->next=NULL;
if(p!=NULL)
p->next=last;
if(first==NULL)
first=last;
}

int pop_d(int n)
{if(first)
   {n=first->key;
   elem *p=first;
   first=first->next;
   delete p;
   return 1;
   }else return 0;
}

int main()
{int n;
cout<<"How many number do you want to input";
cin>>br;
for(int i=0;i<br;i++)
{  cout<<"Input number["<<i<<"]=";
   cin>>n;
   push(n);
}

for(int i=0;i<br;i++)
{pop(n);}


cout<<n<<"";
for(int i=0;i<br-1;i++)
{pop_d(n);}
}


Topic archived. No new replies allowed.