Sunday, 16 October 2011

stack implementation of list


PROGRAMS:
/*stack implementation of list*/
#include<stdio.h>
#include<malloc.h>
int s[10],TOS;
struct node
{
            int element;
            struct node * next;
}*p,*l,*header,*temp;
main()
{
            int choice,c;
            void displaya();                       /*display list using array*/
            void displayp();                       /*display list using pointer*/
            void pusha();                           /*to perform push operation using array*/
            void pushp();                           /*to perform push operation using pointer*/
            void popa();                             /*to perform pop operation using array*/
            void popp();                            /*to perform pop operation using pointer*/
            void topa();                             /*to find the top element in the array list*/
            void topp();                             /*to find the top element in the linklist*/
            void createa();                         /*to create the list using array*/
            void createp();                         /*to create the list using linklist*/
            temp=(struct node *) malloc (sizeof(struct node));
            p=temp;l=temp;header=temp;
            {
                        printf("\nDo you want to implement using \n1.Array \n2.Pointer ");
                        scanf("%d",&c);
            }
            if( c == 1 )
            {
                        createa();
                        displaya();
            }
            else
            {
                        createp();
                        displayp();      
            }
            do
            {
            printf("\n1.Push Operation \n2.Pop Operation \n3.Top Operation \nEnter your choice ");
            scanf("%d",&choice);          /*to get the choice to perform in list*/
            if( c == 1 )
            {
                        switch (choice)                        /*to perform operations in array list*/
                        {
                                    case 1: pusha();displaya();break;
                                    case 2: popa();displaya();break;
                                    case 3: topa();displaya();break;
                        }
            }
            else
            {
                        switch (choice)                        /*to perform operations in linklist*/
                        {
                                    case 1:pushp();displayp();break;
                                    case 2:popp();displayp();break;
                                    case 3:topp();displayp();break;
                        }         
            }
            printf("\nDo you wish to continue? 1.Yes 2.No ");
            scanf("%d",&choice);
            }
            while(choice == 1);
}
void createa()
{
            int i,n,x;
            printf("\nCreating a stack\n");
            printf("Enter the number of elements in the stack ");
            scanf("%d",&n);
            printf("enter the elements");                /*to create a list using array*/
            for(i=1;i<=n;i++)
            {
                        TOS=i;
                        scanf("%d",&x);
                        s[TOS]=x;
            }
}
void pusha()
{
            int x;
            printf("\nPUSH Operation\n");
            printf("Enter the element to be pushed into the stack ");
            scanf("%d",&x);
            if(TOS == 10)
            {
                        printf("\nStack is full");                       /*to perform push operation in array list*/
            }
            else
            {
                        TOS++;
                        s[TOS]=x;
            }
}
void popa()
{
            int n,i;
            printf("\nPOP Operation\n");
            printf("Enter the number of elements to be popped ");          /*to perform pop operation in array list*/
            scanf("%d",&n);
            printf("Popped elements are\n");
            for(i=1;i<=n;i++)
            {
                        if(TOS == 0)
                        {
                                    printf("Stack is empty");
                                    break;
                        }
                        else
                        {
                                    printf("%d\n",s[TOS]);
                                    TOS--;
                        }
            }
}
void displaya()
{
            int i;
            printf("\nThe stack elements are\n");               /*to display the list using array*/
            for(i=TOS;i>0;i--)
            {
                        printf("%d\n",s[i]);
            }
}
void topa()
{
            printf("\nTOP Operation\n");
            printf("The topmost element is %d",s[TOS]);             /*top element in the list*/
}
void createp()
{
            int i,n,x;
            printf("\nCreating a stack\n");
            printf("Enter the number of elements in the stack ");
            scanf("%d",&n);
            printf("enter the elements");                                        /*to create a list using linklist*/
            for(i=1;i<=n;i++)
            {
                        scanf("%d",&x);
                        temp=(struct node *) malloc (sizeof(struct node));
                        temp->element=x;
                        temp->next=header->next;
                        header->next=temp;
            }
}
void displayp()
{
            p=l;
            printf("Stack elements are\n");
            while( p->next != NULL)
            {
                        printf("%d->",p->next->element);
                        p=p->next;
            }
            while(p->next==NULL)
            {
                        printf("NULL");                                              /*to display the elements in linklist*/
                        break;
            }
}
void pushp()
{
            int x;
            printf("\nPUSH  Operation\n");
            printf("Enter the element to be pushed ");
            scanf("%d",&x);
            temp=(struct node *) malloc (sizeof(struct node));                /*to perform push operation in linklist*/
            temp->element=x;
            temp->next=header->next;
            header->next=temp;
}
void popp()
{
            int n,i;
            printf("\nPOP Operation\n");
            printf("Enter the number of elements to be popped ");
            scanf("%d",&n);
            printf("The popped elements are\n");                                      /*to perform pop operation in linklist*/
            for(i=1;i<=n;i++)
            {
                        if (header->next != NULL)
                        {
                                    temp=header->next;
                                    printf("%d\n",temp->element);
                                    header->next=header->next->next;
                                    free(temp);
                        }
            }
}
void topp()
{
            printf("\nTOP Operation\n");
            printf("The topmost element is %d\n",header->next->element);                    /*to find the top element in the list using linklist*/
}

OUTPUT:


Do you want to implement using
1.Array
2.Pointer 1

Creating a stack
Enter the number of elements in the stack 3
enter the elements1
2
3

The stack elements are
3
2
1

1.Push Operation
2.Pop Operation
3.Top Operation
Enter your choice 1

PUSH Operation
Enter the element to be pushed into the stack 4

The stack elements are
4
3
2
1

Do you wish to continue? 1.Yes 2.No 1

1.Push Operation
2.Pop Operation
3.Top Operation
Enter your choice 2

POP Operation
Enter the number of elements to be popped 1
Popped elements are
4

The stack elements are
3
2
1

Do you wish to continue? 1.Yes 2.No 1

1.Push Operation
2.Pop Operation
3.Top Operation
Enter your choice 3

TOP Operation
The topmost element is 3
The stack elements are
3
2
1

Do you wish to continue? 1.Yes 2.No


Do you want to implement using
1.Array
2.Pointer 2

Creating a stack
Enter the number of elements in the stack 3
enter the elements1
2
3
Stack elements are
3->2->1->NULL
1.Push Operation
2.Pop Operation
3.Top Operation
Enter your choice 1

PUSH  Operation
Enter the element to be pushed 3
Stack elements are
3->3->2->1->NULL
Do you wish to continue? 1.Yes 2.No 1

1.Push Operation
2.Pop Operation
3.Top Operation
Enter your choice 2

POP Operation
Enter the number of elements to be popped 1
The popped elements are
3
Stack elements are
3->2->1->NULL
Do you wish to continue? 1.Yes 2.No 1

1.Push Operation
2.Pop Operation
3.Top Operation
Enter your choice 3

TOP Operation
The topmost element is 3
Stack elements are
3->2->1->NULL
Do you wish to continue? 1.Yes 2.No

No comments:

Post a Comment