Question

In: Computer Science

hey I have this program written in c++ and I have a problem with if statement...

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;

        }

Solutions

Expert Solution

#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;

}


Related Solutions

So I have written a code for it but i just have a problem with the...
So I have written a code for it but i just have a problem with the output. For the month with the highest temperature and lowest temperature, my code starts at 0 instead of 1. For example if I input that month 1 had a high of 20 and low of -10, and every other month had much warmer weather than that, it should say "The month with the lowest temperature is 1" but instead it says "The month with...
How do I run a lex program written with C on xcode?
How do I run a lex program written with C on xcode?
This problem needs to be solved with source code. I need a C++ program that will...
This problem needs to be solved with source code. I need a C++ program that will help me solve this question. I need it in C++, please. Writing with comments so it maybe cleared. 1.2. We received the following ciphertext which was encoded with a shift cipher: xultpaajcxitltlxaarpjhtiwtgxktghidhipxciwtvgtpilpit ghlxiwiwtxgqadds. 1. Perform an attack against the cipher based on a letter frequency count: How many letters do you have to identify through a frequency count to recover the key? What is...
I have this matlab program, and need to turn it into a C++ program. Can anyone...
I have this matlab program, and need to turn it into a C++ program. Can anyone help me with this? % Prompt the user for the values x and y x = input ('Enter the x coefficient: '); y = input ('Enter the y coefficient: '); % Calculate the function f(x,y) based upon % the signs of x and y. if x >= 0    if y >= 0        fun = x + y;    else        fun = x + y^2;    end...
A program written in C that asks for the distance to be entered and then prints...
A program written in C that asks for the distance to be entered and then prints the fare A transportation company has the following rates For the first 100 miles                                                       20 cents a mile For the next 100 miles                                                       a) + 10 cents per mile over 100 miles For the next 100 miles                                                       b) + 8 cents per mile over 200 miles more than 300 miles                                                          c) + 5 cents per mile over 300 miles Write a program that asks...
Hey, I have a code that I am working on to make a garden plot calculator,...
Hey, I have a code that I am working on to make a garden plot calculator, however I am reaching an error that I cannot seem to get past. Please help. import math # GarednPlot contains all the utility functions class GardenPlot: # constructor function: Welcomes the user and sets default values def __init__(self): print("Welcome!") self.length = 0 self.radius = 0 self.depth = 0 # Sets the length of the GardenPlot and returns that length def setLength(self): self.length = float(input("Enter...
how to write Man Page documentation ? i have a group project written in C and...
how to write Man Page documentation ? i have a group project written in C and my job is to write man page documentstion. can anyone please explain it and how to write and please provide example. thank you
Hey there! My professor wrote this vague problem on the board and I can't figure out...
Hey there! My professor wrote this vague problem on the board and I can't figure out how to solve it so --- I apologize in advance for any lack of clarity. 50.0 mL of 0.03M of Cysteine is titrated by 0.06M of NaOH pKa 2= 8.37 pKa 1 = 1.92 What is the pH of the solution..... 1) Before any titrant is added 2) 12.5 mL of NaOH 3) 25.0 mL of NaOH 4) 37.5 mL of NaOH 5) 50.0...
Hey, please write in c++. This a two-part lab but I did not put them on...
Hey, please write in c++. This a two-part lab but I did not put them on a separate question then it would be confusing to the who is answering it. The first part is already done and the code is down below but I wanted to provide the instructions for it just in case. Please follow the instructions and provide comments helps me understand if you could, make sure the code meets all the criteria, It should be an easy...
I have a quick question about this C ++ problem in my intro class. I have...
I have a quick question about this C ++ problem in my intro class. I have been able to get the first answer consistently when I test it, but my second seems to either be high or low no matter how I change it. Can you show me how you would do this problem with the setprecision(2) Paula and Danny want to plant evergreen trees along the back side of their yard. They do not want to have an excessive...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT