Singly Linked List

#vpsinghrajput
/* A "BIG" PRG of singly linked list */
#include
#include
void list();
void specific();
void allocate();
void menu();
void fun();
void beg();
void end();
void delete1();
void delete2();
void delete3();
struct node
  {
     int info;
     struct node *ptr;
  };

typedef struct node  NODE;
NODE *start=0,*refresh,*pre,*temp,*del;
main()
  {
     char ch;
     clrscr();
     start=0;
     do
       {
      allocate();
      if(start==0)
       {
         start=refresh;
         pre=refresh;
       }
      else
       {
         pre->ptr=refresh;
         pre=refresh;
       }
      printf("do you want to continue(N/n)-> ");        fflush(stdin);
      ch=getche();
    }while(ch!='n'||ch=='N');
     fun();
 }
void list()
 {
   refresh=start;
      printf("\n\t data entered in the list are\n\n");
      while(refresh!=0)
      {
     printf("\n\t\t\t%d",refresh->info);
     refresh=refresh->ptr;
      }
 }
void allocate()
  {
     refresh=(NODE*)malloc(sizeof(NODE));
     printf("\n\nenter data-> ");
     scanf("%d",&refresh->info);
     refresh->ptr=0;
  }
void menu()
  {
     clrscr();
     gotoxy(35,1);  printf("MENU");
     gotoxy(10,2);printf("----------------------------------------------");
     gotoxy(32,3);  printf("1. List Data");
     gotoxy(32,4);  printf("2. Insert at Biginning ");
     gotoxy(32,5);  printf("3. Insert at the End");
     gotoxy(32,6);  printf("4. Insert at the Specified Position");
     gotoxy(32,7);  printf("5. Delete at Biginning");
     gotoxy(32,8);  printf("6. Delete at the End");
     gotoxy(32,9);  printf("7. Delete at the Specified Position");
     gotoxy(32,10);  printf("8. EXIT");
     gotoxy(10,11);printf("----------------------------------------------");
     printf("\nenter your choice-> ");
  }
void fun()
 {
    char c,ch;
    while(1)
      {
      c=getche();
     menu();     fflush(stdin);
     if(c=='2'||c=='3'||c=='4')
        allocate();
     if(c=='1')
          list();
     if(c=='2')
          beg();
     if(c=='3')
          end();
     if(c=='4')
       specific();
     if(c=='5')
       delete1();
     if(c=='6')
       delete2();
     if(c=='7')
       delete3();
     if(c=='8')
       exit(0);
      }
  }
void beg()
  {
     if(start==0)
       start=refresh;
     else
       refresh->ptr=start;
       start=refresh;
  }
void end()
  {
     if(start==0)
       start=refresh;
     else
       {
     for(pre=start;pre->ptr!=0;pre=pre->ptr);
     pre->ptr=refresh;
       }
  }
void specific()
  {
     int a=1,b;
     printf("enter position no. in the list-. ");
     scanf("%d",&b);
     if(start==0)
      {
    printf("there is no elemens");
    return;
      }
     if(b==1)
    {
      beg();
      return;
    }
     for(pre=start;pre->ptr!=NULL&&a<=b-2;pre=pre->ptr,a++);
     if(pre->ptr==0)
       {
        printf("nodes are les than the number you have entered");
        return;
       }
     temp=pre->ptr;
     pre->ptr=refresh;
     refresh->ptr=temp;
}
void delete1()
   {
       temp=start;
       start=start->ptr;
       free(temp);
   }
void delete2()
  {
       temp=start;
       for(;temp->ptr!=0;pre=temp,temp=temp->ptr);
       temp=temp->ptr;
       pre->ptr=0;
       free(temp);
  }
void delete3()
  {
     int a=1,pos;
     printf("enter the position-> ");
     scanf("%d",&pos);
     temp=start;
     if(pos==1)
       {  delete1();
      return;    }
     for(;aptr!=0;a++,temp=temp->ptr);
     if(temp->ptr==0)
      printf("data are less in the list");
     pre=temp->ptr;
     del=temp->ptr;
     temp->ptr=pre->ptr;
     free(del);
  }


Calculate a Postfix Expression

#vpsinghrajput
/* A prg to calculate a postfix expression */
#include
#include
double arr[40];
int top=-1;
void push(double val)
  {
    top++;
    arr[top]=val;
  }
double pop()
  {
    int a=top;
    top--;
    return(arr[a]);
  }
main()
   {
    char *postfix;
    double a,i,j;
    clrscr();
    printf("entert postfix expression-> ");
    gets(postfix);
    for(a=0;postfix[a]!='\0';a++)
       {
        if(postfix[a]>64&&postfix[a]<91||postfix[a]>96&&postfix[a]<123)
           {

            gotoxy(5,4);
            printf("enter the value of %c -> ",postfix[a]);
            scanf("%lf",&i);
            push(i);
            gotoxy(5,4);  printf("                                  ");
           }
        else
           {
            i=pop();
            j=pop();
            if(postfix[a]=='+')
                push(i+j);
            else
            if(postfix[a]=='-')
                push(i-j);
            else
            if(postfix[a]=='*')
                push(i*j);
            else
            if(postfix[a]=='/')
                push(i/j);
            else
            if(postfix[a]=='^')
                push(pow(j,i));
            }
       }
    printf("\n\n\tValue of the expression->  %lf",pop());
    getch();
   }

Doubly Linked List

#vpsinghrajput
/* A "BIG" PRG using DOUBLY linked list */
#include
#include
void delete_specific();
void delete_end();
void delete_beg();
void allocate();
void create();
void insert_beg();
void insert_specific();
void insert_end();
void list();
void menu();
struct node
   {
    struct node *pre;
    int info;
    struct node *next;
   };
typedef struct node ND;
ND *refresh,*start=0,*temp,*pv=0,*tt;
main()
  {
    char ch='1';
    clrscr();
    create();
    while(1)
      {
        menu();

        if(ch=='2'||ch=='3'||ch=='4')
           allocate();
        if(ch=='1')
           list();
        if(ch=='2')
           insert_beg();
        if(ch=='3')
           insert_end();
        if(ch=='4')
           insert_specific();
        if(ch=='5')
           delete_beg();
        if(ch=='6')
           delete_end();
        if(ch=='7')
           delete_specific();
        if(ch=='8')
           exit(1);
        ch=getche();
          }
  }
void allocate()
  {
        refresh=(ND*)malloc(sizeof(ND));
        printf("\nenter the data-> ");
        scanf("%d",&refresh->info);
        refresh->pre=pv;
        refresh->next=0;
  }
void create()
  {
    char ch;
    do
      {
        allocate();
        if(start==0)
          {
            start=refresh;
            pv=refresh;
          }
        else
          {
            pv->next=refresh;
            pv=refresh;
          }
        printf("\tdo you want to continue(y/n)-> ");      fflush(stdin);
        ch=getch();
      }     while(ch!='n'&&ch!='N');
  }
void list()
  {
    temp=start;
    printf("\n\nTraversing the List \n\n");
    while(temp!=0)
      {
        printf("\n\t\t%d",temp->info);
        temp=temp->next;
      }
    if(start==0)
      pv=0;
    temp=pv;
    printf("\n\nTraversing the List in reverse order\n\n");
    while(temp!=0)
      {
        printf("\n\t\t%d",temp->info);
        temp=temp->pre;
      }
  }
void menu()
  {
     clrscr();
     gotoxy(35,1);  printf("MENU");
     gotoxy(10,2);printf("----------------------------------------------");
     gotoxy(32,4);  printf("1. List data");
     gotoxy(32,5);  printf("2. Insert at Biginning");
     gotoxy(32,6);  printf("3. Insert at the End");
     gotoxy(32,7);  printf("4. Insert at the the specified position");
     gotoxy(32,8);  printf("5. Delete at Beginning");
     gotoxy(32,9);  printf("6. Delete at End");
     gotoxy(32,10);  printf("7. Delete at Specific Position");
     gotoxy(32,11);  printf("8. EXIT");
     gotoxy(10,12);printf("----------------------------------------------");
     printf("\nenter your choice-> ");
  }
void insert_beg()
  {
    start->pre=refresh;
    refresh->next=start;
    refresh->pre=0;
    start=refresh;
  }
void insert_end()
  {
    pv->next=refresh;
    refresh->pre=pv;
    pv=refresh;
  }
void insert_specific()
  {
    int a=1,pos;
    printf("enter the position->");
    scanf("%d",&pos);
    if(pos==1)
       {
          insert_beg();
          return;
       }
    temp=start;
    for(;anext!=0;a++,temp=temp->next);
    if(temp->next==0)
        {
          printf("data in the list are less");
          return;
        }
    refresh->next=temp->next;
    (temp->next)->pre=refresh;
    refresh->pre=temp;
    temp->next=refresh;
   }
void delete_beg()
  {
    temp=start;
    start=start->next;
    start->pre=0;
    free(temp);
  }
void delete_end()
  {
    temp=pv;
    pv=pv->pre;
    pv->next=0;
    free(temp);
  }
void delete_specific()
  {
    int pos,a=1;
    printf("enter position ->");
    scanf("%d",&pos);
    temp=start;
    if(pos==1)
      {  delete_beg();
         return;
      }
    for(;anext);
    if(temp->next==0)
      {
        printf("data are lessthan the number you have entered");
        return;
      }
    tt=temp->next;
    if(tt->next==0)
      pv=tt->pre;

    temp->next=tt->next;
    (tt->next)->pre=temp;
    free(tt);
  }