Skip to main content
Rmove language from title, it is tagged with C
Source Link
rolfl
  • 98.2k
  • 17
  • 220
  • 419

C stack Stack implementation using an array

I am trying a stack implementation using an array. I want to know if this approach is okOK or if there is a logical problem. This program is working fine.

C stack implementation using array

I am trying a stack implementation using array. I want to know if this approach is ok or if there is a logical problem. This program is working fine.

Stack implementation using an array

I am trying a stack implementation using an array. I want to know if this approach is OK or if there is a logical problem. This program is working fine.

edited body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

C stack implemenatationimplementation using array

I am trying implementation ofa stack implementation using array. I want to know if this approach is ok or if there is a logical problem in logic. This program is working fine.

C stack implemenatation using array

I am trying implementation of stack using array. I want to know if this approach is ok or if there is problem in logic. This program is working fine.

C stack implementation using array

I am trying a stack implementation using array. I want to know if this approach is ok or if there is a logical problem. This program is working fine.

Source Link
Ace
  • 183
  • 1
  • 1
  • 4

C stack implemenatation using array

I am trying implementation of stack using array. I want to know if this approach is ok or if there is problem in logic. This program is working fine.

#include <stdio.h>
#define MAXSIZE 5



struct stack            /* Structure definition for stack */
{
    int stk[MAXSIZE];
    int top;
} s;

void push ();
void pop();
void display ();
void search();


int main(){

int element, choice;

s.top = -1;

clrscr();

while (1)
    {
        printf ("------------------------------------------\n");
        printf ("1. PUSH\n");
        printf ("2. POP\n");
        printf ("3. DISPLAY\n");
        printf ("4. SEARCH\n");
        printf ("5. EXIT\n");
        printf ("------------------------------------------\n");

        printf ("Enter your choice\n");
        scanf   ("%d", &choice);

        switch (choice)
        {
            case 1: push();
                break;
            case 2: pop();
                break;
            case 3: display();
                break;
            case 4: search();
                break;
            case 5: return;
        }

}

}

/*Function to add an element to the stack*/
void push() {
    int num;

    if (s.top == (MAXSIZE - 1))
    {
        printf ("Error: Overflow\n");
    }
    else
    {
        printf ("Enter the element to be pushed\n");
        scanf ("%d", &num);
        s.top = s.top + 1;
        s.stk[s.top] = num;
    }
}


/*Function to delete an element from the stack*/
void pop ()
{
    int num;
    if (s.top == - 1)
    {
        printf ("Error: Stack Empty\n");
    }
    else
    {
        num = s.stk[s.top];
        printf ("poped element is = %d\n", num);
        s.top = s.top - 1;
    }
}



/*Function to display the status of the stack*/
void display()
{
    int i;
    if (s.top == -1)
    {
        printf ("Error: Stack Empty\n");
    }
    else
    {
        printf ("\nItems in Stack\n");
        for (i = s.top; i >= 0; i--)
        {
            printf ("%d\n", s.stk[i]);
        }
    }
    printf ("\n");
}


void search(){

    int i;
    int num;
    int counter=0;
    if (s.top == -1)
    {
        printf ("Error: Stack Empty\n");
    }
    else
    {
        printf ("Enter the element to be searched\n");
        scanf ("%d", &num);

        for (i = s.top; i >= 0; i--)
        {
            if(num==s.stk[i]) {
            counter++;
            break;
            }
        }

        if(counter>0){
            printf ("Element %d found in stack\n", num);
        }
        else {
            printf ("Element %d not found in stack\n", num);
        }
    }
    printf ("\n");

}