queue

Hi,
Please help. I have assignment but not able to create the code.
Write a code that inserts 3 1 5 7 8 in an array and delete 3 then and the output should b 1578 and then delete 1 and output should b 578.
Hints provided by teacher,
1=insert
2=delete
3=show
4=exit.
functions hints,
void insert()
void delete()
void show()
Please help me I am very weak in c++.
Thanks in advance.
The first thing to do if you are "very weak in C++" is to stop being weak in C++.

To learn the basics of C++, start here:
http://www.cplusplus.com/doc/tutorial/

Secondly, wow. Does your teacher want you to implement a queue on top of an array? That's unwieldy, to say the least.

A queue is a FIFO (First In First Out) data structure.
This basically means that you always add elements to the end, and always remove elements from the front.
You cannot insert or delete elements from whatever position you want.

So I'd suggest renaming the insert() and delete() functions to push() and pop().

(By the way, if you're supposed to use C++ and still your teacher suggested using a "delete" as a function name... never mind.)

Queue operation example:

push(2)
show(): [2]
push(10)
show(): [2 10]
push(3)
push(3)
push(4)
show(): [2 10 3 3 4]
pop()
show(): [10 3 3 4]
pop()
pop()
show(): [3 4]


So how to use an array to mimic the above behavior?

Let's do it simply by using a fixed-size array like int arr[100];.
This means our queue will be able to hold a maximum of 100 numbers.

Then we need to remember the real size of the queue.
Every time we use push(), we increase the size.
Every time we use pop(), we decrease the size.

The trick is in the pop() function. Not only must we decrease the size, but also shift the array one position left, overwriting the first element.
Can you find out the error.

#include<iostream.h>
#include<conio.h>
int start=0;
int insert=0;
int size=5;
int index=0;
int a[5];
void insert1()
{
cout<<"\t\tEnter values in array"<<endl;
cin>>a[index];
index++;

}
void show()
{
for(int i=start;i<index;i++)
cout<<a[i]<<endl;
}

void del()
{
for(int i=0;i<index-1;i++)
a[i]=a[i++];
index--;
}

void main()
{
abc:
int n;
cout<<"Press 1 for insert"<<endl;
cout<<"Press 2 for delete"<<endl;
cout<<"Press 3 for show"<<endl;
cout<<"Press 4 for exit"<<endl;
cin>>n;
if (n==1&&index<size)
{
insert1 ();
}
if(n==3)
{
show ();
}
if(n==2&&index!=-1)
{
del();
}
goto abc;

getch();
}
First of all, please use code tags when posting code:
http://www.cplusplus.com/articles/jEywvCM9/

Secondly, wow, are you using a very old C++ compiler, such as Borland C++? Because:
1) The header iostream.h does not exist in modern C++.
2) The header conio.h is not part of standard C++.
3) void main() is not standard C++.

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
72
73
74
75
76
77
78
79
#include <iostream>

using namespace std; // should not use this

// int start = 0; // never needed
int insert = 0;
int size = 5;
int index = 0;
int a[5];

void insert1()
{
    cout << "\t\tEnter values in array" << endl;
    cin >> a[index];
    index++;
}

void show()
{
    cout << "[ ";

    for (int i=0; i < index; i++)
        cout << a[i] << ' ';

    cout << "]\n";
}

void del()
{
    for (int i=0; i < index - 1; i++)
        a[i] = a[i + 1];

    index--;
}

int main()
{
abc:

    int n;

    cout << "Press 1 for insert\n";
    cout << "Press 2 for delete\n";
    cout << "Press 3 for show\n";
    cout << "Press 4 for exit\nChoice: " << flush;
    cin >> n;

    if (n == 1)
    {
        if (index < size)
            insert1();
        else
            clog << "Insertion fail.\n";
    }

    if (n == 3)
        show();

    if (n == 2)
    {
        if (index != 0)
            del();
        else
            clog << "Deletion fail.\n";
    }

    if (n == 4)
        goto the_end;

    goto abc; // you should not be using goto!
    // using goto breaks the flow of your program:
    // what you originally created here was an artificial
    // infinite loop

the_end:;

    // getch(); // instead of pausing the program,
    // simply start it in the Command Prompt
}


Edit: small mistakes of my own.
Last edited on
Topic archived. No new replies allowed.