Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int main()
- {
- int limit,i,answer,a[100],value,temp,top;
- printf("Enter the limit: ");
- scanf("%i",&limit);
- temp=limit;
- printf("Enter %i elements in the array.\n",limit);
- for(i=0;i<limit;i++)
- scanf("%i",&a[i]);
- printf("Limit is reached. What type of array is this? \n 1. Queue 2.Stack\n");
- scanf("%i",&answer);
- switch (answer)
- {
- case 1:
- printf("The Queue is: \n");
- for (i=limit-1; i>=0; i--)
- printf("%i ",a[i]);
- do
- {
- printf("\n What do you want to do?\n \n1.Insert 2. Delete. 3.Quit: ");
- scanf("%i",&answer);
- if (answer==1)
- {
- printf("Enter the value: ");
- scanf("%i",&value);
- if(limit==temp)
- {
- limit++; //Insert
- a[limit-1]=value;
- a[0]=0;
- for(i=1; i<limit; i++)
- a[i-1] = a[i];
- limit--;
- }
- else
- {
- limit++;
- a[limit-1]=value;
- }
- printf("The new Queue is: ");
- for (i=limit-1; i>=0; i--)
- printf("%i ",a[i]);
- }
- if(answer == 2) //Delete
- {
- if (limit==0)
- {
- printf("The Queue is empty.");
- continue;
- }
- a[0]=0;
- for(i=1; i<limit; i++)
- a[i-1] = a[i];
- limit--;
- if( limit == 0)
- {
- printf("The Queue is Empty.");
- continue;
- }
- printf("The new array is: ");
- for (i = limit-1; i>=0; i--)
- printf("%i ",a[i]);
- }
- }while (answer != 3);
- break;
- case 2:
- printf("The stack is full. The array is:-\n");
- for(i=limit-1;i>=0;i--)
- printf("%i \n",a[i]);
- do
- {
- top=limit-1;
- printf("\n What do you want to do?\n \n1.Insert 2. Delete. 3.Quit: ");
- scanf("%i",&answer);
- if(answer==1)
- {
- if(limit>=temp)
- {
- printf("The stack is full. Try deleting first.");
- continue;
- }
- limit++;
- printf("Enter the value: ");
- scanf("%i",&value);
- a[top+1]=value;
- top++;
- printf("The new array is:-\n");
- for(i=limit-1;i>=0;i--)
- printf("%i \n",a[i]);
- }
- if(answer==2)
- {
- if(limit==0)
- {
- printf("The stack is empty. \n");
- continue;
- }
- a[top]=0;
- limit--;
- top--;
- if(limit==0)
- {
- printf("The stack is empty. \n");
- continue;
- }
- printf("The new array is:-\n");
- for(i=limit-1;i>=0;i--)
- printf("%i \n",a[i]);
- }
- } while(answer != 3);
- break;
- }
- fflush(stdin);
- getchar;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement