Stack using Arrays and Linked List

 

a) Implementation of Stack Operations using Arrays.

b) Implementation of Stack Operations using Linked List.


a) Implementation of Stack Operations using Arrays.


Source Code:-

 #include<stdio.h>

#include<stdlib.h>

#define SIZE 5

void push();

void pop();

void peep();

void display();

int stack[10],top=-1,ele;

int main()

{

                 int ch;

                 printf("\n 1.push\n 2.pop\n 3.peep\n 4.display");

                 while(1)

                 {

                   printf("\nmenu");

                   printf("\nenter ur choice");

                   scanf("%d",&ch);

                   switch(ch)

                   {

                             case 1:push();

                             break;

                             case 2:pop();

                             break;

                             case 3:peep();

                             break;

                             case 4:display();

                             break;

                             default:exit(1);

                   }

                 }

return 0;

}

void push()

{

                 if(top==SIZE-1)

                 {

                   printf("stack is full");

                 }

                 else

                 {

                   top++;

                   printf("enter a element");

                   scanf("%d",&ele);

                   stack[top]=ele;

                 }

}

void pop()

{

                 if(top==-1)

                 {

                   printf("stack is empty");

                 }

                 else

                 {

                   ele=stack[top];

                   printf("\n deleted item is %d",ele);

                   top--;

                 }

}

void peep()

{

                 if(top==-1)

                 {

                   printf("stack is empty");

                 }

                 else

                 {

                   ele=stack[top];

                   printf("\n top most element is %d",ele);

                 }

}

void display()

{

    int i;

                 printf(" elements of stack are:\n");

                 if(top==-1)

                 {

                   printf("\n stack is empty");

                 }

                 else

                 {

                   for(i=top;i>=0;i--)

                   {        

                             printf("%d\n",stack[i]);

                   }

                 }

}

 

Output:-

 

1.push

 2.pop

 3.peep

 4.display

menu

enter ur choice1

enter a element8

 

menu

enter ur choice1

enter a element16

 

menu

enter ur choice1

enter a element24

 

menu

enter ur choice4

 elements of stack are:

24

16

8

 

menu

enter ur choice2

 

 deleted item is 24

menu

enter ur choice4

 elements of stack are:

16

8

 

menu

enter ur choice3

 

 top most element is 16

menu

enter ur choice4

 elements of stack are:

16

8

 

menu

enter ur choice5

 

--------------------------------

Process exited after 65.82 seconds with return value 1

Press any key to continue . . .

 

 

b) Implementation of Stack Operations using Linked List.

Source Code:-

#include<stdio.h>

#include<stdlib.h>

struct node

{

 

    int data;

    struct node *next;

}*new,*tos,*temp;

 

 void push();

 void pop();

 void peep();

 void display();

int ele;

void main()

{

 

    int ch;

    printf("\n1.push\n 2.pop\n 3.peep\n 4.display");

    while(1)

    {

        printf("\n enter ur choice");

        scanf("%d",&ch);

        switch(ch)

        {

            case 1:push();

            break;

            case 2:pop();

            break;

            case 3:peep();

            break;

            case 4:display();

            break;

            default:exit(1);

 

        }

    }

}

 

void push()

{

    new=(struct node *)malloc(sizeof(struct node));

    printf("enter an element to be inserted");

    scanf("%d",&ele);

    new->next=NULL;

    new->data=ele;

    if(tos==NULL)

    {

        tos=new;

    }

    else

    {

    new->next=tos;

    tos=new;

    }

 

}

void pop()

{

    if(tos==NULL)

    {

        printf("stack is empty");

 

    }

    else

    {

        temp=tos;

        printf("deleted item from the stack is %d",tos->data);

        tos=tos->next;

        temp->next=NULL;

        free(temp);

    }

}

void peep()

{

 

    if(tos==NULL)

        printf("stack is empty");

    else

    {

    printf("display top most element is %d",tos->data);

    }

}

 

void display()

{

    printf("elements of the stack are");

    if(tos==NULL)

        printf("stack is empty");

    else

    {

    temp=tos;

    while(temp!=NULL)

        {

 

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

            temp=temp->next;

        }

    }

 

}               

 

Output:-

 

1.push

 2.pop

 3.peep

 4.display

 enter ur choice1

enter an element to be inserted7

 

 enter ur choice1

enter an element to be inserted14

 

 enter ur choice1

enter an element to be inserted21

 

 enter ur choice1

enter an element to be inserted28

 

 enter ur choice4

elements of the stack are28

21

14

7

 

 enter ur choice2

deleted item from the stack is 28

 enter ur choice4

elements of the stack are21

14

7

 enter ur choice3

display top most element is 21

 enter ur choice5

 

Process exited after 48.08 seconds with return value 1

Press any key to continue . . .

Comments

Popular posts from this blog

Data Structures Module_V

Data Structures Module-IV

DS - Module - II - DLL