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

Language: C++ The program I have written below needs to allow the user to enter in...
Language: C++ The program I have written below needs to allow the user to enter in their own date, however I currently can only get it to output a default date, 10/10/2018. After the user enters the date it needs to go through the exception checks to make sure it is a valid date. Afterword the user needs to be prompted to change their date. Here is the code: #include <iostream> #include <cmath> #include <fstream> #include <cstring> #include <sstream> #include...
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?
I need this written in C # ASAP Write a C# console program that continually asks...
I need this written in C # ASAP Write a C# console program that continually asks the user "Do you want to enter a name (Y/N)? ". Use a "while" loop to accomplish this. As long as the user enters either an upper or lowercase 'Y', then prompt to the screen "Enter First and Last Name: " and then get keyboard input of the name. After entering the name, display the name to the screen.
I need assistance on this problem in Pseudocode and in C++ Program Program 3: Give a...
I need assistance on this problem in Pseudocode and in C++ Program Program 3: Give a baby $5,000! Did you know that, over the last century, the stock market has returned an average of 10%? You may not care, but you’d better pay attention to this one. If you were to give a newborn baby $5000, put that money in the stock market and NOT add any additional money per year, that money would grow to over $2.9 million by...
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...
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 was given the following problem to make a C program* You are a programmer at...
*I was given the following problem to make a C program* You are a programmer at Disney World. You need to create a program that will allow people to choose fast passes for three different attractions. 1. Space mountain 2. Haunted Mansion 3. Pirates of the Caribbean The fast passes are given out in 15 minute increments from 9am - 5pm Sunday-Saturday. The user can only obtain three fast passes per day - you must be able to tell the...
I need assistance on this problem in Pseudocode and in C++ Program 1: Stay on the...
I need assistance on this problem in Pseudocode and in C++ Program 1: Stay on the Screen! Animation in video games is just like animation in movies – it’s drawn image by image (called “frames”). Before the game can draw a frame, it needs to update the position of the objects based on their velocities (among other things). To do that is relatively simple: add the velocity to the position of the object each frame. For this program, imagine we...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT