In: Computer Science
hey I have this program written in c++ and I have a problem with if statement {} brackets and I was wondering if someone can fix it.
//Name: Group1_QueueImplementation.cpp
//made by ibrahem alrefai
//programming project one part 4
//group members: Mohammad Bayat, Seungmin Baek,Ibrahem Alrefai
#include <iostream>
#include<stdlib.h>
using namespace std;
struct node { string data; struct node* next; };
struct node* front = NULL;
struct node* rear = NULL;
struct node* temp;
void Insert()
{
string val;
cout << "enter element to insert : ";
cin >> val;
}
//problems start here
if (rear == NULL)
{
rear = (struct node*)malloc(sizeof(struct node));
rear->next = NULL;
rear->data = val;
front = rear;
}
else
{
temp = (struct node*)malloc(sizeof(struct node));
rear->next = temp;
temp->data = val;
temp->next = NULL;
rear = temp;
}
}
}
void Delete()
{
temp = front;
if (front == NULL)
{
cout << "Underflow" << endl;
return;
}
else
{
if (temp->next != NULL)
{
temp = temp->next;
cout << "deleting :" << front->data << endl;
free(front);
front = temp;
}
else {
cout << "deleting : " << front->data << endl;
free(front);
front = NULL;
rear = NULL;
}
]
}
}
void Display()
{
temp = front;
if ((front == NULL) && (rear == NULL))
{
cout << "Queue is empty" << endl;
return;
}
cout << "Queue elements are: ";
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
void form_queue()
{
int n;
cout << "Enter number of elements : " << endl;
cin >> n;
int i = 0;
while (i < n)
{
Insert();
i++;
}
}
void delete_all()
{
while (front != NULL)
Delete();
}
}
int main()
{
form_queue();
Display();
delete_all();
Display();
return 0;
}
#include <iostream>
#include<stdlib.h>
using namespace std;
struct node { string data; struct node* next; };
struct node* front = NULL;
struct node* rear = NULL;
struct node* temp;
void Insert()
{
string val;
cout << "enter element to insert : ";
cin >> val;
//problems start here
if (rear == NULL)
{
rear = (struct node*)malloc(sizeof(struct node));
rear->next = NULL;
rear->data = val;
front = rear;
}
else
{
temp = (struct node*)malloc(sizeof(struct node));
rear->next = temp;
temp->data = val;
temp->next = NULL;
rear = temp;
}
}
void Delete()
{
temp = front;
if (front == NULL)
{
cout << "Underflow" << endl;
return;
}
else
{
if (temp->next != NULL)
{
temp = temp->next;
cout << "deleting :" << front->data << endl;
free(front);
front = temp;
}
else {
cout << "deleting : " << front->data << endl;
free(front);
front = NULL;
rear = NULL;
}
}
}
void Display()
{
temp = front;
if ((front == NULL) && (rear == NULL))
{
cout << "Queue is empty" << endl;
return;
}
cout << "Queue elements are: ";
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
void form_queue()
{
int n;
cout << "Enter number of elements : " << endl;
cin >> n;
int i = 0;
while (i < n)
{
Insert();
i++;
}
}
void delete_all()
{
while (front != NULL)
Delete();
}
int main()
{
form_queue();
Display();
delete_all();
Display();
return 0;
}