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

  • SEARCH
  • Home
  • 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 7910587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:54:59+00:00 2026-06-03T12:54:59+00:00

Here is a program it is working #include <stdio.h> #include <stdlib.h> struct node {

  • 0

Here is a program it is working

#include <stdio.h>
#include <stdlib.h>
struct node {
    int data;
    struct node *next, *prev;
};
struct node *root = NULL;
void push(int);
void pop(void);
struct node *create_node(int);
void travel(void);
int main()
{
    int i, j, choice, count;
    printf("enter choice\n");
    scanf("%d", &choice);
    count = 0;
    while (choice == 1) {
        printf("enter a data element");
        scanf("%d", &j);
        if (count == 0) {
            root = (struct node *)malloc(sizeof(struct node));
            root->next = NULL;
            root->data = j;
        } else
            push(j);
        count++;
        printf("enter choice\n");
        scanf("%d", &choice);
    }
    printf("the link list is \n");
//travel function to be created
    travel();
}

void push(int data)
{
    struct node *t1;
    t1 = root;
    while (t1->next != NULL) {
        t1 = t1->next;
    }
    t1->next = create_node(data);
}

void pop()
{
}

void travel(void)
{
    struct node *t1;
    t1 = root;
    while (t1->next != NULL) {
        printf("%d ", t1->data);
        t1 = t1->next;
    }
    printf("%d ", t1->data);
}

struct node *create_node(int data)
{
    struct node *p = (struct node *)malloc(sizeof(struct node));
    p->data = data;
    p->next = NULL;
    p->prev = NULL;
    return p;
}

the above program is fully working,I have used a global pointer root.
My problem is if I do not want to use a global pointer root here then how do I maintain
that list because each time I will have to return the root of list in my push pop functions
is there any other way to achieve the same?

  • 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-06-03T12:55:01+00:00Added an answer on June 3, 2026 at 12:55 pm

    The simplest way to achieve this is to pass a pointer to the root node pointer to each of your functions:

    void push(struct node **root, int data) { ... }
    void pop(struct node **root) { ... }
    void travel(struct node *root) { ... }
    

    So, in your main function you might declare a local variable to hold the root pointer:

    struct node *root = NULL;
    

    and then when you call push, for example, you pass the address of the root poiner:

    push(&root, data);
    

    I strongly recommend that you fix your push and travel functions so that they are robust to the root pointer being NULL. This was discussed in a previous question of yours and you should heed the advice.

    If you did that then you could get rid of the test for count being zero and the associated special case code. You would then replace this:

    if (count == 0) {
        root = (struct node *)malloc(sizeof(struct node));
        root->next = NULL;
        root->data = j;
    } else
        push(&root, j);
    

    with this:

    push(&root, j);
    

    To drive home the message, your new push would look like this:

    void push(struct node **root, int data)
    {
        if (*root == NULL)
            *root = create_node(data);
        else
        {
            struct node *last = *root;
            while (last->next != NULL) {
                last = last->next;
            }
            last->next = create_node(data);
        }
    }
    

    You would need to modify travel also to include a check for the root node being NULL. I will leave that as an exercise for you.

    Maintaining both head and tail pointers could be a better approach since it would avoid so many list traversals.

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

Sidebar

Related Questions

Here is my program which creates a link list and also reverses it. #include<stdio.h>
Hi i've got here a simple program, but it's not working properly. When i
there is this check-in-out program here at my workplace, it only takes the data
Here's my program so far: int main() { char choice = 'D'; string inputString;
Here is a simple program to output to a text file: #include <iostream> #include
Why is here the barrier not working? If I use it, the program gets
Here is my stack implementation with linked list. The program is working correctly. Would
I am trying to send mail using SmtpClient() within my Winforms VB.Net program Here
Thanks to pbos help and its program (published here , for xoring a big
OK, here is a program that prompts a user for basketball player input. Well

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.