Sunday, 3 February 2013

Write a C program to insert, delete and view element into Stack.

Write a C program to insert, delete and view element into Stack.

Program:


#include<stdio.h>
#include<conio.h>
void main()
{
 int p=0,i=0,c=0;
 int stack[10];
 clrscr();
 do{
 clrscr();
 printf("\n1 for Insert\n2 for Delete\n3 for View\n4 for Exit.\nEnter Command:");
 scanf("%d",&c);
 switch(c){
  case 1:
  printf("Enter Element:");
  i=p;
  p=p+1;
  if(p!=10){
  scanf("%d",&stack[i]);
  printf("Element Inserted!");
  }
  else{
  p=p-1;
  printf("\nElement over flow!");
  }
  getch();
  break;

  case 2:
  if(p>0){
  printf("1 Element Deleted!");
  p=p-1;
  }
  else{
  printf("Stack is empty!");
  }
  getch();
  break;

  case 3:
  printf("The stack is:\n");
  for(i=0; i<p; i++){
     printf("\t%d",stack[i]);
  }
  getch();
  break;

  case 4:
  break;

  default:
  printf("Command Error!");
  getch();
  }
 }
 while(c!=4);

}

Output:

1 for Insert
2 for Delete
3 for View
4 for Exit
Enter command:1
Enter Element:5
Element Inserted!


1 for Insert
2 for Delete
3 for View
4 for Exit
Enter command:1
Enter Element:2
Element Inserted!

1 for Insert
2 for Delete
3 for View
4 for Exit
Enter command:1
Enter Element:6
Element Inserted!

1 for Insert
2 for Delete
3 for View
4 for Exit
Enter command:2
1 element Deleted!

1 for Insert
2 for Delete
3 for View
4 for Exit
Enter command:3

The Stack is:
          5          2


1 for Insert
2 for Delete
3 for View
4 for Exit
Enter command:4
// this command for exit.

Thanks...Comment it...



3 comments:

  1. hello my self eng.adnan zafer i am a software developer plz tell me that how to print "hello world " on console in turbo c

    ReplyDelete
    Replies
    1. This are basic of programming...
      write this program and compile+run it...
      #include
      int main(){
      printf("Hello World");
      return 0;
      }

      That's it...

      Delete
    2. hi!!
      my name is Syed Saleem , i am a student
      can you tell me about .01 extensions files
      and how to convert .iso file into .01 or .02 etc.

      Delete