Friday, February 6, 2015

C Programming: Stacking with a Singly Linked List of Nested Structures (2 Structures)

No comments :
This is a program that stacks (push) values to a certain range indicated by the user.
Will include an image of the visualization soon, as well as code comments... (updated)

Stack, Singly Linked List, Nested Structures


Program code:
 #include<stdio.h>  
   
 struct stack  
 {  
      int data;  
      struct stack *next;  
 };  
   
 struct range  
 {  
      int rangel, rangeh;  
      struct range *next;  
      struct stack *up;  
 }*range_start=NULL;  
   
 void display(int limit)  
 {  
      struct range *current;  
      int i,j,x=1,y=limit+2,ry=limit+2,ly=0;  
      current=range_start;  
        
      while(current!=NULL)  
      {  
           if(x>=80)  
           {  
                ly=y+1;  
                y=y+((limit+2)+1);  
                x=1;  
                ry=y;  
           }  
           if(ly+limit>=24)  
           {  
                gotoxy(1,25);  
                printf("press any key to continue...");  
                getch();  
                clrscr();  
                x=1;  
                y=limit+2;  
                ry=limit+2;  
                ly=0;  
           }  
           if(x==1)  
           {  
                for(i=1+ly;i<=y;i++)  
                {  
                     gotoxy(1,i);  
                     printf("|");  
                }  
           }  
           gotoxy(x+1,y);  
           if(current->rangel<10&&current->rangeh>9)  
           {  
                printf(" 0%d-%d",current->rangel,current->rangeh);  
           }  
           else if(current->rangeh<10)  
           {  
                printf(" 0%d-0%d",current->rangel,current->rangeh);  
           }  
           else  
           {  
                printf(" %d-%d",current->rangel,current->rangeh);  
           }  
           x=x+4;  
           for(j=x,i=1;i<8;i++,j++)  
           {  
                gotoxy(j-3,y-1);  
                printf("-");  
           }  
           y=y-1;  
           while(current->up!=NULL)  
           {  
                y=y-1;  
                gotoxy(x,y);  
                printf("%d",current->up->data);  
                current->up=current->up->next;  
           }  
           x=x+4;  
           y=ry;  
           current=current->next;  
      }  
 }  
   
 void insertRange(int rangel, int rangeh)  
 {  
      struct range *new_node,*current;  
        
      if(range_start==NULL)  
      {  
           new_node=(struct range *)malloc(sizeof(struct range));  
           new_node->rangel=rangel;  
           new_node->rangeh=rangeh;  
           new_node->up=NULL;  
           new_node->next=NULL;  
           range_start=new_node;  
           current=new_node;  
      }  
      else  
      {  
           current=range_start;  
           while(current->next!=NULL)  
           {  
                current=current->next;  
           }  
           new_node=(struct range *)malloc(sizeof(struct range));  
           new_node->rangel=rangel;  
           new_node->rangeh=rangeh;  
           new_node->up=NULL;  
           new_node->next=NULL;  
           current->next=new_node;  
           current=new_node;  
      }  
 }  
   
 void insertStackData(int data)  
 {  
      struct range *current;  
      struct stack *new_node,*c;  
      current=range_start;  
        
      /*find range*/  
      while(current!=NULL)  
      {  
           if(data>=current->rangel&&data<=current->rangeh)  
           {  
                /*insert data to stack*/  
                if(current->up==NULL)  
                {  
                     new_node=(struct stack *)malloc(sizeof(struct stack));  
                     new_node->data=data;  
                     new_node->next=NULL;  
                     current->up=new_node;  
                     break;  
                }  
                else  
                {  
                     c=current->up;  
                     while(c->next!=NULL)  
                     {  
                          c=c->next;  
                     }  
                     new_node=(struct stack *)malloc(sizeof(struct stack));  
                     new_node->data=data;  
                     new_node->next=NULL;  
                     c->next=new_node;  
                     break;  
                }  
           }  
           current=current->next;  
      }  
 }  
   
 void stack(int limit,int size)  
 {  
      int i=0,rangel=0,rangeh=size-1;  
      int data;  
        
      clrscr();  
        
      /*inserting to node*/  
      while(rangel<100)  
      {  
           if(rangeh>99)  
           {  
                rangeh=99;  
           }  
           insertRange(rangel,rangeh);  
           rangel=rangeh+1;  
           rangeh=rangeh+size;  
      }  
        
      clrscr();  
        
      while(i<limit)  
      {  
           printf("Enter value %d: ",i+1);  
           scanf("%d",&data);  
           if(data>=0&&data<=99)  
           {  
                insertStackData(data);  
                i++;  
           }  
           else  
           {  
                printf("Invalid input! Enter a value from 0-99\n");  
           }  
      }  
      clrscr();  
      display(limit);  
 }  
   
 void freeAllNodes()  
 {  
      free(range_start);  
 }  
   
 int getLimit()  
 {  
      int limit;  
      while(1)  
      {  
           printf("Enter number of inputs: ");  
           scanf("%d",&limit);  
           if(limit<0)  
           {  
                printf(" -Size cannot be lesser than zero!\n");  
           }  
           else  
           {  
                break;  
           }  
      }  
        
      return limit;  
 }  
   
 int getSize()  
 {  
      int size;  
        
      while(1)  
      {  
           printf("Enter size/range: ");  
           scanf("%d",&size);  
           if(size<1)  
           {  
                printf(" -Size cannot be equal to/lesser than 0!\n");  
           }  
           else if(size>99)  
           {  
                printf(" -Size cannot be equal to/greater than 100!\n");  
           }  
           else  
           {  
                break;  
           }  
      }  
        
      return size;  
 }  
   
 main()  
 {  
      int limit,size;  
      char choice=NULL;  
      clrscr();  
        
      while(1)  
      {  
           limit=getLimit();  
           size=getSize();  
           stack(limit,size);  
             
           while(choice!='Y'||choice!='y'||choice!='N'||choice!='n')  
           {  
                gotoxy(1,25);  
                printf("Again [y/n]? ");  
                choice=getche();  
                switch(choice)  
                {  
                     case 'Y': //same with case 'y'  
                     case 'y': /*free all nodes*/  
                               freeAllNodes();  
                               clrscr();  
                               limit=getLimit();  
                               size=getSize();  
                               stack(limit,size);  
                               break;  
                     case 'N': //same with case 'n'
                     case 'n': //PROGRAM EXIT  
                               exit(1);  
                               break;  
                     default:  
                               gotoxy(1,25);  
                               printf("Invalid input! press any key....");  
                               getch();  
                               gotoxy(14,25);  
                               printf("\t\t\t\t");  
                               break;  
                }  
           }  
      }  
 }  

If you have any question, place a comment below.

No comments :

Post a Comment