data structure: stack

Posted By on December 15, 2014


Download PDF
data structure: Queue
data structure : Tree

A stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle. In the pushdown stacks only two operations are allowed: push the item into the stack, and pop the item out of the stack. A stack is a limited access data structure – elements can be added and removed from the stack only at the top. push adds an item to the top of the stack, pop removes the item from the top. A helpful analogy is to think of a stack of books; you can remove only the top book, also you can add a new book on the top.

A stack is a recursive data structure.

A stack may be implemented to have a bounded capacity. If the stack is full and does not contain enough space to accept an entity to be pushed, the stack is then considered to be in an overflow state. The pop operation removes an item from the top of the stack.

 

implementation of stack in c::

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[10],c,i,l,x;
static int top=-1;
clrscr();
do{
printf(“nEnter the operation you want to perform.”);
printf(“n1:insert.”);
printf(“n2:delete.”);
printf(“n3:display.”);
printf(“n4:exit.n”);
printf(“5:change.n”);
printf(“6:value.n”);
scanf(“n%d”,&c);

switch(c)
{
    case 1:printf(“insert the element:”);
            if(top==49)
            {
            printf(“The stack is full.”);
            }
            else
            {
            top++;
            scanf(“%d”,&a[top]);
            }
            printf(“top=%d”,top);

            break;

    case 2: printf(“deleted element is:”);
            if(top==-1)
            {
            printf(“empty.”);
            }
            else
            {
            printf(“%d”,a[top]);
            top–;
            }
            break;

    case 3: printf(“you want to see the stack:”);
                if(top<0)
                {
                     printf(“The stack is empty.”);
                }
                else
                {
                for(i=top; i>=0; i–)
                      {
                        printf(” %dn “,a[i]);
                      }
                     }
            break;

    case 4: exit(0);
                break;

    case 5: printf(“you want to change the element.”);
                printf(“nEnter the location:”);
                printf(“(The topmost value is at: %d)n”,top);
                scanf(“%d”,&l);
                if(l<=top)
                {
                printf(“Enter the value:”);
                scanf(“%d”,&x);
                a[l]=x;
                printf(“The value has been changed.”);
                }
                else
                {
                printf(“you can not change the value.”); }
                break;

    case 6:printf(“You want to see the value.”);
            printf(“Enter the location:”);
            scanf(“%d”,&l);
            if(l<=top)
            {
            for(i=0; i<=top; i++)
            {
            printf(“%d”,a[l]);
            }
            }
            else{
            printf(“no value.”);
            }
            break;
    default:printf(“invalid choice.Enter the valid choice.”);
}
}while(c!=4);
getch();
}

data structure: Queue
data structure : Tree

Download PDF

Posted by