Linked List Stack Question in Regards to the Stack Being Empty












0















I am worked on a Linked list stack and a bunch of functions for it. What I do not understand currently is how come my "isEmpty" function is not working correctly. I believe that the way I have it written makes sense. By nature if Front is Null then the list should have to be empty which would mean that "isEmpty" would return false. The problem that I am having is that my program says that the list is always empty whether or not it actually is or not. I am not sure what the issue is. Any help would be appreciated.



struct node
{
int data;
node *next;
}*front = NULL, *rear = NULL, *p = NULL, *np = NULL;
void push(int x)
{
np = new node;
np->data = x;
np->next = NULL;
if(front == NULL)
{
front = rear = np;
rear->next = NULL;
}
else
{
rear->next = np;
rear = np;
rear->next = NULL;
}
}
int pop()
{
int x;
if (front == NULL)
{
cout<<"empty queuen";
}
else
{
p = front;
x = p->data;
front = front->next;
delete(p);
return(x);
}
}

int peek()
{
int x;
if (front == NULL)
{
cout<<"empty queuen";
}
else
{
p = front;
x = p->data;
front = front->next;
return(x);
}
}

bool isEmpty()
{
if (front == NULL)
{
return true;
}
else if (front != NULL)
{
return false;
}
}

void Display()
{
cout << front;
}

int main()
{
int n, c = 0, x;
bool is_empty = isEmpty();
cout<<"Enter the number of values to be pushed into queuen";
cin>>n;
while (c < n)
{
cout<<"Enter the value to be entered into queuen";
cin>>x;
push(x);
c++;
}
cout<<endl<<"Pop value: ";

if (front != NULL)
cout<<pop()<<endl;

cout<<endl<<"Peak value: ";

if (front != NULL)
cout<<peek()<<endl;

if (is_empty == true)
{
cout<<endl<<"The list is empty";
}
else if (is_empty == false)
{
cout<<endl<<"The list is not empty";
}


cout << endl << "The current contents of the stack are: ";
while(front != NULL)
{
Display();
if(front == NULL)
cout << "The stack is empty";
break;
}
getch();
}









share|improve this question

























  • What do you think is_empty does?

    – Fei Xiang
    Nov 19 '18 at 23:39











  • You set is_empty 1 time at the beginning of your main() it does not change its value after regardless of the condition of the stack. Instead call isEmpty() each time you want to check.

    – drescherjm
    Nov 19 '18 at 23:42













  • @drescherjm thank you that was very helpful.

    – Jenny Nicky
    Nov 19 '18 at 23:49











  • @drescherjm do you also know why this code does not properly display the contents of the list and only displays the list if the list is empty.

    – Jenny Nicky
    Nov 19 '18 at 23:50











  • Did you put at least 2 items in the stack? if (front != NULL) cout<<pop()<<endl; removes the first item if there is one.

    – drescherjm
    Nov 19 '18 at 23:54


















0















I am worked on a Linked list stack and a bunch of functions for it. What I do not understand currently is how come my "isEmpty" function is not working correctly. I believe that the way I have it written makes sense. By nature if Front is Null then the list should have to be empty which would mean that "isEmpty" would return false. The problem that I am having is that my program says that the list is always empty whether or not it actually is or not. I am not sure what the issue is. Any help would be appreciated.



struct node
{
int data;
node *next;
}*front = NULL, *rear = NULL, *p = NULL, *np = NULL;
void push(int x)
{
np = new node;
np->data = x;
np->next = NULL;
if(front == NULL)
{
front = rear = np;
rear->next = NULL;
}
else
{
rear->next = np;
rear = np;
rear->next = NULL;
}
}
int pop()
{
int x;
if (front == NULL)
{
cout<<"empty queuen";
}
else
{
p = front;
x = p->data;
front = front->next;
delete(p);
return(x);
}
}

int peek()
{
int x;
if (front == NULL)
{
cout<<"empty queuen";
}
else
{
p = front;
x = p->data;
front = front->next;
return(x);
}
}

bool isEmpty()
{
if (front == NULL)
{
return true;
}
else if (front != NULL)
{
return false;
}
}

void Display()
{
cout << front;
}

int main()
{
int n, c = 0, x;
bool is_empty = isEmpty();
cout<<"Enter the number of values to be pushed into queuen";
cin>>n;
while (c < n)
{
cout<<"Enter the value to be entered into queuen";
cin>>x;
push(x);
c++;
}
cout<<endl<<"Pop value: ";

if (front != NULL)
cout<<pop()<<endl;

cout<<endl<<"Peak value: ";

if (front != NULL)
cout<<peek()<<endl;

if (is_empty == true)
{
cout<<endl<<"The list is empty";
}
else if (is_empty == false)
{
cout<<endl<<"The list is not empty";
}


cout << endl << "The current contents of the stack are: ";
while(front != NULL)
{
Display();
if(front == NULL)
cout << "The stack is empty";
break;
}
getch();
}









share|improve this question

























  • What do you think is_empty does?

    – Fei Xiang
    Nov 19 '18 at 23:39











  • You set is_empty 1 time at the beginning of your main() it does not change its value after regardless of the condition of the stack. Instead call isEmpty() each time you want to check.

    – drescherjm
    Nov 19 '18 at 23:42













  • @drescherjm thank you that was very helpful.

    – Jenny Nicky
    Nov 19 '18 at 23:49











  • @drescherjm do you also know why this code does not properly display the contents of the list and only displays the list if the list is empty.

    – Jenny Nicky
    Nov 19 '18 at 23:50











  • Did you put at least 2 items in the stack? if (front != NULL) cout<<pop()<<endl; removes the first item if there is one.

    – drescherjm
    Nov 19 '18 at 23:54
















0












0








0








I am worked on a Linked list stack and a bunch of functions for it. What I do not understand currently is how come my "isEmpty" function is not working correctly. I believe that the way I have it written makes sense. By nature if Front is Null then the list should have to be empty which would mean that "isEmpty" would return false. The problem that I am having is that my program says that the list is always empty whether or not it actually is or not. I am not sure what the issue is. Any help would be appreciated.



struct node
{
int data;
node *next;
}*front = NULL, *rear = NULL, *p = NULL, *np = NULL;
void push(int x)
{
np = new node;
np->data = x;
np->next = NULL;
if(front == NULL)
{
front = rear = np;
rear->next = NULL;
}
else
{
rear->next = np;
rear = np;
rear->next = NULL;
}
}
int pop()
{
int x;
if (front == NULL)
{
cout<<"empty queuen";
}
else
{
p = front;
x = p->data;
front = front->next;
delete(p);
return(x);
}
}

int peek()
{
int x;
if (front == NULL)
{
cout<<"empty queuen";
}
else
{
p = front;
x = p->data;
front = front->next;
return(x);
}
}

bool isEmpty()
{
if (front == NULL)
{
return true;
}
else if (front != NULL)
{
return false;
}
}

void Display()
{
cout << front;
}

int main()
{
int n, c = 0, x;
bool is_empty = isEmpty();
cout<<"Enter the number of values to be pushed into queuen";
cin>>n;
while (c < n)
{
cout<<"Enter the value to be entered into queuen";
cin>>x;
push(x);
c++;
}
cout<<endl<<"Pop value: ";

if (front != NULL)
cout<<pop()<<endl;

cout<<endl<<"Peak value: ";

if (front != NULL)
cout<<peek()<<endl;

if (is_empty == true)
{
cout<<endl<<"The list is empty";
}
else if (is_empty == false)
{
cout<<endl<<"The list is not empty";
}


cout << endl << "The current contents of the stack are: ";
while(front != NULL)
{
Display();
if(front == NULL)
cout << "The stack is empty";
break;
}
getch();
}









share|improve this question
















I am worked on a Linked list stack and a bunch of functions for it. What I do not understand currently is how come my "isEmpty" function is not working correctly. I believe that the way I have it written makes sense. By nature if Front is Null then the list should have to be empty which would mean that "isEmpty" would return false. The problem that I am having is that my program says that the list is always empty whether or not it actually is or not. I am not sure what the issue is. Any help would be appreciated.



struct node
{
int data;
node *next;
}*front = NULL, *rear = NULL, *p = NULL, *np = NULL;
void push(int x)
{
np = new node;
np->data = x;
np->next = NULL;
if(front == NULL)
{
front = rear = np;
rear->next = NULL;
}
else
{
rear->next = np;
rear = np;
rear->next = NULL;
}
}
int pop()
{
int x;
if (front == NULL)
{
cout<<"empty queuen";
}
else
{
p = front;
x = p->data;
front = front->next;
delete(p);
return(x);
}
}

int peek()
{
int x;
if (front == NULL)
{
cout<<"empty queuen";
}
else
{
p = front;
x = p->data;
front = front->next;
return(x);
}
}

bool isEmpty()
{
if (front == NULL)
{
return true;
}
else if (front != NULL)
{
return false;
}
}

void Display()
{
cout << front;
}

int main()
{
int n, c = 0, x;
bool is_empty = isEmpty();
cout<<"Enter the number of values to be pushed into queuen";
cin>>n;
while (c < n)
{
cout<<"Enter the value to be entered into queuen";
cin>>x;
push(x);
c++;
}
cout<<endl<<"Pop value: ";

if (front != NULL)
cout<<pop()<<endl;

cout<<endl<<"Peak value: ";

if (front != NULL)
cout<<peek()<<endl;

if (is_empty == true)
{
cout<<endl<<"The list is empty";
}
else if (is_empty == false)
{
cout<<endl<<"The list is not empty";
}


cout << endl << "The current contents of the stack are: ";
while(front != NULL)
{
Display();
if(front == NULL)
cout << "The stack is empty";
break;
}
getch();
}






c++ linked-list stack






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 0:08







Jenny Nicky

















asked Nov 19 '18 at 23:26









Jenny NickyJenny Nicky

114




114













  • What do you think is_empty does?

    – Fei Xiang
    Nov 19 '18 at 23:39











  • You set is_empty 1 time at the beginning of your main() it does not change its value after regardless of the condition of the stack. Instead call isEmpty() each time you want to check.

    – drescherjm
    Nov 19 '18 at 23:42













  • @drescherjm thank you that was very helpful.

    – Jenny Nicky
    Nov 19 '18 at 23:49











  • @drescherjm do you also know why this code does not properly display the contents of the list and only displays the list if the list is empty.

    – Jenny Nicky
    Nov 19 '18 at 23:50











  • Did you put at least 2 items in the stack? if (front != NULL) cout<<pop()<<endl; removes the first item if there is one.

    – drescherjm
    Nov 19 '18 at 23:54





















  • What do you think is_empty does?

    – Fei Xiang
    Nov 19 '18 at 23:39











  • You set is_empty 1 time at the beginning of your main() it does not change its value after regardless of the condition of the stack. Instead call isEmpty() each time you want to check.

    – drescherjm
    Nov 19 '18 at 23:42













  • @drescherjm thank you that was very helpful.

    – Jenny Nicky
    Nov 19 '18 at 23:49











  • @drescherjm do you also know why this code does not properly display the contents of the list and only displays the list if the list is empty.

    – Jenny Nicky
    Nov 19 '18 at 23:50











  • Did you put at least 2 items in the stack? if (front != NULL) cout<<pop()<<endl; removes the first item if there is one.

    – drescherjm
    Nov 19 '18 at 23:54



















What do you think is_empty does?

– Fei Xiang
Nov 19 '18 at 23:39





What do you think is_empty does?

– Fei Xiang
Nov 19 '18 at 23:39













You set is_empty 1 time at the beginning of your main() it does not change its value after regardless of the condition of the stack. Instead call isEmpty() each time you want to check.

– drescherjm
Nov 19 '18 at 23:42







You set is_empty 1 time at the beginning of your main() it does not change its value after regardless of the condition of the stack. Instead call isEmpty() each time you want to check.

– drescherjm
Nov 19 '18 at 23:42















@drescherjm thank you that was very helpful.

– Jenny Nicky
Nov 19 '18 at 23:49





@drescherjm thank you that was very helpful.

– Jenny Nicky
Nov 19 '18 at 23:49













@drescherjm do you also know why this code does not properly display the contents of the list and only displays the list if the list is empty.

– Jenny Nicky
Nov 19 '18 at 23:50





@drescherjm do you also know why this code does not properly display the contents of the list and only displays the list if the list is empty.

– Jenny Nicky
Nov 19 '18 at 23:50













Did you put at least 2 items in the stack? if (front != NULL) cout<<pop()<<endl; removes the first item if there is one.

– drescherjm
Nov 19 '18 at 23:54







Did you put at least 2 items in the stack? if (front != NULL) cout<<pop()<<endl; removes the first item if there is one.

– drescherjm
Nov 19 '18 at 23:54














1 Answer
1






active

oldest

votes


















0














There are some issues with your code.



You have defined an isEmpty() function, but you don't really it. You have declared a separate variable is_empty that you set to the return value of isEmpty() one time before you populate the list, and never update is_empty after making any changes to the list. That is why your code is always reporting the list is "empty" even if it is really not. You need to get rid of the is_empty variable and call isEmpty() every time you need to check for empty.



Also, the return value of peek() and pop() are indeterminate if front is NULL.



And peek() pops the front node from the list. Only pop() should be doing that.



And pop() does not check if rear is pointing at the node being popped. You are not resetting rear to NULL in that case.



And you don't free any nodes remaining in the list before exiting the program.



Try something more like this instead:



#include <iostream>
#include <limits>

struct node
{
int data;
node *next;

node(int value): data(value), next(NULL) {}
};

node *front = NULL;
node *rear = NULL;

void push(int x)
{
node **np = (rear) ? &(rear->next) : &front;
*np = new node(x);
rear = *np;
}

int peek()
{
if (front)
return front->data;
return -1;
}

int pop()
{
int x = peek();

if (front)
{
node *p = front;
front = front->next;
if (p == rear) rear = NULL;
delete p;
}

return x;
}

bool isEmpty()
{
return !front;
}

void clear()
{
while (front)
{
node *p = front;
front = front->next;
delete p;
}
rear = NULL;
}

void display()
{
node *n = front;
if (n)
{
cout << n->data;
while (n = n->next)
cout << ' ' << n->data;
}
}

int askForNumber(const char *prompt)
{
int n;
cout << prompt << ": ";
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), 'n');
return n;
}

int main()
{
int n, x;

n = askForNumber("Enter the number of values to be pushed into queue");

for(int c = 0; c < n; ++c)
{
x = askForNumber("Enter the value to be entered into queue");
push(x);
}
cout << endl;

cout << "Pop value: ";
if (!isEmpty())
cout << pop();
else
cout << "empty queue";
cout << endl;

cout << "Peak value: ";
if (!isEmpty())
cout << peek();
else
cout << "empty queue";
cout << endl;

if (isEmpty())
cout << "The list is empty";
else
cout << "The list is not empty";
cout << endl;

cout << "The current contents of the stack are:" << endl;
display();
cout << endl;

clear();

cin.get();
return 0;
}





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384107%2flinked-list-stack-question-in-regards-to-the-stack-being-empty%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    There are some issues with your code.



    You have defined an isEmpty() function, but you don't really it. You have declared a separate variable is_empty that you set to the return value of isEmpty() one time before you populate the list, and never update is_empty after making any changes to the list. That is why your code is always reporting the list is "empty" even if it is really not. You need to get rid of the is_empty variable and call isEmpty() every time you need to check for empty.



    Also, the return value of peek() and pop() are indeterminate if front is NULL.



    And peek() pops the front node from the list. Only pop() should be doing that.



    And pop() does not check if rear is pointing at the node being popped. You are not resetting rear to NULL in that case.



    And you don't free any nodes remaining in the list before exiting the program.



    Try something more like this instead:



    #include <iostream>
    #include <limits>

    struct node
    {
    int data;
    node *next;

    node(int value): data(value), next(NULL) {}
    };

    node *front = NULL;
    node *rear = NULL;

    void push(int x)
    {
    node **np = (rear) ? &(rear->next) : &front;
    *np = new node(x);
    rear = *np;
    }

    int peek()
    {
    if (front)
    return front->data;
    return -1;
    }

    int pop()
    {
    int x = peek();

    if (front)
    {
    node *p = front;
    front = front->next;
    if (p == rear) rear = NULL;
    delete p;
    }

    return x;
    }

    bool isEmpty()
    {
    return !front;
    }

    void clear()
    {
    while (front)
    {
    node *p = front;
    front = front->next;
    delete p;
    }
    rear = NULL;
    }

    void display()
    {
    node *n = front;
    if (n)
    {
    cout << n->data;
    while (n = n->next)
    cout << ' ' << n->data;
    }
    }

    int askForNumber(const char *prompt)
    {
    int n;
    cout << prompt << ": ";
    cin >> n;
    cin.ignore(numeric_limits<streamsize>::max(), 'n');
    return n;
    }

    int main()
    {
    int n, x;

    n = askForNumber("Enter the number of values to be pushed into queue");

    for(int c = 0; c < n; ++c)
    {
    x = askForNumber("Enter the value to be entered into queue");
    push(x);
    }
    cout << endl;

    cout << "Pop value: ";
    if (!isEmpty())
    cout << pop();
    else
    cout << "empty queue";
    cout << endl;

    cout << "Peak value: ";
    if (!isEmpty())
    cout << peek();
    else
    cout << "empty queue";
    cout << endl;

    if (isEmpty())
    cout << "The list is empty";
    else
    cout << "The list is not empty";
    cout << endl;

    cout << "The current contents of the stack are:" << endl;
    display();
    cout << endl;

    clear();

    cin.get();
    return 0;
    }





    share|improve this answer






























      0














      There are some issues with your code.



      You have defined an isEmpty() function, but you don't really it. You have declared a separate variable is_empty that you set to the return value of isEmpty() one time before you populate the list, and never update is_empty after making any changes to the list. That is why your code is always reporting the list is "empty" even if it is really not. You need to get rid of the is_empty variable and call isEmpty() every time you need to check for empty.



      Also, the return value of peek() and pop() are indeterminate if front is NULL.



      And peek() pops the front node from the list. Only pop() should be doing that.



      And pop() does not check if rear is pointing at the node being popped. You are not resetting rear to NULL in that case.



      And you don't free any nodes remaining in the list before exiting the program.



      Try something more like this instead:



      #include <iostream>
      #include <limits>

      struct node
      {
      int data;
      node *next;

      node(int value): data(value), next(NULL) {}
      };

      node *front = NULL;
      node *rear = NULL;

      void push(int x)
      {
      node **np = (rear) ? &(rear->next) : &front;
      *np = new node(x);
      rear = *np;
      }

      int peek()
      {
      if (front)
      return front->data;
      return -1;
      }

      int pop()
      {
      int x = peek();

      if (front)
      {
      node *p = front;
      front = front->next;
      if (p == rear) rear = NULL;
      delete p;
      }

      return x;
      }

      bool isEmpty()
      {
      return !front;
      }

      void clear()
      {
      while (front)
      {
      node *p = front;
      front = front->next;
      delete p;
      }
      rear = NULL;
      }

      void display()
      {
      node *n = front;
      if (n)
      {
      cout << n->data;
      while (n = n->next)
      cout << ' ' << n->data;
      }
      }

      int askForNumber(const char *prompt)
      {
      int n;
      cout << prompt << ": ";
      cin >> n;
      cin.ignore(numeric_limits<streamsize>::max(), 'n');
      return n;
      }

      int main()
      {
      int n, x;

      n = askForNumber("Enter the number of values to be pushed into queue");

      for(int c = 0; c < n; ++c)
      {
      x = askForNumber("Enter the value to be entered into queue");
      push(x);
      }
      cout << endl;

      cout << "Pop value: ";
      if (!isEmpty())
      cout << pop();
      else
      cout << "empty queue";
      cout << endl;

      cout << "Peak value: ";
      if (!isEmpty())
      cout << peek();
      else
      cout << "empty queue";
      cout << endl;

      if (isEmpty())
      cout << "The list is empty";
      else
      cout << "The list is not empty";
      cout << endl;

      cout << "The current contents of the stack are:" << endl;
      display();
      cout << endl;

      clear();

      cin.get();
      return 0;
      }





      share|improve this answer




























        0












        0








        0







        There are some issues with your code.



        You have defined an isEmpty() function, but you don't really it. You have declared a separate variable is_empty that you set to the return value of isEmpty() one time before you populate the list, and never update is_empty after making any changes to the list. That is why your code is always reporting the list is "empty" even if it is really not. You need to get rid of the is_empty variable and call isEmpty() every time you need to check for empty.



        Also, the return value of peek() and pop() are indeterminate if front is NULL.



        And peek() pops the front node from the list. Only pop() should be doing that.



        And pop() does not check if rear is pointing at the node being popped. You are not resetting rear to NULL in that case.



        And you don't free any nodes remaining in the list before exiting the program.



        Try something more like this instead:



        #include <iostream>
        #include <limits>

        struct node
        {
        int data;
        node *next;

        node(int value): data(value), next(NULL) {}
        };

        node *front = NULL;
        node *rear = NULL;

        void push(int x)
        {
        node **np = (rear) ? &(rear->next) : &front;
        *np = new node(x);
        rear = *np;
        }

        int peek()
        {
        if (front)
        return front->data;
        return -1;
        }

        int pop()
        {
        int x = peek();

        if (front)
        {
        node *p = front;
        front = front->next;
        if (p == rear) rear = NULL;
        delete p;
        }

        return x;
        }

        bool isEmpty()
        {
        return !front;
        }

        void clear()
        {
        while (front)
        {
        node *p = front;
        front = front->next;
        delete p;
        }
        rear = NULL;
        }

        void display()
        {
        node *n = front;
        if (n)
        {
        cout << n->data;
        while (n = n->next)
        cout << ' ' << n->data;
        }
        }

        int askForNumber(const char *prompt)
        {
        int n;
        cout << prompt << ": ";
        cin >> n;
        cin.ignore(numeric_limits<streamsize>::max(), 'n');
        return n;
        }

        int main()
        {
        int n, x;

        n = askForNumber("Enter the number of values to be pushed into queue");

        for(int c = 0; c < n; ++c)
        {
        x = askForNumber("Enter the value to be entered into queue");
        push(x);
        }
        cout << endl;

        cout << "Pop value: ";
        if (!isEmpty())
        cout << pop();
        else
        cout << "empty queue";
        cout << endl;

        cout << "Peak value: ";
        if (!isEmpty())
        cout << peek();
        else
        cout << "empty queue";
        cout << endl;

        if (isEmpty())
        cout << "The list is empty";
        else
        cout << "The list is not empty";
        cout << endl;

        cout << "The current contents of the stack are:" << endl;
        display();
        cout << endl;

        clear();

        cin.get();
        return 0;
        }





        share|improve this answer















        There are some issues with your code.



        You have defined an isEmpty() function, but you don't really it. You have declared a separate variable is_empty that you set to the return value of isEmpty() one time before you populate the list, and never update is_empty after making any changes to the list. That is why your code is always reporting the list is "empty" even if it is really not. You need to get rid of the is_empty variable and call isEmpty() every time you need to check for empty.



        Also, the return value of peek() and pop() are indeterminate if front is NULL.



        And peek() pops the front node from the list. Only pop() should be doing that.



        And pop() does not check if rear is pointing at the node being popped. You are not resetting rear to NULL in that case.



        And you don't free any nodes remaining in the list before exiting the program.



        Try something more like this instead:



        #include <iostream>
        #include <limits>

        struct node
        {
        int data;
        node *next;

        node(int value): data(value), next(NULL) {}
        };

        node *front = NULL;
        node *rear = NULL;

        void push(int x)
        {
        node **np = (rear) ? &(rear->next) : &front;
        *np = new node(x);
        rear = *np;
        }

        int peek()
        {
        if (front)
        return front->data;
        return -1;
        }

        int pop()
        {
        int x = peek();

        if (front)
        {
        node *p = front;
        front = front->next;
        if (p == rear) rear = NULL;
        delete p;
        }

        return x;
        }

        bool isEmpty()
        {
        return !front;
        }

        void clear()
        {
        while (front)
        {
        node *p = front;
        front = front->next;
        delete p;
        }
        rear = NULL;
        }

        void display()
        {
        node *n = front;
        if (n)
        {
        cout << n->data;
        while (n = n->next)
        cout << ' ' << n->data;
        }
        }

        int askForNumber(const char *prompt)
        {
        int n;
        cout << prompt << ": ";
        cin >> n;
        cin.ignore(numeric_limits<streamsize>::max(), 'n');
        return n;
        }

        int main()
        {
        int n, x;

        n = askForNumber("Enter the number of values to be pushed into queue");

        for(int c = 0; c < n; ++c)
        {
        x = askForNumber("Enter the value to be entered into queue");
        push(x);
        }
        cout << endl;

        cout << "Pop value: ";
        if (!isEmpty())
        cout << pop();
        else
        cout << "empty queue";
        cout << endl;

        cout << "Peak value: ";
        if (!isEmpty())
        cout << peek();
        else
        cout << "empty queue";
        cout << endl;

        if (isEmpty())
        cout << "The list is empty";
        else
        cout << "The list is not empty";
        cout << endl;

        cout << "The current contents of the stack are:" << endl;
        display();
        cout << endl;

        clear();

        cin.get();
        return 0;
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 '18 at 3:06

























        answered Nov 20 '18 at 2:51









        Remy LebeauRemy Lebeau

        337k19260456




        337k19260456
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384107%2flinked-list-stack-question-in-regards-to-the-stack-being-empty%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()