Stack :Urgent...

Posted on 2009-02-17
Last Modified: 2012-05-06
Hi ,

I am having some problems with stacks. I am debugging it.

Is this the way to create stacks?

typedef struct numbers{
int num1;
int num2;

typedef struct stack
    NUM *rows;
    int n;
    int topofstack;

in create function..

s->rows=malloc(s->n*sizeof(NUM *));

in push function..


this is where i think the problem is ...
in the create function, i have malloced an array of pointers...
now here, do i malloc for an item ?or this is fine?
Question by:zizi21
    LVL 53

    Accepted Solution

    >> s->rows=malloc(s->n*sizeof(NUM *));

    should be :


    You want to allocate room for n NUM objects.

    You initially set the top of the stack to 0 ... But there are no elements on the stack yet, so maybe you should set it to -1. (make sure to properly check for this value, and interpret it as an empty stack)

    Author Comment

    thank you very much...

    Author Closing Comment

    thank you.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now