File tree 1 file changed +83
-0
lines changed
1 file changed +83
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ using namespace std ;
3
+ struct node {
4
+ int data;
5
+ struct node *next;
6
+ };
7
+ struct node * front = NULL ;
8
+ struct node * rear = NULL ;
9
+ struct node * temp;
10
+ void Insert () {
11
+ int val;
12
+ cout<<" Insert the element in queue : " <<endl;
13
+ cin>>val;
14
+ if (rear == NULL ) {
15
+ // rear =(struct node *)new(sizeof(struct node));
16
+ rear=new (struct node );
17
+ rear->next = NULL ;
18
+ rear->data = val;
19
+ front = rear;
20
+ } else {
21
+ // temp=(struct node *)new(sizeof(struct node));
22
+ temp=new (struct node );
23
+ rear->next = temp;
24
+ temp->data = val;
25
+ temp->next = NULL ;
26
+ rear = temp;
27
+ }
28
+ }
29
+ void Delete () {
30
+ temp = front;
31
+ if (front == NULL ) {
32
+ cout<<" Underflow" <<endl;
33
+ return ;
34
+ }
35
+ else
36
+ if (temp->next != NULL ) {
37
+ temp = temp->next ;
38
+ cout<<" Element deleted from queue is : " <<front->data <<endl;
39
+ delete (front);
40
+ front = temp;
41
+ } else {
42
+ cout<<" Element deleted from queue is : " <<front->data <<endl;
43
+ delete (front);
44
+ front = NULL ;
45
+ rear = NULL ;
46
+ }
47
+ }
48
+ void Display () {
49
+ temp = front;
50
+ if ((front == NULL ) && (rear == NULL )) {
51
+ cout<<" Queue is empty" <<endl;
52
+ return ;
53
+ }
54
+ cout<<" Queue elements are: " ;
55
+ while (temp != NULL ) {
56
+ cout<<temp->data <<" " ;
57
+ temp = temp->next ;
58
+ }
59
+ cout<<endl;
60
+ }
61
+ int main () {
62
+ int ch;
63
+ cout<<" 1) Insert element to queue" <<endl;
64
+ cout<<" 2) Delete element from queue" <<endl;
65
+ cout<<" 3) Display all the elements of queue" <<endl;
66
+ cout<<" 4) Exit" <<endl;
67
+ do {
68
+ cout<<" Enter your choice : " <<endl;
69
+ cin>>ch;
70
+ switch (ch) {
71
+ case 1 : Insert ();
72
+ break ;
73
+ case 2 : Delete ();
74
+ break ;
75
+ case 3 : Display ();
76
+ break ;
77
+ case 4 : cout<<" Exit" <<endl;
78
+ break ;
79
+ default : cout<<" Invalid choice" <<endl;
80
+ }
81
+ } while (ch!=4 );
82
+ return 0 ;
83
+ }
You can’t perform that action at this time.
0 commit comments