Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

Stacks in C programming

October 30, 2013 Posted by: GPA Views: 28053 1 comments

A stack is a type of a linked list. New nodes can be added to a stack and removed from a stack only at the top. Thats why we can say that a stack is last-in, first-out (LIFO) data structure. To indicate the bottom of the stack we set the last node of it to NULL .
The difference between stacks and linked lists is that you can add and delete data anywhere in a linked list, but only at the top of a stack. In this example we add and delete integer value.

 



#include <stdio.h>
#include <stdlib.h>
struct stack{   
    int value;                  
    struct stack *next; 
};
 
typedef struct stack Stack;
typedef Stack *StackPoint; 
 
//function prototypes 
void menu();
void push(StackPoint *topStackPointer, int value );
int pop( StackPoint *topStackPointer );
void showStack(StackPoint currentStackPointer);
int isEmpty( StackPoint topPtr );
 
//main function 
int main()
{ 
    StackPoint stackPointer = NULL; 
    int choice;//user choice
    int value;//input value
    do{  
        menu(); //display the menu 
        scanf( "%d", &choice );
        switch ( choice ) { 
        case 1:      
            printf("Enter an integer: " );
            scanf( "%d", &value );
            push( &stackPointer, value );
            showStack( stackPointer );
            printf("\n\n");
            break;
        case 2:      
            if ( !isEmpty( stackPointer ) ) {
                printf( "You have popped value %d.\n", pop(&stackPointer));
            } 
            showStack( stackPointer );
            printf("\n\n");
            break;
 
        default:
            printf( "Invalid choice.\n\n" );
            break;
        } 
    }while ( choice != 3 );
    printf( "End of run.\n" );
    return 0;
 
}
 
//Method menu show menu for user
void menu()
{ 
    printf("1 - Push a value on the stack\n");
    printf("2 - Pop a value off the stack\n");
    printf("3 - Exit\n");
    printf( "Enter choice: ");
 
} 
 
//Function push - add a new node at the top of the stack.
void push(StackPoint *topStackPointer, int value )
{ 
    StackPoint newStackPointer; //pointer to new node 
    //Create a new node using malloc 
    newStackPointer = malloc(sizeof( Stack));
    // insert new node and value at stack top 
    if ( newStackPointer != NULL ) {   
        //assign to newStackPointer->value the value to be added on the stack and assign
        //*topStackPointer (the stack top pointer) to newStackPointer->next —> the link member
        //of newStackPointer -> is now point to the previous top node.
        newStackPointer->value = value;
        newStackPointer->next = *topStackPointer;
        //assign newStackPointer to *topStackPointer ->is  the new stack top.
        *topStackPointer = newStackPointer;
    }
    else {
        printf("Please enter correct data" );
    } 
 
}
//Function pop deletes a node from the top of the stack
int pop(StackPoint *topStackPointer)
{ 
 
    //temptopStackPoint is used to free the unneeded memory.
    StackPoint temptopStackPoint; 
    int deletingValue;
    //assign *topStackPointer to temptopStackPoint
    temptopStackPoint = *topStackPointer;
    //assign (*topStackPointer)->value to deletingValue to save the value in the top node.
    deletingValue = ( *topStackPointer)->value;
    //assign (*topStackPointer)->next to *topStackPointer so *topStackPointer 
    //contains address of the new top node
    *topStackPointer = ( *topStackPointer)->next;
    free(temptopStackPoint);//Free the memory pointed to by temptopStackPoint
 
    return deletingValue;//return deleted value to the user
}
 
//Function showStack prints informaion in stack
void showStack(StackPoint currentStackPointer )
{     
    if ( stackSize(currentStackPointer) == 0 ) {
        printf( "\nThe stack is empty.\n\n" );
    }
    else { 
        printf( "\nThe stack size is: %d",stackSize(currentStackPointer));
        printf( "\nThe stack is:\n" );
        while ( currentStackPointer != NULL ) { 
            printf("|%d|\n", currentStackPointer->value);
            currentStackPointer = currentStackPointer->next;
        }
        printf("___\n"); 
    }
 
} 
//Function isEmpty check is Satack is Empty true or false
int isEmpty(StackPoint topStack )
{ 
    return topStack == NULL;//Return 1 if the stack is empty, 0 otherwise
}
//Function return total numbers in stack
int stackSize(StackPoint topStack)
{ 
    int size=0;
    if ( topStack == NULL ) {
        return 0; 
    }
    else { 
        while (topStack != NULL ) { 
            size++;
            topStack = topStack->next;
        } 
    }
    return size;
}

Output

Stacks in C Programming

Download this example

1 Comments...

1. Anderson

November 6, 2013, 2:36 pm
Avatar
Thank you so much
Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe