Monday, 18 September 2017

Stack (Dynamic) Using Link List


-------------------------------------------------------------------------------------------------------------------
Dynamic Stack
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;

} ;
struct node *head=NULL  , *newnode,*top=NULL,*temp;//global declaration

void pop();
void push();
voidcreatenode();
void display();

void main()
{
intch;
            clrscr();
            do
             {
            printf("\n 1 . PUSH ");
            printf("\n 2 . POP ");
            printf("\n 3 ..*..... Stack Display...........*. ");
            printf("\n 4 . EXIT ");
            printf("\n Enter your choice............ ");
            scanf("%d",&ch);
            switch(ch)
              {
            case 1:createnode();
                        push();
                        printf("\nNode PUSH Sucessfully \n");
                        break;
            case 2:pop();
                        printf("\nNode POP Sucessfully \n");
                        break;
            case 3:display();
                        printf("\n\n");
                        break;
            case 4:exit(0);

            default  :printf("\n\n Re enter u r choice .......");


                          }
            }while(1);
getch();
}

voidcreatenode()
{
newnode= (struct node * )(malloc) (sizeof (struct node));
newnode->next=NULL;
printf(" Enter data ");
scanf("%d",&newnode->data);
}
//----------------------------------------------------------------
void push()
{
if(head==NULL)
     {
head=newnode;
top=head;
     }
else
      {    top->next=newnode;
            top=newnode;
      }
 }
//---------------------------------------------------------------------------------------------------
void display()
{
struct node *temp ;
temp=head;

while(temp!=NULL)
{   printf("%d      ",temp->data);
temp=temp->next;
}

}
//---------------------------------------------------------------------------------------------------------
void pop()
{
if (head==NULL)
printf("\n\n List is empty .......................");
else
 {
temp=head;
while(temp->next!=top)                                                                                                
 {
temp=temp->next;
 }

printf(" Deleted node is---- %d----",top->data);

free(top);
top=temp;
top->next=NULL;

  }}


Ebook

Ebook
Ebook