Friday, March 9, 2012

Stack & Queue Using LinkList




Stack & Queue Using LinkList



#include <stdio.h>
#include <conio.h>

//Stack

  struct st{int n;struct st *next;};
  struct  st *front,*rear;

  //PUSH
  void push(int num)
      {
           struct st *temp;
           printf("\n Enter a number:");
           temp=(struct st*) malloc(sizeof(struct st));
           temp->n=num;
           if(front==NULL)
              {
                   front=rear=temp;
                   temp->next=NULL;
              }
           else
              {
                   rear->next=temp;
                   rear=temp;
                   temp->next=NULL;
              }
      }

  //Display
  void disp()
      {
           struct st *temp;
           if(front==NULL || rear == NULL)
              {
                   printf("\n\tStack is Empty!!");
                   getch();
                   return;
              }
           printf("\n\tValues in the Stack - ");
           for(temp=front;temp!=NULL;temp=temp->next)
              {
                   printf("%d ",temp->n);
              }
           getch();
      }

  //POP
  int pop()
      {
           int num;
           struct st *temp,*pr;
           if(front==NULL || rear==NULL)
              {
                   printf("\n\t Stack is empty ");
                   getch();
                   return -1;
              }
           for(temp=front;temp->next!=NULL;temp=temp->next)
              {
                   pr=temp;
              }
           if(temp==front)
              {
                   front=NULL;
              }
           else
              {
                   pr->next=temp->next;
              }
           num= temp->n;
           free(temp);
           return num;
      }

  void main()
      {
           int c,num;
           clrscr();
           rear=front=NULL;
           while(c<4)
              {
                   clrscr();
                   printf("\n\n\t1. Push");
                   printf("\n\t2. Display");
                   printf("\n\t3. Pop ");
                   printf("\n\t4. Exit");
                   printf("\n\tEnter your choice : ");
                   scanf("%d",&c);
                   switch(c)
                        {
                            case 1:
                                printf("\n\tEnter a No : ");
                                scanf("%d",&num);
                                push(num);
                                break;
                            case 2:
                                disp();
                                break;
                            case 3:
                                num=pop();
                                if(num>=0)
                                      {
                                                printf("\n\tNo. is Popped : %d ",num);
                                                getch();
                                      }
                                break;
                            case 4:
                                printf("\n\t\tQuit...");
                                getch();
                                exit(0);
                        }
              }
           getch();
      }


#include <stdio.h>
#include <conio.h>

//Queue

  struct Queue
      {
           int n;
           struct Queue *next;
      }*front, *rear,*cur,*prt;
  void push()
      {
           prt=(struct Queue *)malloc(sizeof(struct Queue));
           printf("\n\tEnter a no. : ");
           scanf("%d",&prt->n);
           prt->next=NULL;
           if(front==NULL)
              {
                   front=rear=prt;
              }
           else
              {
                   rear->next=prt;
                   rear=prt;
              }
      }

  void pop()
      {
           if(front==NULL)
              {
                   printf("\n\tQueue is Empty!");
                   getch();
                   return;
              }
           else
              {
                   prt=front;
                   front=front->next;
                   printf("\n\tNo. Poped - %d ",prt->n);
                   free(prt);
              }
           getch();
      }

  void display()
      {
           if(front==NULL)
              {
                   printf("\n\tQueue is Empty!");
                   getch();
                   return;
              }
           else
              {
                   printf("\n\tValues in Queue : ");
                   for(cur=front;cur;cur=cur->next)
                        {
                            printf(" %d ",cur->n);
                        }
                   getch();
              }
      }
  void main()
      {
           int c=0;
           char a;
           clrscr();
           front=rear=cur=prt=NULL;
           while(c!=4)
              {
                   clrscr();
                   printf("\n\n\t1. Push ");
                   printf("\n\t2. Pop");
                   printf("\n\t3. Display ");
                   printf("\n\t4. Exit ");
                   printf("\n\tEnter a Choice : ");
                   scanf("%d",&c);
                   switch(c)
                        {
                            case 1: push();
                                                break;
                            case 2: pop(front);
                                                break;
                            case 3: display();
                                                break;
                            case 4: printf("\n\t\tQuit...");
                                                getch();
                                                exit(0);
                        }
              }
           getch();
      }


No comments: