Stack

Exercise 1:

Create a Menu

Create a menu that will display all the exercises given below (Exercise 2 to Exercise 5) as a list

and prompt user to select any desired option. The menu can be designed in below format.

1. Insert data/ push stack

2. Print stack

3. Pop stack

Exercise 2:

Push Operation

Adding a new data/node in stack is a more than one step activity. First, create a node using

structure and find the location where it has to be inserted. Then input the data and store it in

the allocated memory space. Insert the node at the beginning of the previously inserted node.

Exercise 3:

Pop Operation

After completing exercise 1 you have a newly created stack. Now perform the pop operation on

it.

Exercise 4:

Parsing Unmatched Parenthesis

One of the most important applications of stack is parsing. Parsing is any logic that breaks data

into independent piece for further processing. So parsing unmatched parenthesis is a common

problem of parsing. When parentheses are unmatched then there will be two types of error:

the opening parenthesis is unmatched or the closing parenthesis is missing.

Write a program using stack that will make sure that all parentheses are well paried. For

example,

Input Output

((A+B)/C Opening parentheses not end

(A+B)/C) Closing parentheses not matched

Exercise 5:

Reversing Data

Reversing data requires that a given set of data be reordered so that the first and last elements

are exchanged. The idea of reversing data can be used in solving classical problem such as

converting a decimal number to a binary number. Now write a program using stack that will

convert decimal number to binary number. For example:

Input Output

45 101101

4 100


Solution: 



#include <stdio.h>

#include <stdlib.h>


struct stack{

    int data;

    struct stack *next;

} *top = NULL, *newNode;


int main(){

    int choice, data, y;

    for(;;){

        printf("            => MENU <=           \n");

        printf("1. Display\n");

        printf("2. Push\n");

        printf("3. Pop\n");

        printf("4. Parsing Unmatched Parenthesis\n");

        printf("5. Reversing Data\n");

        printf("6. Exit\n");

        printf("---------------------------------\n");

        printf("Enter your choice between (1-7): ");


        scanf("%d", &choice);

        switch(choice){

            case 1:

                display();

                break;

            case 2:

                printf("Enter your value: ");

                scanf("%d", &y);

                push(y);

                break;

            case 3:

                pop();

                break;

            case 4:

                parsing();

                break;

            case 5:

                reversing();

                break;

            case 6:

                exit(0);

                break;

        }

        printf("\n");

    }

    return 0;

}


void display(){

    printf("    ->> Display value <<-   \n");

    struct stack *temp = top;

    if(temp==NULL){

        printf("Stack is empty");

    }

    while(temp != NULL){

        printf("%d  ", temp -> data);

        temp = temp -> next;

    }

    printf("\n");

}

void push(int a){

    newNode = (struct stack *)malloc(sizeof(struct stack));

    newNode->data = a;

    if(top == NULL){

        newNode->next = NULL;

        top = newNode;

    }else{

        newNode->next = top;

        top = newNode;

    }

}


int pop(){

    struct stack *temp;

    if(top==NULL){

        printf("Stack is empty");

    }

    if(top != NULL){

        temp = top;

        top = top->next;

        free(temp);

    }

}


struct stack1{

    char data1;

    struct stack1 *next1;

} *top1 = NULL, *newNode1;


void push1(){

        newNode1 = (struct stack1 *)malloc(sizeof(struct stack1));

        newNode1->data1 = '(';

        if(top1 == NULL){

            newNode1->next1 = NULL;

            top1 = newNode1;

        }else{

            newNode1->next1 = top1;

            top1 = newNode1;

        }

    }


int pop1(){

    struct stack *temp1;

    if(top1 != NULL){

        temp1 = top1;

        top1 = top1->next1;

        free(temp1);

    }

}


void parsing(){

    int i, c = 0;

    char equation[20];

    printf("Enter equation: ");

    scanf("%s", &equation);

    for(i = 0; i < sizeof(equation); i++){

        if(equation[i] == '('){

            push1();

            ++c;

        }

        if(equation[i] == ')'){

            if(c != 0){

                pop1();

                --c;

            }

            else{

                printf("Closing parentheses not matched..!!");

            }

        }

    }

    if(top1 != NULL && top1->data1 == '('){

        printf("Opening parentheses not end..!!");

        pop1();

    }

}


void reversing(){

    struct stack *temp;

    int x, c = 0, i;

    printf("Enter decimal number: ");

    scanf("%d", &x);

    for(;x > 0;){

        push(x % 2);

        c++;

        x = x /2;

    }

    for(;c>0;){

        temp = top;

        printf("%d", temp -> data);

        top = top -> next;

        free(temp);

        c--;

    }

    printf("\n");

}
















Previous Post Next Post