hi guys, i have a few questions which is actually my assignments, but i'm still confuse about stack and implementations using pointers in C. this is the questions:

1. write a function to find if the stack is empty or not.
2. write a function to find the number of elements present in a stack.
3. write a function to display the top most element and the element at the bottom of the stack.
4. write a function to find the sum of elements in the stack.

please help me :-/

8 Years
Discussion Span
Last Post by Narue

Those aren't questions, they're coding assignments. You also didn't ask any questions about those assignments, so we're generally going to assume you want someone to do it for you. That's cheating, cheating is not encouraged on Daniweb, and if it's what you want, you're welcome to piss off.

Otherwise, here's a very basic stack implementation. It should get you started without giving you the solution to any of the problems:

#include <stdio.h>
#include <stdlib.h>

typedef struct node node;

struct node {
    int value;
    node *next;

static int push(node **top, int value)
    node *new_top = malloc(sizeof *new_top);

    if (new_top != NULL) {
        new_top->value = value;
        new_top->next = *top;
        *top = new_top;

    return new_top != NULL;

static int pop(node **top, int *value)
    node *save = *top;

    if (save != NULL) {
        *value = save->value;
        *top = save->next;

    return save != NULL;

int main(void)
    node *top = NULL;
    int value;
    int i;

    for (i = 0; i < 10; i++) {
        printf("Pushing %d\n", i);

        if (!push(&top, i))
            perror("Unable to push value into stack");

    while (pop(&top, &value))
        printf("Popped %d\n", value);

    return 0;

Edited by Narue: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.