affiliate marketing

Monday, 12 December 2011

LINKED LIST IMPLEMENTATION OF STACK


LINKED LIST IMPLEMENTATION OF STACK

Aim
            To demonstrate linked list implementation of stack using a C program.

Algorithm

            Step 1: [Include all the necessary header files]
            Step 2: [Declare the Variables]
            Step 3: Read operator
            Step 4: IF opt <---THEN
                        Step 4.1: READ n
                        Step 4.2: WHILE (n        n-1)
                                    Step 4.2.1: READ d
                                    Step 4.2.2: CALL INSERT( start , d)
                        Step 4.3: [End of while Structure]
            Step 5: IF opt         2 THEN
                        Step 5.1: READ x
                        Step 5.2: CALL del(start,x)
            Step 6: IF opt            3 THEN
                        Step 6.1: READ x
                        Step 6.2: CALL FIND
            Step 7: IF opt         4 THEN
                        Step 7.1: READ x
                        Step 7.2: CALL FINDPREVIOUS
Step 8: IF opt         5 THEN
                        Step 8.1: READ x
                        Step 8.2: CALL FINDNEXT(start, x)
Step 9: IF opt         6 THEN
            CALL len(Start)
Step 10: IF opt         7 THEN
            CALL printlist(Start)
Step 10: IF opt         8 THEN
            CALL erase (Start)
Step 12: [End of Main]

Algorithm For Find(struct node*p, int x, int *pos)
            Step 1: temp          p
            Step 2 :*pos         1
            Step 3: IF ( TEMP        NULL) THEN
                        RETURN NULL
                        ELSE
                        WHILE ( TEMP!= NULL && TEMP          DATA!= X)
                        ASSIGN 1 TO *POS+1 AND TEMP’S LLINK FIELD TO TEMP
                        RETURN THE TEMP

Algorithm for Previous (struct node*p, int x)
            Step 1: temp          p
            Step2: IF ( TEMP        NULL) THEN
                        RETURN NULL
                        ELSE
                        WHILE (TEMP        LINK != NULL && TEMP       LINK      DATA!= X)
                       

ASSIGN TEMP’S LINK FIELD TO TEMP
                        RETURN THE TEMP

Algorithm For Find next(struct node*p, int x)

            Step 1: temp          p
            Step2: IF ( TEMP        NULL) THEN
                        RETURN NULL
                        ELSE
                        WHILE (TEMP        LINK != NULL && TEMP      DATA!= X)
                        ASSIGN TEMP’S LLINK FIELD TO TEMP
                        RETURN THE TEMP’S LINK FIELD


Coding:

                        #include<stdio.h>
#include<conio.h>
#include<stdlib.h>
push();
void pop();
void display();
struct node
{
            int data;
            struct node *next;
}*top=NULL;
void main()
{
            int ch;
            clrscr();
            printf("\n\n1.Push\n\n2.Pop\n\n3.Display");
            do
            {
                        printf("\n\nEnter your Choice :: ");
                        scanf("%d",&ch);
                        switch(ch)
                        {
                                    case 1:
                                                push();
                                                break;
                                    case 2:
                                                pop();
                                                break;
                                    case 3:
                                                printf("\n\nContents of stack :: \t");
                                                display();
                                                break;
                                    default:
                                    printf("\n\nInvalid Choice......");
                                    getch();
                                    exit(0);
                        }
            }while(ch<4);
            getch();
}
push()
{
            int x;
            struct node *newnode;
            newnode=malloc(sizeof(struct node));
            printf("\n\nEnter the number to be pushed into the stack :: ");
                        scanf("%d",&x);
            newnode->data=x;
            if(top==NULL)
            {
                        newnode->next=top;
                        top=newnode;
            }
            else
            {
                        newnode->next=top;
                        top=newnode;
            }
            printf("\n\nNumber pushed is %d",x);
            return(x);
            }
void pop()
{
            struct node *t;
            if(top==NULL)
                        printf("\n\nStack Underflow");
            else
            {
                        t=top;
                        top=top->next;
                        printf("\nDeleted element is %d",t->data);
                        free(t);
            }
            getch();
}
void display()
{
            struct node*i;
            for(i=top;i!=NULL;i=i->next)
                        printf("%d , ",i->data);
            if(top==NULL)
                        printf("Stack is empty");
            getch();
}

Output:

1.Push
2.Pop
3.Display

Enter your Choice :: 1

Enter the number to be pushed into the stack :: 5

Number pushed is 5

Enter your Choice :: 1

Enter the number to be pushed into the stack :: 10

Number pushed is 10

Enter your Choice :: 3

Contents of stack :: 10 , 5 ,

Enter your Choice :: 2

Deleted element is 10

Enter your Choice :: 3

Contents of stack ::  5 ,

Enter your Choice :: 5

Invalid Choice......

No comments:

Post a Comment