Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6380245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:14:36+00:00 2026-05-25T02:14:36+00:00

I’m trying to create a program that simulates a stack. The requirements are: a

  • 0

I’m trying to create a program that simulates a stack. The requirements are:

a structure called node
an integer named data
two pointers of the same type node named next and previous
void push(int) prototype
int pop() prototype

I have built my push() function like so:

#include <stdio.h>

struct node {
    int data;
    struct node *next;
    struct node *prev;
};

struct node *first = NULL;
void push(int number);
int pop();

int main() {
int choice = 0, number;

printf("Enter your choice: \n"
    "1) Push integer\n"
    "2) Pop integer\n"
    "3) Exit\n");
scanf("%d", &choice);

while (choice != 3) {
    if (choice == 1) {
        printf("Enter Integer: ");
        scanf("%d", &number);
        printf("\n");
        push(number);
    }
    if (choice == 2) {
        number = pop();
        if (number == -1) {
            printf("Error: Stack empty.\n\n");
        }
        else {
            printf("Integer %d is popped.\n\n", number);
        }
    }

    printf("Enter your choice: \n"
        "1) Push integer\n"
        "2) Pop integer\n"
        "3) Exit\n");
    scanf("%d", &choice);
}
}


void push(int number)
{
 struct node *cur;
 cur = first;

 if (cur == NULL) {
     cur = (struct node *) malloc(sizeof(struct node));
     cur->data = number;
     cur->next = NULL;
     cur->prev = cur;
     first = cur;
     return;
 }

 if (cur != NULL) {
     while (cur->next != NULL) {
         cur = cur->next;
     }
     (cur->next) = (struct node *) malloc(sizeof(struct node));
     (cur->next)->data = number;
     (cur->next)->next = NULL;
     (cur->next)->prev = cur;
 }
}    

int pop() {
int number;

if (first == NULL) {
    return -1;
}
else {
    struct node *cur, *prev;
    cur = first;
    prev = NULL;

    while (cur->next != NULL) {
        prev = cur;
        cur = cur->next;
    }

    number = cur->data;

    if (prev == NULL) {
        first = NULL;
    }
    else {
        prev->next = cur->next;
    }

    return number;
}
}

Does this look okay? My main program freezes up after the user enters a number to push.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:14:37+00:00Added an answer on May 25, 2026 at 2:14 am

    Do like this…This is meet your requirements…

     void push(int number)
     {
        struct node *cur;
        cur = first;
        if(cur == NULL)  //if it is first node
         {
        cur = (struct node*) malloc(sizeof(struct node));
        cur->data = number;
        cur->next = NULL;
        cur->prev = cur;
        first = cur;
        return;
         }
    
        //note here Iam running the loop till cur->next!=NULL and not till cur != NULL. cur!=NULL makes the cur to act as head of a yet another new Linked List.
    
        while (cur->next != NULL)
        cur = cur->next;
    
        (cur->next) = (struct node*) malloc(sizeof(struct node));
        (cur->next)->data = number;
        (cur->next)->next = NULL;
        (cur->next)->prev = cur;
    }    
    

    Or you want to make your implementation….
    Then…

    void push(int number)
    {
     struct node *cur;
     cur = first;
    
     if (cur != NULL) {
         while (cur->next != NULL) {
             cur = cur->next;
         }
         (cur->next) = (struct node *) malloc(sizeof(struct node));
         (cur->next)->data = number;
         (cur->next)->next = NULL;
         (cur->next)->prev = cur;
     }
     else {/*Take action if it is a first node (if cur is NULL)*/}
    }  
    

    Facts on your old code..

    void push(int number) {
    struct node *cur;
    cur = first;
    
    if (cur != NULL) {
        cur->prev = NULL;//no need. cur->prev must be NULL,already. since cur points to first.
                         //dont change cur->prev=someAddress it will change your head node
    }
    
    
    while (cur != NULL) {//flaw: run till cur->next!= NULL.Dont make cur NULL.
            cur->prev = cur; //during iteration to last element no need of prev.
            cur = cur->next;
        }
    
    //Do memory allocation,for cur->next and not for cur after this loop
    
    cur->data = number; // change this to cur->next->data = number.
    //initialize cur->next->prev,cur->next->next
    
    
    if (cur->prev == NULL) {
        first = cur;
    }
    //I can get your idea. But if you want to do this way,
    //you have to use additional pointer like temp.
    
    else {
        cur->prev->next = cur;
    }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.