Program of stack using linked list

Two basic methods used in stack(LIFO….Last In First Out) are: statically (Array based implementation) and dynamically(pointer-based(Linked List) implementation).

 Here, the memory is used dynamically(not at a fixed distance).

Push & Pop

 For each push(insert, add new element) operation……… the memory space for one element is allocated at run-time and the element(ONE BY ONE) is inserted into the stack.

For every pop(delete, remove an element(ONE BY ONE) ) operation………. the memory space for the deleted element is de-allocated and returned to the free space spool.

/* Program of stack using linked list*/

# include<stdio.h>

# include<malloc.h>

struct nodee

{

                int infoo;

                struct nodee *liink;

} *toop=NULL;

main()

{

                int choicee;

                while(1)

                {              printf("Press 1....Push\n");

                                printf("Press 2....Pop\n");

                                printf("Press 3....Display\n");

                                printf("Press 4....Quit\n");

                                printf("Enter your choicee : ") ;

                                scanf("%d", &choicee);

                                switch(choicee)

                                {

                                case 1:

                                                push();

                                                break;

                                case 2:

                                                pop();

                                                break;

                                case 3:

                                                display();

                                                break;

                                case 4:

                                                exit(1);

                                default :

                                                printf("Oops!Wrong choicee\n");

                                }/* switch End */

                }/*while End */

}/*main() End */

push()

{

                struct nodee *tmpp;

                int pushed_item;

                tmpp = (struct nodee *)malloc(sizeof(struct nodee)); //create a node

                printf("Enter(go into) the New Value.....>>> : ");

                scanf("%d",&pushed_item);

                tmpp->infoo=pushed_item;

                tmpp->liink=toop;

                toop=tmpp;

}/* push() End*/

pop()

{

                struct nodee *tmpp;

                if(toop == NULL)

                                printf("Sorry,no elements-----\n");

                else

                {       tmpp=toop;

                                printf("Popped item is %d\n",tmpp->infoo);

                                toop=toop->liink;

                                free(tmpp);

                }

}/* pop() End*/

display()

{       struct nodee *ptrr;

                ptrr=toop;

                if(toop==NULL)

                                printf("Sorry,no elements-----\n");

                else

                {

                                printf("All elements are----- :\n");

                                while(ptrr!= NULL)

                                {

                                                printf("%d\n",ptrr->infoo);

                                                ptrr = ptrr->liink;

                                }/*End while */

                }/* else End*/

}/* display() End*/

Output...

stack using linked list

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @