W.A.P. C++

WAP | CPP | Programs in C++ | C++ Solutions

Snehil Khanor's Binary Log
Showing posts with label traverse. Show all posts
Showing posts with label traverse. Show all posts

Write a program to Implement a Queue in C++


User is asked to Insert into.. Delete from or Display the entire queue..

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<stdio.h> 
  2. #include<conio.h> 
  3. #include<process.h> 
  4. int queue[5]; 
  5. long front,rear; 
  6. void initqueue(); 
  7. void display(); 
  8. void main() 
  9. { 
  10. int choice,info; 
  11. clrscr(); 
  12. while(1) 
  13. { 
  14. clrscr(); 
  15. printf(" MENU \n"); 
  16. printf("1.Insert an element in queue\n"); 
  17. printf("2.Delete an element from queue\n"); 
  18. printf("3.Display the queue\n"); 
  19. printf("4.Exit!\n"); 
  20. printf("Your choice: "); 
  21. scanf("%i",&choice); 
  22. //Coding by: Snehil Khanor  
  23. //http://WapCPP.blogspot.com  
  24. switch(choice) 
  25. { 
  26. case 1:if(rear<4) 
  27. { 
  28. printf("enter the number: "); 
  29. scanf("%d",&info); 
  30. if (front==-1) 
  31. { 
  32. front=0; 
  33. rear=0; 
  34. } 
  35. else 
  36. rear=rear+1; 
  37. queue[rear]=info; 
  38. } 
  39. else 
  40. printf("queue is full"); 
  41. getch(); 
  42. break; 
  43. case 2: int info; 
  44. if(front!=-1) 
  45. { 
  46. info=queue[front]; 
  47. if(front==rear) 
  48. { 
  49. front=-1; 
  50. rear=-1; 
  51. } 
  52. else 
  53. front=front+1; 
  54. printf("no deleted is = %d",info); 
  55. } 
  56. else 
  57. printf("queue is empty"); 
  58. getch(); 
  59. break; 
  60. case 3: display(); 
  61. getch(); 
  62. break; 
  63. case 4: exit(1); 
  64. break; 
  65. default:printf("You entered wrong choice!"); 
  66. getch(); 
  67. break; 
  68. } 
  69. } 
  70. } 
  71. void initqueue() 
  72. { 
  73. front=rear=-1; 
  74. } 
  75. void display() 
  76. { 
  77. int i; 
  78. for(i=front;i<=rear;i++) 
  79. printf("%i\n",queue[i]); 
  80. } 

Write a program to Implement a stack in C++


User is asked to PUSH, POP , or TRAVERSE.. PUSH is to add items to the Stack, POP is to remove item from the Stack, and TRAVERSE is to traverse the whole Stack

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<stdio.h> 
  2. #include<conio.h> 
  3. #include<process.h> 
  4. #define MAXSIZE 10 
  5. void push(); 
  6. int pop(); 
  7. void traverse(); 
  8. int stack[MAXSIZE]; 
  9. int Top=-1; 
  10. void main() 
  11. { 
  12. int choice; 
  13. char ch; 
  14. do 
  15. { 
  16. clrscr(); 
  17. printf("\n1. PUSH "); 
  18. printf("\n2. POP "); 
  19. printf("\n3. TRAVERSE "); 
  20. printf("\nEnter your choice "); 
  21. scanf("%d",&choice); 
  22. switch(choice) 
  23. { 
  24. case 1: push(); 
  25. break; 
  26. case 2: printf("\nThe deleted element is %d ",pop()); 
  27. break; 
  28. case 3: traverse(); 
  29. break; 
  30. default: printf("\nYou Entered Wrong Choice"); 
  31. } 
  32. printf("\nDo You Wish To Continue (Y/N)"); 
  33. fflush(stdin); 
  34. scanf("%c",&ch); 
  35. } 
  36. while(ch=='Y' || ch=='y'); 
  37. } 
  38. //Coding by: Snehil Khanor  
  39. //http://WapCPP.blogspot.com  
  40. void push() 
  41. { 
  42. int item; 
  43. if(Top == MAXSIZE - 1) 
  44. { 
  45. printf("\nThe Stack Is Full"); 
  46. getch(); 
  47. exit(0); 
  48. } 
  49. else 
  50. { 
  51. printf("Enter the element to be inserted "); 
  52. scanf("%d",&item); 
  53. Top= Top+1; 
  54. stack[Top] = item; 
  55. } 
  56. } 
  57.  
  58. int pop() 
  59. { 
  60. int item; 
  61. if(Top == -1) 
  62. { 
  63. printf("The stack is Empty"); 
  64. getch(); 
  65. exit(0); 
  66. } 
  67. else 
  68. { 
  69. item = stack[Top]; 
  70. Top = Top-1; 
  71. } 
  72. return(item); 
  73. } 
  74.  
  75. void traverse() 
  76. { 
  77. int i; 
  78. if(Top == -1) 
  79. { 
  80. printf("The Stack is Empty"); 
  81. getch(); 
  82. exit(0); 
  83. } 
  84. else 
  85. { 
  86. for(i=Top;i>=0;i--) 
  87. { 
  88. printf("Traverse the element "); 
  89. printf("%d\n",stack[i]); 
  90. } 
  91. } 
  92. } 

Search

About WAP C++

Here you'll find a wide range of programs' solution ranging from beginer level to advanced level.
All programs here are made, compiled and tested by me..and are running absolutely fine.. still if you find any bug in any program do let me know :)

Followers

Subscribe via email

Enter your email address:

Delivered by FeedBurner

my Binary Log



eXTReMe Tracker