1
#include "stdio.h"
#include "stdlib.h"

struct node_type {
    int data;
    struct node_type *next;
};

struct stack_type{
    node_type *top;
    int length;
};

void push(node_type *head,stack_type stack);

int main()
{
    struct stack_type stack;
    node_type *list;
    list = (node_type *)malloc(sizeof(node_type));
    list->next = NULL;
    node_type *head;
    head = list;
    stack.length = 0;   //set stack empty
    push(head, stack);
    list = head->next;
    printf("The entegers are:");
    do {
        printf("%d", stack.top->data);
        list = list->next;
        stack.top = list;
    } while (list->next != 0);
    system("pause");
    return 0;
}

void push(node_type *head,stack_type stack)
{
    int i, n;
    node_type *p;
    p = (node_type*)malloc(sizeof(node_type));
    p = head;
    printf("Enter the integers(0 to end):");
    for (;;) {
        scanf("%d", &n);
        if (n == 0)
            break;
        stack.top->data = n;
        p->next = stack.top;
        stack.length++;
    }
}

when I debug, it said that p->next=NULL and stop,I'm not clear about this.

why I wrong? How to fix it, I'm not clear about the use of NULL and top finger Thanks for the answers in advance.

3
  • In which specific section it is stopping ? Commented Apr 1, 2016 at 9:17
  • This is not proper stack operation....Proper implementation of stack should be : there should be a "push(item, top)" function to push the elements and "pop(top)" function to show the elements Commented Apr 1, 2016 at 9:23
  • There are so many things wrong with your code. You don't initialise the variables properly; you allocate a node for an empty list; you allocate to p in push and then immediately overwrite the handle to the allocated data; you pass the stack struct by value, thus not reflecting any changes that will be made by pushand ´pop`; you use external variables for the nodes whereas the whole stack should be defined by the members of the stack struct. There's no simple fix for this program, I'm afraid. Commented Apr 1, 2016 at 9:29

2 Answers 2

3

Two problems: First in the main function you don't initialize stack.top meaning it will have an indeterminate value, dereferencing this seemingly random pointer like you do in the push function will lead to undefined behavior.

The second problem is that you pass the stack structure to push by value, which means that it's copied and all changes done in the push function is only done on the local copy of the structure. You need to emulate passing by value by using pointers and the address-of operator &.

Sign up to request clarification or add additional context in comments.

Comments

2

The program does not make sense.

It is written used C constructions but compiled like a C++ program.

Function push is entirely wrong.

void push(node_type *head,stack_type stack)
{
    int i, n;
    node_type *p;
    p = (node_type*)malloc(sizeof(node_type));
    p = head;
    //...

The stack is accepted by value. Thus the original stack will not be changed. There is a memory leak because pointer p is reassigned after allocating memory. Data member top of the object stack was not initialized. So this statement

stack.top->data = n;

results in undefined behaviour of the program.

First of all you should compile your program as a C program. In this case the compiler will report numerous errors because there is no declaration of names node_type and stack_type. So even the second structure declaration is invalid

struct stack_type{
    node_type *top;
    ^^^^^^^^^^^^^^
    int length;
};

There must be

struct stack_type{
    struct node_type *top;
    ^^^^^^^^^^^^^^^^
    int length;
};

Function push should be declared at least like

void push( struct stack_type *stack, int data );

It should do just one thing: push the vakue of data in the stack.

Take into account that the function main without parameters shall be declared like

int main( void )

The definition of the function push can look the following way

void push( struct stack_type *stack, int data );

int main( void )
{
    struct stack_type stack = { NULL, 0 };

    //...

    int data;

    while ( scanf( "%d", &data ) == 1 && data != 0 )
    {
        push( &stack, data );
    }
    //...
}

void push( struct stack_type *stack, int data )
{
    struct node_type *node = malloc( sizeof( struct node_type ) );

    if ( node != NULL )
    {
        node->data = data;
        node->next = stack->top;
        stack->top = node;
        ++stack->length;
    }
}

2 Comments

But I have to use a linked list to imply the stack.
@jesse左 I have not understood wht you wanted to say. I showed you even the function push implementation. So what you need is to copy and paste the code in my post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.