You must Sign In to post a response.
  • C program to implement stack using array and using structure.


    Are you studying for BCA exams? finding difficult to wirte C program for implementing stack using array nad structure? Here, you can find advice from epxerts for your query.

    Following is the question asked in Nalanda Open University Bachelor in Computer Application (BCA), Part-I practical question paper-VI (C Programming and Data Structure (CS-62P) Set A) for 2015 Annual examination.
    This question is asked for 20 marks and hence requires some explanation along with program code. Please let me know the solution.
    Q) Write a program in C language for the implementation of stack. [20 Marks]
  • Answers

    1 Answers found.
  • Stack is called as an ADT that is Abstract Data Type. ADT is user defined data type which is combination of built in data type with some legal functions. Stack is implemented using array and some legal functions like push(), pop(). Stack is also implemented using linked list also.


    This is the implementation of stack using array.
    Please include stdio.h, conio.h ans stdlib.h header files and a symbolic constant # define MAX 10 before main function.
    #include
    #include
    #include
    #define MAX 10

    void push (void);
    void pop (void);
    int isfull (void) ;
    int isempty (void);
    void display(void);


    struct stack
    {
    int it[MAX];
    int top;
    } s;

    void main(void)
    {
    int ch;
    s.top=-1;
    clrscr();
    do
    {
    printf("\n1.push \n 2.pop \n 3.display \n 4.exit\n");
    printf("\n\n Enter your choice\n");
    scanf("%d",&ch);
    switch(ch)
    {
    case 1:
    push();
    break;
    case 2:
    pop();
    break;
    case 3:
    display();
    break;
    case 4:
    exit(0);
    default:
    printf("\n invalid choice\n");
    }
    }while(ch<=4);
    getch();
    }

    int isfull (void)
    {
    if(s.top== MAX -1)
    {
    return(1);
    }
    else
    {
    return(0);
    }
    }

    int isempty (void)
    {
    if(s.top==-1)
    {
    return(1);
    }
    else
    {
    return(0);
    }
    }
    void push (void)
    { int x;
    if(isfull()==1)
    {
    printf("\n\n stack is full\n");
    }
    else
    {
    s.top=s.top+1;
    printf("\n\nEnter the element\n");
    scanf("%d",&x);
    s.it[s.top]=x;
    printf("\n\n%d is pushed",s.it[s.top]);
    }
    }
    void pop (void)
    {
    if(isempty()==1)
    {
    printf("\n\n stack is empty\n");
    }
    else
    {
    printf("\n\n %d is poped", s.it[s.top]);
    s.top=s.top-1;
    }
    }

    void display(void)
    {
    int i;
    if(isempty()==1)
    {
    printf("\n\n stack is empty\n");
    }
    else
    {
    printf("\nElements of stack are as follows\n");
    for(i=s.top;i>=0;i--)
    {
    printf("%d ",s.it[i]);
    }
    }
    }

    Below is the program which implement the stack using linked list.
    Please include stdio.h, conio.h, stdlib.h and alloc.h header files and a symbolic constant #define NULL 0 before main function.
    #define NULL 0
    #include
    #include
    #include
    #include
    void push (void);
    void pop (void);
    void display(void);
    struct stack
    {
    int no;
    struct stack * next;
    };
    typedef struct stack node;
    node *top,*tp;
    void main(void)
    {
    int ch;
    top=NULL;
    clrscr();
    do
    {
    printf("\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
    printf("\n Enter your choice\n");
    scanf("%d",&ch);
    switch(ch)
    {
    case 1:
    push();
    break;
    case 2:
    pop();
    break;
    case 3:
    display();
    break;
    case 4:
    exit(0);
    default:
    printf("\ninvalid choice\n");
    }
    }while(ch<5);
    getch();
    }
    void push(void)
    {
    int num;
    tp=(node*)malloc(sizeof(node));
    if(tp==NULL)
    {
    printf("\n\n stack is full\n");
    }
    else
    {
    printf("\n\nEnter the number\n");
    scanf("%d",&tp ->no);
    tp->next=top;
    top=tp;
    }
    }
    void pop (void)
    {
    if(top==NULL)
    {
    printf("\n\n stack is empty\n");
    }
    else
    {
    tp=top;
    top=top->next;
    printf("\n\n number deleted is %d",tp->no);
    free(tp);
    }
    }
    void display(void)
    {
    printf("\nElements of stack are as follows\n");
    tp=top;
    if(top==NULL)
    {
    printf("\n\n no element is available in stack\n");
    }
    else
    {
    do
    {
    printf("%d ",tp->no);
    tp=tp->next;
    }while(tp!=NULL);
    }
    }


  • Sign In to post your comments