Search and delete an exist element in Queue


Hello!
Now the program make:
Insert 5 numbers:
5,10,15,20,25
disp:
delete: 5
display:
10,15,20,25,5


How to make?
Example

Insert 5 numbers:

cin >> ;
display:10,20,30,40,50

search for number:
cin>> 20;
display: 10,30,40,50,20????

Please help !

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  #include <iostream>
#include<conio.h>
#include<stdlib.h>
#define siz 5



using namespace std;



struct node
{
	int data;
	node *next;
};

class queue
{int a [siz];
	node *rear, *front;
public:
	queue()
	{
		rear = NULL;front = NULL;
	}
	void qinsert();
	void qdelete();
	void qdisplay();
	~queue();
};

void queue::qinsert()
{
	for (int i = 0;i < siz;i++) {
		node *temp;
		temp = new node;
		cout << "\nData :";
		cin >> temp->data;
		temp->next = NULL;
		if (rear == NULL)

		{
			rear = temp;
			front = temp;
		}
		else
		{
			rear->next = temp;
			rear = temp;
		}
	}
}
void queue::qdelete()
{
	if (front != NULL)
	{
		node *temp = front;
		cout << "\nDeleted: " << front->data << endl;
		front = front->next;
		delete temp;
		temp->data;
		temp->next = NULL;
		if (rear == NULL)
		{
			rear = temp;
			front = temp;
		}

		if (front == NULL)
		{
			rear = NULL;
		}
		else
		{
			rear->next = temp;
			rear = temp;
		}


	}
	else
		cout << "\nQueue Empty..";

}

void queue::qdisplay()
{
	node *temp = front;
	while (temp != NULL)
	{
		cout << temp->data << endl;
		temp = temp->next;
	}
}

queue::~queue()
{
	while (front != NULL)
	{
		node *temp = front;
		front = front->next;
		delete temp;
	}
}
int main()
{
	queue obj;

	cout << "\n Insert five numbers ";

	obj.qinsert();
	obj.qdelete();
	obj.qdisplay();
	return 0;
}


Last edited on
1
2
3
		delete temp;
		temp->data;
		temp->next = NULL;

You can't dereference temp after calling delete.

> #include<conio.h>
You should stop relying on this obsolete header file.

> int a [siz];
You don't use this.
Nor would you want to.
A queue is an expandable data structure known only through the front/rear pointers.

you can use library classes, ie <list>: http://www.cplusplus.com/reference/list/list/
store a element: http://www.cplusplus.com/reference/list/list/push_back/
remove a element: http://www.cplusplus.com/reference/list/list/remove/

also there is a 'queqe' class in stl: http://www.cplusplus.com/reference/queue/queue/
so if you want to build your custom class, it better to use a different name than stl classes.
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
#include <iostream>
#include <list>
const int size = 5;
using namespace std;


int main (){
	list<int> li;
	int k;
	for (int i=0; i<size; i++){
		cout << "number= ";
		cin>> k;
		li.push_back(k);
	}
	
	cout << "which number to be removed= ";
	cin>>k;
	li.remove(k);
	
	cout << "now list contains= ";
	for(auto x: li){
		cout << x << ", ";
	}

return 0;	
}
qdelete() is all wrong:
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
void queue::qdelete()
{
        if (front != NULL)
        {
                node *temp = front;
                cout << "\nDeleted: " << front->data << endl;
                front = front->next;
                delete temp;
                temp->data;     // does nothing, except access deleted pointer
                temp->next = NULL; // don't access deleted pointer
                if (rear == NULL)  // since front was non-null, rear won't be NULL
                {
                    rear = temp;
                    front = temp;
                }

                if (front == NULL)
                {
                        rear = NULL;
                }
                else
                {
                        rear->next = temp; // why are you pointing rear->next to now-deleted poitner?
                        rear = temp;       // rear now points to unallocated memory
                }


        }

This is actually pretty simple:
1
2
3
4
5
6
7
8
9
10
11
12
13
void queue::qdelete()
{
    if (front != NULL) {
        node *temp = front;
        cout << "\nDeleted: " << front->data << endl;
        front = front->next;
        delete temp;
        if (front == NULL) {
            rear = NULL;
        }
    } else
        cout << "\nQueue Empty. in qdelete()";
}

Thank you very much .

Work like a list. Like that
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
	#include <iostream>
#include <list>


using namespace std;

int main()
{
{


	const int size = 7;
	cout << "\n Insert seven numbers ";
	list<int> li;
	int k;
	for (int i = 0; i < size ; i++) {
		cout << "\t\nnumber= ";
		cin >> k;
		li.push_back(k);
	}

	cout << "\nWhich number to be removed: ";
	cin >> k;

	li.remove(k);
	li.push_back(k);

	cout << "now list contains= ";
	for (auto x : li) {
		cout << x << ", ";
	}


	}while (k!=size[i])
return 0;
}



how to make it with queue?
Topic archived. No new replies allowed.