Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,381 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,203 people online right now. Registration is fast and FREE... Join Now!




stacks in datasturcture in c

 
Reply to this topicStart new topic

stacks in datasturcture in c

soumava
post 22 Aug, 2008 - 11:34 PM
Post #1


New D.I.C Head

*
Joined: 22 Aug, 2008
Posts: 1

sir..
i am a 12+level student...and i have some confution with stacks implementation in case of data structure in c....

so,most humbly and respectfully i am requesting u to help me in this problem...

-soumava
User is offlineProfile CardPM

Go to the top of the page

kummu4help
post 22 Aug, 2008 - 11:43 PM
Post #2


D.I.C Head

Group Icon
Joined: 4 Aug, 2008
Posts: 153



Dream Kudos: 25
My Contributions


Hi soumava,

there is nothing complexity u feel about stacks

it's a simple mechanism. it follows LIFO(last in first out)
ex.. if u go for a dinner party there u will see stack of plates near food stall.
there if some one puts a new plate above the already existing stack of plates then that newly placed plate becomes *lastin plate*

some one goes to the food stall and picks up the plate which was top on the stack.. so as we stated above the last placed plate will be on the top of stack of plates and so u'll pick that *lastinplate* first

thus we got LIFO procedure.. the last inserted plate will be picked up first..

in this way try to understand the stack functionality first

later u can try coding on it... cause there are so many snippets available for stack implementation but the main thing is to understand the functionality and application of stacks..

hope u got my point cool.gif
User is offlineProfile CardPM

Go to the top of the page

rambabu4help
post 23 Aug, 2008 - 03:45 AM
Post #3


New D.I.C Head

*
Joined: 13 Aug, 2008
Posts: 16

Hi soumava,

there is nothing complexity u feel about stacks

it's a simple mechanism. it follows LIFO(last in first out) . i wrote a program,which is very simple logic. it did't check on c editor if u have found any errors correct urself. if u unable to clear errors post reply.
here is a simple logic like the following steps
1. suppose we take bundle of books which are arranged one has on another top(here it is stack)
2. if u want take one book from there,i.e., u must take first book,which is coming late and which was on top (here poping element from stack)
3.if another one wants to put another book on the bundle of books, the they must put that book on the top of bundle(here pushing element)
i send u a code u concentrate on it care fully. i hope it will help
CODE

#include<stdio.h>
#include<conio.h>

//structure for stack
struct stack
{
    int stack[20];
    int top=-1;
    int bottom=0;
}

//fuction to check the stack is empty or not
int IsEmpty(struct *stack)
{
    if(top==-1 || top==bottom) return 1;
    else return 0;
}

//function to check the stack is overflow or not
int IsOverflow(struct *stack)
{
    if(top==20) return 1;
    else return 0;
}

//function to push the element into stack
push(struct *stack, int element)
{
    if(IsOverflow(stack)==1)
    {
        printf("The given stack is over flow please try again...");
    }
    else
    {
        stack[++top]=element;
        printf("%d Element was pushed into stack..",stack[top]);
    }
}

//function to pop the element from the stack
pop(struct *stack)
{
    if(IsEmpty(stack)==1)
    {
        printf("Sorry Your stack has no elemnts to pop..");
    }
    else
    {
        printf("The element %d was Poped",stack[top--]);
    }
}

//function to display stack elements
display(struct *s)
{
    int i=0;
    while(i<=top)
        printf("%d",stack[i++]);
        
}
main()
{
    struct stack stack;
    //push elements to your stack like
    push(stack,5);
    .....etc
    //pop elements as like
    pop(stack);//it poped 5 value from stack
    ...etc.
    display(stack);//to display elements
}


User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 06:27AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month