Stack and Queue

I am to create user menu for users to choose Stack and Queue. When I compile this code there's no error but when I run the program, it stops and i cant cin the data
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <iostream>
#include <string>
using namespace std;

struct book {
   string name;
   int year;
   book *next;
};
book *frontptr;
book *rearptr;
book *topptr;
void createEmptyQueue();
void createEmptyStack();
void pop();
void push();
void displayStack();
void displayQueue();
void searchStack();
void searchQueue();
void removeAll();
void queueMenu();
void stackMenu();
void enqueue();
void dequeue();
void removeAllNodes();

void createEmptyStack(){
   topptr = NULL;
}

int main() {
   char choice;
   cout<<"Choose A=Queue and B=Stack"<<endl;
   cin >> choice;
   if (choice=='A')
      queueMenu();
   else if (choice=='B')
      stackMenu();
   else
      cout<<"Please choose between A and B";
   return 0;
}

void queueMenu(){
   cout<<"\t\t\tQueue Menu\t\t\t"<<endl;
   enqueue(); 
   enqueue();
   enqueue();
   cout<<"The list of book is :"<<endl;
   displayQueue();
   dequeue();
   cout<<"After deleted the first data the list of book is:"<<endl;
   displayQueue();
   searchQueue();
   removeAllNodes();
}

void stackMenu(){
   cout<<"\t\t\tStack Menu\t\t\t"<<endl;
   push();
   push();
   push();
   cout<<"The list of book is :"<<endl;
   displayStack();
   pop();
   searchStack();
   cout<<"After deleted the first data the list of book is:"<<endl;
   displayStack();
   removeAll();
}

void push(){
   book *newptr;
   string title;
   newptr=new book;
   cout<<"Please enter the book name"<<endl;
   cin.ignore();
   getline(cin,title);
   newptr->name=title;
   cout<<"Please enter the year"<<endl;
   cin>>newptr->year;
   newptr->next=NULL;
  
   if(topptr==NULL)
      topptr=newptr;
   else{
      newptr->next=topptr;
      topptr=newptr;
   }
}

void pop(){
   book *newptr;
   if(topptr==NULL)
      cout<<"The list is empty";
   else{
      newptr=topptr;
      topptr=topptr->next;
      delete newptr;
   }
}

void displayStack(){
   book *curptr;
   curptr=topptr;
   int num=1;
   cout<<"\t\t\tNo."<<"\t\t\tTitle"<<"\t\t\tYear"<<endl;
   while(curptr!=NULL){
      cout<<"\n\t\t\t"<<num<<"\t\t\t"<<curptr->name<<"\t\t\t"<<curptr->year<<endl;
      curptr=curptr->next;
      num++;
   }
}

void displayQueue(){
   book *curptr;
   curptr=frontptr;
   int num=1;
   cout<<"\t\t\tNo."<<"\t\t\tTitle"<<"\t\t\tYear"<<endl;
   while(curptr!=NULL){
      cout<<"\n\t\t\t"<<num<<"\t\t\t"<<curptr->name<<"\t\t\t"<<curptr->year<<endl;
      curptr=curptr->next;
      num++;
   }
}

void searchQueue(){
   book *curptr;
   curptr=frontptr;
   string data;
   cout<<"Which book do you want to search for?";
   cin.ignore();
   getline(cin,data);
   while(curptr!=NULL && curptr->name != data){
      curptr=curptr->next;
   }
   if(curptr->next == NULL)
      cout<<"The book is not found"<<endl;
   else
      cout<<"The book is in the list"<<endl;
}

void searchStack(){
   book *curptr;
   curptr=topptr;
   string data;
   cout<<"Which book do you want to search for?";
   cin.ignore();
   getline(cin,data);
   while(curptr!=NULL && curptr->name != data){
      curptr=curptr->next;
   }
   if(curptr->next == NULL)
      cout<<"The book is not found"<<endl;
   else
      cout<<"The book is in the list"<<endl;
}


void removeAll(){
   book *curptr=topptr;
   while(curptr!=NULL){
      pop();
   }
}

void removeAllNodes(){
   book *curptr=frontptr;
   while(curptr!=NULL){
      dequeue();
   }  

}
void createEmptyQueue(){
   frontptr=NULL;
   rearptr=NULL;
}

void enqueue(){
   book *newptr;
   string title;
   int produced;
   newptr = new book;
   cout<<"Enter the book title:"<<endl;
   cin.ignore();
   getline(cin,title);
   cout<<"Enter the year of the book published"<<endl;
   cin>>produced;
   newptr->name = title;
   newptr->year = produced;
   if(rearptr==NULL && frontptr==NULL){
      frontptr=newptr;
      rearptr=newptr;
   }
   else{
      rearptr->next=newptr;
      rearptr=newptr;
   }

}

void dequeue(){
   book *newptr;
   if(frontptr==NULL)
      cout<<"Sorry,the list is empty"<<endl;
   else{
      newptr=frontptr;
      frontptr=frontptr->next;
      delete newptr;
   }
}
Last edited on
Please use code tags. Edit your post, highlight the code and click the "<>" button to the right of the edit window.

When I run the program I get:
Choose A=Queue and B=Stack
A
                        Queue Menu
Enter the book title:
Silent
Enter the year of the book published
1983
Enter the book title:
Bobby
Enter the year of the book published
1969
Enter the book title:
The world and You
Enter the year of the book published
2533
The list of book is :
                        No.                     Title                   Year

                        1                       Silent                  1983

                        2                       Bobby                   1969

                        3                       The world and You              2533
Segmentation fault (core dumped)


removeAll() contains an infinite loop because curptr never changes inside the loop.

removeAllNodes() has the same problem.

enqueue() leaves newptr->next uninitialized when inserting into an empty list.

searchQueue() will mistakenly say it hasn't found the book when the book found is the last one in the list.

searchStack() has the same bug as searchQueue

Fix these bugs and then consider restructuring the code to make Stack and Queue classes. Also notice the duplicate code to prompt for a book in enqueue and push(). It would be a lot cleaner to break enqueue() into 2 functions: one that prompts for a book and another that inserts a book into the queue, then you'd call them like this:
1
2
book *b = promptForBook();
enqueue(b);


and to push a book:
1
2
book *b = promptForBook();
push(b);



thank you very much !
what kind of compiler u used? i was using c++ shell and jgraps but both of them i cant insert data
I'm using GNU g++ 4.9.3
Topic archived. No new replies allowed.